service.llm.openai module

class service.llm.openai.OPENAI(api_key, cache_collection=None, **kwargs)[source]

Bases: BASE_LLM_CACHE

A class to interact with OpenAI’s language model while using a cache mechanism to optimize requests. Inherits from BASE_LLM_CACHE.

class service.llm.openai.OPENAI_SERVICE[source]

Bases: object

A service class for managing OPENAI LLM requests through a queue mechanism using MongoDB and RabbitMQ.

collection = Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'llm'), 'openai')
static get_response(job_id)[source]

Retrieves the response of a job with the given ID.

Parameters:

job_id (ObjectId) – The ID of the job to retrieve the response for.

Returns:

The response of the job, or None if the job is not found or has not completed.

Return type:

str

static get_response_sync(job_id, timeout=300)[source]

Retrieves the response of a job with the given ID synchronously.

Parameters:
  • job_id (ObjectId) – The ID of the job to retrieve the response for.

  • timeout (int, optional) – The maximum time to wait for the job to complete.

Returns:

The response of the job, or None if the job is not found or has not completed within the timeout.

Return type:

str

static launch_worker()[source]

Launches a worker to process jobs from the RabbitMQ queue. The worker interacts with the LLM and stores the response back in MongoDB.

logger = <Logger service.llm.openai (INFO)>
queue_name = 'llm-openai'
static trigger(parent_service, parent_job_id=None, use_cache=False, **query)[source]

Creates and triggers a new job for an LLM request.

Parameters:
  • caller_service (str) – The service initiating the request.

  • use_cache (bool, optional) – Whether to use cached responses if available.

  • **query – The query to send to the LLM.

Returns:

The job ID of the triggered request.

Return type:

str

service.llm.openai.now(tz=None)

Returns new datetime object representing current time local to tz.

tz

Timezone object.

If no tz is specified, uses local timezone.