Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat and Completion Example added as example project. #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions OpenAI_API.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions examples/OpenAI_Example.Console/Applications/ChatExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using OpenAI_API;
using System;
using System.Threading.Tasks;

namespace OpenAI_Example.ConsoleApp.Applications
{
internal sealed 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()}");
}
}
}
17 changes: 17 additions & 0 deletions examples/OpenAI_Example.Console/Applications/CompletionExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using OpenAI_API;
using System;
using System.Threading.Tasks;

namespace OpenAI_Example.ConsoleApp.Applications
{
internal sealed 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);
}
}
}
17 changes: 17 additions & 0 deletions examples/OpenAI_Example.Console/IProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;

namespace OpenAI_Example.ConsoleApp
{
/// <summary>
/// Defines an interface for different types of programs to run as an example.
/// </summary>
public interface IProgram
{
/// <summary>
/// Runs a dedicated application example.
/// </summary>
/// <param name="apiKey">The Api key to authorize with OpenAI.</param>
/// <returns>A task which can be awaited.</returns>
Task RunAsync(string apiKey);
}
}
18 changes: 18 additions & 0 deletions examples/OpenAI_Example.Console/OpenAI_Example.ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<TrimUnusedDependencies>true</TrimUnusedDependencies>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\OpenAI_API\OpenAI_API.csproj" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions examples/OpenAI_Example.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}