Search code examples
c#httpclientopenai-api

How to work with sessions in GPT API using C#


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.


Solution

  • I searched a lot and tried many ways, and the reason is that ChatGPT doesn't rely on sessions nor cookies as the answer is being processed and sent via API.

    To keep context with ChatGPT I did not find a better way than to send part of the previous conversation to the API using the following code:

    messages = new[] {
            new { role = "system", content =  "You are a helpful assistant." },
            new { role = "user", content =  "Who won the world series in 2020?" },
            new { role = "assistant", content = "The Los Angeles Dodgers won the World Series in 2020." },
            new { role = "user", content = "Where was it played?" },
        },
    

    or

    messages = new[] { 
        new { role = "user", content =  "Who won the world series in 2020?" },
        new { role = "assistant", content = "The Los Angeles Dodgers won the World Series in 2020." },
        new { role = "user", content = "Where was it played?" },
    },
    

    Note: If you do not enter system message, the default message will be written, which is "You are a helpful assistant."

    According to the ChatGPT team, The current version does not support system messages professionally, But they are working to ensure that system messages have an impact on conversations.


    You can learn more by trying the OpenAI Playground