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

parentMessageId is not valid,How to use parentMessageId #553

Closed lwm98 closed 3 months ago

lwm98 commented 1 year ago

Verify latest release

Verify webapp is working

Environment details

node14

Describe the Bug

const api = new ChatGPTAPI({ apiKey: apiKey })

const res = await api.sendMessage("Who is Iron Man?") --> "Iron Man is a superhero character from Marvel Comics, created by writer Stan Lee, designed by artist Don Heck and first appeared in Tales of Suspense #39 in 1963. The character's real name is Tony Stark, a billionaire inventor and playboy who builds a suit of powered armor to become a high-tech crime fighter known as Iron Man."

const res1 = await api.sendMessage("Is he dead?", { parentMessageId: res.id }) --> "I'm sorry, I don't have enough information to answer your question. Could you please provide me with more context about who or what you are referring to?"

olafthiele commented 1 year ago

Same problem here, would be great to have a fix. @transitive-bullshit do you need any more info to debug this?

sidan5 commented 1 year ago

Same here....

juiiyang commented 1 year ago

When u new a ChatGPTAPI, don't write it in a function.

don't do this:

import ... from ...

function click() {
  const api = new ChatGPTAPI({...});
  const res = await api.sendMessage("hello world");
...
}

do this:

import ... from ...
const api = new ChatGPTAPI({...});

function click() {
  const res = await api.sendMessage("hello world");
...
}
olafthiele commented 1 year ago

When u new a ChatGPTAPI, don't write it in a function.

I am calling a script that is rerun once I have a follow-up and I am calling the object just once, but per script call :-) Judging from the docs this should be possible, or @transitive-bullshit?

sidan5 commented 1 year ago

When u new a ChatGPTAPI, don't write it in a function.

don't do this:

import ... from ...

function click() {
  const api = new ChatGPTAPI({...});
  const res = await api.sendMessage("hello world");
...
}

do this:

import ... from ...
const api = new ChatGPTAPI({...});

function click() {
  const res = await api.sendMessage("hello world");
...
}

This is not possible when using CommonJS (Error related to that 'ChatGPTAPI' is not a constructor).

sidan5 commented 1 year ago

I looked into the messages in debug mode and it seems that the library is sending the whole history to openAI. I think this means:

  1. The library is doing something internal with 'parentMessageId' which doesn't work when it's not running from same constructor.
  2. It would cost much more that using the same parameter using the formal API.

Am I wrong? could this be fixed?

olafthiele commented 1 year ago

Thanks @sidan5, that would explain why it doesn't work for me as the script calls the constructor several times.

sidan5 commented 1 year ago

Thanks @sidan5, that would explain why it doesn't work for me as the script calls the constructor several times.

Check this issue out (just noticed): https://github.com/transitive-bullshit/chatgpt-api/issues/489

sidan5 commented 1 year ago

@olafthiele Have you found any solution for that? I'm trying to figure out if there is a way to actually using this library with multiple messages and I'm not sure it's feasible....

olafthiele commented 1 year ago

@sidan5 I have switched to the official Python lib from openai. They require you to send all previous questions and answers together with the new one. It therefore makes sense that you can't use it stateless. Openai doesn't store (at least for you) the conversation. You have to :-) @transitive-bullshit, could you change sendMessage(s) to us providing all previous messages and send that?

sidan5 commented 1 year ago

@olafthiele Thanks for the reference. Following your comment I've also implemented my use case with OpenAI official with node.js library. Seems to work...

Guess the best case for me to use this library was storing/following the whole user session, as that doesn't happen to my understanding, guess using the official library is better (sadly I would need to implement all the other stuff - sessions per user etc).

youngle316 commented 1 year ago

Singleton pattern can be used.

import { ChatGPTAPI } from "chatgpt";

class CreateAPI {
  private static instance: ChatGPTAPI;

    public static getInstance(apiKey?: string, apiBaseUrl?: string): ChatGPTAPI {
      if (!CreateAPI.instance) {
        CreateAPI.instance = new ChatGPTAPI({
          apiKey: apiKey || "",
          apiBaseUrl: apiBaseUrl || "xxxxxxxx",
        });
      }
    return CreateAPI.instance;
  }
}

export default CreateAPI;
olafthiele commented 1 year ago

Thanks @youngle316, I just need a stateless implementation, but a singletion solves this problem. It's just OpenAI's API, you have to send all previous questions and answers yourself as they count towards the tokens. More of a misunderstanding on my part.

olafthiele commented 1 year ago

@lwm98, ready to close this as it is due to how the API from OpenAI works?

dearsina commented 1 year ago

@lwm98, ready to close this as it is due to how the API from OpenAI works?

As long as people are aware that this project doesn't do anything clever with chat history, it simply manages the sending of the entire history every time you ask a new question. And that over time, this will become a very expensive way of ensuring history.

transitive-bullshit commented 3 months ago

This project is undergoing a major revamp; closing out old issues as part of the prep process.

The chatgpt package is pretty outdated at this point. I recommend that you use the openai package or the openai-fetch package instead.