LlmTornado 3.1.19

There is a newer version of this package available.
See the version list below for details.
dotnet add package LlmTornado --version 3.1.19                
NuGet\Install-Package LlmTornado -Version 3.1.19                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LlmTornado" Version="3.1.19" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LlmTornado --version 3.1.19                
#r "nuget: LlmTornado, 3.1.19"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LlmTornado as a Cake Addin
#addin nuget:?package=LlmTornado&version=3.1.19

// Install LlmTornado as a Cake Tool
#tool nuget:?package=LlmTornado&version=3.1.19                

LlmTornado

🌪️ LLM Tornado - one .NET library to consume OpenAI, Anthropic, Cohere, Google, Azure, Groq, and self-hosted APIs.

Each month at least one new large language model is released. Would it be awesome if using the new model was as easy as switching one argument? LLM Tornado acts as an aggregator allowing you to do just that. Think SearX but for LLMs!

OpenAI, Anthropic, Cohere, Google, Azure, and Groq (LLama 3, Mixtral, Gemma 2..) are currently supported along with KoboldCpp and Ollama.

The following video captures one conversation, running across OpenAI, Cohere, and Anthropic, with parallel tools calling & streaming:

https://github.com/lofcz/LlmTornado/assets/10260230/05c27b37-397d-4b4c-96a4-4138ade48dbe

Try it, the code of the demo is here! Try asking for previously fetched information and verify the context is correctly constructed even when switching Providers mid-conversation.

⚡Getting Started

Install LLM Tornado via NuGet:

Install-Package LlmTornado

🔮 Quick Inference

Switching vendors

Switching the vendor is as easy as changing ChatModel argument. Tornado instance can be constructed with multiple API keys, the correct key is then used based on the model.

TornadoApi api = new TornadoApi(new List<ProviderAuthentication>
{
    new ProviderAuthentication(LLmProviders.OpenAi, "OPEN_AI_KEY"),
    new ProviderAuthentication(LLmProviders.Anthropic, "ANTHROPIC_KEY"),
    new ProviderAuthentication(LLmProviders.Cohere, "COHERE_KEY"),
    new ProviderAuthentication(LLmProviders.Google, "GOOGLE_KEY"),
    new ProviderAuthentication(LLmProviders.Groq, "GROQ_KEY")
});

List<ChatModel> models =
[
    ChatModel.OpenAi.Gpt4.Turbo,
    ChatModel.Anthropic.Claude3.Sonnet,
    ChatModel.Cohere.Claude3.CommandRPlus,
    ChatModel.Google.Gemini.Gemini15Flash,
    ChatModel.Groq.Meta.Llama370B
];

foreach (ChatModel model in models)
{
    string? response = await api.Chat.CreateConversation(model)
        .AppendSystemMessage("You are a fortune teller.")
        .AppendUserInput("What will my future bring?")
        .GetResponse();
    
    Console.WriteLine(response);
}

Streaming

Tornado offers several levels of abstraction, trading more details for more complexity. The simple use cases where only plaintext is needed can be represented in a terse format.

await api.Chat.CreateConversation(ChatModel.Anthropic.Claude3.Sonnet)
    .AppendSystemMessage("You are a fortune teller.")
    .AppendUserInput("What will my future bring?")
    .StreamResponse(Console.Write);

Tools with deferred resolve

When plaintext is insufficient, switch to GetResponseRich() or StreamResponseRich() APIs. Tools requested by the model can be resolved later and never returned to the model. This is useful in scenarios where we use the tools without intending to continue the conversation.

Conversation chat = api.Chat.CreateConversation(new ChatRequest
{
    Model = ChatModel.OpenAi.Gpt4.Turbo,
    Tools = new List<Tool>
    {
        new Tool
        {
            Function = new ToolFunction("get_weather", "gets the current weather")
        }
    },
    ToolChoice = new OutboundToolChoice(OutboundToolChoiceModes.Required)
});

chat.AppendUserInput("Who are you?"); // user asks something unrelated, but we force the model to use the tool
ChatRichResponse response = await chat.GetResponseRich(); // the response contains one block of type Function

GetResponseRichSafe() API is also available, which is guaranteed not to throw on the network level. The response is wrapped in a network-level wrapper, containing additional information. For production use cases, either use try {} catch {} on all the HTTP request producing Tornado APIs, or use the safe APIs.

Tools with immediate resolve

Tools requested by the model can also be resolved and the results returned immediately. This has the benefit of automatically continuing the conversation.

Conversation chat = api.Chat.CreateConversation(new ChatRequest
{
    Model = ChatModel.OpenAi.Gpt4.O,
    Tools =
    [
        new Tool(new ToolFunction("get_weather", "gets the current weather", new
        {
            type = "object",
            properties = new
            {
                location = new
                {
                    type = "string",
                    description = "The location for which the weather information is required."
                }
            },
            required = new List<string> { "location" }
        }))
    ]
})
.AppendSystemMessage("You are a helpful assistant")
.AppendUserInput("What is the weather like today in Prague?");

ChatStreamEventHandler handler = new ChatStreamEventHandler
{
  MessageTokenHandler = (x) =>
  {
      Console.Write(x);
      return Task.CompletedTask;
  },
  FunctionCallHandler = (calls) =>
  {
      calls.ForEach(x => x.Result = new FunctionResult(x, "A mild rain is expected around noon.", null));
      return Task.CompletedTask;
  },
  AfterFunctionCallsResolvedHandler = async (results, handler) => { await chat.StreamResponseRich(handler); }
};

