Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DifY API is not bringing the conversation/session history #8910

Closed
5 tasks done
edrizo opened this issue Sep 30, 2024 · 42 comments · Fixed by #8749, #8965 or #8984
Closed
5 tasks done

DifY API is not bringing the conversation/session history #8910

edrizo opened this issue Sep 30, 2024 · 42 comments · Fixed by #8749, #8965 or #8984
Assignees
Labels
🐞 bug Something isn't working

Comments

@edrizo
Copy link

edrizo commented Sep 30, 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.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.

Copy link

dosubot bot commented Sep 30, 2024

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.

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

Endpoint used:

image
image

@dosubot dosubot bot added 🐞 bug Something isn't working 🤔 cant-reproduce We can not reproduce it or the information is limited labels Sep 30, 2024
@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

Chatbot:
image

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

  • 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.

Copy link

dosubot bot commented Sep 30, 2024

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.

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

  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
Copy link
Author

edrizo commented Sep 30, 2024

@dosu

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

  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

Copy link

dosubot bot commented Sep 30, 2024

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.

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

I did it. And still remains the issue.

@ramishi
Copy link

ramishi commented Sep 30, 2024

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

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

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.

@ramishi
Copy link

ramishi commented Sep 30, 2024

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

@cuongphamduc
Copy link

cuongphamduc commented Sep 30, 2024

I'm having this problem too...

@ramishi
Copy link

ramishi commented Sep 30, 2024

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

@fujita-h
Copy link
Contributor

fujita-h commented Sep 30, 2024

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.

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

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?

@eduardochaves1
Copy link

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

@braiaagency
Copy link

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."

@xwineapplex
Copy link

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?😭

@fujita-h
Copy link
Contributor

fujita-h commented Sep 30, 2024

self-hosted v0.8.3 (7f3282e) -> issue NOT happened
self-hosted v0.9.0 (d2ce496) -> issue happened
cloud ->issue happened

@fujita-h
Copy link
Contributor

I tested the cloud version about 2 hours ago and found that a problem had occurred, and when I checked just now, the history was displayed normally.
However, when I tried the same test just now, the problem was still occurring.

I have experienced the problem going away after a certain amount of time has passed.

Either way, the history where the problem occurred has a parent_message_id of null.

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

Complicated.

What to do now?

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

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?😭

I don't know how to do it.

@xwineapplex
Copy link

xwineapplex commented Sep 30, 2024

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?😭

I don't know how to do it.

Alright, I got it.
Go to the folder and find 'docker-compose.yaml.'
Edit that file and change the image versions for api, worker, and web from 0.9.0 to 0.8.3.

Then,

docker compose down
docker compose pull
docker compose up -d

At this point, just hope that the developers or other users will also notice the issue.

Is using Dify through the API really that uncommon? The missing conversation history should be pretty obvious...

(Oh, I’m sorry I didn’t notice that you’re using the cloud version.
I feel bad for you, but maybe this is a good opportunity to consider using the self-hosted Docker version.)

@braiaagency
Copy link

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?😭

I don't know how to do it.

Alright, I got it. Go to the folder and find 'docker-compose.yaml.' Edit that file and change the image versions for api, worker, and web from 0.9.0 to 0.8.3.

Then,

docker compose down
docker compose pull
docker compose up -d

At this point, just hope that the developers or other users will also notice the issue.

Is using Dify through the API really that uncommon? The missing conversation history should be pretty obvious...

I use Dify with N8N and Z-API and the only solution I found for this issue is to pay for Redis and save the information inside their server (or something like that), which is not optimal for something that is a bug, hopefully they will fix it.

@crazywoola
Copy link
Member

See #8749, this is a follow-up pr to display the regenerated things.

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

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?😭

I don't know how to do it.

Alright, I got it. Go to the folder and find 'docker-compose.yaml.' Edit that file and change the image versions for api, worker, and web from 0.9.0 to 0.8.3.
Then,

docker compose down
docker compose pull
docker compose up -d

At this point, just hope that the developers or other users will also notice the issue.
Is using Dify through the API really that uncommon? The missing conversation history should be pretty obvious...

I use Dify with N8N and Z-API and the only solution I found for this issue is to pay for Redis and save the information inside their server (or something like that), which is not optimal for something that is a bug, hopefully they will fix it.

