最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Gemini chat history on React - Stack Overflow

programmeradmin0浏览0评论

Im stuck. My setChatHistory state is being called twice and data is dublicated. And only place where i use it is in the function below, which is being called by onClick event. I have discovered that it works fine if i remove it from chat settings: const chat = model.startChat({ history: chatHistory, generationConfig: { maxOutputTokens: 3000, }, });

const chat = model.startChat({
            history: chatHistory,
            generationConfig: {
                maxOutputTokens: 3000,
            },
        });
        let modelRes = {};
        try {
            await chat.sendMessage(query).then((value)=> {
                console.log(value)
                modelRes = formatAIText(value.response.text());
            });
        } catch (error) {
            console.error("Error sending message:", error);
        }
        if(modelRes.text) {
            setModelResult(modelRes);
            setChatHistory((prev)=> [
                ...prev,
                { role: "user", parts: [{ text: query }] },
                { role: "model", parts: [{ text: modelRes.text }] }
            ])
        }

i expect it to return not dublicated messages to render it using map.

Im stuck. My setChatHistory state is being called twice and data is dublicated. And only place where i use it is in the function below, which is being called by onClick event. I have discovered that it works fine if i remove it from chat settings: const chat = model.startChat({ history: chatHistory, generationConfig: { maxOutputTokens: 3000, }, });

const chat = model.startChat({
            history: chatHistory,
            generationConfig: {
                maxOutputTokens: 3000,
            },
        });
        let modelRes = {};
        try {
            await chat.sendMessage(query).then((value)=> {
                console.log(value)
                modelRes = formatAIText(value.response.text());
            });
        } catch (error) {
            console.error("Error sending message:", error);
        }
        if(modelRes.text) {
            setModelResult(modelRes);
            setChatHistory((prev)=> [
                ...prev,
                { role: "user", parts: [{ text: query }] },
                { role: "model", parts: [{ text: modelRes.text }] }
            ])
        }

i expect it to return not dublicated messages to render it using map.

Share Improve this question asked yesterday Adilet AitmatovAdilet Aitmatov 131 silver badge2 bronze badges New contributor Adilet Aitmatov is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

Based on the code and the context provided, it is possible that your issue is caused not by your code but in the way you are managing chatHistory.

It looks like that the chat instance remembers the messages you are sending throughout a chat, meaning that when you initialize the chat with:

const chat = model.startChat({ history: chatHistory, generationConfig: { maxOutputTokens: 3000 }, });

the chat instance was already “seeded” with your previous messages.

Long story short, when you send a new message first it gets combined with model "internal history" (which includes all previous messages) and then you add it again to your state.

A quick fix could be:

  1. As you mention you could get rid of chatHistory so that the chat does not preload the history and then you only manage your conversation through setChatHistory

Therefore call startChat like this:

const chat = model.startChat({
    generationConfig: { maxOutputTokens: 3000 },
});

or

  1. Let the chat API manage the state for you, meaning that you might not need a dedicated state for tracking chat changes at all.
发布评论

评论列表(0)

  1. 暂无评论