How to work with sessions in GPT API using C#
Asked Answered
G

3

7

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.

Guileless answered 18/3, 2023 at 22:45 Comment(0)
G
5

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

Guileless answered 22/5, 2023 at 15:9 Comment(0)
C
1

The gpt-3.5-turbo API does not store your conversation history or context at all. This is functionality of the ChatGPT web--application.

You have to create this functionality within your application. The most complex part is to truncated and summarize your previous messages to not exceed the token limit.

Introduction Guide

You can also find a python example of what you want to do here.

Cointon answered 22/5, 2023 at 11:29 Comment(0)
P
0

I worked around this by doing the following.

It gets the job done, but could use improvements.

I created the following class

public class Message
{
    public string role = string.Empty;
    public string content = string.Empty;
}

Just before the while loop I initialized a List<Message> chatHistory = new();

and added the system message right afterwards
chatHistory.Add(new Message() { role = "system", content = "foo" });

and before sending request I added this

var numberOfUserMessages = chatHistory.Select(c=>c.role).Where(r => r == "user").Count();
var chatHistoryLimit = 8;

if (numberOfUserMessages >= chatHistoryLimit)
{
    for (var i = 0; i < chatHistory.Count() - 1; i++)
    {
        var chat = chatHistory[i];
        if (chat.role == "user" && chatHistory.Select(c => c.role).Where(r => r == "user").Count() > chatHistoryLimit)
        {
            chatHistory.RemoveRange(i, 2);
            i--;
        }
    }
}

in the end it looked like this

List<Message> chatHistory = new();
chatHistory.Add(new Message() { role = "system", content = "foo" });
while (true)
{
    // Prompt the user for input
    Console.Write("> ");
    var userInput = Console.ReadLine();

    chatHistory.Add(new Message() { role = "user", content = userInput });
    // Set up the request parameters
    var parameters = new
    {
        model = "gpt-3.5-turbo",
        messages = chatHistory,
        max_tokens = 1024,
        temperature = 0.2f,
    };

    var numberOfUserMessages = chatHistory.Select(c => c.role).Where(r => r == "user").Count();
    var chatHistoryLimit = 8;
    //To remove old chat messages in the chatHistory, and to always send the last 8 conversations between the user and the bot.
    //Needs improvement; just a quick work around.
    if (numberOfUserMessages >= chatHistoryLimit)
    {
        for (var i = 0; i < chatHistory.Count() - 1; i++)
        {
            var chat = chatHistory[i];
            if (chat.role == "user" && chatHistory.Select(c => c.role).Where(r => r == "user").Count() > chatHistoryLimit)
            {
                chatHistory.RemoveRange(i, 2);
                i--;
            }
        }
    }

    // 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;

    chatHistory.Add(new Message() { role = "assistant", content = generatedText });

    // Print the generated text
    Console.WriteLine("Bot: " + generatedText);
}
Prudy answered 8/8, 2023 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.