LLMTool and the LLMTool Repository

Bob Sandheinrich

What is a tool for an LLM?

An LLM tool is a code that runs outside of the LLM (like in Wolfram Language) that the LLM can call in the middle of a conversation by providing a specific response.

Using an LLM tool turns a single interaction into a sequence of messages

Why do we need tools?

LLMs are great at writing text (like really great). But they don’t compute like computers normally compute. They can be comically wrong about facts that we’re accustomed to relying on computers for.

randmath="20904.2134234^4.234234";

LLMSynthesize[randmath]

ToExpression[randmath]

Wolfram Language is great at computation (like really great). Tools allow the LLM to ask for help with specific operations.

LLMSynthesize[randmath,LLMEvaluator-><|"Tools"->{LLMResourceTool["WolframLanguageEvaluator"]}|>]

LLMTool

The primary symbol for LLM tools is LLMTool

A simple tool

simpletool=LLMTool["DemoTool",{"param1"->"String"},f[#param1]&]

The parameters  are usually provided by the LLM, but can be provided manually:

simpletool[<|"param1"->"hello world"|>]

A more interesting tool

A tool for converting currency amounts within a body of text:

currencytool=LLMTool[{"CurrencyConvert","Convert all currency values within a sentence to a different currency. The tool expects full sentences containing multiple currency values.
For example, a tool call like this
{currency: \"Euro\",
text: \"I found $10 and C$5 in my jacket pocket.\"}
Will create a tool response like this
\"I found €9.55 and €3.48 in my jacket pocket.\""},
{
"text"->"String",
"currency"->"CurrencyName"
},
StringReplace[#text,Rule@@@MapAt[Function[{qu},TextString@CurrencyConvert[qu,#currency]],TextCases[#text,"CurrencyAmount"->{"String","Interpretation"}],{All,2}]]&]

Test it manually:

currencytool[<|"text"->"The cup of coffee cost $3.50","currency"->"yen"|>]

Programmatic Use

Wolfram`Chatbook`SetModel["GPT4"]

Use the currency tool in LLMSynthesize:

result=LLMSynthesize["Create brief conversation about the price of gasoline. Then convert the entire conversation to use Euros using the tool. Respond with both the original and converted conversations.",All,LLMEvaluator-><|"Tools"->{currencytool}|>];

result["CompletionText"]

result["ToolRequests"]

Last[result["ToolResponses"]]["Output"]

HighlightString[result["FullText"],Style[#,Background->Lighter[RandomColor[],.8]]&/@Keys[result["FullTextAnnotations"]]]

It also works in other functions

jingleconvert=LLMFunction[StringTemplate["Write a jingle about a candy bar that only costs a quarter. Then use the tool to convert the jingle for the currency of ``"],LLMEvaluator-><|"Tools"->{currencytool},"Temperature"->0.9|>]

jingleconvert/@{"Austria","Australia"}

Notebook Use

Several standard tools are available in Chat Notebook. The available tools depends on the selected persona.

Customizing tool use in Chat Notebooks is a work-in-progress.

nbo=CreateNotebook["Chat"]

Personas and Tools

Prompt resources (like personas from the Prompt Repository) will be able to include default tools to include when used in a chat notebook.

For example a “ProofWriter” persona could include a tool to search an index of mathematical proofs.

Default tools can be tools from the LLMTool repository or custom tools only for that persona.

NotebookOpen[FileNameJoin[{,"LLMToolChatSample.nb"}]]

LLMTool Repository

The LLMTool Repository is a new resource for finding and publishing useful tools.

Hyperlink[Labeled[Import["/Users/bobs/wolfram-workspace/stash/resourcesystem/ResourceSystem/Kernel/Repositories/LLMToolRepository/Website/webresources/repo-logo.svg",ImageSize->500],Style["Wolfram LLMTool Repository",32,FontFamily->"Source Sans Code"]],"https://resources.wolframcloud.com/LLMToolRepository"]

Using Repository Tools

To use a tool from the repository in code use LLMResourceTool. The tool is provided as a template, because often you want to configure the tool before giving it to the LLM.

tooltemplate=LLMResourceTool["TextStorage"]

tool=tooltemplate[<|"Location"->"Local"|>]

Use the tool manually to store content:

tool[<|"content"->"Demo text created by a human and his inferior meat brain","name"->"RepositoryToolDemo"|>]

Another tool exists to retrieve the content:

retrievaltool=LLMResourceTool["TextRetrieval"][<|"Location"->"Local"|>]

retrievaltool[<|"name"->"RepositoryToolDemo","property"->"Content"|>]

Creating an LLMTool for the Repository

Just like other Wolfram repositories, you can create an LLM tool for the repository using a definition notebook.

ResourceFunction["CreateResourceNotebook"]["LLMTool"]

Learn More

More Talks

Chat Notebook Basics  - Thursday 11AM CT

Advanced Chat Notebook Use  - Thursday 11:30 AM CT

The Future of API Integration - Friday 8:30AM CT

Workshop: LLM Tech  - Friday 2PM CT

Information Links

LLMTool Repository

Prompt Repository

Stephen Wolfram's announcement of Chat Notebooks

Support getting started with Chat Notebooks

Video Links

Wolfram U Chat Videos | YouTube

Prompt Repository Introduction (with ME!)