Last active
November 1, 2024 19:56
-
-
Save midshipman/f9b686f82c721d4ac99160481dfc26a9 to your computer and use it in GitHub Desktop.
Sample node.js code to use Dify Bot API in stream mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fetch = require('node-fetch'); | |
| class DifyService{ | |
| constructor(apiUrl, apiKey) { | |
| this.apiUrl = apiUrl; | |
| this.apiKey = apiKey; | |
| this.conversation_ids = new Map(); | |
| } | |
| async getResponse(query, number) { | |
| const headers = { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${this.apiKey}`, | |
| }; | |
| const data = { | |
| inputs: {}, | |
| query: query, | |
| response_mode: 'streaming', | |
| conversation_id: this.conversation_ids.get(number) || '', | |
| user: '124', | |
| }; | |
| try { | |
| const response = await fetch(this.apiUrl, { | |
| method: 'POST', | |
| headers: headers, | |
| body: JSON.stringify(data), | |
| }); | |
| if (!response.ok) { | |
| throw new Error(`HTTP error! status: ${response.status} - ${await response.text()}`); //Improved error message | |
| } | |
| let answers = []; | |
| let buffer = '' | |
| response.body.on('data', (chunk) => { | |
| buffer += chunk.toString().trim(); // Append current chunk to buffer | |
| // console.log('\n buffer: =====\n', buffer, '\n=======\n'); | |
| // Process complete JSON objects in the buffer | |
| if (buffer.endsWith('}')) { | |
| // console.log('buffer: \n', buffer, '\n'); | |
| const lines = buffer.split('\n'); | |
| buffer = ''; // Clear buffer after splitting | |
| lines.forEach(line => { | |
| line = line.trim(); | |
| if (line.startsWith('data: ') && line.endsWith('}')) { | |
| const decodedLine = line.substring(6); // Remove 'data: ' | |
| try { | |
| const jsonLine = JSON.parse(decodedLine); | |
| // console.log('jsonLine: \n', jsonLine, '\n'); | |
| if (jsonLine.event === "message") { | |
| // console.log('jsonLine conv id: ', jsonLine.conversation_id, '\n'); | |
| console.log('jsonLine answer: ', jsonLine.answer, '\n'); | |
| this.conversation_ids.set(number, jsonLine.conversation_id); | |
| answers.push(jsonLine.answer); | |
| //todo: whatever you need to handle the answer | |
| } | |
| if (jsonLine.event === "message_end") { | |
| const mergedAnswer = answers.join(''); | |
| console.log('answers', mergedAnswer); | |
| //todo: whatever to handle the end of the message | |
| } | |
| } catch (error) { | |
| console.error("JSON parsing error:", error, "Line:", decodedLine); | |
| } | |
| } | |
| }); | |
| } | |
| }); | |
| } catch (error) { | |
| console.error('Error fetching Diffy response:', error); | |
| // throw error; | |
| } | |
| } | |
| } | |
| // Example usage: | |
| const dify = new DifyService('https://api.dify.ai/v1/chat-messages', 'your_key_here'); // Replace with your actual key | |
| const query = 'what is my last pizza order'; | |
| dify.getResponse(query, '123456'); | |
| module.exports = {DifyService}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment