Initial Checkin of the AI-projects

This commit is contained in:
Joe Tretter
2023-11-23 13:04:37 -06:00
commit 3f7d293eeb
8 changed files with 222 additions and 0 deletions

View File

@@ -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<string> 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;
}
}
}
}

View File

@@ -0,0 +1,6 @@
{
"OpenAICredentials": {
"ApiKey": "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",
"ApiHost": "https://api.openai.com/v1/"
}
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenAI.ChatGPT" Version="3.2.0" />
</ItemGroup>
</Project>

View File

@@ -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