Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP request Multiple conversations with ChatGPT while keeping context
#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; }
    }
}


Messages In This Thread
RE: HTTP request Multiple conversations with ChatGPT while keeping context - by Davider - 04-28-2024, 09:22 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)