transitive-bullshit / agentic

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

API members context only after launch #490

Closed vvmspace closed 1 year ago

vvmspace commented 1 year ago

Verify latest release

Verify webapp is working

Environment details

const { ChatGPTAPI } = await import('chatgpt');

const api = new ChatGPTAPI({
    apiKey: process.env.OPEN_AI_TOKEN,
});

Describe the Bug

The problem was described there: https://github.com/transitive-bullshit/chatgpt-api/issues/316

But issue was closed.

The problem is when you get a new API instance, for example, this will not retain history:

let api = new ChatGPTAPI({
  apiKey: process.env.OPENAI
})
let res = await api.sendMessage('Write two paragraphs about cotton candy.')
console.log(res.text)
api = new ChatGPTAPI({
  apiKey: process.env.OPENAI
})
res = await api.sendMessage('What were we talking about?', {conversationId: res.conversationId, parentMessageId: res.id})
console.log(res.text)

But this will:

let api = new ChatGPTAPI({
  apiKey: process.env.OPENAI
})
let res = await api.sendMessage('Write two paragraphs about cotton candy.')
console.log(res.text)
res = await api.sendMessage('What were we talking about?', {conversationId: res.conversationId, parentMessageId: res.id})
console.log(res.text)

It means that we need always initialized object for user's chat. It is not always possible.

For example I'm using Serverless and repository ups on function call only.

zhukunpenglinyutong commented 1 year ago

我解决了

// 内存属性 const keyApiData = {} // 先获取下内存中有没有key实例,没有就创建,有就读取 if (keyApiData[optionsKey.apiKey] === undefined) { keyApiData[optionsKey.apiKey] = new ChatGPTAPI({ ...optionsKey })
} api = keyApiData[optionsKey.apiKey]

jyxinye commented 1 year ago

我解决了

// 内存属性 const keyApiData = {} // 先获取下内存中有没有key实例,没有就创建,有就读取 if (keyApiData[optionsKey.apiKey] === undefined) { keyApiData[optionsKey.apiKey] = new ChatGPTAPI({ ...optionsKey }) } api = keyApiData[optionsKey.apiKey]

我也遇到了同样的问题,按照你的方式并没有解决。你是如何解决这个问题。parentMessageId这个参数,存储下来也不好用

transitive-bullshit commented 1 year ago

You can pass a custom key/value store to store and fetch previous messages.

The default uses an in-memory store. Search existing issues or take a look at this example for how to do this: https://github.com/transitive-bullshit/chatgpt-twitter-bot/blob/main/src/index.ts#L134-L149