GroqSharp 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package GroqSharp --version 1.0.0                
NuGet\Install-Package GroqSharp -Version 1.0.0                
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="GroqSharp" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GroqSharp --version 1.0.0                
#r "nuget: GroqSharp, 1.0.0"                
#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 GroqSharp as a Cake Addin
#addin nuget:?package=GroqSharp&version=1.0.0

// Install GroqSharp as a Cake Tool
#tool nuget:?package=GroqSharp&version=1.0.0                

GroqSharp

GroqSharp is a C# client library that makes it easy to interact with GroqCloud. It's designed to provide a simple and flexible interface, allowing you to seamlessly integrate the Groq service into your C# applications.

Why GroqSharp?

GroqSharp aims to simplify and streamline your interactions with Groq, offering:

  • Fluent API: Set up client options and parameters fluently for convenient configuration.
  • Structured Responses: Define specific JSON response structures for more predictable outputs.
  • Retry Mechanism: Configurable retry policies ensure robust interactions, even in the face of transient errors.
  • Streaming Support: Allows for streaming responses, processing data as it becomes available.

Getting Started

Installation

To install GroqSharp via NuGet:

dotnet add package GroqSharp

API Key and Model

You'll need an API key and model to connect to the Groq service. Once acquired, they can be integrated as follows:

var apiKey = "<your-api-key>";
var apiModel = "llama3-70b-8192"; // Or other supported

var groqClient = new GroqClient(apiKey, apiModel);

Fluent Configuration

You can configure your client fluently by chaining various setup options:

IGroqClient groqClient = new GroqClient(apiKey, apiModel)
    .SetTemperature(0.5)
    .SetMaxTokens(512)
    .SetTopP(1)
    .SetStop("NONE")
    .SetStructuredRetryPolicy(5) // Retry up to 5 times on failure
    .SetDefaultSystemMessage(new Message { Role = MessageRole.System, Content = "You are a drunk assistant with a love for fluffy carpets." }); // Default system message

Examples

1. Synchronous Chat Completion (Non-JSON)
var response = await groqClient.CreateChatCompletionAsync(
    new Message { Role = MessageRole.System, Content = "You are a helpful, smart, kind, and efficient AI assistant. You always fulfill the user's requests to the best of your ability." },
    new Message { Role = MessageRole.Assistant, Content = "The user only knows the pain from using commercial models like ChatGPT, and it's the first time they're using Groq." },
    new Message { Role = MessageRole.User, Content = "Explain the importance of fast language models." }
);
2. Synchronous Chat Completion (JSON)

You can customize the default retry of 3 using the fluent SetRetryPolicy. Since models can inherently not return JSON, be sure to handle error conditions. Be aware that extra costs can be incurred with excessive retries. The client will automatically add JSON instructions to the system message, but feel free to add more.

var jsonStructure = @"
{
    ""name"": ""string"",
    ""powers"": {
        ""rank"": ""string"",
        ""abilities"": ""string""
    }
}
";

try
{
    response = await groqClient.GetStructuredChatCompletionAsync(jsonStructure,
        new Message { Role = MessageRole.System, Content = "You are a helpful, smart, kind, and efficient AI assistant." },
        new Message { Role = MessageRole.User, Content = "Give me a few Pok�mon characters." }
    );

    Console.WriteLine(response);
}
catch (Exception ex)
{
    Console.WriteLine("Error: " + ex.Message);
}
3. Streaming Chat Completion
try
{
    var messages = new[]
    {
        new Message { Role = MessageRole.User, Content = "Give some lyrics to a song." },
        new Message { Role = MessageRole.System, Content = "You are the love child of Britney Spears and Eminem." }
    };

    await foreach (var streamingResponse in groqClient.CreateChatCompletionStreamAsync(messages))
    {
        Console.Write(streamingResponse);
    }
    Console.WriteLine();
}
catch (Exception ex)
{
    Console.WriteLine("Error: " + ex.Message);
}

Conclusion

GroqSharp offers a flexible and streamlined way to work with the Groq service in your C# projects. You can easily configure its settings, handle structured responses, and manage different interaction types, making it an invaluable tool for any C# developer looking to integrate Groq's capabilities.

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.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on GroqSharp:

Package Downloads
LangChain.Providers.Groq

Groq model provider.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.2 2,592 5/7/2024
1.1.1 104 5/7/2024
1.1.0 136 5/6/2024
1.0.3 117 5/6/2024
1.0.2 115 4/29/2024
1.0.1 109 4/29/2024
1.0.0 116 4/29/2024

Initial release of GroqSharp:
- Fluent API for setting up client configurations.
- Supports synchronous and streaming chat completions.
- Offers structured response handling with custom JSON formats.
- Configurable retry policy for robust interactions.