openai
OpenAI specific module.
Hint
Use pip to install the necessary dependencies for this module:
pip install mltb2[openai]
- class mltb2.openai.OpenAiAzureChat(azure_endpoint: str, model: str, api_key: str | None = None, base_url: str | None = None, api_version: str | None = None, azure_ad_token: str | None = None)[source]
Bases:
OpenAiChat
,_OpenAiAzureChatBase
Tool to interact with Azure OpenAI chat models.
This can also be constructed with
from_yaml()
.See also
OpenAI API reference: Create chat completion
Quickstart: Get started generating text using Azure OpenAI Service
- Parameters:
- class mltb2.openai.OpenAiChat(model: str, api_key: str | None = None, base_url: str | None = None)[source]
Bases:
object
Tool to interact with OpenAI chat models.
This also be constructed with
from_yaml()
.See also
OpenAI API reference: Create chat completion
- Parameters:
- create_completions(prompt: str | list[dict[str, str]], completion_kwargs: dict[str, Any] | None = None, clean_openai_tokens: bool = False) OpenAiChatResult [source]
Create a model response for the given prompt (chat conversation).
- Parameters:
prompt (str | list[dict[str, str]]) – The prompt for the model.
completion_kwargs (dict[str, Any] | None) –
Keyword arguments for the OpenAI completion.
model
can not be set viacompletion_kwargs
! Please set themodel
in the initializer.messages
can not be set viacompletion_kwargs
! Please set theprompt
argument.
Also see:
openai.resources.chat.completions.Completions.create()
OpenAI API reference: Create chat completion
clean_openai_tokens (bool) – Remove OpenAI special tokens from the prompt.
- Returns:
The result of the OpenAI completion.
- Return type:
- async create_completions_async(prompt: str | list[dict[str, str]], completion_kwargs: dict[str, Any] | None = None, clean_openai_tokens: bool = False) OpenAiChatResult [source]
Create a model response for the given prompt (chat conversation).
- Parameters:
prompt (str | list[dict[str, str]]) – The prompt for the model.
completion_kwargs (dict[str, Any] | None) –
Keyword arguments for the OpenAI completion.
model
can not be set viacompletion_kwargs
! Please set themodel
in the initializer.messages
can not be set viacompletion_kwargs
! Please set theprompt
argument.
Also see:
openai.resources.chat.completions.Completions.create()
OpenAI API reference: Create chat completion
clean_openai_tokens (bool) – Remove OpenAI special tokens from the prompt.
- Returns:
The result of the OpenAI completion.
- Return type:
- classmethod from_yaml(yaml_file, api_key: str | None = None, **kwargs)[source]
Construct this class from a yaml file.
If the
api_key
is not set in the yaml file, it will be loaded from the environment variableOPENAI_API_KEY
.- Parameters:
yaml_file – The yaml file.
api_key (str | None) – The OpenAI API key.
kwargs – extra kwargs to override parameters
- Returns:
The constructed class.
- class mltb2.openai.OpenAiChatResult(content: str | None = None, model: str | None = None, prompt_tokens: int | None = None, completion_tokens: int | None = None, total_tokens: int | None = None, finish_reason: str | None = None, completion_args: dict[str, Any] | None = None)[source]
Bases:
object
Result of an OpenAI chat completion.
If you want to convert this to a
dict
useasdict(open_ai_chat_result)
from thedataclasses
module.See also
OpenAI API reference: The chat completion object
- Parameters:
content (str | None) – the result of the OpenAI completion
model (str | None) – model name which has been used
prompt_tokens (int | None) – number of tokens of the prompt
completion_tokens (int | None) – number of tokens of the completion (
content
)total_tokens (int | None) – number of total tokens (
prompt_tokens + content_tokens
)finish_reason (str | None) –
The reason why the completion stopped.
stop
: Means the API returned the full completion without running into any token limit.length
: Means the API stopped the completion because of running into a token limit.content_filter
: When content was omitted due to a flag from the OpenAI content filters.tool_calls
: When the model called a tool.function_call
(deprecated): When the model called a function.
completion_args (dict[str, Any] | None) –
The arguments which have been used for the completion. Examples:
model
: always setmax_tokens
: only set ifcompletion_kwargs
containedmax_tokens
temperature
: only set ifcompletion_kwargs
containedtemperature
top_p
: only set ifcompletion_kwargs
containedtop_p
- class mltb2.openai.OpenAiTokenCounter(model_name: str, show_progress_bar: bool = False)[source]
Bases:
object
Count OpenAI tokens.
- Parameters:
- class mltb2.openai._OpenAiAzureChatBase(azure_endpoint: str)[source]
Bases:
object
- Parameters:
azure_endpoint (str)