Custom API Chatbot with nodejs no history of the conversation Included #5063

Closed
opened 2026-02-21 18:09:13 -05:00 by yindo · 2 comments
Owner

Originally created by @andsty on GitHub (Aug 15, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.6.16

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Create a chatbot with nodejs

✔️ Expected Behavior

When asking follow up question to remember history

Actual Behavior

It does not remember previous conversation

Originally created by @andsty on GitHub (Aug 15, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.6.16 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce Create a chatbot with nodejs ### ✔️ Expected Behavior When asking follow up question to remember history ### ❌ Actual Behavior It does not remember previous conversation
yindo added the 🐞 bug label 2026-02-21 18:09:13 -05:00
yindo closed this issue 2026-02-21 18:09:13 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 15, 2024):

To fix the issue where your custom API chatbot created using Node.js and Dify version 0.6.16 does not remember the previous conversation when asking follow-up questions, you need to ensure that you are correctly managing the conversation_id in your API requests. Here is how you can do it:

  1. Retrieve the conversation_id after the initial chat request.
  2. Use the same conversation_id for subsequent requests to maintain the context of the conversation.

Here is an example of how you can achieve this:

const axios = require('axios');

const API_URL = 'https://api.dify.ai/v1/chat-messages';
const API_KEY = 'YOUR_SECRET_KEY';
let conversationId = '';

async function sendMessage(query) {
    try {
        const response = await axios.post(API_URL, {
            inputs: {},
            query: query,
            response_mode: 'streaming',
            conversation_id: conversationId,
            user: 'abc-123'
        }, {
            headers: {
                'Authorization': `Bearer ${API_KEY}`,
                'Content-Type': 'application/json'
            }
        });

        // Update conversationId with the one from the response
        if (response.data.conversation_id) {
            conversationId = response.data.conversation_id;
        }

        console.log(response.data);
    } catch (error) {
        console.error('Error sending message:', error);
    }
}

// Example usage
sendMessage('Hello, how are you?').then(() => {
    sendMessage('Can you tell me more about that?');
});

In this example:

  • The conversationId is initially empty.
  • After the first message is sent, the conversationId is updated with the value from the response.
  • Subsequent messages use the updated conversationId to maintain the conversation context.

Make sure to replace YOUR_SECRET_KEY with your actual API secret key. This approach ensures that the chatbot remembers the previous conversation when handling follow-up questions.

Additionally, as per Dify's best practices, API keys should be called through the backend, rather than being directly exposed in plaintext within frontend code or requests. This helps prevent your application from being abused or attacked [1][2].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Aug 15, 2024): <!-- Answer --> To fix the issue where your custom API chatbot created using Node.js and Dify version 0.6.16 does not remember the previous conversation when asking follow-up questions, you need to ensure that you are correctly managing the `conversation_id` in your API requests. Here is how you can do it: 1. **Retrieve the `conversation_id`** after the initial chat request. 2. **Use the same `conversation_id`** for subsequent requests to maintain the context of the conversation. Here is an example of how you can achieve this: ```javascript const axios = require('axios'); const API_URL = 'https://api.dify.ai/v1/chat-messages'; const API_KEY = 'YOUR_SECRET_KEY'; let conversationId = ''; async function sendMessage(query) { try { const response = await axios.post(API_URL, { inputs: {}, query: query, response_mode: 'streaming', conversation_id: conversationId, user: 'abc-123' }, { headers: { 'Authorization': `Bearer ${API_KEY}`, 'Content-Type': 'application/json' } }); // Update conversationId with the one from the response if (response.data.conversation_id) { conversationId = response.data.conversation_id; } console.log(response.data); } catch (error) { console.error('Error sending message:', error); } } // Example usage sendMessage('Hello, how are you?').then(() => { sendMessage('Can you tell me more about that?'); }); ``` In this example: - The `conversationId` is initially empty. - After the first message is sent, the `conversationId` is updated with the value from the response. - Subsequent messages use the updated `conversationId` to maintain the conversation context. Make sure to replace `YOUR_SECRET_KEY` with your actual API secret key. This approach ensures that the chatbot remembers the previous conversation when handling follow-up questions. Additionally, as per Dify's best practices, API keys should be called through the backend, rather than being directly exposed in plaintext within frontend code or requests. This helps prevent your application from being abused or attacked <sup>[[1]](https://github.com/langgenius/dify-docs/blob/main/en/learn-more/use-cases/build-an-notion-ai-assistant.md)</sup><sup>[[2]](https://github.com/langgenius/dify-docs/blob/main/en/guides/application-publishing/developing-with-apis.md)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@hunter-eric commented on GitHub (Aug 21, 2024):

can we submit history without conversion_id? We need custom some content of history.

@hunter-eric commented on GitHub (Aug 21, 2024): can we submit history without conversion_id? We need custom some content of history.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5063