Autogen OpenAi Class
Autogen OpenAi Class
openai
class OpenAIChatCompletionClient(**kwargs: Unpack)[source]
Bases: BaseOpenAIChatCompletionClient,
Component[OpenAIClientConfigurationConfigModel]
For non-OpenAI models, please first take a look at our community extensions for additional
model clients.
Parameters
:
model (str) – Which OpenAI model to use.
api_key (optional, str) – The API key to use. Required if ‘OPENAI_API_KEY’ is not found in
the environment variables.
base_url (optional, str) – The base URL to use. Required if the model is not hosted on
OpenAI.
model_info (optional, ModelInfo) – The capabilities of the model. Required if the model name
is not a valid OpenAI model.
n (optional, int)
default_headers (optional, dict[str, str]) – Custom headers; useful for authentication or other
custom requirements.
Examples
The following code snippet shows how to use the client with an OpenAI model:
openai_client = OpenAIChatCompletionClient(
model="gpt-4o-2024-08-06",
# api_key="sk-...", # Optional if you have an OPENAI_API_KEY environment variable set.
)
custom_model_client = OpenAIChatCompletionClient(
model="deepseek-r1:1.5b",
base_url="http://localhost:11434/v1",
api_key="placeholder",
model_info={
"vision": False,
"function_calling": False,
"json_output": False,
"family": ModelFamily.R1,
},
)
To use structured output as well as function calling, you can use the following code snippet:
import asyncio
from typing import Literal
asyncio.run(main())
To load the client from a configuration, you can use the load_component method:
config = {
"provider": "OpenAIChatCompletionClient",
"config": {"model": "gpt-4o", "api_key": "REPLACE_WITH_YOUR_API_KEY"},
}
client = ChatCompletionClient.load_component(config)
To view the full list of available configuration options, see the
OpenAIClientConfigurationConfigModel class.
component_config_schema
alias of OpenAIClientConfigurationConfigModel
_to_config() → OpenAIClientConfigurationConfigModel[source]
Dump the configuration that would be requite to create a new instance of a component
matching the configuration of this instance.
Returns
:
T – The configuration of the component.
Parameters
:
config (T) – The configuration object.
Returns
:
Self – The new instance of the component.
Parameters
:
model (str) – Which OpenAI model to use.
azure_endpoint (str) – The endpoint for the Azure model. Required for Azure models.
azure_deployment (str) – Deployment name for the Azure model. Required for Azure
models.
api_version (str) – The API version to use. Required for Azure models.
api_key (optional, str) – The API key to use, use this if you are using key based
authentication. It is optional if you are using Azure AD token based authentication or
AZURE_OPENAI_API_KEY environment variable.
model_info (optional, ModelInfo) – The capabilities of the model. Required if the model name
is not a valid OpenAI model.
n (optional, int)
default_headers (optional, dict[str, str]) – Custom headers; useful for authentication or other
custom requirements.
To use this client, you must install the azure and openai extensions:
The following code snippet shows how to use AAD authentication. The identity used must be
assigned the Cognitive Services OpenAI User role.
config = {
"provider": "AzureOpenAIChatCompletionClient",
"config": {
"model": "gpt-4o-2024-05-13",
"azure_endpoint": "https://{your-custom-endpoint}.openai.azure.com/",
"azure_deployment": "{your-azure-deployment}",
"api_version": "2024-06-01",
"azure_ad_token_provider": {
"provider": "autogen_ext.auth.azure.AzureTokenProvider",
"config": {
"provider_kind": "DefaultAzureCredential",
"scopes": ["https://cognitiveservices.azure.com/.default"],
},
},
},
}
client = ChatCompletionClient.load_component(config)
To view the full list of available configuration options, see the
AzureOpenAIClientConfigurationConfigModel class.
Note
Right now only DefaultAzureCredential is supported with no additional args passed to it.
See here for how to use the Azure client directly or for more info.
component_config_schema
alias of AzureOpenAIClientConfigurationConfigModel
component_provider_override: ClassVar[str | None] =
'autogen_ext.models.openai.AzureOpenAIChatCompletionClient'
Override the provider string for the component. This should be used to prevent internal
module names being a part of the module name.
_to_config() → AzureOpenAIClientConfigurationConfigModel[source]
Dump the configuration that would be requite to create a new instance of a component
matching the configuration of this instance.
Returns
:
T – The configuration of the component.
Parameters
:
config (T) – The configuration object.
Returns
:
Self – The new instance of the component.
json_output (Optional[bool], optional) – If True, the output will be in JSON format. Defaults to
None.
Yields
:
AsyncGenerator[Union[str, CreateResult], None] – A generator yielding the completion
results as they are produced.
In streaming, the default behaviour is not return token usage counts. See: [OpenAI API
reference for possible args](https://platform.openai.com/docs/api-reference/chat/create).
However extra_create_args={“stream_options”: {“include_usage”: True}} will (if supported by
the accessed API) return a final chunk with usage set to a RequestUsage object having
prompt and completion token counts, all preceding chunks will have usage as None. See:
[stream_options](https://platform.openai.com/docs/api-reference/chat/create#chat-create-
stream_options).
top_p (float): An alternative to sampling with temperature, called nucleus sampling, where
the model considers the results of the tokens with top_p probability mass.
frequency_penalty (float): A value between -2.0 and 2.0 that penalizes new tokens based on
their existing frequency in the text so far, decreasing the likelihood of repeated phrases.
presence_penalty (float): A value between -2.0 and 2.0 that penalizes new tokens based on
whether they appear in the text so far, encouraging the model to talk about new topics.
actual_usage() → RequestUsage[source]
total_usage() → RequestUsage[source]
count_tokens(messages: Sequence[Annotated[SystemMessage | UserMessage |
AssistantMessage | FunctionExecutionResultMessage,
FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools:
Sequence[Tool | ToolSchema] = []) → int[source]
remaining_tokens(messages: Sequence[Annotated[SystemMessage |
UserMessage | AssistantMessage | FunctionExecutionResultMessage,
FieldInfo(annotation=NoneType, required=True, discriminator='type')]], *, tools:
Sequence[Tool | ToolSchema] = []) → int[source]
property capabilities: ModelCapabilities
property model_info: ModelInfo
pydantic model AzureOpenAIClientConfigurationConfigModel[source]
Bases: BaseOpenAIClientConfigurationConfigModel
azure_endpoint (str)
model (str)
n (int | None)