By using sessions, developers can build chat applications that maintain context across multiple interactions with the user, which can lead to more engaging and natural conversations.
The question: how can I make sessions with gpt-3.5-turbo API using c# http?
using Newtonsoft.Json;
using System.Text;
namespace MyChatGPT
{
internal class Program
{
static async Task Main(string[] args)
{
// Set up the HttpClient
var client = new HttpClient();
var baseUrl = "https://api.openai.com/v1/chat/completions";
// Set up the API key
var apiKey = "YOUR_API_KEY";
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiKey);
while (true)
{
// Prompt the user for input
Console.Write("> ");
var userInput = Console.ReadLine();
// Set up the request parameters
var parameters = new
{
model = "gpt-3.5-turbo",
messages = new[] { new { role = "user", content = userInput }, },
max_tokens = 1024,
temperature = 0.2f,
};
// Send the HTTP request
var response = await client.PostAsync(baseUrl, new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json"));// new StringContent(json));
// Read the response
var responseContent = await response.Content.ReadAsStringAsync();
// Extract the completed text from the response
dynamic responseObject = JsonConvert.DeserializeObject(responseContent);
string generatedText = responseObject.choices[0].message.content;
// Print the generated text
Console.WriteLine("Bot: " + generatedText);
}
}
}
}
This code works fine, but the problem is that it does not save the previous context of the conversation. Can you help me to create multiple sessions, and can I choose the session as requested? Thanks for your help in advance.