transitive-bullshit / agentic

AI agent stdlib that works with any LLM and TypeScript AI SDK.
https://agentic.so
MIT License
16.22k stars 2.12k forks source link

Conversations cannot be tracked #576

Closed inannan423 closed 1 year ago

inannan423 commented 1 year ago

Verify latest release

Verify webapp is working

Environment details

Describe the Bug

I integrated ChatGPT into my own Next.js application, and everything was working fine yesterday. However, when I ran my program today, I found that ChatGPT no longer supports context! In the second round of dialogue, it prompts me "I'm sorry your question lacks context and I can't properly understand what it refers to. Please provide more information for a more accurate answer.", and the same is true for the next few rounds. And I'm pretty sure I successfully passed the parentMessageId to the chatgpt API before the second round of conversation. Please see the logs below.

{
  role: 'assistant',
  id: 'chatcmpl-7Kne0AdojOKKLI52SrQj2Nm*********',
  conversationId: undefined,
  parentMessageId: 'cc7ccf50-8d6b-41bf-8f7a-*********',
  text: 'Content omitted',
  detail: {
    id: 'chatcmpl-7Kne0AdojOKKLI52SrQj2Nm*********',
    object: 'chat.completion',
    created: 1685191748,
    model: 'gpt-3.5-turbo-0301',
    usage: { prompt_tokens: 638, completion_tokens: 638, total_tokens: 1276 },
    choices: [ [Object] ]
  }
}
{
  role: 'assistant',
  id: 'chatcmpl-7KnfXdwG4zkvp2CHNZ8sQZ*******',
  conversationId: undefined,
  // Why isn't the parentMessageId here the one I passed in???
  parentMessageId: '69207f69-6ea4-4bac-9b69-12fdca*******',
  text: 'I'm sorry your question lacks context and I can't properly understand what it refers to. Please provide more information for a more accurate answer.',
  detail: {
    id: 'chatcmpl-7KnfXdwG4zkvp2CHNZ8sQZ*******',
    object: 'chat.completion',
    created: 1685191843,
    model: 'gpt-3.5-turbo-0301',
    usage: { prompt_tokens: 73, completion_tokens: 53, total_tokens: 126 },
    choices: [ [Object] ]
  }
}

I have tried multiple times, but the issue persists. I haven't found any information about this on the internet. As you can see in the code below, I first created a 'chat' request in the API route of Next.js to redirect the request to ChatGPT. The parentMessageId is not set for the first conversation, but it is set to the ID of the previous conversation for subsequent ones.

// nextjs /pages/api/chat.ts
const api = new ChatGPTAPI({
  apiKey: process.env.OPENAI_API_KEY as string,
  apiBaseUrl: process.env.OPENAI_API_BASE_URL as string,
  completionParams: {
    model: 'gpt-3.5-turbo-0301'
  }
})

export default async function handler (
  req: NextApiRequest,
  res: NextApiResponse<ChatGPTResponse>
): Promise<void> {
  // Get the message and parentMessageId from the query
  const { message, parentMessageId } = req.query

  console.log('parentMessageId is: ', parentMessageId)

  // Send the message to the API
  await api.sendMessage(
    message as string,
    {
      parentMessageId: ((parentMessageId?.toString()) != null) ? parentMessageId.toString() : undefined
    }
  ).then(async (rep) => {
    console.log(rep)
    res.status(200).json({ status: 'ok', text: rep.text, timestamp: new Date().getTime(), id: rep.id })
  }).catch((err) => {
    // Return the error
    console.log(err)
    res.status(500).json({ status: 'error', text: err.message, timestamp: new Date().getTime() })
  })
}
 // Here chatgptParent was successfully fetched
 await fetch('/api/chat?message=' + '' : '') + '&parentMessageId=' + chatgptParent, {
      method: 'GET',
      headers: {
        accept: 'application/json',
        'content-type': 'application/json'
      }
    }).then(async (res) => await res.json())
      .then((res) => {
          // ... Some unimportant operations
          setChatgptParent(res.id)
          setStatus('ok')
          console.log(chat)
        }
      })

Since the issue occurred suddenly, I suspect it might be a bug. I would greatly appreciate it if you could take a look at this problem.

(By the way, I'm using Cloudflare's worker service to redirect the ChatGPT API service because ChatGPT is inaccessible in some countries. Could that be the issue?)

inannan423 commented 1 year ago

Problem Solved

The problem has been resolved. There was an error in the code above the API request, which triggered the recompilation of Next.js and the reinitialization of ChatGPTAPI. Consequently, the conversation was forgotten. If you encounter similar issues, please consider troubleshooting the following two aspects:

  1. Place the API initialization at the top of the module instead of initializing it each time the interface is requested.
  2. Check for any bugs in the code that may cause the dev environment to restart.