OpenAIManager package

OpenAIManager

class OpenAIManager.OpenAIManager.OpenAIManager(api_key: str)

Bases: object

Manages interactions with the OpenAI API, specifically for retrieving available models.

This class is designed to encapsulate the functionality related to the OpenAI API, primarily focusing on listing the available models. It currently offers limited additional utility beyond what the OpenAI Python client library provides. This redundancy might not justify its use in a context where direct usage of the OpenAI client library would suffice.

Attributes:

list_models_api_url (str): The API URL for listing OpenAI models.

list_models_api_url = 'https://api.openai.com/v1/models'
get_available_models()

Retrieves a list of available models from the OpenAI API.

This method makes an HTTP request to the OpenAI API to fetch the currently available models. It returns a list of model identifiers. Note that this method directly interacts with the API and does not utilize the OpenAI client library, which might be seen as a redundancy given that the client library already provides this functionality.

Returns:

A list of available model names.

Return type:

list[str]

Raises:

HTTPError or other network-related errors if the request to the OpenAI API fails.

AssistantManager

class OpenAIManager.AssistantManager.AssistantManager(client: OpenAI, assistant_id: str = None, assistant_params: AssistantParams = None)

Bases: object

A class for managing openAI assistants. Handles assistant creation and message sending via this assistant.

send_message(message: Message) str

Sends a message to the OpenAI Assistant and receives a response.

This method handles message sending to the OpenAI Assistant within a thread. If the thread associated with the message does not exist, it creates a new thread remotely and locally. It then adds the message to the thread and runs the assistant to generate a response. The method waits until the assistant’s run status is ‘completed’ to ensure that a response is generated. Finally, it retrieves and returns the new message from the assistant.

Parameters:

message (Message) – The message object containing the message to be sent.

Raises:

Various exceptions related to network issues, API errors, or unexpected response formats.

Returns:

The assistant’s text response.

Return type:

str

create_assitant(params: AssistantParams)

Creates a new OpenAI Assistant based on provided AssistantParams object.

This method creates a new assistant using the OpenAI API. It takes the parameters defined by the assistant, converts them into the appropriate format using the to_dict method of the AssistantParams class, and then calls the OpenAI API to create the assistant. The newly created assistant is returned.

Parameters:

params (AssistantParams) – Parameters for the assistant to be created.

Raises:

TypeError – If params is not of type AssistantParams.

Returns:

The newly created assistant object.

Return type:

openai.types.beta.assistant.Assistant

ThreadsManager

class OpenAIManager.ThreadsManager.ThreadsManager(client: OpenAI)

Bases: object

Manages the storage and retrieval of threads and messages for an OpenAI application.

This class handles the interactions between local storage and the remote OpenAI API, specifically focusing on thread management. It provides methods to create, retrieve, and manage threads both locally and remotely. Due to API limitations and the current scope of the class design, it lacks functionality to fetch all threads directly from the remote server, and its capabilities are primarily centered around individual management of known threads.

get_thread_remote(thread_id: str) Thread

Retrieves a specific thread from the remote OpenAI server using its ID.

This method attempts to fetch a thread from the OpenAI API. If the thread does not exist or an error occurs during retrieval, an exception is caught and handled.

Parameters:

thread_id (str) – The unique identifier of the thread to retrieve.

Returns:

The retrieved thread or None if not found or an error occurs.

Return type:

openai.types.beta.thread.Thread or None

get_thread_id_local(thread_key: str) str

Retrieves the ID of a local thread based on a provided “thread key”.

This method looks up a thread’s ID in the local shelve database using the provided thread key. If the thread key does not exist in the local storage, None is returned.

Parameters:

thread_key (str) – The key used to identify the thread in local storage.

Returns:

The ID of the thread associated with the given key, or None if not found.

Return type:

str or None

create_thread_remote()

Creates a new thread on the remote OpenAI server.

This method attempts to create a new thread using the OpenAI API. If an error occurs during creation, it is printed and the thread is left as None.

Returns:

The created thread or None if an error occurs.

Return type:

Thread or None

create_thread_local(thread: Thread = None, thread_key: str = None)

Creates a new thread in local storage or updates an existing one.

This method adds a new thread to local storage using the provided thread object and key. If the thread is not provided, a new one is created remotely. If the thread already exists in local storage, it is updated.

Parameters:
  • thread (Thread, optional) – The thread object to store or update locally.

  • thread_key (str, optional) – The key to associate with the thread in local storage.

Returns:

The thread object after creation or update.

Return type:

Thread

get_threads_local() dict

Retrieves all threads stored in local storage.

This method reads the entire local shelve database and returns its contents as a dictionary. The dictionary maps thread keys to thread IDs.

Returns:

A dictionary of thread keys and their corresponding IDs.

Return type:

dict

get_messages_remote(thread)

Retrieves the message history for a given thread from the OpenAI API.

This method fetches the message history associated with the specified thread directly from the OpenAI server. The messages are returned in a list, with the latest message at index 0.

Parameters:

thread (Thread) – The thread object for which to retrieve messages.

Returns:

A list of messages from the thread, latest first.

Return type:

list

Helpers

class OpenAIManager.Helpers.Message(message_text: str, thread_key: str = None, id: str = None, thread_id: str = None)

Bases: object

Represents a message in a conversation with an OpenAI assistant.

This class encapsulates the data related to a single message, including its text, associated thread key, and unique identifiers. It provides functionality to convert the message data into a dictionary format suitable for API calls.

to_dict() str

Converts the message data into a dictionary format.

This method is particularly useful for preparing the message data to be sent through the OpenAI API. It structures the message content, role, and any file attachments in a dictionary.

Returns:

A dictionary representation of the message.

Return type:

dict

class OpenAIManager.Helpers.AssistantParams(openai_manager: OpenAIManager, model: str, name: str = 'DefaultAssistant', description: str = '', instructions: str = '', tools: list = None, file_ids: list = None)

Bases: object

Encapsulates the parameters for creating or configuring an OpenAI assistant.

This class holds various configuration options for an OpenAI assistant, including the model to be used, a name for the assistant, a description, instructions, and any associated tools or file IDs. It supports converting these parameters into a dictionary format suitable for API calls.

to_dict() dict

Converts the assistant parameters into a dictionary format.

This method structures the assistant’s configuration options into a dictionary, making it suitable for use in API calls or other configurations where a dictionary representation is required.

Returns:

A dictionary representation of the assistant parameters.

Return type:

dict