Skip to content

Commit

Permalink
feat: Added custom providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Aug 1, 2024
1 parent 34aa93f commit c19e028
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions OpenAI.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=anyscale/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=MIRACL/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=MTEB/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ GetCurrentWeather({"location":"Dubai, UAE","unit":"celsius"})
The current temperature in Dubai, UAE is 22°C with sunny weather.
```

### Custom providers
```csharp
using OpenAI;

using var api = Providers.GitHubModels("GITHUB_TOKEN");
using var api = Providers.Azure("API_KEY", "ENDPOINT");
using var api = Providers.DeepInfra("API_KEY");
using var api = Providers.DeepSeek("API_KEY");
using var api = Providers.Fireworks("API_KEY");
using var api = Providers.OpenRouter("API_KEY");
using var api = Providers.Together("API_KEY");
```

### Constants
All `tryGetXXX` methods return `null` if the value is not found.
There also non-try methods that throw an exception if the value is not found.
Expand Down
92 changes: 92 additions & 0 deletions src/libs/OpenAI/Providers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// ReSharper disable once CheckNamespace
namespace OpenAI;

/// <summary>
/// Predefined providers uses OpenAI SDK compatible API.
/// </summary>
public static class Providers
{
/// <summary>
/// Create an API to use for GitHub Models: https://github.com/marketplace/models
/// </summary>
/// <returns></returns>
public static OpenAiApi GitHubModels(string githubToken)
{
var api = new OpenAiApi(baseUri: new Uri("https://models.inference.ai.azure.com/"));
api.AuthorizeUsingBearer(githubToken);

return api;
}

/// <summary>
/// Create an API to use for Azure.
/// </summary>
/// <returns></returns>
public static OpenAiApi Azure(string apiKey, string endpoint)
{
var api = new OpenAiApi(baseUri: new Uri(endpoint));
api.AuthorizeUsingBearer(apiKey);

return api;
}

/// <summary>
/// Create an API to use for DeepInfra.
/// </summary>
/// <returns></returns>
public static OpenAiApi DeepInfra(string apiKey)
{
var api = new OpenAiApi(baseUri: new Uri("https://api.deepinfra.com/"));
api.AuthorizeUsingBearer(apiKey);

return api;
}

/// <summary>
/// Create an API to use for DeepSeek.
/// </summary>
/// <returns></returns>
public static OpenAiApi DeepSeek(string apiKey)
{
var api = new OpenAiApi(baseUri: new Uri("https://api.deepseek.com/"));
api.AuthorizeUsingBearer(apiKey);

return api;
}

/// <summary>
/// Create an API to use for Fireworks AI.
/// </summary>
/// <returns></returns>
public static OpenAiApi Fireworks(string apiKey)
{
var api = new OpenAiApi(baseUri: new Uri("https://api.fireworks.ai/inference/v1"));
api.AuthorizeUsingBearer(apiKey);

return api;
}

/// <summary>
/// Create an API to use for Open Router.
/// </summary>
/// <returns></returns>
public static OpenAiApi OpenRouter(string apiKey)
{
var api = new OpenAiApi(baseUri: new Uri("https://openrouter.ai/api"));
api.AuthorizeUsingBearer(apiKey);

return api;
}

/// <summary>
/// Create an API to use for Together AI.
/// </summary>
/// <returns></returns>
public static OpenAiApi Together(string apiKey)
{
var api = new OpenAiApi(baseUri: new Uri("https://api.together.xyz/"));
api.AuthorizeUsingBearer(apiKey);

return api;
}
}

0 comments on commit c19e028

Please sign in to comment.