await chat.StreamResponseRich(handler);

REPL

This interactive demo can be expanded into an end-user-facing interface in the style of ChatGPT. Shows how to use strongly typed tools together with streaming and resolve parallel tool calls. ChatStreamEventHandler is a convenient class allowing subscription to only the events your use case needs.

public static async Task OpenAiFunctionsStreamingInteractive()
{
    // 1. set up a sample tool using a strongly typed model
    ChatPluginCompiler compiler = new ChatPluginCompiler();
    compiler.SetFunctions([
        new ChatPluginFunction("get_weather", "gets the current weather in a given city", [
            new ChatFunctionParam("city_name", "name of the city", ChatPluginFunctionAtomicParamTypes.String)
        ])
    ]);
    
    // 2. in this scenario, the conversation starts with the user asking for the current weather in two of the supported cities.
    // we can try asking for the weather in the third supported city (Paris) later.
    Conversation chat = api.Chat.CreateConversation(new ChatRequest
    {
        Model = ChatModel.OpenAi.Gpt4.Turbo,
        Tools = compiler.GetFunctions()
    }).AppendUserInput("Please call functions get_weather for Prague and Bratislava (two function calls).");

    // 3. repl
    while (true)
    {
        // 3.1 stream the response from llm
        await StreamResponse();

        // 3.2 read input
        while (true)
        {
            Console.WriteLine();
            Console.Write("> ");
            string? input = Console.ReadLine();

            if (input?.ToLowerInvariant() is "q" or "quit")
            {
                return;
            }
            
            if (!string.IsNullOrWhiteSpace(input))
            {
                chat.AppendUserInput(input);
                break;
            }
        }
    }

    async Task StreamResponse()
    {
        await chat.StreamResponseRich(new ChatStreamEventHandler
        {
            MessageTokenHandler = async (token) =>
            {
                Console.Write(token);
            },
            FunctionCallHandler = async (fnCalls) =>
            {
                foreach (FunctionCall x in fnCalls)
                {
                    if (!x.TryGetArgument("city_name", out string? cityName))
                    {
                        x.Result = new FunctionResult(x, new
                        {
                            result = "error",
                            message = "expected city_name argument"
                        }, null, true);
                        continue;
                    }

                    x.Result = new FunctionResult(x, new
                    {
                        result = "ok",
                        weather = cityName.ToLowerInvariant() is "prague" ? "A mild rain" : cityName.ToLowerInvariant() is "paris" ? "Foggy, cloudy" : "A sunny day"
                    }, null, true);
                }
            },
            AfterFunctionCallsResolvedHandler = async (fnResults, handler) =>
            {
                await chat.StreamResponseRich(handler);
            }
        });
    }
}

Other endpoints such as Images, Embedding, Speech, Assistants, Threads and Vision are also supported!
Check the links for simple to-understand examples!

Why Tornado?

  • 25,000+ installs on NuGet under previous names Lofcz.Forks.OpenAI, OpenAiNg.
  • Used in commercial projects incurring charges of thousands of dollars monthly.
  • The license will never change. Looking at you HashiCorp and Tiny.
  • Supports streaming, functions/tools, modalities (images, audio), and strongly typed LLM plugins/connectors.
  • Great performance, nullability annotations.
  • Extensive tests suite.
  • Maintained actively for over a year.

Documentation

Every public class, method, and property has extensive XML documentation, using LLM Tornado should be intuitive if you've used any other LLM library previously. Feel free to open an issue here if you have any questions.

PRs are welcome! ❤️

License

This library is licensed under MIT license.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on LlmTornado:

Package Downloads
LlmTornado.Contrib

Provides extra functionality to LlmTornado.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.29 79 12/19/2024
3.1.28 65 12/19/2024
3.1.27 120 12/14/2024
3.1.26 367 11/22/2024
3.1.25 81 11/22/2024
3.1.24 96 11/21/2024
3.1.23 89 11/20/2024
3.1.22 95 11/20/2024
3.1.21 94 11/18/2024
3.1.20 74 11/18/2024
3.1.19 88 11/17/2024
3.1.18 82 11/16/2024
3.1.17 153 11/5/2024
3.1.16 85 11/4/2024
3.1.15 178 10/22/2024
3.1.14 345 9/14/2024
3.1.13 163 9/1/2024
3.1.12 165 8/20/2024
3.1.11 128 8/18/2024
3.1.10 117 8/6/2024
3.1.9 102 8/6/2024
3.1.8 97 7/24/2024
3.1.7 83 7/24/2024
3.1.6 79 7/23/2024
3.1.5 118 7/19/2024
3.1.4 100 7/19/2024
3.1.3 144 6/23/2024
3.1.2 134 6/15/2024
3.1.1 108 6/15/2024
3.1.0 100 6/15/2024
3.0.17 110 6/8/2024
3.0.16 99 6/8/2024
3.0.15 151 5/21/2024
3.0.14 117 5/21/2024
3.0.13 136 5/20/2024
3.0.11 115 5/18/2024
3.0.10 120 5/15/2024
3.0.9 138 5/15/2024
3.0.8 120 5/9/2024
3.0.7 143 5/5/2024
3.0.6 92 5/2/2024
3.0.5 98 5/1/2024
3.0.4 101 5/1/2024
3.0.3 95 5/1/2024
3.0.2 97 5/1/2024
3.0.1 124 4/27/2024
3.0.0 124 4/27/2024

chat: audio streaming modality