Initial commit
This commit is contained in:
49
sdks/nodejs-client/.gitignore
vendored
Normal file
49
sdks/nodejs-client/.gitignore
vendored
Normal 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
|
46
sdks/nodejs-client/README.md
Normal file
46
sdks/nodejs-client/README.md
Normal 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
140
sdks/nodejs-client/index.js
Normal 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)
|
||||
}
|
||||
}
|
||||
|
20
sdks/nodejs-client/package.json
Normal file
20
sdks/nodejs-client/package.json
Normal 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"
|
||||
}
|
||||
}
|
51
sdks/php-client/README.md
Normal file
51
sdks/php-client/README.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Dify PHP SDK
|
||||
|
||||
This is the PHP SDK for the Dify API, which allows you to easily integrate Dify into your PHP applications.
|
||||
|
||||
## Requirements
|
||||
|
||||
- PHP 7.2 or later
|
||||
- Guzzle HTTP client library
|
||||
|
||||
## Usage
|
||||
|
||||
After installing the SDK, you can use it in your project like this:
|
||||
|
||||
```
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use YourVendorName\DifyPHP\DifyClient;
|
||||
use YourVendorName\DifyPHP\CompletionClient;
|
||||
use YourVendorName\DifyPHP\ChatClient;
|
||||
|
||||
$apiKey = 'your-api-key-here';
|
||||
|
||||
$difyClient = new DifyClient($apiKey);
|
||||
|
||||
// Create a completion client
|
||||
$completionClient = new CompletionClient($apiKey);
|
||||
$response = $completionClient->create_completion_message($inputs, $query, $response_mode, $user);
|
||||
|
||||
// Create a chat client
|
||||
$chatClient = new ChatClient($apiKey);
|
||||
$response = $chatClient->create_chat_message($inputs, $query, $user, $response_mode, $conversation_id);
|
||||
|
||||
// Fetch application parameters
|
||||
$response = $difyClient->get_application_parameters($user);
|
||||
|
||||
// Provide feedback for a message
|
||||
$response = $difyClient->message_feedback($message_id, $rating, $user);
|
||||
|
||||
// Other available methods:
|
||||
// - get_conversation_messages()
|
||||
// - get_conversations()
|
||||
// - rename_conversation()
|
||||
```
|
||||
|
||||
Replace 'your-api-key-here' with your actual Dify API key.
|
||||
|
||||
## License
|
||||
|
||||
This SDK is released under the MIT License.
|
109
sdks/php-client/dify-client.php
Normal file
109
sdks/php-client/dify-client.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class DifyClient {
|
||||
protected $api_key;
|
||||
protected $base_url;
|
||||
protected $client;
|
||||
|
||||
public function __construct($api_key) {
|
||||
$this->api_key = $api_key;
|
||||
$this->base_url = "https://api.dify.ai/v1";
|
||||
$this->client = new Client([
|
||||
'base_uri' => $this->base_url,
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $this->api_key,
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
protected function send_request($method, $endpoint, $data = null, $params = null, $stream = false) {
|
||||
$options = [
|
||||
'json' => $data,
|
||||
'query' => $params,
|
||||
'stream' => $stream,
|
||||
];
|
||||
|
||||
$response = $this->client->request($method, $endpoint, $options);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function message_feedback($message_id, $rating, $user) {
|
||||
$data = [
|
||||
'rating' => $rating,
|
||||
'user' => $user,
|
||||
];
|
||||
return $this->send_request('POST', "/messages/{$message_id}/feedbacks", $data);
|
||||
}
|
||||
|
||||
public function get_application_parameters($user) {
|
||||
$params = ['user' => $user];
|
||||
return $this->send_request('GET', '/parameters', null, $params);
|
||||
}
|
||||
}
|
||||
|
||||
class CompletionClient extends DifyClient {
|
||||
public function create_completion_message($inputs, $query, $response_mode, $user) {
|
||||
$data = [
|
||||
'inputs' => $inputs,
|
||||
'query' => $query,
|
||||
'response_mode' => $response_mode,
|
||||
'user' => $user,
|
||||
];
|
||||
return $this->send_request('POST', '/completion-messages', $data, null, $response_mode === 'streaming');
|
||||
}
|
||||
}
|
||||
|
||||
class ChatClient extends DifyClient {
|
||||
public function create_chat_message($inputs, $query, $user, $response_mode = 'blocking', $conversation_id = null) {
|
||||
$data = [
|
||||
'inputs' => $inputs,
|
||||
'query' => $query,
|
||||
'user' => $user,
|
||||
'response_mode' => $response_mode,
|
||||
];
|
||||
if ($conversation_id) {
|
||||
$data['conversation_id'] = $conversation_id;
|
||||
}
|
||||
|
||||
return $this->send_request('POST', '/chat-messages', $data, null, $response_mode === 'streaming');
|
||||
}
|
||||
|
||||
public function get_conversation_messages($user, $conversation_id = null, $first_id = null, $limit = null) {
|
||||
$params = ['user' => $user];
|
||||
|
||||
if ($conversation_id) {
|
||||
$params['conversation_id'] = $conversation_id;
|
||||
}
|
||||
if ($first_id) {
|
||||
$params['first_id'] = $first_id;
|
||||
}
|
||||
if ($limit) {
|
||||
$params['limit'] = $limit;
|
||||
}
|
||||
|
||||
return $this->send_request('GET', '/messages', null, $params);
|
||||
}
|
||||
|
||||
public function get_conversations($user, $first_id = null, $limit = null, $pinned = null) {
|
||||
$params = [
|
||||
'user' => $user,
|
||||
'first_id' => $first_id,
|
||||
'limit' => $limit,
|
||||
'pinned'=> $pinned,
|
||||
];
|
||||
return $this->send_request('GET', '/conversations', null, $params);
|
||||
}
|
||||
|
||||
public function rename_conversation($conversation_id, $name, $user) {
|
||||
$data = [
|
||||
'name' => $name,
|
||||
'user' => $user,
|
||||
];
|
||||
return $this->send_request('PATCH', "/conversations/{$conversation_id}", $data);
|
||||
}
|
||||
}
|
21
sdks/python-client/LICENSE
Normal file
21
sdks/python-client/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 LangGenius
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
1
sdks/python-client/MANIFEST.in
Normal file
1
sdks/python-client/MANIFEST.in
Normal file
@@ -0,0 +1 @@
|
||||
recursive-include dify_client *.py
|
100
sdks/python-client/README.md
Normal file
100
sdks/python-client/README.md
Normal file
@@ -0,0 +1,100 @@
|
||||
# dify-client
|
||||
|
||||
A Dify App Service-API Client, using for build a webapp by request Service-API
|
||||
|
||||
## Usage
|
||||
|
||||
First, install `dify-client` python sdk package:
|
||||
|
||||
```
|
||||
pip install dify-client
|
||||
```
|
||||
|
||||
Write your code with sdk:
|
||||
|
||||
- completion generate with `blocking` response_mode
|
||||
|
||||
```
|
||||
import json
|
||||
from dify_client import CompletionClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize CompletionClient
|
||||
completion_client = CompletionClient(api_key)
|
||||
|
||||
# Create Completion Message using CompletionClient
|
||||
completion_response = completion_client.create_completion_message(inputs={}, query="Hello", response_mode="blocking", user="user_id")
|
||||
completion_response.raise_for_status()
|
||||
|
||||
result = completion_response.text
|
||||
result = json.loads(result)
|
||||
|
||||
print(result.get('answer'))
|
||||
```
|
||||
|
||||
- chat generate with `streaming` response_mode
|
||||
|
||||
```
|
||||
import json
|
||||
from dify_client import ChatClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize ChatClient
|
||||
chat_client = ChatClient(api_key)
|
||||
|
||||
# Create Chat Message using ChatClient
|
||||
chat_response = chat_client.create_chat_message(inputs={}, query="Hello", user="user_id", response_mode="streaming")
|
||||
chat_response.raise_for_status()
|
||||
|
||||
for line in chat_response.iter_lines(decode_unicode=True):
|
||||
line = line.split('data:', 1)[-1]
|
||||
if line.strip():
|
||||
line = json.loads(line.strip())
|
||||
print(line.get('answer'))
|
||||
```
|
||||
|
||||
- Others
|
||||
|
||||
```
|
||||
import json
|
||||
from dify_client import ChatClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize Client
|
||||
client = ChatClient(api_key)
|
||||
|
||||
# Get App parameters
|
||||
parameters = client.get_application_parameters(user="user_id")
|
||||
parameters.raise_for_status()
|
||||
parameters = json.loads(parameters.text)
|
||||
|
||||
print('[parameters]')
|
||||
print(parameters)
|
||||
|
||||
# Get Conversation List (only for chat)
|
||||
conversations = client.get_conversations(user="user_id")
|
||||
conversations.raise_for_status()
|
||||
conversations = json.loads(conversations.text)
|
||||
|
||||
print('[conversations]')
|
||||
print(conversations)
|
||||
|
||||
# Get Message List (only for chat)
|
||||
messages = client.get_conversation_messages(user="user_id", conversation_id="conversation_id")
|
||||
messages.raise_for_status()
|
||||
messages = json.loads(messages.text)
|
||||
|
||||
print('[messages]')
|
||||
print(messages)
|
||||
|
||||
# Rename Conversation (only for chat)
|
||||
rename_conversation_response = client.rename_conversation(conversation_id="conversation_id", name="new_name", user="user_id")
|
||||
rename_conversation_response.raise_for_status()
|
||||
rename_conversation_result = json.loads(rename_conversation_response.text)
|
||||
|
||||
print('[rename result]')
|
||||
print(rename_conversation_result)
|
||||
```
|
9
sdks/python-client/build.sh
Executable file
9
sdks/python-client/build.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
rm -rf build dist *.egg-info
|
||||
|
||||
pip install setuptools wheel twine
|
||||
python setup.py sdist bdist_wheel
|
||||
twine upload dist/*
|
1
sdks/python-client/dify_client/__init__.py
Normal file
1
sdks/python-client/dify_client/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from dify_client.client import ChatClient, CompletionClient
|
74
sdks/python-client/dify_client/client.py
Normal file
74
sdks/python-client/dify_client/client.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import requests
|
||||
|
||||
|
||||
class DifyClient:
|
||||
def __init__(self, api_key):
|
||||
self.api_key = api_key
|
||||
self.base_url = "https://api.dify.ai/v1"
|
||||
|
||||
def _send_request(self, method, endpoint, data=None, params=None, stream=False):
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
url = f"{self.base_url}{endpoint}"
|
||||
response = requests.request(method, url, json=data, params=params, headers=headers, stream=stream)
|
||||
|
||||
return response
|
||||
|
||||
def message_feedback(self, message_id, rating, user):
|
||||
data = {
|
||||
"rating": rating,
|
||||
"user": user
|
||||
}
|
||||
return self._send_request("POST", f"/messages/{message_id}/feedbacks", data)
|
||||
|
||||
def get_application_parameters(self, user):
|
||||
params = {"user": user}
|
||||
return self._send_request("GET", "/parameters", params=params)
|
||||
|
||||
|
||||
class CompletionClient(DifyClient):
|
||||
def create_completion_message(self, inputs, query, response_mode, user):
|
||||
data = {
|
||||
"inputs": inputs,
|
||||
"query": query,
|
||||
"response_mode": response_mode,
|
||||
"user": user
|
||||
}
|
||||
return self._send_request("POST", "/completion-messages", data, stream=True if response_mode == "streaming" else False)
|
||||
|
||||
|
||||
class ChatClient(DifyClient):
|
||||
def create_chat_message(self, inputs, query, user, response_mode="blocking", conversation_id=None):
|
||||
data = {
|
||||
"inputs": inputs,
|
||||
"query": query,
|
||||
"user": user,
|
||||
"response_mode": response_mode
|
||||
}
|
||||
if conversation_id:
|
||||
data["conversation_id"] = conversation_id
|
||||
|
||||
return self._send_request("POST", "/chat-messages", data, stream=True if response_mode == "streaming" else False)
|
||||
|
||||
def get_conversation_messages(self, user, conversation_id=None, first_id=None, limit=None):
|
||||
params = {"user": user}
|
||||
|
||||
if conversation_id:
|
||||
params["conversation_id"] = conversation_id
|
||||
if first_id:
|
||||
params["first_id"] = first_id
|
||||
if limit:
|
||||
params["limit"] = limit
|
||||
|
||||
return self._send_request("GET", "/messages", params=params)
|
||||
|
||||
def get_conversations(self, user, first_id=None, limit=None, pinned=None):
|
||||
params = {"user": user, "first_id": first_id, "limit": limit, "pinned": pinned}
|
||||
return self._send_request("GET", "/conversations", params=params)
|
||||
|
||||
def rename_conversation(self, conversation_id, name, user):
|
||||
data = {"name": name, "user": user}
|
||||
return self._send_request("POST", f"/conversations/{conversation_id}/name", data)
|
28
sdks/python-client/setup.py
Normal file
28
sdks/python-client/setup.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from setuptools import setup
|
||||
|
||||
with open("README.md", "r", encoding="utf-8") as fh:
|
||||
long_description = fh.read()
|
||||
|
||||
setup(
|
||||
name="dify-client",
|
||||
version="0.1.7",
|
||||
author="Dify",
|
||||
author_email="hello@dify.ai",
|
||||
description="A package for interacting with the Dify Service-API",
|
||||
long_description=long_description,
|
||||
long_description_content_type="text/markdown",
|
||||
url="https://github.com/langgenius/dify",
|
||||
license='MIT',
|
||||
packages=['dify_client'],
|
||||
classifiers=[
|
||||
"Programming Language :: Python :: 3",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
],
|
||||
python_requires=">=3.6",
|
||||
install_requires=[
|
||||
"requests"
|
||||
],
|
||||
keywords='dify nlp ai language-processing',
|
||||
include_package_data=True,
|
||||
)
|
0
sdks/python-client/tests/__init__.py
Normal file
0
sdks/python-client/tests/__init__.py
Normal file
49
sdks/python-client/tests/test_client.py
Normal file
49
sdks/python-client/tests/test_client.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import unittest
|
||||
from dify_client.client import ChatClient, CompletionClient, DifyClient
|
||||
|
||||
API_KEY = os.environ.get("API_KEY")
|
||||
APP_ID = os.environ.get("APP_ID")
|
||||
|
||||
|
||||
class TestChatClient(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.chat_client = ChatClient(API_KEY)
|
||||
|
||||
def test_create_chat_message(self):
|
||||
response = self.chat_client.create_chat_message({}, "Hello, World!", "test_user")
|
||||
self.assertIn("message_id", response)
|
||||
|
||||
def test_get_conversation_messages(self):
|
||||
response = self.chat_client.get_conversation_messages("test_user")
|
||||
self.assertIsInstance(response, list)
|
||||
|
||||
def test_get_conversations(self):
|
||||
response = self.chat_client.get_conversations("test_user")
|
||||
self.assertIsInstance(response, list)
|
||||
|
||||
|
||||
class TestCompletionClient(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.completion_client = CompletionClient(API_KEY)
|
||||
|
||||
def test_create_completion_message(self):
|
||||
response = self.completion_client.create_completion_message({}, "What's the weather like today?", "blocking", "test_user")
|
||||
self.assertIn("message_id", response)
|
||||
|
||||
|
||||
class TestDifyClient(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dify_client = DifyClient(API_KEY)
|
||||
|
||||
def test_message_feedback(self):
|
||||
response = self.dify_client.message_feedback("test_message_id", 5, "test_user")
|
||||
self.assertIn("success", response)
|
||||
|
||||
def test_get_application_parameters(self):
|
||||
response = self.dify_client.get_application_parameters("test_user")
|
||||
self.assertIsInstance(response, dict)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Reference in New Issue
Block a user