mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 03:32:23 +08:00
chore: apply ruff reformat for python-client sdk (#8752)
This commit is contained in:
parent
cb1942c242
commit
2328944987
|
@ -1,28 +1,23 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class DifyClient:
|
class DifyClient:
|
||||||
def __init__(self, api_key, base_url: str = 'https://api.dify.ai/v1'):
|
def __init__(self, api_key, base_url: str = "https://api.dify.ai/v1"):
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
self.base_url = base_url
|
self.base_url = base_url
|
||||||
|
|
||||||
def _send_request(self, method, endpoint, json=None, params=None, stream=False):
|
def _send_request(self, method, endpoint, json=None, params=None, stream=False):
|
||||||
headers = {
|
headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
|
||||||
"Authorization": f"Bearer {self.api_key}",
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
}
|
|
||||||
|
|
||||||
url = f"{self.base_url}{endpoint}"
|
url = f"{self.base_url}{endpoint}"
|
||||||
response = requests.request(method, url, json=json, params=params, headers=headers, stream=stream)
|
response = requests.request(method, url, json=json, params=params, headers=headers, stream=stream)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def _send_request_with_files(self, method, endpoint, data, files):
|
def _send_request_with_files(self, method, endpoint, data, files):
|
||||||
headers = {
|
headers = {"Authorization": f"Bearer {self.api_key}"}
|
||||||
"Authorization": f"Bearer {self.api_key}"
|
|
||||||
}
|
|
||||||
|
|
||||||
url = f"{self.base_url}{endpoint}"
|
url = f"{self.base_url}{endpoint}"
|
||||||
response = requests.request(method, url, data=data, headers=headers, files=files)
|
response = requests.request(method, url, data=data, headers=headers, files=files)
|
||||||
|
@ -30,10 +25,7 @@ class DifyClient:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def message_feedback(self, message_id, rating, user):
|
def message_feedback(self, message_id, rating, user):
|
||||||
data = {
|
data = {"rating": rating, "user": user}
|
||||||
"rating": rating,
|
|
||||||
"user": user
|
|
||||||
}
|
|
||||||
return self._send_request("POST", f"/messages/{message_id}/feedbacks", data)
|
return self._send_request("POST", f"/messages/{message_id}/feedbacks", data)
|
||||||
|
|
||||||
def get_application_parameters(self, user):
|
def get_application_parameters(self, user):
|
||||||
|
@ -41,52 +33,37 @@ class DifyClient:
|
||||||
return self._send_request("GET", "/parameters", params=params)
|
return self._send_request("GET", "/parameters", params=params)
|
||||||
|
|
||||||
def file_upload(self, user, files):
|
def file_upload(self, user, files):
|
||||||
data = {
|
data = {"user": user}
|
||||||
"user": user
|
|
||||||
}
|
|
||||||
return self._send_request_with_files("POST", "/files/upload", data=data, files=files)
|
return self._send_request_with_files("POST", "/files/upload", data=data, files=files)
|
||||||
|
|
||||||
def text_to_audio(self, text:str, user:str, streaming:bool=False):
|
def text_to_audio(self, text: str, user: str, streaming: bool = False):
|
||||||
data = {
|
data = {"text": text, "user": user, "streaming": streaming}
|
||||||
"text": text,
|
|
||||||
"user": user,
|
|
||||||
"streaming": streaming
|
|
||||||
}
|
|
||||||
return self._send_request("POST", "/text-to-audio", data=data)
|
return self._send_request("POST", "/text-to-audio", data=data)
|
||||||
|
|
||||||
def get_meta(self,user):
|
def get_meta(self, user):
|
||||||
params = { "user": user}
|
params = {"user": user}
|
||||||
return self._send_request("GET", f"/meta", params=params)
|
return self._send_request("GET", "/meta", params=params)
|
||||||
|
|
||||||
|
|
||||||
class CompletionClient(DifyClient):
|
class CompletionClient(DifyClient):
|
||||||
def create_completion_message(self, inputs, response_mode, user, files=None):
|
def create_completion_message(self, inputs, response_mode, user, files=None):
|
||||||
data = {
|
data = {"inputs": inputs, "response_mode": response_mode, "user": user, "files": files}
|
||||||
"inputs": inputs,
|
return self._send_request(
|
||||||
"response_mode": response_mode,
|
"POST", "/completion-messages", data, stream=True if response_mode == "streaming" else False
|
||||||
"user": user,
|
)
|
||||||
"files": files
|
|
||||||
}
|
|
||||||
return self._send_request("POST", "/completion-messages", data,
|
|
||||||
stream=True if response_mode == "streaming" else False)
|
|
||||||
|
|
||||||
|
|
||||||
class ChatClient(DifyClient):
|
class ChatClient(DifyClient):
|
||||||
def create_chat_message(self, inputs, query, user, response_mode="blocking", conversation_id=None, files=None):
|
def create_chat_message(self, inputs, query, user, response_mode="blocking", conversation_id=None, files=None):
|
||||||
data = {
|
data = {"inputs": inputs, "query": query, "user": user, "response_mode": response_mode, "files": files}
|
||||||
"inputs": inputs,
|
|
||||||
"query": query,
|
|
||||||
"user": user,
|
|
||||||
"response_mode": response_mode,
|
|
||||||
"files": files
|
|
||||||
}
|
|
||||||
if conversation_id:
|
if conversation_id:
|
||||||
data["conversation_id"] = conversation_id
|
data["conversation_id"] = conversation_id
|
||||||
|
|
||||||
return self._send_request("POST", "/chat-messages", data,
|
return self._send_request(
|
||||||
stream=True if response_mode == "streaming" else False)
|
"POST", "/chat-messages", data, stream=True if response_mode == "streaming" else False
|
||||||
|
)
|
||||||
|
|
||||||
def get_suggested(self, message_id, user:str):
|
def get_suggested(self, message_id, user: str):
|
||||||
params = {"user": user}
|
params = {"user": user}
|
||||||
return self._send_request("GET", f"/messages/{message_id}/suggested", params=params)
|
return self._send_request("GET", f"/messages/{message_id}/suggested", params=params)
|
||||||
|
|
||||||
|
@ -110,8 +87,8 @@ class ChatClient(DifyClient):
|
||||||
|
|
||||||
return self._send_request("GET", "/messages", params=params)
|
return self._send_request("GET", "/messages", params=params)
|
||||||
|
|
||||||
def rename_conversation(self, conversation_id, name,auto_generate:bool, user:str):
|
def rename_conversation(self, conversation_id, name, auto_generate: bool, user: str):
|
||||||
data = {"name": name, "auto_generate": auto_generate,"user": user}
|
data = {"name": name, "auto_generate": auto_generate, "user": user}
|
||||||
return self._send_request("POST", f"/conversations/{conversation_id}/name", data)
|
return self._send_request("POST", f"/conversations/{conversation_id}/name", data)
|
||||||
|
|
||||||
def delete_conversation(self, conversation_id, user):
|
def delete_conversation(self, conversation_id, user):
|
||||||
|
@ -125,7 +102,7 @@ class ChatClient(DifyClient):
|
||||||
|
|
||||||
|
|
||||||
class WorkflowClient(DifyClient):
|
class WorkflowClient(DifyClient):
|
||||||
def run(self, inputs:dict, response_mode:str="streaming", user:str="abc-123"):
|
def run(self, inputs: dict, response_mode: str = "streaming", user: str = "abc-123"):
|
||||||
data = {"inputs": inputs, "response_mode": response_mode, "user": user}
|
data = {"inputs": inputs, "response_mode": response_mode, "user": user}
|
||||||
return self._send_request("POST", "/workflows/run", data)
|
return self._send_request("POST", "/workflows/run", data)
|
||||||
|
|
||||||
|
@ -137,10 +114,8 @@ class WorkflowClient(DifyClient):
|
||||||
return self._send_request("GET", f"/workflows/run/{workflow_run_id}")
|
return self._send_request("GET", f"/workflows/run/{workflow_run_id}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class KnowledgeBaseClient(DifyClient):
|
class KnowledgeBaseClient(DifyClient):
|
||||||
|
def __init__(self, api_key, base_url: str = "https://api.dify.ai/v1", dataset_id: str = None):
|
||||||
def __init__(self, api_key, base_url: str = 'https://api.dify.ai/v1', dataset_id: str = None):
|
|
||||||
"""
|
"""
|
||||||
Construct a KnowledgeBaseClient object.
|
Construct a KnowledgeBaseClient object.
|
||||||
|
|
||||||
|
@ -150,10 +125,7 @@ class KnowledgeBaseClient(DifyClient):
|
||||||
dataset_id (str, optional): ID of the dataset. Defaults to None. You don't need this if you just want to
|
dataset_id (str, optional): ID of the dataset. Defaults to None. You don't need this if you just want to
|
||||||
create a new dataset. or list datasets. otherwise you need to set this.
|
create a new dataset. or list datasets. otherwise you need to set this.
|
||||||
"""
|
"""
|
||||||
super().__init__(
|
super().__init__(api_key=api_key, base_url=base_url)
|
||||||
api_key=api_key,
|
|
||||||
base_url=base_url
|
|
||||||
)
|
|
||||||
self.dataset_id = dataset_id
|
self.dataset_id = dataset_id
|
||||||
|
|
||||||
def _get_dataset_id(self):
|
def _get_dataset_id(self):
|
||||||
|
@ -162,10 +134,10 @@ class KnowledgeBaseClient(DifyClient):
|
||||||
return self.dataset_id
|
return self.dataset_id
|
||||||
|
|
||||||
def create_dataset(self, name: str, **kwargs):
|
def create_dataset(self, name: str, **kwargs):
|
||||||
return self._send_request('POST', '/datasets', {'name': name}, **kwargs)
|
return self._send_request("POST", "/datasets", {"name": name}, **kwargs)
|
||||||
|
|
||||||
def list_datasets(self, page: int = 1, page_size: int = 20, **kwargs):
|
def list_datasets(self, page: int = 1, page_size: int = 20, **kwargs):
|
||||||
return self._send_request('GET', f'/datasets?page={page}&limit={page_size}', **kwargs)
|
return self._send_request("GET", f"/datasets?page={page}&limit={page_size}", **kwargs)
|
||||||
|
|
||||||
def create_document_by_text(self, name, text, extra_params: dict = None, **kwargs):
|
def create_document_by_text(self, name, text, extra_params: dict = None, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -193,14 +165,7 @@ class KnowledgeBaseClient(DifyClient):
|
||||||
}
|
}
|
||||||
:return: Response from the API
|
:return: Response from the API
|
||||||
"""
|
"""
|
||||||
data = {
|
data = {"indexing_technique": "high_quality", "process_rule": {"mode": "automatic"}, "name": name, "text": text}
|
||||||
'indexing_technique': 'high_quality',
|
|
||||||
'process_rule': {
|
|
||||||
'mode': 'automatic'
|
|
||||||
},
|
|
||||||
'name': name,
|
|
||||||
'text': text
|
|
||||||
}
|
|
||||||
if extra_params is not None and isinstance(extra_params, dict):
|
if extra_params is not None and isinstance(extra_params, dict):
|
||||||
data.update(extra_params)
|
data.update(extra_params)
|
||||||
url = f"/datasets/{self._get_dataset_id()}/document/create_by_text"
|
url = f"/datasets/{self._get_dataset_id()}/document/create_by_text"
|
||||||
|
@ -233,10 +198,7 @@ class KnowledgeBaseClient(DifyClient):
|
||||||
}
|
}
|
||||||
:return: Response from the API
|
:return: Response from the API
|
||||||
"""
|
"""
|
||||||
data = {
|
data = {"name": name, "text": text}
|
||||||
'name': name,
|
|
||||||
'text': text
|
|
||||||
}
|
|
||||||
if extra_params is not None and isinstance(extra_params, dict):
|
if extra_params is not None and isinstance(extra_params, dict):
|
||||||
data.update(extra_params)
|
data.update(extra_params)
|
||||||
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/update_by_text"
|
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/update_by_text"
|
||||||
|
@ -269,16 +231,11 @@ class KnowledgeBaseClient(DifyClient):
|
||||||
:return: Response from the API
|
:return: Response from the API
|
||||||
"""
|
"""
|
||||||
files = {"file": open(file_path, "rb")}
|
files = {"file": open(file_path, "rb")}
|
||||||
data = {
|
data = {"process_rule": {"mode": "automatic"}, "indexing_technique": "high_quality"}
|
||||||
'process_rule': {
|
|
||||||
'mode': 'automatic'
|
|
||||||
},
|
|
||||||
'indexing_technique': 'high_quality'
|
|
||||||
}
|
|
||||||
if extra_params is not None and isinstance(extra_params, dict):
|
if extra_params is not None and isinstance(extra_params, dict):
|
||||||
data.update(extra_params)
|
data.update(extra_params)
|
||||||
if original_document_id is not None:
|
if original_document_id is not None:
|
||||||
data['original_document_id'] = original_document_id
|
data["original_document_id"] = original_document_id
|
||||||
url = f"/datasets/{self._get_dataset_id()}/document/create_by_file"
|
url = f"/datasets/{self._get_dataset_id()}/document/create_by_file"
|
||||||
return self._send_request_with_files("POST", url, {"data": json.dumps(data)}, files)
|
return self._send_request_with_files("POST", url, {"data": json.dumps(data)}, files)
|
||||||
|
|
||||||
|
@ -352,11 +309,11 @@ class KnowledgeBaseClient(DifyClient):
|
||||||
"""
|
"""
|
||||||
params = {}
|
params = {}
|
||||||
if page is not None:
|
if page is not None:
|
||||||
params['page'] = page
|
params["page"] = page
|
||||||
if page_size is not None:
|
if page_size is not None:
|
||||||
params['limit'] = page_size
|
params["limit"] = page_size
|
||||||
if keyword is not None:
|
if keyword is not None:
|
||||||
params['keyword'] = keyword
|
params["keyword"] = keyword
|
||||||
url = f"/datasets/{self._get_dataset_id()}/documents"
|
url = f"/datasets/{self._get_dataset_id()}/documents"
|
||||||
return self._send_request("GET", url, params=params, **kwargs)
|
return self._send_request("GET", url, params=params, **kwargs)
|
||||||
|
|
||||||
|
@ -383,9 +340,9 @@ class KnowledgeBaseClient(DifyClient):
|
||||||
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/segments"
|
url = f"/datasets/{self._get_dataset_id()}/documents/{document_id}/segments"
|
||||||
params = {}
|
params = {}
|
||||||
if keyword is not None:
|
if keyword is not None:
|
||||||
params['keyword'] = keyword
|
params["keyword"] = keyword
|
||||||
if status is not None:
|
if status is not None:
|
||||||
params['status'] = status
|
params["status"] = status
|
||||||
if "params" in kwargs:
|
if "params" in kwargs:
|
||||||
params.update(kwargs["params"])
|
params.update(kwargs["params"])
|
||||||
return self._send_request("GET", url, params=params, **kwargs)
|
return self._send_request("GET", url, params=params, **kwargs)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user