Skip to content

Commit

Permalink
feat: Added Perplexity custom provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Sep 10, 2024
1 parent f2c501f commit 6b955c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/libs/OpenAI/CustomProviders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public static class CustomProviders
/// </summary>
public const string TogetherBaseUrl = "https://api.together.xyz/";

/// <summary>
///
/// </summary>
public const string PerplexityBaseUrl = "https://api.perplexity.ai/";

/// <summary>
/// Creates an API to use for GitHub Models: https://github.com/marketplace/models
/// </summary>
Expand Down Expand Up @@ -98,4 +103,13 @@ public static OpenAiApi Together(string apiKey)
{
return new OpenAiApi(apiKey, baseUri: new Uri(TogetherBaseUrl));
}

/// <summary>
/// Create an API to use for Perplexity AI.
/// </summary>
/// <returns></returns>
public static OpenAiApi Perplexity(string apiKey)
{
return new OpenAiApi(apiKey, baseUri: new Uri(PerplexityBaseUrl));
}
}
1 change: 1 addition & 0 deletions src/tests/OpenAI.IntegrationTests/CustomProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public enum CustomProvider
DeepSeek,
OpenRouter,
GitHub,
Perplexity,
}
9 changes: 8 additions & 1 deletion src/tests/OpenAI.IntegrationTests/Tests.Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public partial class Tests
[DataRow(CustomProvider.OpenRouter)]
[DataRow(CustomProvider.Together)]
[DataRow(CustomProvider.GitHub)]
[DataRow(CustomProvider.Perplexity)]
public async Task GenerateFiveRandomWords(CustomProvider customProvider)
{
var pair = GetAuthorizedChatApi(customProvider);
Expand All @@ -18,7 +19,13 @@ public async Task GenerateFiveRandomWords(CustomProvider customProvider)
string response = await api.Chat.CreateChatCompletionAsync(
messages: ["Generate five random words."],
model: pair.Model,
user: "tryAGI.OpenAI.IntegrationTests.Tests.CreateChatCompletion");
user: "tryAGI.OpenAI.IntegrationTests.Tests.CreateChatCompletion",
frequencyPenalty: customProvider switch
{
CustomProvider.Perplexity => 0.5,
_ => null,
},
logprobs: null);
response.Should().NotBeEmpty();

Console.WriteLine(response);
Expand Down
7 changes: 7 additions & 0 deletions src/tests/OpenAI.IntegrationTests/Tests.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ internal static (OpenAiApi Api, string Model) GetAuthorizedChatApi(CustomProvide
throw new AssertInconclusiveException("TOKEN_FOR_GITHUB_MODELS environment variable is not found.")),
model ?? "gpt-4o");
}
if (customProvider == CustomProvider.Perplexity)
{
return (CustomProviders.Perplexity(apiKey:
Environment.GetEnvironmentVariable("PERPLEXITY_API_KEY") ??
throw new AssertInconclusiveException("PERPLEXITY_API_KEY environment variable is not found.")),
model ?? "llama-3.1-sonar-small-128k-online");
}

var apiKey =
Environment.GetEnvironmentVariable("OPENAI_API_KEY") ??
Expand Down

0 comments on commit 6b955c7

Please sign in to comment.