GroqApiLibrary 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet tool install --global GroqApiLibrary --version 1.0.3                
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local GroqApiLibrary --version 1.0.3                
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=GroqApiLibrary&version=1.0.3                
nuke :add-package GroqApiLibrary --version 1.0.3                

Groq API C# Client Library

This library provides a simple interface to interact with the Groq AI API. It allows you to send requests to the API and receive responses asynchronously using .NET 8.

Installation

To use this library in your .NET 8 project:

  1. Copy the GroqApiClient.cs file into your project.
  2. Ensure your project targets .NET 8 or later.

Usage

  1. Create an instance of the GroqApiClient class, providing your API key.
  2. Create a JsonObject with your request parameters as documented in the Groq API documentation.
  3. Use the client to send requests and receive responses.

Examples

Standard Chat Completion

using GroqApiLibrary;
using System.Text.Json.Nodes;

class Program
{
    static async Task Main(string[] args)
    {
        string apiKey = "your_api_key_here";
        var groqApi = new GroqApiClient(apiKey);

        var request = new JsonObject
        {
            ["model"] = "mixtral-8x7b-32768", // Other models: llama2-70b-chat, gemma-7b-it, llama3-70b-8192, llama3-8b-8192
            ["temperature"] = 0.5,
            ["max_tokens"] = 100,
            ["top_p"] = 1,
            ["stop"] = "TERMINATE",
            ["messages"] = new JsonArray
            {
                new JsonObject
                {
                    ["role"] = "system",
                    ["content"] = "You are a helpful assistant."
                },
                new JsonObject
                {
                    ["role"] = "user",
                    ["content"] = "Write a haiku about coding."
                }
            }
        };

        var result = await groqApi.CreateChatCompletionAsync(request);
        var response = result?["choices"]?[0]?["message"]?["content"]?.ToString() ?? "No response found";
        Console.WriteLine(response);
    }
}

Streaming Chat Completion

using GroqApiLibrary;
using System.Text.Json.Nodes;

class Program
{
    static async Task Main()
    {
        string apiKey = "your_api_key_here";
        var groqApi = new GroqApiClient(apiKey);

        var request = new JsonObject
        {
            ["model"] = "mixtral-8x7b-32768",
            ["temperature"] = 0.5,
            ["max_tokens"] = 100,
            ["top_p"] = 1,
            ["stop"] = "TERMINATE",
            ["messages"] = new JsonArray
            {
                new JsonObject
                {
                    ["role"] = "system",
                    ["content"] = "You are a helpful assistant."
                },
                new JsonObject
                {
                    ["role"] = "user",
                    ["content"] = "Explain quantum computing in simple terms."
                }
            }
        };

        await foreach (var chunk in groqApi.CreateChatCompletionStreamAsync(request))
        {
            var delta = chunk?["choices"]?[0]?["delta"]?["content"]?.ToString() ?? string.Empty;
            Console.Write(delta);
        }
    }
}

Features

  • Built for .NET 8, taking advantage of the latest C# features.
  • Uses System.Text.Json for efficient JSON handling.
  • Supports both synchronous and streaming API calls.
  • Implements IDisposable for proper resource management.
  • Nullable aware, helping to prevent null reference exceptions.

Latest Updates

  • Upgraded to .NET 8 compatibility.
  • Removed dependency on Newtonsoft.Json, now using System.Text.Json.
  • Improved null handling with nullable reference types.
  • Simplified API calls using HttpClient.PostAsJsonAsync.
  • Enhanced streaming support with IAsyncEnumerable.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This library is licensed under the MIT License. See the LICENSE file for more information.

Special Thanks

  • Marcus Cazzola for significant contributions to the library's development.
  • Joaquin Grech for advocating the transition from Newtonsoft.Json to System.Text.Json.
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.

This package has no dependencies.

Version Downloads Last updated
1.0.8 194 10/4/2024
1.0.6 137 8/30/2024
1.0.5 94 8/2/2024
1.0.4 62 7/31/2024
1.0.3 95 7/25/2024
1.0.2 90 7/23/2024
1.0.1 93 7/23/2024
1.0.0 102 7/22/2024