feat: use routes

This commit is contained in:
crazywoola 2023-05-12 10:45:43 +08:00
parent 8b20dec783
commit a2a6734751
2 changed files with 47 additions and 9 deletions

View File

@ -1,7 +1,41 @@
import axios from 'axios' import axios from 'axios'
const BASE_URL = 'https://api.dify.ai/v1'
const routes = {
application: {
method: 'GET',
url: () => `parameters`
},
feedback: {
method: 'POST',
url: (messageId) => `/messages/${messageId}/feedbacks`,
},
createCompletionMessage: {
method: 'POST',
url: () => `/completion-messages`,
},
createChatMessage: {
method: 'POST',
url: () => `/chat-message`,
},
getConversationMessages: {
method: 'GET',
url: () => '/messages',
},
getConversations: {
method: 'GET',
url: () => '/conversations',
},
renameConversation: {
method: 'PATCH',
url: (conversationId) => `/conversations/${conversationId}`,
}
}
export class LangGeniusClient { export class LangGeniusClient {
constructor(apiKey, baseUrl = 'https://api.dify.ai/v1') { constructor(apiKey, baseUrl = BASE_URL) {
this.apiKey = apiKey this.apiKey = apiKey
this.baseUrl = baseUrl this.baseUrl = baseUrl
} }
@ -43,12 +77,12 @@ export class LangGeniusClient {
rating, rating,
user, user,
} }
return this.sendRequest('POST', `/messages/${messageId}/feedbacks`, data) return this.sendRequest(routes.feedback.method, routes.feedback.url(messageId), data)
} }
getApplicationParameters(user) { getApplicationParameters(user) {
const params = { user } const params = { user }
return this.sendRequest('GET', '/parameters', null, params) return this.sendRequest(routes.application.method, routes.application.url(), null, params)
} }
} }
@ -60,7 +94,7 @@ export class CompletionClient extends LangGeniusClient {
responseMode, responseMode,
user, user,
} }
return this.sendRequest('POST', '/completion-messages', data, null, responseMode === 'streaming') return this.sendRequest(routes.createCompletionMessage.method, routes.createCompletionMessage.url(), data, null, responseMode === 'streaming')
} }
} }
@ -75,7 +109,7 @@ export class ChatClient extends LangGeniusClient {
if (conversationId) if (conversationId)
data.conversation_id = conversationId data.conversation_id = conversationId
return this.sendRequest('POST', '/chat-messages', data, null, responseMode === 'streaming') return this.sendRequest(routes.createChatMessage.method, routes.createChatMessage.url(), data, null, responseMode === 'streaming')
} }
getConversationMessages(user, conversationId = '', firstId = null, limit = null) { getConversationMessages(user, conversationId = '', firstId = null, limit = null) {
@ -90,17 +124,18 @@ export class ChatClient extends LangGeniusClient {
if (limit) if (limit)
params.limit = limit params.limit = limit
return this.sendRequest('GET', '/messages', null, params) return this.sendRequest(routes.getConversationMessages.method, routes.getConversationMessages.url(), null, params)
} }
getConversations(user, firstId = null, limit = null, pinned = null) { getConversations(user, firstId = null, limit = null, pinned = null) {
const params = { user, first_id: firstId, limit, pinned } const params = { user, first_id: firstId, limit, pinned }
return this.sendRequest('GET', '/conversations', null, params) return this.sendRequest(routes.getConversations.method, routes.getConversations.url(), null, params)
} }
renameConversation(conversationId, name, user) { renameConversation(conversationId, name, user) {
const data = { name, user } const data = { name, user }
return this.sendRequest('PATCH', `/conversations/${conversationId}`, data) // return this.sendRequest('PATCH', `/conversations/${conversationId}`, data)
return this.sendRequest(routes.renameConversation.method, routes.renameConversation.url(conversationId), data)
} }
} }

View File

@ -1,7 +1,7 @@
{ {
"name": "langgenius-client", "name": "langgenius-client",
"version": "1.1.1", "version": "1.1.1",
"description": "This is the Node.js SDK for the LangGenius API, which allows you to easily integrate LangGenius into your Node.js applications.", "description": "This is the Node.js SDK for the Dify.AI API, which allows you to easily integrate Dify.AI into your Node.js applications.",
"main": "index.js", "main": "index.js",
"type": "module", "type": "module",
"keywords": [ "keywords": [
@ -9,6 +9,9 @@
"LLM" "LLM"
], ],
"author": "Joel", "author": "Joel",
"contributors": [
"<crazywoola> <<427733928@qq.com>> (https://github.com/crazywoola)"
],
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^1.3.5" "axios": "^1.3.5"