Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP request Multiple conversations with ChatGPT while keeping context
#1
I created a function named GetGPTMes for partial class Functions, and I put it in the project at the following link.
https://www.quickmacros.com/forum/showth...6#pid36746

I can use the PowerShell code below to execute the GetGPTMes function, but I need to implement Multiple conversations with ChatGPT while keeping context!

I can use the following C# code to implement a multi-turn ChatGPT conversation in a console window and maintain context. But how can I achieve the same effect in an HTTP request?

Additionally, there will be multiple HTTP requests simultaneously accessing the GetGPTMes function. How to handle this is a bit challenging for me! Sad

Thanks in advance for any suggestions and help.
 
Code:
Copy      Help
var bot = new ChatGpt(sk, options);
var prompt = string.Empty;

while (true)
{
    Console.Write("You: ");
    prompt = Console.ReadLine();
    if (prompt is null) break;
    if (string.IsNullOrWhiteSpace(prompt)) break;
    if (prompt == "exit") break;
    Console.Write("Gpt: ");
    await bot.AskStream(Console.Write, prompt, "default");
    Console.WriteLine();
}

 Powershell Code Test:
 
Code:
Copy      Help
# I need to implement Multiple conversations with ChatGPT while keeping context

# 1
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{ userMes = 'What is the population of the United States?' }
# 2
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{ userMes = 'Translate the previous answer into French.' }
# 3
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{ userMes = 'Please make it shorter.' }
 
 
Code:
Copy      Help
// class "Functions2.cs"
/*/ nuget ChatGPTNet\ChatGPT.Net; /*/

using ChatGPT.Net;
using ChatGPT.Net.DTO.ChatGPT;

static partial class Functions
{
    public static string GetGPTMes(string userMes)
    {

                
        string sk = "sk-XXXXX"; //ChatGpt apiKey
        ChatGptOptions options = new ChatGptOptions()
        {

            BaseUrl = "https://api.openai.com", // The base URL for the OpenAI API
            Model = "gpt-3.5-turbo", // The specific model to use
            Temperature = 0.7, // Controls randomness in the response (0-1)
            TopP = 0.9, // Controls diversity in the response (0-1)
            MaxTokens = 3500, // The maximum number of tokens in the response
            Stop = null, // Sequence of tokens that will stop generation
            PresencePenalty = 0.0, // Penalizes new tokens based on their existing presence in the context
            FrequencyPenalty = 0.0 // Penalizes new tokens based on their frequency in the context
        };
        
        var bot = new ChatGpt(sk, options);
        var task = bot.Ask(userMes);
        task.Wait(); // Wait for the task to complete
        
        var r = task.Result; // Get the result synchronously
        print.it(r);
        return r;
    }
}
#2
I would try a static ChatGpt variable.
#3
Thanks for your help.
I don't know where to start, and achieving my goal seems more complex than I imagined. I tried asking ChatGPT for assistance, but to no avail. It provided the following seemingly feasible guidance.
Quote:When handling multiple concurrent HTTP requests, you need to ensure that your server-side code can asynchronously process these requests and manage session state appropriately. You can use asynchronous methods to achieve this. Additionally, you need to maintain separate session contexts for each user. You may consider using a dictionary-like data structure to store session state, where the user's ID or other identifier serves as the key. Furthermore, you need to design your HTTP endpoint to accept POST requests with user messages. This endpoint should be capable of handling multiple concurrent requests and appropriately managing the session flow based on the session context for each user.
 
Code:
Copy      Help
using Microsoft.AspNetCore.Mvc;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using ChatGPT.Net;

