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.
- π¬ 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
- π Model Listing: Retrieve available AI models
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.
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());
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());
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);
}
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());
}
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());
}
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);
To retrieve a list of available AI models:
var modelsResponse = await groqApi.ListModelsAsync();
if (modelsResponse != null && modelsResponse.TryGetPropertyValue("data", out var dataNode))
{
var models = dataNode.AsArray();
foreach (var model in models)
{
Console.WriteLine(model?["id"]?.GetValue<string>());
}
}
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}");
}
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
This library is licensed under the MIT License. Mention J. Gravelle if you use this code. He's sort of full of himself.
- 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! π