OpenAIHandler Class
Internal class for calls to OpenAI API's.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Inheritance
-
OpenAIHandlerOpenAIHandler
Constructor
OpenAIHandler(*, client: AsyncOpenAI, ai_model_type: OpenAIModelTypes = OpenAIModelTypes.CHAT, prompt_tokens: int = 0, completion_tokens: int = 0, total_tokens: int = 0)
Keyword-Only Parameters
Name | Description |
---|---|
client
Required
|
|
ai_model_type
|
Default value: OpenAIModelTypes.CHAT
|
prompt_tokens
Required
|
|
completion_tokens
Required
|
|
total_tokens
Required
|
|
Methods
store_usage |
Store the usage information from the response. |
store_usage
Store the usage information from the response.
store_usage(response: ChatCompletion | Completion | AsyncStream[ChatCompletionChunk] | AsyncStream[Completion] | CreateEmbeddingResponse)
Parameters
Name | Description |
---|---|
response
Required
|
|
Attributes
model_computed_fields
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}
model_config
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'validate_assignment': True}
model_fields
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.fields from Pydantic V1.
model_fields: ClassVar[Dict[str, FieldInfo]] = {'ai_model_type': FieldInfo(annotation=OpenAIModelTypes, required=False, default=<OpenAIModelTypes.CHAT: 'chat'>), 'client': FieldInfo(annotation=AsyncOpenAI, required=True), 'completion_tokens': FieldInfo(annotation=int, required=False, default=0), 'prompt_tokens': FieldInfo(annotation=int, required=False, default=0), 'total_tokens': FieldInfo(annotation=int, required=False, default=0)}
ai_model_type
ai_model_type: OpenAIModelTypes
client
client: AsyncOpenAI
completion_tokens
completion_tokens: int
prompt_tokens
prompt_tokens: int
total_tokens
total_tokens: int