evolving_ideas.infra.open_ai_client

evolving_ideas.infra.open_ai_client

Attributes

Exceptions

OpenAIClientError

Custom exception for OpenAI client errors.

Classes

OpenAITransport

OpenAI client for interacting with the OpenAI API.

OpenAICredentialValidator

Validates OpenAI API credentials.

OpenAILLM

OpenAI Language Model Wrapper

Functions

cached_validation_wrapper([key])

Decorator to check for a cached validation flag before executing a function.

Module Contents

evolving_ideas.infra.open_ai_client.logger
evolving_ideas.infra.open_ai_client.cache
exception evolving_ideas.infra.open_ai_client.OpenAIClientError

Bases: Exception

Custom exception for OpenAI client errors.

class evolving_ideas.infra.open_ai_client.OpenAITransport(api_key: str | None = None)

OpenAI client for interacting with the OpenAI API.

client
get_models() dict

# Get the available models from OpenAI.

Returns:

A dictionary categorizing available models.

Return type:

dict

chat_completion(model: str, messages: list, temperature: float = 0.7) dict

Chat completion using the OpenAI API.

Parameters:
  • model (str) – The model to use (e.g., “gpt-4”).

  • messages (list) – The messages to send to the model.

  • temperature (float) – The temperature for the model (default is 0.7).

Returns:

The response from the model.

Return type:

dict

abstract create_thread()

Create a new thread for the OpenAI API.

abstract run_thread(assistant_id: str, thread_id: str)

Run a thread for the OpenAI API.

Parameters:
  • assistant_id (str) – The ID of the assistant.

  • thread_id (str) – The ID of the thread.

abstract add_message(thread_id: str, role: str, content: str)

Add a message to a thread.

Parameters:
  • thread_id (str) – The ID of the thread.

  • role (str) – The role of the message sender (e.g., “user”, “assistant”).

  • content (str) – The content of the message.

abstract get_messages(thread_id: str)

Get messages from a thread.

Parameters:

thread_id (str) – The ID of the thread.

evolving_ideas.infra.open_ai_client.cached_validation_wrapper(key: str = 'valid_api_key')

Decorator to check for a cached validation flag before executing a function.

Parameters:

key – Key in the cached file to verify.

class evolving_ideas.infra.open_ai_client.OpenAICredentialValidator(client: openai.OpenAI)

Validates OpenAI API credentials.

client
validate() bool

Validate the OpenAI API key.

Returns:

True if the API key is valid, False otherwise.

Return type:

bool

Raises:

OpenAIClientError – If the API key is invalid.

class evolving_ideas.infra.open_ai_client.OpenAILLM(model='gpt-4.1', transport: OpenAITransport | None = None, **kwargs)

Bases: evolving_ideas.infra.llm_interface.LLMInterface

OpenAI Language Model Wrapper

name: str = 'openai'
model = 'gpt-4.1'
transport = None
ask(prompt: str, context: str | None = 'You are a helpful assistant.') str

Ask the OpenAI model a question and return the answer.

Parameters:
  • prompt (str) – The question to ask.

  • role (str) – The role of the user (e.g., “user”, “assistant”).

  • content (str) – The content of the message.

Returns:

The answer from the model.

Return type:

str

chat(chatlog: list[dict]) str

Chat with the OpenAI model using a chat log.

Parameters:

chatlog (list[dict]) – The chat log to send to the model.

Returns:

The response from the model.

Ytype:

str