PredictionGuard.NET is a .NET Standard 2.1 wrapper for the PredictionGuard API. The library supports the chat completions endpoint (with and without streaming), including function calling.
- Chat Completions: Generate text completions based on input prompts.
- Function Calling: Invoke functions based on chat input.
- Embeddings: Generate embeddings for text and image inputs.
builder.Services.AddPredictionGuardChatClient(config["ApiKey"]);
Customize the endpoint, specify a model, etc.
builder.Services.AddPredictionGuardChatClient(config["ApiKey"], options =>
{
options.Endpoint = new Uri(config["Endpoint"]);
options.Model = config["Model"];
});
Console Application
var predictionGuardChatClient = app.Services.GetRequiredService<PredictionGuardChatClient>();
Blazor
@inject PredictionGuardChatClient PredictionGuardChatClient
API controller
private readonly PredictionGuardChatClient _predictionGuardChatClient;
public MyController(PredictionGuardChatClient predictionGuardChatClient)
{
_predictionGuardChatClient = predictionGuardChatClient;
}
var responseText = await predictionGuardChatClient.CompleteAsync(input);
await foreach (var chunk in predictionGuardChatClient.CompleteStreamingAsync(input))
{
Console.Write(chunk);
}
The library is capable of invoking one or more functions to assist with the user query. Function output is sent back to the API, which then generates a natural language response based on the data.
builder.Services.AddPredictionGuardChatClient(config["ApiKey"])
.UseFunctionInvocation();
MethodInfo getForecastMethod = ToolBuilder.GetMethod<WeatherService>("GetForecast");
ChatOptions chatOptions = new()
{
Tools = [ getForecastMethod ]
};
var predictionGuardChatClient = app.Services.GetRequiredService<PredictionGuardChatClient>();
var responseText = await predictionGuardChatClient.CompleteAsync("Do I need an umbrella today in Nantes?", chatOptions);
public class Weather
{
public int Day { get; set; }
public string Location { get; set; }
public string Summary { get; set; }
}
[Description("Gets a weather forecast for the given number of days")]
public List<Weather> GetForecast(string location, int numDays)
{
List<Weather> forecast = new();
for (int i = 0; i < numDays; i++)
{
forecast.Add(new Weather() { Day = i, Location = location, Summary = Random.Shared.NextDouble() > 0.5 ? "Sunny" : "Rainy" });
}
return forecast;
}
builder.Services.AddPredictionGuardEmbeddingsClient(config["ApiKey"]);
Customize the endpoint, specify a model, etc.
builder.Services.AddPredictionGuardEmbeddingsClient(config["ApiKey"], options =>
{
options.Endpoint = new Uri(config["Endpoint"]);
options.Model = config["Model"];
});
var embeddings = await predictionGuardEmbeddingsClient.GenerateEmbeddingsAsync("Your input text here");
var inputs = new List<EmbeddingInput>
{
new EmbeddingInput
{
Text = "Your input text here"
},
new EmbeddingInput
{
Text = "Another input"
}
};
var embeddings = await predictionGuardEmbeddingsClient.GenerateEmbeddingsAsync(inputs);
foreach (var embeddingData in embeddings)
{
Console.WriteLine($"Index: {embeddingData.Index}");
Console.WriteLine($"Embedding: {string.Join(", ", embeddingData.Embedding)}");
}