Initial commit

This commit is contained in:
John Wang
2023-05-15 08:51:32 +08:00
commit db896255d6
744 changed files with 56028 additions and 0 deletions

49
sdks/nodejs-client/.gitignore vendored Normal file
View File

@@ -0,0 +1,49 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# npm
package-lock.json
# yarn
.pnp.cjs
.pnp.loader.mjs
.yarn/
yarn.lock
.yarnrc.yml
# pmpm
pnpm-lock.yaml

View File

@@ -0,0 +1,46 @@
# Dify Node.js SDK
This is the Node.js SDK for the Dify API, which allows you to easily integrate Dify into your Node.js applications.
## Install
```bash
npm install dify-client
```
## Usage
After installing the SDK, you can use it in your project like this:
```js
import { DifyClient, ChatClient, CompletionClient } from 'dify-client'
const API_KEY = 'your-api-key-here';
const user = `random-user-id`:
// Create a completion client
const completionClient = new CompletionClient(API_KEY)
// Create a completion message
completionClient.createCompletionMessage(inputs, query, responseMode, user)
// Create a chat client
const chatClient = new ChatClient(API_KEY)
// Create a chat message
chatClient.createChatMessage(inputs, query, responseMode, user, conversationId)
// Fetch conversations
chatClient.getConversations(user)
// Fetch conversation messages
chatClient.getConversationMessages(conversationId, user)
// Rename conversation
chatClient.renameConversation(conversationId, name, user)
const client = new DifyClient(API_KEY)
// Fetch application parameters
client.getApplicationParameters(user)
// Provide feedback for a message
client.messageFeedback(messageId, rating, user)
```
Replace 'your-api-key-here' with your actual Dify API key.Replace 'your-app-id-here' with your actual Dify APP ID.
## License
This SDK is released under the MIT License.

140
sdks/nodejs-client/index.js Normal file
View File

@@ -0,0 +1,140 @@
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 DifyClient {
constructor(apiKey, baseUrl = BASE_URL) {
this.apiKey = apiKey
this.baseUrl = baseUrl
}
updateApiKey(apiKey) {
this.apiKey = apiKey
}
async sendRequest(method, endpoint, data = null, params = null, stream = false) {
const headers = {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
}
const url = `${this.baseUrl}${endpoint}`
let response
if (!stream) {
response = await axios({
method,
url,
data,
params,
headers,
responseType: stream ? 'stream' : 'json',
})
} else {
response = await fetch(url, {
headers,
method,
body: JSON.stringify(data),
})
}
return response
}
messageFeedback(messageId, rating, user) {
const data = {
rating,
user,
}
return this.sendRequest(routes.feedback.method, routes.feedback.url(messageId), data)
}
getApplicationParameters(user) {
const params = { user }
return this.sendRequest(routes.application.method, routes.application.url(), null, params)
}
}
export class CompletionClient extends DifyClient {
createCompletionMessage(inputs, query, user, responseMode) {
const data = {
inputs,
query,
responseMode,
user,
}
return this.sendRequest(routes.createCompletionMessage.method, routes.createCompletionMessage.url(), data, null, responseMode === 'streaming')
}
}
export class ChatClient extends DifyClient {
createChatMessage(inputs, query, user, responseMode = 'blocking', conversationId = null) {
const data = {
inputs,
query,
user,
responseMode,
}
if (conversationId)
data.conversation_id = conversationId
return this.sendRequest(routes.createChatMessage.method, routes.createChatMessage.url(), data, null, responseMode === 'streaming')
}
getConversationMessages(user, conversationId = '', firstId = null, limit = null) {
const params = { user }
if (conversationId)
params.conversation_id = conversationId
if (firstId)
params.first_id = firstId
if (limit)
params.limit = limit
return this.sendRequest(routes.getConversationMessages.method, routes.getConversationMessages.url(), null, params)
}
getConversations(user, firstId = null, limit = null, pinned = null) {
const params = { user, first_id: firstId, limit, pinned }
return this.sendRequest(routes.getConversations.method, routes.getConversations.url(), null, params)
}
renameConversation(conversationId, name, user) {
const data = { name, user }
return this.sendRequest(routes.renameConversation.method, routes.renameConversation.url(conversationId), data)
}
}

View File

@@ -0,0 +1,20 @@
{
"name": "dify-client",
"version": "1.0.2",
"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",
"type": "module",
"keywords": [
"Dify",
"Dify.AI",
"LLM"
],
"author": "Joel",
"contributors": [
"<crazywoola> <<427733928@qq.com>> (https://github.com/crazywoola)"
],
"license": "MIT",
"dependencies": {
"axios": "^1.3.5"
}
}