agentlab.llm.llm_utils

Functions

compress_string(text)

Compress a string by replacing redundant paragraphs and lines with identifiers.

count_tokens(text[, model])

download_and_save_model(model_name[, save_dir])

extract_code_blocks(text)

extract_html_tags(text, keys)

Extract the content within HTML tags for a list of keys.

get_tokenizer([model_name])

get_tokenizer_old([model_name])

image_to_jpg_base64_url(image)

Convert a numpy array to a base64 encoded image url.

json_parser(message)

Parse a json message for the retry function.

messages_to_dict(messages)

parse_html_tags(text[, keys, optional_keys, ...])

Satisfy the parse api, extracts 1 match per key and validates that all keys are present

parse_html_tags_raise(text[, keys, ...])

A version of parse_html_tags that raises an exception if the parsing is not successful.

retry(chat, messages, n_retry, parser[, log])

Retry querying the chat models with the response from the parser until it returns a valid value.

retry_multiple(chat, messages, n_retry, parser)

Retry querying the chat models with the response from the parser until it returns a valid value.

truncate_tokens(text[, max_tokens, start, ...])

Use tiktoken to truncate a text to a maximum number of tokens.

yaml_parser(message)

Parse a yaml message for the retry function.

Classes

AIMessage(content)

BaseMessage(role, content)

Discussion([messages])

HumanMessage(content)

SystemMessage(content)

Exceptions

ParseError

RetryError

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

add_content(type: str, content: Any)
add_image(image: ndarray | Image | str, detail: str = None)
add_text(text: str)
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_content(type: str, content: Any)

Add content to the last message.

add_image(image: ndarray | Image | str, detail: str = None)

Add an image to the last message.

add_message(message: BaseMessage | dict = None, role: str = None, content: str | list[dict] = None)
add_text(text: str)

Add text to the last message.

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.ParseError

Bases: Exception

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.download_and_save_model(model_name: str, save_dir: str = '.')
agentlab.llm.llm_utils.extract_code_blocks(text) list[tuple[str, str]]
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.

Parameters:
  • text (str) – The input string containing the HTML tags.

  • keys (list[str]) – The HTML tags to extract the content from.

Returns:

A dictionary mapping each key to a list of subset in text that match the key.

Return type:

dict

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.messages_to_dict(messages: list[dict] | list[BaseMessage]) dict
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:
  • text (str) – The input string containing the HTML tags.

  • keys (list[str]) – The HTML tags to extract the content from.

  • optional_keys (list[str]) – The HTML tags to extract the content from, but are optional.

  • merge_multiple (bool) – Whether to merge multiple instances of the same key.

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:

dict

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:

dict

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:

list[dict]

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.