Skip to content

Agent with Tools

Defining a Function tool

Any function method can be used as a tool. Try to include DescriptionAttribute where you can to help define the function and its parameters by more than just its name.

csharp

[JsonConverter(typeof(StringEnumConverter))]
public enum Unit
{
    Celsius, 
    Fahrenheit
}

[Description("Get the current weather in a given location")]
public static string GetCurrentWeather(
    [Description("The city and state, e.g. Boston, MA")] string location,
    [Description("unit of temperature measurement in C or F")] Unit unit = Unit.Celsius)
{
    // Call the weather API here.
    return $"31 C";
}

[JsonConverter(typeof(StringEnumConverter))] is required to deserialize Json enums properly.

Your First Chat Bot with tools

csharp
using LlmTornado;
using LlmTornado.Agents;
using LlmTornado.Chat.Models;

TornadoApi api = new TornadoApi("your-api-key");

TornadoAgent agent = new TornadoAgent(api, ChatModel.OpenAi.Gpt41.V41Mini, instructions: "You are a useful assistant.", tools:[GetCurrentWeather]);

Conversation conv = agent.Client.Chat.CreateConversation(agent.Options);

Console.Write("\n[Assistant]: Hello");
string topic = "";
while (topic != "exit")
{
    Console.Write("\n[User]: ");
    topic = Console.ReadLine();
    if (topic == "exit") break;
    Console.Write("\n[Assistant]: ");
    conv = await agent.RunAsync(topic, appendMessages: conv.Messages.ToList());
    Console.Write(conv.Messages.Last().Content);
}

Setup API key

csharp
TornadoApi api = new TornadoApi("your-api-key");

Create the agent

tools is a list of any delegate that will return a string. Both sync and async functions are supported.

csharp
TornadoAgent agent = new TornadoAgent(api, ChatModel.OpenAi.Gpt41.V41Mini, instructions: "You are a useful assistant.", tools:[GetCurrentWeather]);

Setup a Conversation to keep message history

csharp
Conversation conv = agent.Client.Chat.CreateConversation(agent.Options);

Setup a simple chat loop to continue conversation

csharp
Console.Write("\n[Assistant]: Hello");
string topic = "";
while (topic != "exit")
{
    Console.Write("\n[User]: ");
    topic = Console.ReadLine();
    if (topic == "exit") break;
    Console.Write("\n[Assistant]: ");

    ...
}