### Setting up Python Virtual Environment with Poetry Source: https://github.com/requestyai/requestyai-python/blob/main/README.md These commands initialize a Python virtual environment using Poetry, installing project dependencies and then activating the shell within that environment. This ensures a clean and isolated development setup. ```Shell poetry install poetry shell ``` -------------------------------- ### Installing RequestyAI Python Library Source: https://github.com/requestyai/requestyai-python/blob/main/README.md This snippet demonstrates how to install the RequestyAI Python library using pip, the standard package installer for Python. It's the first step required to use the AInsights client and integrate RequestyAI into your Python projects. ```bash pip install requestyai ``` -------------------------------- ### Capturing OpenAI Chat Completions with Global AInsights Client in Python Source: https://github.com/requestyai/requestyai-python/blob/main/README.md This Python example demonstrates how to integrate the global `ainsights` client with OpenAI's chat completions. It sends a user query to an OpenAI model (`gpt-4o-mini`) and then uses `ainsights.capture()` to log the `messages`, `response`, and `args` for future analysis, regardless of output format (free-text, JSON, or tools). ```python import os from openai import OpenAI from .ainsights import ainsights openai.api_key = os.environ["OPENAI_API_KEY"] if __name__ == '__main__': user_input = input("Ask anything: ") messages = [{"role": "user", "content": user_input}] args = {"model": "gpt-4o-mini", "temperature": 0.7, "max_tokens": 150} response = openai.chat.completions.create(messages=messages, **args) ainsights.capture(messages=messages, response=response, args=args) print(response.choices[0].message.content) ``` -------------------------------- ### Running OpenAI Basic Sample Application Source: https://github.com/requestyai/requestyai-python/blob/main/README.md This command executes the basic OpenAI sample application, passing RequestyAI and OpenAI API keys as environment variables. Replace and with your actual API keys. ```Shell REQUESTY_KEY="" OPENAI_KEY="" \ python -m samples.openai.basic.app ``` -------------------------------- ### Initializing Global RequestyAI AInsights Client in Python Source: https://github.com/requestyai/requestyai-python/blob/main/README.md This Python snippet initializes a global instance of the `AInsights` client from the `requestyai` library. It retrieves the Requesty API key from an environment variable (`REQUESTY_API_KEY`) and creates a thread-safe, asynchronous client for capturing AI interaction insights across the application. ```python import os from requestyai import AInsights ainsights = AInsights.new_client(api_key=os.environ["REQUESTY_API_KEY"]) ``` -------------------------------- ### Integrating Per-Instance AInsights Client with OpenAI Chat in Python Source: https://github.com/requestyai/requestyai-python/blob/main/README.md This Python class demonstrates creating and using a dedicated `AInsights` client instance alongside an `OpenAI` client. The `chat` method handles user input, interacts with OpenAI, and then captures the interaction details, including `messages`, `response`, `args`, custom `meta` tags, and a `user_id`, providing granular control over insights data. ```python from openai import OpenAI from pydantic import BaseModel from requestyai import AInsights class Model: def __init__(self, openai_api_key, openai_args, requesty_api_key): self.__model = OpenAI(api_key=openai_api_key) self.__args = openai_args self.__insights = AInsights.new_client(api_key=requesty_api_key) def chat(self, user_id: str, user_input: str): messages = [ {"role": "system", "content": "You are a helpful search assistant."}, {"role": "user", "content": user_input} ] meta = {"class": "Search assistant"} class Response(BaseModel): answer: str response = self.__model.beta.chat.completions.parse( messages=messages, response_format=Response, **self.args ) self.__insights.capture(messages=messages, response=response, args=self.args, meta=meta, user_id=user_id) content = response.choices[0].message.content if not content: return None return Response.model_validate_json(content) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.