Skip to content

Commit

Permalink
Merge pull request #3 from betalgo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kayhantolga authored Jan 7, 2022
2 parents 45a54eb + 2a4a1e3 commit 30d0370
Show file tree
Hide file tree
Showing 21 changed files with 313 additions and 56 deletions.
2 changes: 1 addition & 1 deletion OpenAI.Playground/ApiSettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{

// How to use user secret => right click your project name in solution explorer then click Manage User Secret, it is a good way to keep your api keys :)
"OpenAISdkSettings": {
"OpenAIServiceOptions": {
// "Organization": "Put your Organization Id to here or to User Secret file" //https:// beta.openai.com/account/org-settings
//,"ApiKey":"Put your api key to here or to User Secret file" //How can I get an app key? visit here: https://openai.com/
},
Expand Down
20 changes: 10 additions & 10 deletions OpenAI.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@
using LaserCatEyes.HttpClientListener;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenAI.Playground.TestHelpers;
using OpenAI.GPT3;
using OpenAI.GPT3.Extensions;
using OpenAI.GPT3.Interfaces;
using OpenAI.GPT3.Managers;
using OpenAI.GPT3.Models;
using OpenAI.Playground.TestHelpers;

var builder = new ConfigurationBuilder()
.AddJsonFile("ApiSettings.json")
.AddUserSecrets<Program>();

IConfiguration configuration = builder.Build();
var serviceCollection = new ServiceCollection();
serviceCollection.AddOptions<OpenAiSettings>();
serviceCollection.AddScoped(_ => configuration);


// Laser cat eyes will help us to track request and responses between OpenAI server and our client
//It is in Beta version, if you have consider about your data privacy or if you don't want to use it just comment out from here
serviceCollection.Configure<LaserCatEyesOptions>(configuration.GetSection("LaserCatEyesOptions"));
serviceCollection.AddHttpClient<IOpenAISdk, OpenAISdk>();
serviceCollection.AddLaserCatEyesHttpClientListener();
serviceCollection.AddOpenAIService(settings => { settings.ApiKey = "TEST"; });


//// to here, and uncomment from here
//serviceCollection.AddHttpClient<IOpenAISdk, OpenAISdk>();
//serviceCollection.AddHttpClient<IOpenAIService, OpenAIService>();
//// to here

serviceCollection.Configure<OpenAiSettings>(configuration.GetSection(OpenAiSettings.SettingKey));
var serviceProvider = serviceCollection.BuildServiceProvider();
var sdk = serviceProvider.GetRequiredService<IOpenAISdk>();
//await EngineTestHelper.FetchEnginesTest(sdk);
var sdk = serviceProvider.GetRequiredService<IOpenAIService>();
await EngineTestHelper.FetchEnginesTest(sdk);
//await CompletionTestHelper.RunSimpleCompletionTest(sdk);
//await SearchTestHelper.SearchDocuments(sdk);
//await ClassificationsTestHelper.RunSimpleClassificationTest(sdk);
//await AnswerTestHelper.RunSimpleAnswerTest(sdk);
//await FileTestHelper.RunSimpleFileTest(sdk);
////await FineTuningTestHelper.CleanUpAllFineTunings(sdk);
await FineTuningTestHelper.RunCaseStudyIsTheModelMakingUntrueStatements(sdk);
////await FineTuningTestHelper.CleanUpAllFineTunings(sdk); //!!!!! will delete all fine-tunings
//await FineTuningTestHelper.RunCaseStudyIsTheModelMakingUntrueStatements(sdk);
Console.ReadLine();
98 changes: 98 additions & 0 deletions OpenAI.Playground/SampleData/FineTuningSample1.jsonl
Original file line number Diff line number Diff line change
@@ -1,2 +1,100 @@
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}
2 changes: 1 addition & 1 deletion OpenAI.Playground/TestHelpers/AnswerTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace OpenAI.Playground.TestHelpers
{
internal static class AnswerTestHelper
{
public static async Task RunSimpleAnswerTest(IOpenAISdk sdk)
public static async Task RunSimpleAnswerTest(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Completion Testing is starting:", ConsoleColor.Cyan);

Expand Down
2 changes: 1 addition & 1 deletion OpenAI.Playground/TestHelpers/ClassificationsTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace OpenAI.Playground.TestHelpers
{
internal static class ClassificationsTestHelper
{
public static async Task RunSimpleClassificationTest(IOpenAISdk sdk)
public static async Task RunSimpleClassificationTest(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Run Simple Classification Test is starting:", ConsoleColor.Cyan);

Expand Down
14 changes: 13 additions & 1 deletion OpenAI.Playground/TestHelpers/CompletionTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace OpenAI.Playground.TestHelpers
{
internal static class CompletionTestHelper
{
public static async Task RunSimpleCompletionTest(IOpenAISdk sdk)
public static async Task RunSimpleCompletionTest(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Completion Testing is starting:", ConsoleColor.Cyan);

Expand All @@ -19,6 +19,18 @@ public static async Task RunSimpleCompletionTest(IOpenAISdk sdk)
MaxTokens = 5
}, Engines.Engine.Davinci);

if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.FirstOrDefault());
}
else
{
if (completionResult.Error == null)
{
throw new Exception("Unknown Error");
}
Console.WriteLine($"{completionResult.Error.Code}: {completionResult.Error.Message}");
}
Console.WriteLine(completionResult.Choices.FirstOrDefault());
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion OpenAI.Playground/TestHelpers/EngineTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace OpenAI.Playground.TestHelpers
{
internal static class EngineTestHelper
{
public static async Task FetchEnginesTest(IOpenAISdk sdk)
public static async Task FetchEnginesTest(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Engine List Testing is starting:", ConsoleColor.Cyan);

Expand Down
4 changes: 2 additions & 2 deletions OpenAI.Playground/TestHelpers/FileTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OpenAI.Playground.TestHelpers
{
internal static class FileTestHelper
{
public static async Task RunSimpleFileTest(IOpenAISdk sdk)
public static async Task RunSimpleFileTest(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("File Testing is starting:", ConsoleColor.Cyan);

Expand Down Expand Up @@ -66,7 +66,7 @@ public static async Task RunSimpleFileTest(IOpenAISdk sdk)
}


public static async Task CleanAllFiles(IOpenAISdk sdk)
public static async Task CleanAllFiles(IOpenAIService sdk)
{
try
{
Expand Down
39 changes: 23 additions & 16 deletions OpenAI.Playground/TestHelpers/FineTuningTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace OpenAI.Playground.TestHelpers
{
internal static class FineTuningTestHelper
{
public static async Task RunCaseStudyIsTheModelMakingUntrueStatements(IOpenAISdk sdk)
public static async Task RunCaseStudyIsTheModelMakingUntrueStatements(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Run Case Study Is The Model Making Untrue Statements:", ConsoleColor.Cyan);

Expand All @@ -30,7 +30,7 @@ public static async Task RunCaseStudyIsTheModelMakingUntrueStatements(IOpenAISdk
var createFineTuneResponse = await sdk.FineTunes.CreateFineTune(new FineTuneCreateRequest()
{
TrainingFile = uploadFilesResponse.Id,
Model = Engines.Curie
Model = Engines.Ada
});

var listFineTuneEventsStream = await sdk.FineTunes.ListFineTuneEvents(createFineTuneResponse.Id, true);
Expand All @@ -54,20 +54,27 @@ public static async Task RunCaseStudyIsTheModelMakingUntrueStatements(IOpenAISdk
await Task.Delay(10_000);
} while (true);

var completionResult = await sdk.FineTunes.FineTuneCompletions(new FineTuneCompletionsRequest()
{
MaxTokens = 1,
Prompt = @"https://t.co/f93xEd2 Excited to share my latest blog post! ->",
Model = retrieveFineTuneResponse.FineTunedModel
});
if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.FirstOrDefault());
}
else
do
{
throw new Exception($"failed{completionResult.Error?.Message}");
}
var completionResult = await sdk.FineTunes.FineTuneCompletions(new FineTuneCompletionsRequest()
{
MaxTokens = 1,
Prompt = @"https://t.co/f93xEd2 Excited to share my latest blog post! ->",
Model = retrieveFineTuneResponse.FineTunedModel,
Logprobs = 2
});
if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.FirstOrDefault());
break;
}
else
{
ConsoleExtensions.WriteLine($"failed{completionResult.Error?.Message}",ConsoleColor.DarkRed);
}

} while (true);

}
catch (Exception e)
{
Expand All @@ -76,7 +83,7 @@ public static async Task RunCaseStudyIsTheModelMakingUntrueStatements(IOpenAISdk
}
}

public static async Task CleanUpAllFineTunings(IOpenAISdk sdk)
public static async Task CleanUpAllFineTunings(IOpenAIService sdk)
{
var fineTunes = await sdk.FineTunes.ListFineTunes();
foreach (var datum in fineTunes.Data)
Expand Down
6 changes: 3 additions & 3 deletions OpenAI.Playground/TestHelpers/SearchTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace OpenAI.Playground.TestHelpers
{
internal static class SearchTestHelper
{
public static async Task SearchDocuments(IOpenAISdk sdk)
public static async Task SearchDocuments(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Search Documents Test is starting:", ConsoleColor.Cyan);

Expand Down Expand Up @@ -38,7 +38,7 @@ public static async Task SearchDocuments(IOpenAISdk sdk)
}
}

public static async Task UploadSampleFileAndGetSearchResponse(IOpenAISdk sdk)
public static async Task UploadSampleFileAndGetSearchResponse(IOpenAIService sdk)
{
const string fileName = "SearchSample.jsonl";

Expand Down Expand Up @@ -94,7 +94,7 @@ public static async Task UploadSampleFileAndGetSearchResponse(IOpenAISdk sdk)
}
}

public static async Task UploadSampleFile(IOpenAISdk sdk)
public static async Task UploadSampleFile(IOpenAIService sdk)
{
const string fileName = "SearchSample.json";

Expand Down
26 changes: 26 additions & 0 deletions OpenAI.SDK/Extensions/OpenAIServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenAI.GPT3.Interfaces;
using OpenAI.GPT3.Managers;

namespace OpenAI.GPT3.Extensions
{
public static class OpenAIServiceCollectionExtensions
{
public static IServiceCollection AddOpenAIService(this IServiceCollection services)
{
services.AddOptions<OpenAiOptions>();
services.AddHttpClient<IOpenAIService, OpenAIService>();
var configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
services.Configure<OpenAiOptions>(configuration.GetSection(OpenAiOptions.SettingKey));
return services;
}

public static IServiceCollection AddOpenAIService(this IServiceCollection services, Action<OpenAiOptions> setupAction)
{
services.AddOptions<OpenAiOptions>().Configure(setupAction);
services.AddHttpClient<IOpenAIService, OpenAIService>();
return services;
}
}
}
Loading

0 comments on commit 30d0370

Please sign in to comment.