refactor: refactor python sdk (#28118)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from typing import Literal, Dict, List, Any, IO
|
||||
from typing import Literal, Dict, List, Any, IO, Optional, Union
|
||||
|
||||
import httpx
|
||||
from .base_client import BaseClientMixin
|
||||
from .exceptions import (
|
||||
APIError,
|
||||
AuthenticationError,
|
||||
RateLimitError,
|
||||
ValidationError,
|
||||
FileUploadError,
|
||||
)
|
||||
|
||||
|
||||
class DifyClient:
|
||||
class DifyClient(BaseClientMixin):
|
||||
"""Synchronous Dify API client.
|
||||
|
||||
This client uses httpx.Client for efficient connection pooling and resource management.
|
||||
@@ -21,6 +30,9 @@ class DifyClient:
|
||||
api_key: str,
|
||||
base_url: str = "https://api.dify.ai/v1",
|
||||
timeout: float = 60.0,
|
||||
max_retries: int = 3,
|
||||
retry_delay: float = 1.0,
|
||||
enable_logging: bool = False,
|
||||
):
|
||||
"""Initialize the Dify client.
|
||||
|
||||
@@ -28,9 +40,13 @@ class DifyClient:
|
||||
api_key: Your Dify API key
|
||||
base_url: Base URL for the Dify API
|
||||
timeout: Request timeout in seconds (default: 60.0)
|
||||
max_retries: Maximum number of retry attempts (default: 3)
|
||||
retry_delay: Delay between retries in seconds (default: 1.0)
|
||||
enable_logging: Whether to enable request logging (default: True)
|
||||
"""
|
||||
self.api_key = api_key
|
||||
self.base_url = base_url
|
||||
# Initialize base client functionality
|
||||
BaseClientMixin.__init__(self, api_key, base_url, timeout, max_retries, retry_delay, enable_logging)
|
||||
|
||||
self._client = httpx.Client(
|
||||
base_url=base_url,
|
||||
timeout=httpx.Timeout(timeout, connect=5.0),
|
||||
@@ -53,12 +69,12 @@ class DifyClient:
|
||||
self,
|
||||
method: str,
|
||||
endpoint: str,
|
||||
json: dict | None = None,
|
||||
params: dict | None = None,
|
||||
json: Dict[str, Any] | None = None,
|
||||
params: Dict[str, Any] | None = None,
|
||||
stream: bool = False,
|
||||
**kwargs,
|
||||
):
|
||||
"""Send an HTTP request to the Dify API.
|
||||
"""Send an HTTP request to the Dify API with retry logic.
|
||||
|
||||
Args:
|
||||
method: HTTP method (GET, POST, PUT, PATCH, DELETE)
|
||||
@@ -71,23 +87,91 @@ class DifyClient:
|
||||
Returns:
|
||||
httpx.Response object
|
||||
"""
|
||||
# Validate parameters
|
||||
if json:
|
||||
self._validate_params(**json)
|
||||
if params:
|
||||
self._validate_params(**params)
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
# httpx.Client automatically prepends base_url
|
||||
response = self._client.request(
|
||||
method,
|
||||
endpoint,
|
||||
json=json,
|
||||
params=params,
|
||||
headers=headers,
|
||||
**kwargs,
|
||||
)
|
||||
def make_request():
|
||||
"""Inner function to perform the actual HTTP request."""
|
||||
# Log request if logging is enabled
|
||||
if self.enable_logging:
|
||||
self.logger.info(f"Sending {method} request to {endpoint}")
|
||||
# Debug logging for detailed information
|
||||
if self.logger.isEnabledFor(logging.DEBUG):
|
||||
if json:
|
||||
self.logger.debug(f"Request body: {json}")
|
||||
if params:
|
||||
self.logger.debug(f"Request params: {params}")
|
||||
|
||||
# httpx.Client automatically prepends base_url
|
||||
response = self._client.request(
|
||||
method,
|
||||
endpoint,
|
||||
json=json,
|
||||
params=params,
|
||||
headers=headers,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
# Log response if logging is enabled
|
||||
if self.enable_logging:
|
||||
self.logger.info(f"Received response: {response.status_code}")
|
||||
|
||||
return response
|
||||
|
||||
# Use the retry mechanism from base client
|
||||
request_context = f"{method} {endpoint}"
|
||||
response = self._retry_request(make_request, request_context)
|
||||
|
||||
# Handle error responses (API errors don't retry)
|
||||
self._handle_error_response(response)
|
||||
|
||||
return response
|
||||
|
||||
def _handle_error_response(self, response, is_upload_request: bool = False) -> None:
|
||||
"""Handle HTTP error responses and raise appropriate exceptions."""
|
||||
|
||||
if response.status_code < 400:
|
||||
return # Success response
|
||||
|
||||
try:
|
||||
error_data = response.json()
|
||||
message = error_data.get("message", f"HTTP {response.status_code}")
|
||||
except (ValueError, KeyError):
|
||||
message = f"HTTP {response.status_code}"
|
||||
error_data = None
|
||||
|
||||
# Log error response if logging is enabled
|
||||
if self.enable_logging:
|
||||
self.logger.error(f"API error: {response.status_code} - {message}")
|
||||
|
||||
if response.status_code == 401:
|
||||
raise AuthenticationError(message, response.status_code, error_data)
|
||||
elif response.status_code == 429:
|
||||
retry_after = response.headers.get("Retry-After")
|
||||
raise RateLimitError(message, retry_after)
|
||||
elif response.status_code == 422:
|
||||
raise ValidationError(message, response.status_code, error_data)
|
||||
elif response.status_code == 400:
|
||||
# Check if this is a file upload error based on the URL or context
|
||||
current_url = getattr(response, "url", "") or ""
|
||||
if is_upload_request or "upload" in str(current_url).lower() or "files" in str(current_url).lower():
|
||||
raise FileUploadError(message, response.status_code, error_data)
|
||||
else:
|
||||
raise APIError(message, response.status_code, error_data)
|
||||
elif response.status_code >= 500:
|
||||
# Server errors should raise APIError
|
||||
raise APIError(message, response.status_code, error_data)
|
||||
elif response.status_code >= 400:
|
||||
raise APIError(message, response.status_code, error_data)
|
||||
|
||||
def _send_request_with_files(self, method: str, endpoint: str, data: dict, files: dict):
|
||||
"""Send an HTTP request with file uploads.
|
||||
|
||||
@@ -102,6 +186,12 @@ class DifyClient:
|
||||
"""
|
||||
headers = {"Authorization": f"Bearer {self.api_key}"}
|
||||
|
||||
# Log file upload request if logging is enabled
|
||||
if self.enable_logging:
|
||||
self.logger.info(f"Sending {method} file upload request to {endpoint}")
|
||||
self.logger.debug(f"Form data: {data}")
|
||||
self.logger.debug(f"Files: {files}")
|
||||
|
||||
response = self._client.request(
|
||||
method,
|
||||
endpoint,
|
||||
@@ -110,9 +200,17 @@ class DifyClient:
|
||||
files=files,
|
||||
)
|
||||
|
||||
# Log response if logging is enabled
|
||||
if self.enable_logging:
|
||||
self.logger.info(f"Received file upload response: {response.status_code}")
|
||||
|
||||
# Handle error responses
|
||||
self._handle_error_response(response, is_upload_request=True)
|
||||
|
||||
return response
|
||||
|
||||
def message_feedback(self, message_id: str, rating: Literal["like", "dislike"], user: str):
|
||||
self._validate_params(message_id=message_id, rating=rating, user=user)
|
||||
data = {"rating": rating, "user": user}
|
||||
return self._send_request("POST", f"/messages/{message_id}/feedbacks", data)
|
||||
|
||||
@@ -144,6 +242,72 @@ class DifyClient:
|
||||
"""Get file preview by file ID."""
|
||||
return self._send_request("GET", f"/files/{file_id}/preview")
|
||||
|
||||
# App Configuration APIs
|
||||
def get_app_site_config(self, app_id: str):
|
||||
"""Get app site configuration.
|
||||
|
||||
Args:
|
||||
app_id: ID of the app
|
||||
|
||||
Returns:
|
||||
App site configuration
|
||||
"""
|
||||
url = f"/apps/{app_id}/site/config"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def update_app_site_config(self, app_id: str, config_data: Dict[str, Any]):
|
||||
"""Update app site configuration.
|
||||
|
||||
Args:
|
||||
app_id: ID of the app
|
||||
config_data: Configuration data to update
|
||||
|
||||
Returns:
|
||||
Updated app site configuration
|
||||
"""
|
||||
url = f"/apps/{app_id}/site/config"
|
||||
return self._send_request("PUT", url, json=config_data)
|
||||
|
||||
def get_app_api_tokens(self, app_id: str):
|
||||
"""Get API tokens for an app.
|
||||
|
||||
Args:
|
||||
app_id: ID of the app
|
||||
|
||||
Returns:
|
||||
List of API tokens
|
||||
"""
|
||||
url = f"/apps/{app_id}/api-tokens"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def create_app_api_token(self, app_id: str, name: str, description: str | None = None):
|
||||
"""Create a new API token for an app.
|
||||
|
||||
Args:
|
||||
app_id: ID of the app
|
||||
name: Name for the API token
|
||||
description: Description for the API token (optional)
|
||||
|
||||
Returns:
|
||||
Created API token information
|
||||
"""
|
||||
data = {"name": name, "description": description}
|
||||
url = f"/apps/{app_id}/api-tokens"
|
||||
return self._send_request("POST", url, json=data)
|
||||
|
||||
def delete_app_api_token(self, app_id: str, token_id: str):
|
||||
"""Delete an API token.
|
||||
|
||||
Args:
|
||||
app_id: ID of the app
|
||||
token_id: ID of the token to delete
|
||||
|
||||
Returns:
|
||||
Deletion result
|
||||
"""
|
||||
url = f"/apps/{app_id}/api-tokens/{token_id}"
|
||||
return self._send_request("DELETE", url)
|
||||
|
||||
|
||||
class CompletionClient(DifyClient):
|
||||
def create_completion_message(
|
||||
@@ -151,8 +315,16 @@ class CompletionClient(DifyClient):
|
||||
inputs: dict,
|
||||
response_mode: Literal["blocking", "streaming"],
|
||||
user: str,
|
||||
files: dict | None = None,
|
||||
files: Dict[str, Any] | None = None,
|
||||
):
|
||||
# Validate parameters
|
||||
if not isinstance(inputs, dict):
|
||||
raise ValidationError("inputs must be a dictionary")
|
||||
if response_mode not in ["blocking", "streaming"]:
|
||||
raise ValidationError("response_mode must be 'blocking' or 'streaming'")
|
||||
|
||||
self._validate_params(inputs=inputs, response_mode=response_mode, user=user)
|
||||
|
||||
data = {
|
||||
"inputs": inputs,
|
||||
"response_mode": response_mode,
|
||||
@@ -175,8 +347,18 @@ class ChatClient(DifyClient):
|
||||
user: str,
|
||||
response_mode: Literal["blocking", "streaming"] = "blocking",
|
||||
conversation_id: str | None = None,
|
||||
files: dict | None = None,
|
||||
files: Dict[str, Any] | None = None,
|
||||
):
|
||||
# Validate parameters
|
||||
if not isinstance(inputs, dict):
|
||||
raise ValidationError("inputs must be a dictionary")
|
||||
if not isinstance(query, str) or not query.strip():
|
||||
raise ValidationError("query must be a non-empty string")
|
||||
if response_mode not in ["blocking", "streaming"]:
|
||||
raise ValidationError("response_mode must be 'blocking' or 'streaming'")
|
||||
|
||||
self._validate_params(inputs=inputs, query=query, user=user, response_mode=response_mode)
|
||||
|
||||
data = {
|
||||
"inputs": inputs,
|
||||
"query": query,
|
||||
@@ -238,7 +420,7 @@ class ChatClient(DifyClient):
|
||||
data = {"user": user}
|
||||
return self._send_request("DELETE", f"/conversations/{conversation_id}", data)
|
||||
|
||||
def audio_to_text(self, audio_file: IO[bytes] | tuple, user: str):
|
||||
def audio_to_text(self, audio_file: Union[IO[bytes], tuple], user: str):
|
||||
data = {"user": user}
|
||||
files = {"file": audio_file}
|
||||
return self._send_request_with_files("POST", "/audio-to-text", data, files)
|
||||
@@ -313,7 +495,48 @@ class ChatClient(DifyClient):
|
||||
"""
|
||||
data = {"value": value, "user": user}
|
||||
url = f"/conversations/{conversation_id}/variables/{variable_id}"
|
||||
return self._send_request("PATCH", url, json=data)
|
||||
return self._send_request("PUT", url, json=data)
|
||||
|
||||
def delete_annotation_with_response(self, annotation_id: str):
|
||||
"""Delete an annotation with full response handling."""
|
||||
url = f"/apps/annotations/{annotation_id}"
|
||||
return self._send_request("DELETE", url)
|
||||
|
||||
def list_conversation_variables_with_pagination(
|
||||
self, conversation_id: str, user: str, page: int = 1, limit: int = 20
|
||||
):
|
||||
"""List conversation variables with pagination."""
|
||||
params = {"page": page, "limit": limit, "user": user}
|
||||
url = f"/conversations/{conversation_id}/variables"
|
||||
return self._send_request("GET", url, params=params)
|
||||
|
||||
def update_conversation_variable_with_response(self, conversation_id: str, variable_id: str, user: str, value: Any):
|
||||
"""Update a conversation variable with full response handling."""
|
||||
data = {"value": value, "user": user}
|
||||
url = f"/conversations/{conversation_id}/variables/{variable_id}"
|
||||
return self._send_request("PUT", url, json=data)
|
||||
|
||||
# Enhanced Annotation APIs
|
||||
def get_annotation_reply_job_status(self, action: str, job_id: str):
|
||||
"""Get status of an annotation reply action job."""
|
||||
url = f"/apps/annotation-reply/{action}/status/{job_id}"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def list_annotations_with_pagination(self, page: int = 1, limit: int = 20, keyword: str | None = None):
|
||||
"""List annotations with pagination."""
|
||||
params = {"page": page, "limit": limit, "keyword": keyword}
|
||||
return self._send_request("GET", "/apps/annotations", params=params)
|
||||
|
||||
def create_annotation_with_response(self, question: str, answer: str):
|
||||
"""Create an annotation with full response handling."""
|
||||
data = {"question": question, "answer": answer}
|
||||
return self._send_request("POST", "/apps/annotations", json=data)
|
||||
|
||||
def update_annotation_with_response(self, annotation_id: str, question: str, answer: str):
|
||||
"""Update an annotation with full response handling."""
|
||||
data = {"question": question, "answer": answer}
|
||||
url = f"/apps/annotations/{annotation_id}"
|
||||
return self._send_request("PUT", url, json=data)
|
||||
|
||||
|
||||
class WorkflowClient(DifyClient):
|
||||
@@ -376,6 +599,68 @@ class WorkflowClient(DifyClient):
|
||||
stream=(response_mode == "streaming"),
|
||||
)
|
||||
|
||||
# Enhanced Workflow APIs
|
||||
def get_workflow_draft(self, app_id: str):
|
||||
"""Get workflow draft configuration.
|
||||
|
||||
Args:
|
||||
app_id: ID of the workflow app
|
||||
|
||||
Returns:
|
||||
Workflow draft configuration
|
||||
"""
|
||||
url = f"/apps/{app_id}/workflow/draft"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def update_workflow_draft(self, app_id: str, workflow_data: Dict[str, Any]):
|
||||
"""Update workflow draft configuration.
|
||||
|
||||
Args:
|
||||
app_id: ID of the workflow app
|
||||
workflow_data: Workflow configuration data
|
||||
|
||||
Returns:
|
||||
Updated workflow draft
|
||||
"""
|
||||
url = f"/apps/{app_id}/workflow/draft"
|
||||
return self._send_request("PUT", url, json=workflow_data)
|
||||
|
||||
def publish_workflow(self, app_id: str):
|
||||
"""Publish workflow from draft.
|
||||
|
||||
Args:
|
||||
app_id: ID of the workflow app
|
||||
|
||||
Returns:
|
||||
Published workflow information
|
||||
"""
|
||||
url = f"/apps/{app_id}/workflow/publish"
|
||||
return self._send_request("POST", url)
|
||||
|
||||
def get_workflow_run_history(
|
||||
self,
|
||||
app_id: str,
|
||||
page: int = 1,
|
||||
limit: int = 20,
|
||||
status: Literal["succeeded", "failed", "stopped"] | None = None,
|
||||
):
|
||||
"""Get workflow run history.
|
||||
|
||||
Args:
|
||||
app_id: ID of the workflow app
|
||||
page: Page number (default: 1)
|
||||
limit: Number of items per page (default: 20)
|
||||
status: Filter by status (optional)
|
||||
|
||||
Returns:
|
||||
Paginated workflow run history
|
||||
"""
|
||||
params = {"page": page, "limit": limit}
|
||||
if status:
|
||||
params["status"] = status
|
||||
url = f"/apps/{app_id}/workflow/runs"
|
||||
return self._send_request("GET", url, params=params)
|
||||
|
||||
|
||||
class WorkspaceClient(DifyClient):
|
||||
"""Client for workspace-related operations."""
|
||||
@@ -385,6 +670,41 @@ class WorkspaceClient(DifyClient):
|
||||
url = f"/workspaces/current/models/model-types/{model_type}"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def get_available_models_by_type(self, model_type: str):
|
||||
"""Get available models by model type (enhanced version)."""
|
||||
url = f"/workspaces/current/models/model-types/{model_type}"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def get_model_providers(self):
|
||||
"""Get all model providers."""
|
||||
return self._send_request("GET", "/workspaces/current/model-providers")
|
||||
|
||||
def get_model_provider_models(self, provider_name: str):
|
||||
"""Get models for a specific provider."""
|
||||
url = f"/workspaces/current/model-providers/{provider_name}/models"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def validate_model_provider_credentials(self, provider_name: str, credentials: Dict[str, Any]):
|
||||
"""Validate model provider credentials."""
|
||||
url = f"/workspaces/current/model-providers/{provider_name}/credentials/validate"
|
||||
return self._send_request("POST", url, json=credentials)
|
||||
|
||||
# File Management APIs
|
||||
def get_file_info(self, file_id: str):
|
||||
"""Get information about a specific file."""
|
||||
url = f"/files/{file_id}/info"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def get_file_download_url(self, file_id: str):
|
||||
"""Get download URL for a file."""
|
||||
url = f"/files/{file_id}/download-url"
|
||||
return self._send_request("GET", url)
|
||||
|
||||
def delete_file(self, file_id: str):
|
||||
"""Delete a file."""
|
||||
url = f"/files/{file_id}"
|
||||
return self._send_request("DELETE", url)
|
||||
|
||||
|
||||
class KnowledgeBaseClient(DifyClient):
|
||||
def __init__(
|
||||
@@ -416,7 +736,7 @@ class KnowledgeBaseClient(DifyClient):
|
||||
def list_datasets(self, page: int = 1, page_size: int = 20, **kwargs):
|
||||
return self._send_request("GET", "/datasets", params={"page": page, "limit": page_size}, **kwargs)
|
||||
|
||||
def create_document_by_text(self, name, text, extra_params: dict | None = None, **kwargs):
|
||||
def create_document_by_text(self, name, text, extra_params: Dict[str, Any] | None = None, **kwargs):
|
||||
"""
|
||||
Create a document by text.
|
||||
|
||||
@@ -458,7 +778,7 @@ class KnowledgeBaseClient(DifyClient):
|
||||
document_id: str,
|
||||
name: str,
|
||||
text: str,
|
||||
extra_params: dict | None = None,
|
||||
extra_params: Dict[str, Any] | None = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""
|
||||
@@ -497,7 +817,7 @@ class KnowledgeBaseClient(DifyClient):
|
||||
self,
|
||||
file_path: str,
|
||||
original_document_id: str | None = None,
|
||||
extra_params: dict | None = None,
|
||||
extra_params: Dict[str, Any] | None = None,
|
||||
):
|
||||
"""
|
||||
Create a document by file.
|
||||
@@ -537,7 +857,12 @@ class KnowledgeBaseClient(DifyClient):
|
||||
url = f"/datasets/{self._get_dataset_id()}/document/create_by_file"
|
||||
return self._send_request_with_files("POST", url, {"data": json.dumps(data)}, files)
|
||||
|
||||
def update_document_by_file(self, document_id: str, file_path: str, extra_params: dict | None = None):
|
||||
def update_document_by_file(
|
||||
self,
|
||||
document_id: str,
|
||||
file_path: str,
|
||||
extra_params: Dict[str, Any] | None = None,
|
||||
):
|
||||
"""
|
||||
Update a document by file.
|
||||
|
||||
@@ -893,3 +1218,50 @@ class KnowledgeBaseClient(DifyClient):
|
||||
url = f"/datasets/{ds_id}/documents/status/{action}"
|
||||
data = {"document_ids": document_ids}
|
||||
return self._send_request("PATCH", url, json=data)
|
||||
|
||||
# Enhanced Dataset APIs
|
||||
def create_dataset_from_template(self, template_name: str, name: str, description: str | None = None):
|
||||
"""Create a dataset from a predefined template.
|
||||
|
||||
Args:
|
||||
template_name: Name of the template to use
|
||||
name: Name for the new dataset
|
||||
description: Description for the dataset (optional)
|
||||
|
||||
Returns:
|
||||
Created dataset information
|
||||
"""
|
||||
data = {
|
||||
"template_name": template_name,
|
||||
"name": name,
|
||||
"description": description,
|
||||
}
|
||||
return self._send_request("POST", "/datasets/from-template", json=data)
|
||||
|
||||
def duplicate_dataset(self, dataset_id: str, name: str):
|
||||
"""Duplicate an existing dataset.
|
||||
|
||||
Args:
|
||||
dataset_id: ID of dataset to duplicate
|
||||
name: Name for duplicated dataset
|
||||
|
||||
Returns:
|
||||
New dataset information
|
||||
"""
|
||||
data = {"name": name}
|
||||
url = f"/datasets/{dataset_id}/duplicate"
|
||||
return self._send_request("POST", url, json=data)
|
||||
|
||||
def list_conversation_variables_with_pagination(
|
||||
self, conversation_id: str, user: str, page: int = 1, limit: int = 20
|
||||
):
|
||||
"""List conversation variables with pagination."""
|
||||
params = {"page": page, "limit": limit, "user": user}
|
||||
url = f"/conversations/{conversation_id}/variables"
|
||||
return self._send_request("GET", url, params=params)
|
||||
|
||||
def update_conversation_variable_with_response(self, conversation_id: str, variable_id: str, user: str, value: Any):
|
||||
"""Update a conversation variable with full response handling."""
|
||||
data = {"value": value, "user": user}
|
||||
url = f"/conversations/{conversation_id}/variables/{variable_id}"
|
||||
return self._send_request("PUT", url, json=data)
|
||||
|
||||
Reference in New Issue
Block a user