How can I bring historical chat records to Dify through API? #5489

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

Originally created by @hanhaiszw on GitHub (Sep 9, 2024).

Self Checks

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

1. Is this request related to a challenge you're experiencing? Tell me about your story.

In our application scenario, the chat data is stored in the business database and needs to be filled to dify through the API. Currently, the /chat-messages interface has no parameters for filling the historical chat data.

2. Additional context or comments

Carrying historical chat data to diffify is very important for LLM and business integration.

3. Can you help us with this feature?

  • I am interested in contributing to this feature.
Originally created by @hanhaiszw on GitHub (Sep 9, 2024). ### Self Checks - [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. ### 1. Is this request related to a challenge you're experiencing? Tell me about your story. In our application scenario, the chat data is stored in the business database and needs to be filled to dify through the API. Currently, the /chat-messages interface has no parameters for filling the historical chat data. ### 2. Additional context or comments Carrying historical chat data to diffify is very important for LLM and business integration. ### 3. Can you help us with this feature? - [X] I am interested in contributing to this feature.
yindo added the 💪 enhancement label 2026-02-21 18:11:13 -05:00
yindo closed this issue 2026-02-21 18:11:13 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 9, 2024):

To bring historical chat records to Dify through the API, you can use the create_chat_message method from the ChatClient class. This method allows you to create chat messages, which can be used to import historical chat records.

Here is an example of how you can use this method to import historical chat records:

<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;

class DifyClient {
    private $api_key;
    private $base_url;

    public function __construct($api_key, $base_url = 'https://api.dify.ai/v1') {
        $this->api_key = $api_key;
        $this->base_url = $base_url;
    }

    protected function send_request($method, $endpoint, $data = null, $params = null, $stream = false) {
        $headers = [
            'Authorization' => "Bearer {$this->api_key}",
            'Content-Type' => 'application/json'
        ];

        $url = "{$this->base_url}{$endpoint}";
        $options = [
            'headers' => $headers,
            'json' => $data,
            'query' => $params,
            'stream' => $stream
        ];

        $client = new Client();
        return $client->request($method, $url, $options);
    }
}

class ChatClient extends DifyClient {
    public function create_chat_message($inputs, $query, $user, $response_mode = 'blocking', $conversation_id = null, $files = null) {
        $data = [
            'inputs' => $inputs,
            'query' => $query,
            'user' => $user,
            'response_mode' => $response_mode,
            'files' => $files,
        ];
        if ($conversation_id) {
            $data['conversation_id'] = $conversation_id;
        }

        return $this->send_request('POST', '/chat-messages', $data, null, $response_mode === 'streaming');
    }
}

// Example usage
$api_key = 'your_api_key';
$base_url = 'https://api.dify.ai/v1/';
$chatClient = new ChatClient($api_key, $base_url);

$inputs = ['input1', 'input2']; // Replace with actual inputs
$query = 'Historical chat message content';
$user = 'user_id';
$response_mode = 'blocking'; // or 'streaming' if needed
$conversation_id = 'conversation_id'; // Optional, if you want to associate with a specific conversation

$response = $chatClient->create_chat_message($inputs, $query, $user, $response_mode, $conversation_id);

echo $response->getBody();

In this example, replace 'your_api_key', 'input1', 'input2', 'Historical chat message content', 'user_id', and 'conversation_id' with your actual data. This will create a new chat message in Dify, effectively importing your historical chat record [1][2].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Sep 9, 2024): <!-- Answer --> To bring historical chat records to Dify through the API, you can use the `create_chat_message` method from the `ChatClient` class. This method allows you to create chat messages, which can be used to import historical chat records. Here is an example of how you can use this method to import historical chat records: ```php <?php require 'vendor/autoload.php'; use GuzzleHttp\Client; class DifyClient { private $api_key; private $base_url; public function __construct($api_key, $base_url = 'https://api.dify.ai/v1') { $this->api_key = $api_key; $this->base_url = $base_url; } protected function send_request($method, $endpoint, $data = null, $params = null, $stream = false) { $headers = [ 'Authorization' => "Bearer {$this->api_key}", 'Content-Type' => 'application/json' ]; $url = "{$this->base_url}{$endpoint}"; $options = [ 'headers' => $headers, 'json' => $data, 'query' => $params, 'stream' => $stream ]; $client = new Client(); return $client->request($method, $url, $options); } } class ChatClient extends DifyClient { public function create_chat_message($inputs, $query, $user, $response_mode = 'blocking', $conversation_id = null, $files = null) { $data = [ 'inputs' => $inputs, 'query' => $query, 'user' => $user, 'response_mode' => $response_mode, 'files' => $files, ]; if ($conversation_id) { $data['conversation_id'] = $conversation_id; } return $this->send_request('POST', '/chat-messages', $data, null, $response_mode === 'streaming'); } } // Example usage $api_key = 'your_api_key'; $base_url = 'https://api.dify.ai/v1/'; $chatClient = new ChatClient($api_key, $base_url); $inputs = ['input1', 'input2']; // Replace with actual inputs $query = 'Historical chat message content'; $user = 'user_id'; $response_mode = 'blocking'; // or 'streaming' if needed $conversation_id = 'conversation_id'; // Optional, if you want to associate with a specific conversation $response = $chatClient->create_chat_message($inputs, $query, $user, $response_mode, $conversation_id); echo $response->getBody(); ``` In this example, replace `'your_api_key'`, `'input1'`, `'input2'`, `'Historical chat message content'`, `'user_id'`, and `'conversation_id'` with your actual data. This will create a new chat message in Dify, effectively importing your historical chat record <sup>[[1]](https://github.com/langgenius/dify/blob/main/sdks/php-client/dify-client.php)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/sdks/python-client/dify_client/client.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@crazywoola commented on GitHub (Sep 9, 2024):

See https://docs.dify.ai/guides/knowledge-base for details.
You could import historical messages via knowledge api here https://docs.dify.ai/guides/knowledge-base/maintain-dataset-via-api.
Then you would be able to refer the knowledge in our application.

@crazywoola commented on GitHub (Sep 9, 2024): See https://docs.dify.ai/guides/knowledge-base for details. You could import historical messages via knowledge api here https://docs.dify.ai/guides/knowledge-base/maintain-dataset-via-api. Then you would be able to refer the knowledge in our application.
Author
Owner

@hanhaiszw commented on GitHub (Sep 9, 2024):

Thanks for your answer, but this is not what I need.
I need to dynamically fill in the historical chat data through the API, rather than relying on the knowledge base.
The data in the knowledge base is difficult to maintain dynamically, and the Dify application cannot dynamically switch the knowledge base.

@hanhaiszw commented on GitHub (Sep 9, 2024): Thanks for your answer, but this is not what I need. I need to dynamically fill in the historical chat data through the API, rather than relying on the knowledge base. The data in the knowledge base is difficult to maintain dynamically, and the Dify application cannot dynamically switch the knowledge base.
Author
Owner

@New-King commented on GitHub (Apr 8, 2025):

You spoke my heart. I hope I can send my chat history in the message field like openai api.

@New-King commented on GitHub (Apr 8, 2025): You spoke my heart. I hope I can send my chat history in the message field like openai api.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5489