Skip to main content

mellea.backends.tools

Utilities for dealing with tools.

Functions

add_tools_from_model_options

add_tools_from_model_options(tools_dict: dict[str, Callable], model_options: dict[str, Any])
If model_options has tools, add those tools to the tools_dict.

add_tools_from_context_actions

add_tools_from_context_actions(tools_dict: dict[str, Callable], ctx_actions: list[Component | CBlock] | None)
If any of the actions in ctx_actions have tools in their template_representation, add those to the tools_dict.

convert_tools_to_json

convert_tools_to_json(tools: dict[str, Callable]) -> list[dict]
Convert tools to json dict representation. Notes:
  • Huggingface transformers library lets you pass in an array of functions but doesn’t like methods.
  • WatsonxAI uses from langchain_ibm.chat_models import convert_to_openai_tool in their demos, but it gives the same values.
  • OpenAI uses the same format / schema.

json_extraction

json_extraction(text: str) -> Generator[dict, None, None]
Yields the next valid json object in a given string.

find_func

find_func(d) -> tuple[str | None, Mapping | None]
Find the first function in a json-like dictionary. Most llms output tool requests in the form ...{"name": string, "arguments": {}}...

parse_tools

parse_tools(llm_response: str) -> list[tuple[str, Mapping]]
A simple parser that will scan a string for tools and attempt to extract them.
I