namespace ChatGPT.API.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ChatController : ControllerBase
    {
        private readonly ConcurrentDictionary<string, ChatGpt> conversationContext = new ConcurrentDictionary<string, ChatGpt>();

        [HttpPost]
        public async Task<ActionResult<string>> PostAsync([FromBody] UserMessage message)
        {
            if (string.IsNullOrEmpty(message.UserId))
            {
                return BadRequest("User ID cannot be empty.");
            }

            var bot = conversationContext.GetOrAdd(message.UserId, CreateChatGptInstance);

            var response = await bot.Ask(message.UserMessage);
            return Ok(response);
        }

        private ChatGpt CreateChatGptInstance(string userId)
        {
            string apiKey = "sk-XXXXX"; // Your ChatGpt apiKey
            var options = new ChatGptOptions
            {
                BaseUrl = "https://api.openai.com",
                Model = "gpt-3.5-turbo",
                Temperature = 0.7,
                TopP = 0.9,
                MaxTokens = 3500,
                Stop = null,
                PresencePenalty = 0.0,
                FrequencyPenalty = 0.0
            };

            return new ChatGpt(apiKey, options);
        }
    }

    public class UserMessage
    {
        public string UserId { get; set; }
        public string UserMessage { get; set; }
    }
}
#4
Suppose there are two users: userA and userB. They use the following PowerShell code and send the HTTP requests respectively., The expected output results are as follows.
-------------------------------------------------------------------------------------------------------------------------------------------------------
# UserA 1
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{ userMes = 'What is the capital of Japan?' }
#The correct answer: The capital of Japan is Tokyo.

# UserA 2
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{ userMes = 'Translate the previous answer into French.' }
#The correct answer: La capitale du Japon est Tokyo.
-------------------------------------------------------------------------------------------------------------------------------------------------------
# UserB 1
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{ userMes = 'What is the capital of the United States?' }
#The correct answer: The capital of the United States is Washington, D.C.

# UserB 2
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{ userMes = 'Translate the previous answer into Japanese.' }
#The correct answer: アメリカ合衆国の首都はワシントンD.C.です.
-------------------------------------------------------------------------------------------------------------------------------------------------------

How can the above functionality be achieved? Are there any relevant libraries that can accomplish the above functionality?
#5
https://chat.openai.com/share/9284bace-4...8803188e89
#6
I don't quite understand the meaning of the code. The result of the code below is not correct.
 
Code:
Copy      Help
using ChatGPT.Net;
using ChatGPT.Net.DTO.ChatGPT;

void Main()
{
    // First question
    string firstQuestion = "What is the capital of Japan?";
    string firstResponse = GetGPTMes(firstQuestion, true);
    Console.WriteLine($"Question: {firstQuestion}\nAnswer: {firstResponse}\n");

    // Second question
    string secondQuestion = $"Translate the previous answer into French.";
    string secondResponse = GetGPTMes(secondQuestion, false);
    Console.WriteLine($"Question: {secondQuestion}\nAnswer: {secondResponse}\n");
}

// Define ChatGPT related functions and variables
private static string apiKey = "sk-xxx"; // ChatGpt apiKey
private static ChatGptOptions options = new ChatGptOptions()
{
    BaseUrl = "https://api.openai.com", // The base URL for the OpenAI API
    Model = "gpt-3.5-turbo", // The specific model to use
    Temperature = 0.7, // Controls randomness in the response (0-1)
    TopP = 0.9, // Controls diversity in the response (0-1)
    MaxTokens = 3500, // The maximum number of tokens in the response
    Stop = null, // Sequence of tokens that will stop generation
    PresencePenalty = 0.0, // Penalizes new tokens based on their existing presence in the context
    FrequencyPenalty = 0.0 // Penalizes new tokens based on their frequency in the context
};
private static ChatGpt bot;

// Define ChatGPT related functions
public static string GetGPTMes(string userMes, bool createBot)
{
  
    if (bot == null || createBot)
    {
        bot = new ChatGpt(apiKey, options);
    }

    var task = bot.Ask(userMes);
    task.Wait(); // Wait for the task to complete

    var r = task.Result; // Get the result synchronously
    return r;
}
#7
The code is for the HTTP server, as you asked, not to be used like in your last posted script.

The PowerShell script should also use parameter createBot. In the first call pass true, elsewhere false.

I did not test it, but I would try code like this. Maybe also need to ensure that the code always runs in the same thread, I don't know.
#8
thanks for your help.
I've tried the PowerShell code below, but the execution result is the same as the C# code above.
I've searched through a lot of resources, but still haven't found what I need. It's much more complex than I imagined.
 
Code:
Copy      Help
# UserA 1
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{
    userMes   = 'What is the capital of Japan?'
    createBot = $true
}

sleep 5

# UserA 2
Invoke-RestMethod http://localhost:4455/GetGPTMes -Method Post -Body @{
    userMes   = 'Translate the previous answer into French.'
    createBot = $false
}


Forum Jump:


Users browsing this thread: 1 Guest(s)