GroqApiLibrary 1.0.5
See the version list below for details.
dotnet tool install --global GroqApiLibrary --version 1.0.5
dotnet new tool-manifest # if you are setting up this repo dotnet tool install --local GroqApiLibrary --version 1.0.5
#tool dotnet:?package=GroqApiLibrary&version=1.0.5
nuke :add-package GroqApiLibrary --version 1.0.5
Groq API C# Client Library
Welcome to the Groq API C# Client Library! This powerful and flexible library provides a seamless interface to interact with the cutting-edge Groq AI API. Designed for .NET 8 and above, our library offers a range of features to enhance your AI-powered applications.
🌟 Features
- 💬 Chat Completions: Engage in dynamic conversations with AI models
- 🔊 Audio Transcription: Convert speech to text with high accuracy
- 🌐 Audio Translation: Translate audio content across languages
- 🛠️ Tool Usage: Extend AI capabilities with custom tools
- 🌊 Streaming Support: Real-time responses for interactive applications
📦 Installation
To use this library in your .NET 8+ project:
- Clone this repository or download the
GroqApiClient.cs
file. - Add the file to your project.
- Ensure your project targets .NET 8 or later.
🚀 Quick Start
Here's a simple example to get you started:
using GroqApiLibrary;
using System.Text.Json.Nodes;
var apiKey = "your_api_key_here";
var groqApi = new GroqApiClient(apiKey);
var request = new JsonObject
{
["model"] = "mixtral-8x7b-32768",
["messages"] = new JsonArray
{
new JsonObject
{
["role"] = "user",
["content"] = "Hello, Groq! What can you do?"
}
}
};
var result = await groqApi.CreateChatCompletionAsync(request);
Console.WriteLine(result?["choices"]?[0]?["message"]?["content"]?.ToString());
📚 Detailed Usage
Chat Completions
Standard Chat Completion
var request = new JsonObject
{
["model"] = "mixtral-8x7b-32768",
["temperature"] = 0.7,
["max_tokens"] = 150,
["messages"] = new JsonArray
{
new JsonObject
{
["role"] = "system",
["content"] = "You are a helpful assistant."
},
new JsonObject
{
["role"] = "user",
["content"] = "Write a haiku about artificial intelligence."
}
}
};
var result = await groqApi.CreateChatCompletionAsync(request);
Console.WriteLine(result?["choices"]?[0]?["message"]?["content"]?.ToString());
Streaming Chat Completion
var request = new JsonObject
{
["model"] = "mixtral-8x7b-32768",
["messages"] = new JsonArray
{
new JsonObject
{
["role"] = "user",
["content"] = "Explain the concept of quantum entanglement."
}
}
};
await foreach (var chunk in groqApi.CreateChatCompletionStreamAsync(request))
{
var delta = chunk?["choices"]?[0]?["delta"]?["content"]?.ToString() ?? string.Empty;
Console.Write(delta);
}
Audio Transcription
using (var audioStream = File.OpenRead("path/to/your/audio.mp3"))
{
var result = await groqApi.CreateTranscriptionAsync(
audioStream,
"audio.mp3",
"whisper-large-v3",
prompt: "Transcribe the following tech conference",
language: "en"
);
Console.WriteLine(result?["text"]?.ToString());
}
Audio Translation
using (var audioStream = File.OpenRead("path/to/your/french_audio.mp3"))
{
var result = await groqApi.CreateTranslationAsync(
audioStream,
"french_audio.mp3",
"whisper-large-v3",
prompt: "Translate the following French speech to English"
);
Console.WriteLine(result?["text"]?.ToString());
}
Tool Usage
Enhance your AI's capabilities by integrating custom tools. Here's an example using a simple math calculator:
var calculateTool = new Tool
{
Type = "function",
Function = new Function
{
Name = "calculate",
Description = "Perform a mathematical calculation",
Parameters = new JsonObject
{
["type"] = "object",
["properties"] = new JsonObject
{
["expression"] = new JsonObject
{
["type"] = "string",
["description"] = "The mathematical expression to evaluate"
}
},
["required"] = new JsonArray { "expression" }
},
ExecuteAsync = async (args) =>
{
var jsonArgs = JsonDocument.Parse(args);
var expression = jsonArgs.RootElement.GetProperty("expression").GetString();
try
{
var result = new System.Data.DataTable().Compute(expression, null);
return JsonSerializer.Serialize(new { result = result.ToString() });
}
catch (Exception ex)
{
return JsonSerializer.Serialize(new { error = $"Error calculating: {ex.Message}" });
}
}
}
};
var tools = new List<Tool> { calculateTool };
var model = "mixtral-8x7b-32768";
var systemMessage = "You are an assistant that can perform calculations.";
var userPrompt = "What is the square root of 144 plus 50?";
var result = await groqApi.RunConversationWithToolsAsync(userPrompt, tools, model, systemMessage);
Console.WriteLine(result);
🎛️ Advanced Configuration
Supported Models
Our library supports a wide range of Groq models, including:
- mixtral-8x7b-32768
- llama3-70b-8192
- llama3-8b-8192
- gemma-7b-it
Error Handling
The library uses exception handling to manage errors. Always wrap your API calls in try-catch blocks for robust error management:
try
{
var result = await groqApi.CreateChatCompletionAsync(request);
// Process result
}
catch (HttpRequestException e)
{
Console.WriteLine($"API request failed: {e.Message}");
}
catch (JsonException e)
{
Console.WriteLine($"Failed to parse API response: {e.Message}");
}
🛠️ Contributing
We welcome contributions to the Groq API C# Client Library! If you have suggestions for improvements or bug fixes, please:
- Fork the repository
- Create a new branch for your feature
- Commit your changes
- Push to the branch
- Create a new Pull Request
📄 License
This library is licensed under the MIT License. See the LICENSE file for details. Mention J. Gravelle if you use this code. He's sort of full of himself.
🙏 Acknowledgements
- Special thanks to the Groq team for their incredible AI models and API.
- Shoutout to all contributors who have helped improve this library.
We hope you enjoy using the Groq API C# Client Library! If you have any questions or need further assistance, please open an issue in this repository. Happy coding! 🚀
Product | Versions 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. |
This package has no dependencies.