From 0e264ef25d800f6347f612d5399bc90b8fe633b7 Mon Sep 17 00:00:00 2001 From: Arjan Spieard | Asgard Sings! Date: Fri, 10 Mar 2023 15:32:12 +0100 Subject: [PATCH 1/2] Chat and Completion Example added as example project. --- OpenAI_API.sln | 11 ++++ .../Applications/ChatExample.cs | 22 ++++++++ .../Applications/CompletionExample.cs | 17 ++++++ examples/OpenAI_Example.Console/IProgram.cs | 17 ++++++ .../OpenAI_Example.ConsoleApp.csproj | 18 ++++++ examples/OpenAI_Example.Console/Program.cs | 56 +++++++++++++++++++ 6 files changed, 141 insertions(+) create mode 100644 examples/OpenAI_Example.Console/Applications/ChatExample.cs create mode 100644 examples/OpenAI_Example.Console/Applications/CompletionExample.cs create mode 100644 examples/OpenAI_Example.Console/IProgram.cs create mode 100644 examples/OpenAI_Example.Console/OpenAI_Example.ConsoleApp.csproj create mode 100644 examples/OpenAI_Example.Console/Program.cs diff --git a/OpenAI_API.sln b/OpenAI_API.sln index 7b86ccb..3ad9307 100644 --- a/OpenAI_API.sln +++ b/OpenAI_API.sln @@ -13,6 +13,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1. Examples", "1. Examples", "{B6A90DB1-5DC1-468E-BEEC-93302D9FD949}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI_Example.ConsoleApp", "examples\OpenAI_Example.Console\OpenAI_Example.ConsoleApp.csproj", "{CB645694-1347-4D64-8D83-B8B7279370F4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,10 +31,17 @@ Global {066EC5A5-47CE-4B91-B924-F236644037C1}.Debug|Any CPU.Build.0 = Debug|Any CPU {066EC5A5-47CE-4B91-B924-F236644037C1}.Release|Any CPU.ActiveCfg = Release|Any CPU {066EC5A5-47CE-4B91-B924-F236644037C1}.Release|Any CPU.Build.0 = Release|Any CPU + {CB645694-1347-4D64-8D83-B8B7279370F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB645694-1347-4D64-8D83-B8B7279370F4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB645694-1347-4D64-8D83-B8B7279370F4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB645694-1347-4D64-8D83-B8B7279370F4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {CB645694-1347-4D64-8D83-B8B7279370F4} = {B6A90DB1-5DC1-468E-BEEC-93302D9FD949} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {971477B1-6BBA-40CD-8B76-AEBC01D99130} EndGlobalSection diff --git a/examples/OpenAI_Example.Console/Applications/ChatExample.cs b/examples/OpenAI_Example.Console/Applications/ChatExample.cs new file mode 100644 index 0000000..1b1113f --- /dev/null +++ b/examples/OpenAI_Example.Console/Applications/ChatExample.cs @@ -0,0 +1,22 @@ +using OpenAI_API; +using System; +using System.Threading.Tasks; + +namespace OpenAI_Example.ConsoleApp.Applications +{ + internal class ChatExample : IProgram + { + public async Task RunAsync(string apiKey) + { + Console.WriteLine($"Running the {GetType()}...."); + Console.WriteLine("Please ask a question:"); + var api = new OpenAIAPI(apiKey); + + var str = Console.ReadLine(); + var result = await api.Chat.CreateChatCompletionAsync(str); + + var reply = result.Choices[0].Message; + Console.WriteLine($"{reply.Role}: {reply.Content.Trim()}"); + } + } +} \ No newline at end of file diff --git a/examples/OpenAI_Example.Console/Applications/CompletionExample.cs b/examples/OpenAI_Example.Console/Applications/CompletionExample.cs new file mode 100644 index 0000000..ecf4b2c --- /dev/null +++ b/examples/OpenAI_Example.Console/Applications/CompletionExample.cs @@ -0,0 +1,17 @@ +using OpenAI_API; +using System; +using System.Threading.Tasks; + +namespace OpenAI_Example.ConsoleApp.Applications +{ + internal class CompletionExample : IProgram + { + public async Task RunAsync(string apiKey) + { + Console.WriteLine($"Running the {GetType()}...."); + var api = new OpenAIAPI(apiKey); + var result = await api.Completions.GetCompletion("One Two Three One Two"); + Console.WriteLine(result); + } + } +} \ No newline at end of file diff --git a/examples/OpenAI_Example.Console/IProgram.cs b/examples/OpenAI_Example.Console/IProgram.cs new file mode 100644 index 0000000..4af43f7 --- /dev/null +++ b/examples/OpenAI_Example.Console/IProgram.cs @@ -0,0 +1,17 @@ +using System.Threading.Tasks; + +namespace OpenAI_Example.ConsoleApp +{ + /// + /// Defines an interface for different types of programs to run as an example. + /// + public interface IProgram + { + /// + /// Runs a dedicated application example. + /// + /// The Api key to authorize with OpenAI. + /// A task which can be awaited. + Task RunAsync(string apiKey); + } +} \ No newline at end of file diff --git a/examples/OpenAI_Example.Console/OpenAI_Example.ConsoleApp.csproj b/examples/OpenAI_Example.Console/OpenAI_Example.ConsoleApp.csproj new file mode 100644 index 0000000..7421fae --- /dev/null +++ b/examples/OpenAI_Example.Console/OpenAI_Example.ConsoleApp.csproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + true + true + latest + enable + true + false + + + + + + + diff --git a/examples/OpenAI_Example.Console/Program.cs b/examples/OpenAI_Example.Console/Program.cs new file mode 100644 index 0000000..626609c --- /dev/null +++ b/examples/OpenAI_Example.Console/Program.cs @@ -0,0 +1,56 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; + +namespace OpenAI_Example.ConsoleApp +{ + internal sealed class Program + { + private static async Task Main() + { + Console.WriteLine("Welcome to the OpenAI Example program."); + Console.WriteLine("Please provide a valid OpenAI Api Key:"); + var apiKey = Console.ReadLine(); + if (String.IsNullOrWhiteSpace(apiKey)) + { + throw new InvalidOperationException("Cannot authorize with OpenAI when no valid API Key is provided."); + } + + Console.WriteLine("What do you want to do?"); + + Type[] types = GetExamples(); + _ = int.TryParse(Console.ReadLine(), out var programToRun); + + var instance = (IProgram?)Activator.CreateInstance(types[programToRun]); + if (instance is not null) + { + await instance.RunAsync(apiKey); + } + + Console.WriteLine("Thank you for using the OpenAI Example program."); + Console.WriteLine("Exiting now...."); + } + + private static Type[] GetExamples() + { + var type = typeof(IProgram); + var types = AppDomain.CurrentDomain.GetAssemblies() + .SelectMany(s => s.GetTypes()) + .Where(type.IsAssignableFrom) + .ToArray(); + + for (var i = 0; i < types.Length; i++) + { + var option = types[i]; + if (option.Name == nameof(IProgram)) // Don't include the IProgram type. + { + continue; + } + Console.WriteLine($"\t{i}){option.Name}"); + } + + return types; + } + } +} \ No newline at end of file From 80e01a9636eecfcfd8ab2db1cbe0ce743c8167c4 Mon Sep 17 00:00:00 2001 From: Arjan Spieard | Asgard Sings! Date: Fri, 10 Mar 2023 15:39:37 +0100 Subject: [PATCH 2/2] Changed Applications to be sealed classes. --- examples/OpenAI_Example.Console/Applications/ChatExample.cs | 2 +- .../OpenAI_Example.Console/Applications/CompletionExample.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/OpenAI_Example.Console/Applications/ChatExample.cs b/examples/OpenAI_Example.Console/Applications/ChatExample.cs index 1b1113f..b82cf6d 100644 --- a/examples/OpenAI_Example.Console/Applications/ChatExample.cs +++ b/examples/OpenAI_Example.Console/Applications/ChatExample.cs @@ -4,7 +4,7 @@ namespace OpenAI_Example.ConsoleApp.Applications { - internal class ChatExample : IProgram + internal sealed class ChatExample : IProgram { public async Task RunAsync(string apiKey) { diff --git a/examples/OpenAI_Example.Console/Applications/CompletionExample.cs b/examples/OpenAI_Example.Console/Applications/CompletionExample.cs index ecf4b2c..8d0264b 100644 --- a/examples/OpenAI_Example.Console/Applications/CompletionExample.cs +++ b/examples/OpenAI_Example.Console/Applications/CompletionExample.cs @@ -4,7 +4,7 @@ namespace OpenAI_Example.ConsoleApp.Applications { - internal class CompletionExample : IProgram + internal sealed class CompletionExample : IProgram { public async Task RunAsync(string apiKey) {