Removable.AIGatewayDotNet 0.0.6

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Removable.AIGatewayDotNet --version 0.0.6
NuGet\Install-Package Removable.AIGatewayDotNet -Version 0.0.6
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="Removable.AIGatewayDotNet" Version="0.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Removable.AIGatewayDotNet --version 0.0.6
#r "nuget: Removable.AIGatewayDotNet, 0.0.6"
#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 Removable.AIGatewayDotNet as a Cake Addin
#addin nuget:?package=Removable.AIGatewayDotNet&version=0.0.6

// Install Removable.AIGatewayDotNet as a Cake Tool
#tool nuget:?package=Removable.AIGatewayDotNet&version=0.0.6

AIGatewayDotNet

A .NET client of the CloudFlare AI Gateway.

Installation

dotnet add package AIGatewayDotNet

or

Install-Package AIGatewayDotNet

Usage

Add single service

Add the configuration to your project:

"AIGatewayOptions": {
  "CloudFlareAccountTag": "Your CloudFlare Account Tag goes here",
  "CloudFlareGateway": "CloudFlare Gateway name goes here",
  "CloudFlareGatewayVersion": "v1",
  "Provider": "Azure or OpenAI (case insensitive)",
  "ApiKey": "Your api key goes here",
  "AzureResourceName": "If you are using Azure, the resource name goes here",
  "AzureApiVersion": "If you are using Azure, the api version goes here",
  "OpenAiOrganization": "If you are using OpenAI, the organization id goes here (It's an optional field)."
}

Then, add the service to your project:

services.AddAIGatewayService();

The service will bind the configuration automatically.

Add multiple services

Add a separate configuration for each service with a different field name. For example:

"AIGatewayOptions": {
  "OpenAI": {
    "CloudFlareAccountTag": "Your CloudFlare Account Tag goes here",
    // Other fields
  },
  "Azure": {
    "CloudFlareAccountTag": "Your CloudFlare Account Tag goes here",
    // Other fields
  }
}

Then, create a service for each configuration:

public class OpenAIService : AIGatewayService
{
    public const string SettingKey = "OpenAI";
    [ActivatorUtilitiesConstructor]
    public AIGatewayService(HttpClient httpClient, IOptionsSnapshot<OpenAiOptions> settings) : base(settings.Get(SettingKey),httpClient){}
    public AIGatewayService(OpenAiOptions settings, HttpClient? httpClient = null) : base(settings, httpClient){}
}

public class AzureService : AIGatewayService
{
    public const string SettingKey = "Azure";
    [ActivatorUtilitiesConstructor]
    public AIGatewayService(HttpClient httpClient, IOptionsSnapshot<AzureOptions> settings) : base(settings.Get(SettingKey),httpClient){}
    public AIGatewayService(AzureOptions settings, HttpClient? httpClient = null) : base(settings, httpClient){}
}

Then, add the services to your project:

services.AddAIGatewayService<OpenAIService>(OpenAIService.SettingKey);
services.AddAIGatewayService<AzureService>(AzureService.SettingKey);

Use the service

Get the service from the DI container:

// If you have a single service
var gatewayService = serviceProvider.GetRequiredService<IAIGatewayService>();

or

// If you have multiple services
var openAiGateway = serviceProvider.GetRequiredService<OpenAIService>();
var azureGateway = serviceProvider.GetRequiredService<AzureService>();

Then, use the service to make requests:

var chatCompletionCreateRequest = new ChatCompletionCreateRequest
{
    MaxTokens = 1000,
    Model = "gpt-3.5-turbo",
    Messages =
    [
        ChatMessage.FromSystem("You are a helpful assistant."),
        ChatMessage.FromUser("What is the meaning of life?")
    ]
};

var streamResponse = gatewayService.ChatCompletionCreateStream(chatCompletionCreateRequest);
await foreach (var res in streamResponse)
{
    Console.WriteLine($"Response: {res.Choices.FirstOrDefault()?.Delta?.Content}");
}

Thanks

Part of the code is based on the betalgo/openai project.

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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.8 81 4/21/2024
0.0.7 90 4/21/2024
0.0.6 72 4/19/2024
0.0.5 84 4/16/2024
0.0.4 78 4/15/2024
0.0.3 81 4/12/2024