Files

104 lines
3.6 KiB
C#
Raw Permalink Normal View History

2023-11-23 13:04:37 -06:00
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
{
2023-11-23 14:42:04 -06:00
2023-11-23 13:04:37 -06:00
static async Task Main()
{
string apiKey = "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";
2023-11-23 14:42:04 -06:00
singleMessage[] allMessages = [];
//allMessages = allMessages.Append(new singleMessage("system", "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair.")).ToArray();
//allMessages = allMessages.Append(new singleMessage("system", "You are a personal assistant, you give brief but exact answers.")).ToArray();
allMessages = allMessages.Append(new singleMessage("system", @"
2023-12-12 17:36:59 -06:00
You are a Spanish teacher.
You aim to improve the vocabulary and spelling of the student.
2023-11-23 14:42:04 -06:00
2023-12-12 17:36:59 -06:00
When the student makes errors
- Error feedback to the user using English language
- Do not correct accents of characters.
- Do not correct upper- lower case errors.
")).ToArray();
2023-11-23 13:04:37 -06:00
2023-11-23 14:42:04 -06:00
while (true)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(">> ");
var prompt = Console.ReadLine();
if (prompt?.ToLower() == "bye")
{
break;
}
allMessages = allMessages.Append(new singleMessage("user", prompt)).ToArray();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine();
var currentResponse = String.Empty;
var response = await GetChatGPTResponse(apiKey, allMessages);
Console.WriteLine(response);
Console.WriteLine();
allMessages = allMessages.Append(new singleMessage("assistant", response)).ToArray();
}
2023-11-23 13:04:37 -06:00
}
2023-11-23 14:42:04 -06:00
static async Task<string> GetChatGPTResponse(string apiKey, singleMessage[] allMessages)
2023-11-23 13:04:37 -06:00
{
using (HttpClient client = new HttpClient())
{
string apiUrl = "https://api.openai.com/v1/chat/completions";
// Prepare the request data
2023-11-23 14:42:04 -06:00
var requestData = new
2023-11-23 13:04:37 -06:00
{
model = "gpt-3.5-turbo",
2023-11-23 14:42:04 -06:00
messages = allMessages,
max_tokens = 256,
top_p = 1,
frequency_penalty = 0,
presence_penalty = 0
2023-11-23 13:04:37 -06:00
};
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;
}
}
}
}
2023-11-23 14:42:04 -06:00
class singleMessage
{
public string role { get; set; }
public string content { get; set; }
public singleMessage(string intitRole, string initContent)
{
role = intitRole;
content = initContent;
}
}