From 3f7d293eeb7ab635a1b05441f02f04e06767464f Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Thu, 23 Nov 2023 13:04:37 -0600 Subject: [PATCH] Initial Checkin of the AI-projects --- .gitignore | 43 +++++++++++++ simpleOpenAiAzureChatBot/Program.cs | 32 ++++++++++ simpleOpenAiAzureChatBot/simpleChatBot.csproj | 14 +++++ simpleOpenAiAzureChatBot/simpleChatBot.sln | 25 ++++++++ simpleOpenAiDirectChatBot/Program.cs | 63 +++++++++++++++++++ simpleOpenAiDirectChatBot/appsettings.json | 6 ++ .../simpleOpenAiDirectChatBot.csproj | 14 +++++ .../simpleOpenAiDirectChatBot.sln | 25 ++++++++ 8 files changed, 222 insertions(+) create mode 100644 .gitignore create mode 100644 simpleOpenAiAzureChatBot/Program.cs create mode 100644 simpleOpenAiAzureChatBot/simpleChatBot.csproj create mode 100644 simpleOpenAiAzureChatBot/simpleChatBot.sln create mode 100644 simpleOpenAiDirectChatBot/Program.cs create mode 100644 simpleOpenAiDirectChatBot/appsettings.json create mode 100644 simpleOpenAiDirectChatBot/simpleOpenAiDirectChatBot.csproj create mode 100644 simpleOpenAiDirectChatBot/simpleOpenAiDirectChatBot.sln diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bfca666 --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +*.swp +*.*~ +project.lock.json +.DS_Store +*.pyc +nupkg/ + +# Visual Studio Code +.vscode/ + +# Rider +.idea/ + +# Visual Studio +.vs/ + +# Fleet +.fleet/ + +# Code Rush +.cr/ + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +msbuild.log +msbuild.err +msbuild.wrn diff --git a/simpleOpenAiAzureChatBot/Program.cs b/simpleOpenAiAzureChatBot/Program.cs new file mode 100644 index 0000000..9a7eb9c --- /dev/null +++ b/simpleOpenAiAzureChatBot/Program.cs @@ -0,0 +1,32 @@ +using Azure; +using Azure.AI.OpenAI; +using System.Text; + +var client=new OpenAIClient(new Uri("https://espopenaitest1.openai.azure.com/"), new AzureKeyCredential("simpleChatBot.Keys.OpenAIKey")); +//var options=new ChatCompletionsOptions { MaxTokens = 4096, Temperature = 0.7f, ChoicesPerPrompt = 1 }; +var options=new ChatCompletionsOptions { MaxTokens = 4096, Temperature = 0.7f }; + +options.Messages.Add(new ChatMessage(ChatRole.System, """ + You are a friendly and funny chap, used to amuse an online audience. + You respond as if you were John Cleese from Monty Python but with an old-English, medieval way of talking. + """)); + +while (true){ + Console.ForegroundColor = ConsoleColor.White; + Console.Write(">> "); + var prompt = Console.ReadLine(); + if (prompt?.ToLower() == "exit") break; + + options.Messages.Add(new ChatMessage(ChatRole.User, prompt)); + + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine(); + var currentResponse = String.Empty; + //var response = await client.GetChatCompletionsAsync("ESPChatTest",options); + var response = await client.GetChatCompletionsAsync(options); + + Console.WriteLine(response.Value.Choices[0].Message.Content); + Console.WriteLine(); + + options.Messages.Add(new ChatMessage(ChatRole.Assistant, currentResponse)); +} diff --git a/simpleOpenAiAzureChatBot/simpleChatBot.csproj b/simpleOpenAiAzureChatBot/simpleChatBot.csproj new file mode 100644 index 0000000..2ea9b69 --- /dev/null +++ b/simpleOpenAiAzureChatBot/simpleChatBot.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/simpleOpenAiAzureChatBot/simpleChatBot.sln b/simpleOpenAiAzureChatBot/simpleChatBot.sln new file mode 100644 index 0000000..32c69a2 --- /dev/null +++ b/simpleOpenAiAzureChatBot/simpleChatBot.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "simpleChatBot", "simpleChatBot.csproj", "{225DB841-135B-4D4C-B66F-462516705837}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {225DB841-135B-4D4C-B66F-462516705837}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {225DB841-135B-4D4C-B66F-462516705837}.Debug|Any CPU.Build.0 = Debug|Any CPU + {225DB841-135B-4D4C-B66F-462516705837}.Release|Any CPU.ActiveCfg = Release|Any CPU + {225DB841-135B-4D4C-B66F-462516705837}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F32F20DA-CA42-422F-B5FF-3166D6149D35} + EndGlobalSection +EndGlobal diff --git a/simpleOpenAiDirectChatBot/Program.cs b/simpleOpenAiDirectChatBot/Program.cs new file mode 100644 index 0000000..b88f740 --- /dev/null +++ b/simpleOpenAiDirectChatBot/Program.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + string apiKey = "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"; + string prompt = "What is the meaning of life?"; + + string response = await GetChatGPTResponse(apiKey, prompt); + + Console.WriteLine("ChatGPT Response: "); + Console.WriteLine(response); + } + + static async Task GetChatGPTResponse(string apiKey, string prompt) + { + using (HttpClient client = new HttpClient()) + { + string apiUrl = "https://api.openai.com/v1/chat/completions"; + + // Prepare the request data + var requestData = new + { + messages = new[] { + new { role = "system", content = "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair." }, + new { role = "user", content = "Compose a poem that explains the concept of recursion in programming." } + }, + model = "gpt-3.5-turbo", + max_tokens = 100, + temperature = 0.7 + }; + + string jsonData = JsonSerializer.Serialize(requestData); + var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); + + // Set OpenAI API key in the Authorization header + client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); + + // Make the API request + HttpResponseMessage response = await client.PostAsync(apiUrl, content); + + if (response.IsSuccessStatusCode) + { + string responseBody = await response.Content.ReadAsStringAsync(); + //Console.WriteLine(responseBody); + var responseObject = JsonDocument.Parse(responseBody); + return responseObject?.RootElement.GetProperty("choices")[0].GetProperty("message").GetProperty("content").ToString().Trim(); + } + else + { + Console.WriteLine($"Error: {response.StatusCode}"); + return null; + } + } + } +} diff --git a/simpleOpenAiDirectChatBot/appsettings.json b/simpleOpenAiDirectChatBot/appsettings.json new file mode 100644 index 0000000..caa7fbc --- /dev/null +++ b/simpleOpenAiDirectChatBot/appsettings.json @@ -0,0 +1,6 @@ +{ + "OpenAICredentials": { + "ApiKey": "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", + "ApiHost": "https://api.openai.com/v1/" + } +} \ No newline at end of file diff --git a/simpleOpenAiDirectChatBot/simpleOpenAiDirectChatBot.csproj b/simpleOpenAiDirectChatBot/simpleOpenAiDirectChatBot.csproj new file mode 100644 index 0000000..0ca175a --- /dev/null +++ b/simpleOpenAiDirectChatBot/simpleOpenAiDirectChatBot.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/simpleOpenAiDirectChatBot/simpleOpenAiDirectChatBot.sln b/simpleOpenAiDirectChatBot/simpleOpenAiDirectChatBot.sln new file mode 100644 index 0000000..7f18fb6 --- /dev/null +++ b/simpleOpenAiDirectChatBot/simpleOpenAiDirectChatBot.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "simpleOpenAiDirectChatBot", "simpleOpenAiDirectChatBot.csproj", "{4FDE452E-E09C-44D3-B6B7-6298BCF8CF35}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4FDE452E-E09C-44D3-B6B7-6298BCF8CF35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4FDE452E-E09C-44D3-B6B7-6298BCF8CF35}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4FDE452E-E09C-44D3-B6B7-6298BCF8CF35}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4FDE452E-E09C-44D3-B6B7-6298BCF8CF35}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {79592704-E9CD-4A38-821E-7D80E745FD19} + EndGlobalSection +EndGlobal