dify icon indicating copy to clipboard operation
dify copied to clipboard

DifY API is not bringing the conversation/session history

Open edrizo opened this issue 2 years ago • 46 comments

Self Checks

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

Dify version

0.8.3

Cloud or Self Hosted

Cloud

Steps to reproduce

When we click view through DiFy itself, the conversation is complete. The session is being considered in the conversation.

However, when using the API call, this does not happen.

image

image

image

✔️ Expected Behavior

API calls working with the conversation session being respected.

❌ Actual Behavior

image

All previous conversations do not appear in the session history.

edrizo avatar Sep 30 '24 03:09 edrizo

Hello, @edrizo! I'm here to help you with any bugs, questions, or contributions you have regarding DifY. Let's tackle this issue together!

I found a similar open issue that might be relevant to your problem: Custom API Chatbot with nodejs no history of the conversation Included. The suggested solution involves ensuring that the conversation_id is correctly managed in your API requests. Here is a brief overview of how to handle 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 in 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 [1].

Additionally, the Dify API returns specific error codes or messages when the conversation_id is missing or incorrect, such as 404, Conversation does not exists and 400, invalid_param, abnormal parameter input [2][3].

For securely managing and using API keys, it is recommended to call them through the backend rather than exposing them in plaintext within frontend code or requests. This helps prevent your application from being abused or attacked [4][5][6][7].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 30 '24 03:09 dosubot[bot]

Endpoint used:

image image

edrizo avatar Sep 30 '24 03:09 edrizo

Chatbot: image

edrizo avatar Sep 30 '24 03:09 edrizo

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

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

I'm doing this. The conversation_id is corret. But still have the issue.

edrizo avatar Sep 30 '24 03:09 edrizo

When using the Dify API, if the conversation_id is correct but the conversation history is still not maintained, ensure the following:

  1. Correct Structure: The inputs field should be an empty object {} when using an existing conversation_id. Only the query is processed for the ongoing conversation.

    Example structure:

    {
        "inputs": {},
        "query": "ed",
        "response_mode": "blocking",
        "conversation_id": "your-conversation-id",
        "user": "abc-123"
    }
    
  2. Text Completion Model: Conversation history is only available in text completion models for conversational applications. Ensure that your application is using a compatible model.

  3. Error Codes: Check for any specific error codes returned by the API that might indicate issues with the conversation_id or other parameters. Common errors include:

    • 404, Conversation does not exist
    • 400, invalid_param, abnormal parameter input
    • 400, app_unavailable, App configuration unavailable
    • 400, provider_not_initialize, no available model credential configuration
    • 400, provider_quota_exceeded, model invocation quota insufficient
    • 400, model_currently_not_support, current model unavailable
    • 400, completion_request_error, text generation failed
    • 500, internal server error [1][2].
  4. Built-in Rules: Dify assembles and concatenates historical conversation records according to built-in rules and replaces the conversation history variable. Ensure that the Human and Assistant prefixes are correctly set if modified [3][4].

If all these conditions are met and the issue persists, it might be necessary to review the specific implementation details or consult the Dify documentation for any additional configuration requirements.

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 30 '24 03:09 dosubot[bot]

  1. This is the first call: image Then i get the conversation_id generated.

  2. Now, with the id:

image image image

  1. Still the same

image

edrizo avatar Sep 30 '24 03:09 edrizo

@dosu

edrizo avatar Sep 30 '24 03:09 edrizo

  1. This is the first call: image Then i get the conversation_id generated.
  2. Now, with the id:

image image image

  1. Still the same

image

@dosu

edrizo avatar Sep 30 '24 03:09 edrizo

To ensure the conversation history is maintained using the Dify API, follow these steps:

  1. Initial Call: Make the first API call without a conversation_id to generate a new one.
  2. Subsequent Calls: Use the generated conversation_id for all subsequent API calls to maintain the conversation context.

Here is a detailed example:

Initial Call

Make the first call to generate a conversation_id:

{
    "inputs": {},
    "query": "Hi",
    "response_mode": "blocking",
    "user": "abc-123"
}

The response will include a conversation_id:

{
    "event": "message",
    "task_id": "task-123",
    "id": "id-123",
    "message_id": "msg-123",
    "conversation_id": "conv-123",
    "mode": "blocking",
    "answer": "Hello! How can I help you?",
    "metadata": {
        "created_at": "2024-09-30T12:34:56Z"
    }
}

Subsequent Calls

Use the conversation_id from the initial call for subsequent requests:

{
    "inputs": {},
    "query": "Can you tell me more about that?",
    "response_mode": "blocking",
    "conversation_id": "conv-123",
    "user": "abc-123"
}

Key Points

  1. Empty inputs: Ensure the inputs field is an empty object {} when using an existing conversation_id.
  2. Text Completion Model: Verify that your application is using a text completion model, as conversation history is only available for these models.
  3. Error Handling: Check for specific error codes that might indicate issues with the conversation_id or other parameters.

If the issue persists despite following these steps, ensure that the Human and Assistant prefixes are correctly set if they have been modified [1][2].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 30 '24 03:09 dosubot[bot]

I did it. And still remains the issue.

edrizo avatar Sep 30 '24 04:09 edrizo

I have the same issue, not happening in the previous version. whats has changed?

ramishi avatar Sep 30 '24 04:09 ramishi

I have the same issue, not happening in the previous version. whats has changed?

Which version?

I'm using cloud version. Not self hosted.

Weird.

edrizo avatar Sep 30 '24 05:09 edrizo

yes I mean the previous cloud version doesn't have this issue.

ramishi avatar Sep 30 '24 06:09 ramishi

I'm having this problem too...

cuongphamduc avatar Sep 30 '24 08:09 cuongphamduc

confirm its only in cloud version. self-host version working fine even with the latest 0.8.3

ramishi avatar Sep 30 '24 10:09 ramishi

I also confirmed this problem with the cloud version. When looking at communications in the developer console, all history seems to remain, but it doesn't seem to be displayed on the history screen.

There is a difference in the parent_message_id is assigned between the history in the regular chat UI and the history data in the API. In the case of the API, parent_message_id is always null. This seems to be why the chat history cannot be displayed and organized on the history screen. I suspect that this may be due to a change in the way parent_message_id is handled in the API, or a change in the way chat history is displayed.

fujita-h avatar Sep 30 '24 11:09 fujita-h

confirm its only in cloud version. self-host version working fine even with the latest 0.8.3

Did you self hosted it? And the issue is not happening?

edrizo avatar Sep 30 '24 13:09 edrizo

Also having the same problem, I'll buy a server so I self-host Dify due to this problem.

eduardochaves1 avatar Sep 30 '24 13:09 eduardochaves1

Endpoint used:

image image

"How did you make the conversation_Id appear before the HTTP Request from Dify? In mine, it only appears in the output."

braiaagency avatar Sep 30 '24 13:09 braiaagency

This is it! After upgrading the Docker version from 0.8.3 to 0.9.0, I encountered the same issue. Is there any way to roll back to version 0.8.3?😭

xwineapplex avatar Sep 30 '24 13:09 xwineapplex