agentlab.llm.llm_utils
Functions
|
Compress a string by replacing redundant paragraphs and lines with identifiers. |
|
|
|
|
|
|
|
Extract the content within HTML tags for a list of keys. |
|
|
|
|
|
Convert a numpy array to a base64 encoded image url. |
|
Parse a json message for the retry function. |
|
|
|
Satisfy the parse api, extracts 1 match per key and validates that all keys are present |
|
A version of parse_html_tags that raises an exception if the parsing is not successful. |
|
Retry querying the chat models with the response from the parser until it returns a valid value. |
|
Retry querying the chat models with the response from the parser until it returns a valid value. |
|
Use tiktoken to truncate a text to a maximum number of tokens. |
|
Parse a yaml message for the retry function. |
Classes
|
|
|
|
|
|
|
|
|
Exceptions
- class agentlab.llm.llm_utils.AIMessage(content: str | list[dict])
Bases:
BaseMessage
- class agentlab.llm.llm_utils.BaseMessage(role: str, content: str | list[dict])
Bases:
dict- merge()
Merges content elements of type ‘text’ if they are adjacent.
- to_markdown()
- class agentlab.llm.llm_utils.Discussion(messages: list[BaseMessage] | BaseMessage = None)
Bases:
object- add_message(message: BaseMessage | dict = None, role: str = None, content: str | list[dict] = None)
- append(message: BaseMessage | dict)
- property last_message
- merge()
- to_markdown()
- to_openai()
- to_string()
- class agentlab.llm.llm_utils.HumanMessage(content: str | list[dict])
Bases:
BaseMessage
- exception agentlab.llm.llm_utils.RetryError
Bases:
ValueError
- class agentlab.llm.llm_utils.SystemMessage(content: str | list[dict])
Bases:
BaseMessage
- agentlab.llm.llm_utils.compress_string(text)
Compress a string by replacing redundant paragraphs and lines with identifiers.
- agentlab.llm.llm_utils.count_tokens(text, model='openai/gpt-4')
- agentlab.llm.llm_utils.extract_html_tags(text, keys)
Extract the content within HTML tags for a list of keys.
All text and keys will be converted to lowercase before matching.
- agentlab.llm.llm_utils.get_tokenizer(model_name='gpt-4')
- agentlab.llm.llm_utils.get_tokenizer_old(model_name='openai/gpt-4')
- agentlab.llm.llm_utils.image_to_jpg_base64_url(image: ndarray | Image)
Convert a numpy array to a base64 encoded image url.
- agentlab.llm.llm_utils.json_parser(message)
Parse a json message for the retry function.
- agentlab.llm.llm_utils.parse_html_tags(text, keys=(), optional_keys=(), merge_multiple=False)
Satisfy the parse api, extracts 1 match per key and validates that all keys are present
- Parameters:
- Returns:
A dictionary mapping each key to a subset of text that match the key. bool: Whether the parsing was successful. str: A message to be displayed to the agent if the parsing was not successful.
- Return type:
- agentlab.llm.llm_utils.parse_html_tags_raise(text, keys=(), optional_keys=(), merge_multiple=False)
A version of parse_html_tags that raises an exception if the parsing is not successful.
- agentlab.llm.llm_utils.retry(chat: ChatModel, messages: Discussion, n_retry: int, parser: callable, log: bool = True)
Retry querying the chat models with the response from the parser until it returns a valid value.
If the answer is not valid, it will retry and append to the chat the retry message. It will stop after n_retry.
Note, each retry has to resend the whole prompt to the API. This can be slow and expensive.
- Parameters:
chat (ChatModel) – a ChatModel object taking a list of messages and returning a list of answers, all in OpenAI format.
messages (list) – the list of messages so far. This list will be modified with the new messages and the retry messages.
n_retry (int) – the maximum number of sequential retries.
parser (callable) – a function taking a message and retruning a parsed value, or raising a ParseError
log (bool) – whether to log the retry messages.
- Returns:
the parsed value, with a string at key “action”.
- Return type:
- Raises:
ParseError – if the parser could not parse the response after n_retry retries.
- agentlab.llm.llm_utils.retry_multiple(chat: ChatModel, messages: Discussion, n_retry: int, parser: callable, log: bool = True, num_samples: int = 1)
Retry querying the chat models with the response from the parser until it returns a valid value.
If the answer is not valid, it will retry and append to the chat the retry message. It will stop after n_retry.
Note, each retry has to resend the whole prompt to the API. This can be slow and expensive.
- Parameters:
chat (ChatModel) – a ChatModel object taking a list of messages and returning a list of answers, all in OpenAI format.
messages (list) – the list of messages so far. This list will be modified with the new messages and the retry messages.
n_retry (int) – the maximum number of sequential retries.
parser (callable) – a function taking a message and retruning a parsed value, or raising a ParseError
log (bool) – whether to log the retry messages.
num_samples (int) – the number of samples to generate from the model.
- Returns:
the parsed value, with a string at key “action”.
- Return type:
- Raises:
ParseError – if the parser could not parse the response after n_retry retries.
- agentlab.llm.llm_utils.truncate_tokens(text, max_tokens=8000, start=0, model_name='gpt-4')
Use tiktoken to truncate a text to a maximum number of tokens.
- agentlab.llm.llm_utils.yaml_parser(message)
Parse a yaml message for the retry function.