Mine also have
-sandbox (latest), and
-weviate (latest)

they also should be 0.8.3?

@crazywoola crazywoola self-assigned this Sep 30, 2024
@crazywoola crazywoola removed the 🤔 cant-reproduce We can not reproduce it or the information is limited label Sep 30, 2024
@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

image

@xwineapplex
Copy link

@edrizo

Mine also have -sandbox (latest), and -weviate (latest)

they also should be 0.8.3?

  api:
    image: langgenius/dify-api:0.8.3
    
  worker:
    image: langgenius/dify-api:0.8.3
    
  web:
    image: langgenius/dify-web:0.8.3  

As for the others, like the sandbox and such, I didn’t make any changes. Keeping them as they are should be fine.

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

@edrizo

Mine also have -sandbox (latest), and -weviate (latest)
they also should be 0.8.3?

  api:
    image: langgenius/dify-api:0.8.3
    
  worker:
    image: langgenius/dify-api:0.8.3
    
  web:
    image: langgenius/dify-web:0.8.3  

As for the others, like the sandbox and such, I didn’t make any changes. Keeping them as they are should be fine.

image

@xwineapplex
Copy link

xwineapplex commented Sep 30, 2024

That's strange, but I did roll back to version 0.8.3.

At least we’ve got the attention and response from the official team. I’m glad they’re aware of the issue and are working on it, and I really hope everything can be resolved soon.

螢幕擷取畫面 2024-09-30 231654

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

Running migrations
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
sagemaker.config INFO - Not applying SDK defaults from location: /etc/xdg/sagemaker/config.yaml
sagemaker.config INFO - Not applying SDK defaults from location: /root/.config/sagemaker/config.yaml
INFO:matplotlib.font_manager:generated new fontManager
Preparing database migration...
Start database migration.
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
ERROR [flask_migrate] Error: Can't locate revision identified by '33f5fac87f29'

image

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

image

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

@crazywoola

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

Running migrations None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used. sagemaker.config INFO - Not applying SDK defaults from location: /etc/xdg/sagemaker/config.yaml sagemaker.config INFO - Not applying SDK defaults from location: /root/.config/sagemaker/config.yaml INFO:matplotlib.font_manager:generated new fontManager Preparing database migration... Start database migration. INFO [alembic.runtime.migration] Context impl PostgresqlImpl. INFO [alembic.runtime.migration] Will assume transactional DDL. ERROR [flask_migrate] Error: Can't locate revision identified by '33f5fac87f29'

image

@xd845217xd this is the error when trying to use 0.8.3

@edrizo
Copy link
Author

edrizo commented Sep 30, 2024

Anyone?

@perzeuss
Copy link
Contributor

perzeuss commented Sep 30, 2024

I am able to reproduce this with a chatflow in both streaming and blocking mode. Dify Cloud v 0.8.3.
Looks like Dify overwrites the history on every message. The conversation id remains the same when new messages come in, but the last message-answer pair replaces the whole conversation history.

@takatost that's a critical issue, it's breaking a lot of applications right now.

@perzeuss
Copy link
Contributor

See #8749, this is a follow-up pr to display the regenerated things.

@crazywoola That's another issue. Using the Advanced Chat App API results in conversation history being lost and LLM calls do not contain the history. I assume the breaking change coming with the regeneration feature broke the Advanced Chat App API.

@IsraelHenriquee
Copy link

Same problem here!!!!

@fujita-h
Copy link
Contributor

@edrizo Once you upgrade from 0.8.3 to 0.9.0, the database contents will be upgraded for 0.9.0, so even if you revert only the Docker image to 0.8.3, it will not start.

You will also need to write back the contents of the volumes directory obtained following the steps in the Upgrade Guide at https://github.com/langgenius/dify/releases.

@edrizo
Copy link
Author

edrizo commented Oct 1, 2024

@edrizo Once you upgrade from 0.8.3 to 0.9.0, the database contents will be upgraded for 0.9.0, so even if you revert only the Docker image to 0.8.3, it will not start.

You will also need to write back the contents of the volumes directory obtained following the steps in the Upgrade Guide at https://github.com/langgenius/dify/releases.

I hadn't installed it on the VPS yet. When I did it, the "latest" version is 0.9.1, and the same bug also happened.

The devs are already working on it. I'll wait for the repair!

Thanks a lot @fujita-h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment