GithubHelp home page GithubHelp logo

aigatewaydotnet's Introduction

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.

aigatewaydotnet's People

Contributors

removable avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.