language_models
langroid/language_models/init.py
LLMMessage
¶
Bases: BaseModel
Class representing an entry in the msg-history sent to the LLM API. It could be one of these: - a user message - an LLM ("Assistant") response - a fn-call or tool-call-list from an OpenAI-compatible LLM API response - a result or results from executing a fn or tool-call(s)
api_dict()
¶
Convert to dictionary for API request, keeping ONLY the fields that are expected in an API call! E.g., DROP the tool_id, since it is only for use in the Assistant API, not the completion API. Returns: dict: dictionary representation of LLM message
Source code in langroid/language_models/base.py
LLMFunctionCall
¶
Bases: BaseModel
Structure of LLM response indicating it "wants" to call a function.
Modeled after OpenAI spec for function_call
field in ChatCompletion API.
from_dict(message)
staticmethod
¶
Initialize from dictionary. Args: d: dictionary containing fields to initialize
Source code in langroid/language_models/base.py
LLMFunctionSpec
¶
Bases: BaseModel
Description of a function available for the LLM to use.
To be used when calling the LLM chat()
method with the functions
parameter.
Modeled after OpenAI spec for functions
fields in ChatCompletion API.
LLMResponse
¶
Bases: BaseModel
Class representing response from LLM.
to_LLMMessage()
¶
Convert LLM response to an LLMMessage, to be included in the message-list sent to the API. This is currently NOT used in any significant way in the library, and is only provided as a utility to construct a message list for the API when directly working with an LLM object.
In a ChatAgent
, an LLM response is first converted to a ChatDocument,
which is in turn converted to an LLMMessage via ChatDocument.to_LLMMessage()
See ChatAgent._prep_llm_messages()
and ChatAgent.llm_response_messages
Source code in langroid/language_models/base.py
get_recipient_and_message()
¶
If message
or function_call
of an LLM response contains an explicit
recipient name, return this recipient name and message
stripped
of the recipient name if specified.
Two cases:
(a) message
contains addressing string "TO: message
is empty and function_call/tool_call with explicit recipient
Returns:
Type | Description |
---|---|
str
|
name of recipient, which may be empty string if no recipient |
str
|
content of message |
Source code in langroid/language_models/base.py
OpenAIChatModel
¶
Bases: str
, Enum
Enum for OpenAI Chat models
AnthropicModel
¶
Bases: str
, Enum
Enum for Anthropic models
OpenAICompletionModel
¶
Bases: str
, Enum
Enum for OpenAI Completion models
OpenAIGPTConfig(**kwargs)
¶
Bases: LLMConfig
Class for any LLM with an OpenAI-like API: besides the OpenAI models this includes: (a) locally-served models behind an OpenAI-compatible API (b) non-local models, using a proxy adaptor lib like litellm that provides an OpenAI-compatible API. We could rename this class to OpenAILikeConfig.
Source code in langroid/language_models/openai_gpt.py
create(prefix)
classmethod
¶
Create a config class whose params can be set via a desired prefix from the .env file or env vars. E.g., using
you can have a group of params prefixed by "OLLAMA_", to be used with models served viaollama
.
This way, you can maintain several setting-groups in your .env file,
one per model type.
Source code in langroid/language_models/openai_gpt.py
OpenAIGPT(config=OpenAIGPTConfig())
¶
Bases: LanguageModel
Class for OpenAI LLMs
Source code in langroid/language_models/openai_gpt.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
chat_context_length()
¶
Context-length for chat-completion models/endpoints Get it from the dict, otherwise fail-over to general method
Source code in langroid/language_models/openai_gpt.py
completion_context_length()
¶
Context-length for completion models/endpoints Get it from the dict, otherwise fail-over to general method
Source code in langroid/language_models/openai_gpt.py
chat_cost()
¶
(Prompt, Generation) cost per 1000 tokens, for chat-completion models/endpoints. Get it from the dict, otherwise fail-over to general method
Source code in langroid/language_models/openai_gpt.py
set_stream(stream)
¶
Enable or disable streaming output from API. Args: stream: enable streaming output from API Returns: previous value of stream
Source code in langroid/language_models/openai_gpt.py
get_stream()
¶
tool_deltas_to_tools(tools)
staticmethod
¶
Convert accumulated tool-call deltas to OpenAIToolCall objects. Adapted from this excellent code: https://community.openai.com/t/help-for-function-calls-with-streaming/627170/2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tools |
List[Dict[str, Any]]
|
list of tool deltas received from streaming API |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
plain text corresponding to tool calls that failed to parse |
List[OpenAIToolCall]
|
List[OpenAIToolCall]: list of OpenAIToolCall objects |
|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: list of tool dicts (to reconstruct OpenAI API response, so it can be cached) |
Source code in langroid/language_models/openai_gpt.py
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
|
MockLM(config=MockLMConfig())
¶
Bases: LanguageModel
Source code in langroid/language_models/mock_lm.py
chat(messages, max_tokens=200, tools=None, tool_choice='auto', functions=None, function_call='auto')
¶
Mock chat function for testing
Source code in langroid/language_models/mock_lm.py
achat(messages, max_tokens=200, tools=None, tool_choice='auto', functions=None, function_call='auto')
async
¶
Mock chat function for testing
Source code in langroid/language_models/mock_lm.py
generate(prompt, max_tokens=200)
¶
agenerate(prompt, max_tokens=200)
async
¶
MockLMConfig
¶
Bases: LLMConfig
Mock Language Model Configuration.
Attributes:
Name | Type | Description |
---|---|---|
response_dict |
Dict[str, str]
|
A "response rule-book", in the form of a dictionary; if last msg in dialog is x,then respond with response_dict[x] |
AzureConfig(**kwargs)
¶
Bases: OpenAIGPTConfig
Configuration for Azure OpenAI GPT.
Attributes:
Name | Type | Description |
---|---|---|
type |
str
|
should be |
api_version |
str
|
can be set in the |
deployment_name |
str
|
can be set in the |
model_name |
str
|
can be set in the |
model_version |
str
|
can be set in the |
Source code in langroid/language_models/openai_gpt.py
AzureGPT(config)
¶
Bases: OpenAIGPT
Class to access OpenAI LLMs via Azure. These env variables can be obtained from the
file .azure_env
. Azure OpenAI doesn't support completion
Attributes:
config (AzureConfig): AzureConfig object
api_key (str): Azure API key
api_base (str): Azure API base url
api_version (str): Azure API version
model_name (str): the name of gpt model in your deployment
model_version (str): the version of gpt model in your deployment
Source code in langroid/language_models/azure_openai.py
set_chat_model()
¶
Sets the chat model configuration based on the model name specified in the
.env
. This function checks the model_name
in the configuration and sets
the appropriate chat model in the config.chat_model
. It supports handling for
'35-turbo' and 'gpt-4' models. For 'gpt-4', it further delegates the handling
to handle_gpt4_model
method. If the model name does not match any predefined
models, it defaults to OpenAIChatModel.GPT4
.
Source code in langroid/language_models/azure_openai.py
handle_gpt4_model()
¶
Handles the setting of the GPT-4 model in the configuration.
This function checks the model_version
in the configuration.
If the version is not set, it raises a ValueError indicating
that the model version needs to be specified in the .env
file. It sets OpenAIChatMode.GPT4o
if the version is
'2024-05-13', OpenAIChatModel.GPT4_TURBO
if the version is
'1106-Preview', otherwise, it defaults to setting
OpenAIChatModel.GPT4
.