切换到dify,还是不要直连,dify更方便
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import json
|
||||
import mimetypes
|
||||
import time
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
from urllib.parse import urlparse
|
||||
@@ -67,9 +70,10 @@ class UnifiedLLMClient:
|
||||
user: str,
|
||||
inputs: Optional[Dict[str, Any]] = None,
|
||||
tag: str = "",
|
||||
files: Optional[List[Dict[str, Any]]] = None,
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
if self.provider == "dify":
|
||||
return self.generate(prompt=prompt, user=user, inputs=inputs or {}, tag=tag)
|
||||
return self.generate(prompt=prompt, user=user, inputs=inputs or {}, tag=tag, files=files or [])
|
||||
|
||||
effective_prompt = prompt or self._stringify_inputs(inputs or {})
|
||||
return self.generate(
|
||||
@@ -78,8 +82,80 @@ class UnifiedLLMClient:
|
||||
user=user,
|
||||
inputs=inputs or {},
|
||||
tag=tag,
|
||||
files=files or [],
|
||||
)
|
||||
|
||||
def upload_dify_file(
|
||||
self,
|
||||
*,
|
||||
user: str,
|
||||
file_bytes: bytes,
|
||||
filename: str,
|
||||
mime_type: str = "",
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
self.last_error = ""
|
||||
if self.provider != "dify":
|
||||
self.last_error = "upload_not_supported_for_provider"
|
||||
return None
|
||||
if not self.base_url or not self.api_key or not user or not file_bytes or not filename:
|
||||
self.last_error = "upload_missing_required_fields"
|
||||
return None
|
||||
|
||||
upload_url = f"{self.base_url}/files/upload"
|
||||
headers = {"Authorization": self._build_auth_header(self.api_key)}
|
||||
detected_mime = mime_type or mimetypes.guess_type(filename)[0] or "application/octet-stream"
|
||||
files = {
|
||||
"file": (filename, file_bytes, detected_mime),
|
||||
}
|
||||
data = {"user": user}
|
||||
|
||||
for attempt in range(1, self.max_retries + 1):
|
||||
try:
|
||||
response = requests.post(upload_url, headers=headers, files=files, data=data, timeout=self.timeout_seconds)
|
||||
response.raise_for_status()
|
||||
payload = response.json() or {}
|
||||
if payload.get("id"):
|
||||
return payload
|
||||
self.last_error = "upload_missing_file_id"
|
||||
except Exception as exc:
|
||||
self.last_error = f"upload_failed:attempt_{attempt}:{exc}"
|
||||
if attempt < self.max_retries:
|
||||
time.sleep(self.retry_delay_seconds * attempt)
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def build_dify_file_ref(
|
||||
*,
|
||||
file_type: str = "image",
|
||||
remote_url: str = "",
|
||||
upload_file_id: str = "",
|
||||
) -> Dict[str, Any]:
|
||||
if upload_file_id:
|
||||
return {
|
||||
"type": file_type,
|
||||
"transfer_method": "local_file",
|
||||
"upload_file_id": upload_file_id,
|
||||
}
|
||||
if remote_url:
|
||||
return {
|
||||
"type": file_type,
|
||||
"transfer_method": "remote_url",
|
||||
"url": remote_url,
|
||||
}
|
||||
return {}
|
||||
|
||||
@staticmethod
|
||||
def decode_data_url(data_url: str) -> Tuple[bytes, str]:
|
||||
raw = str(data_url or "").strip()
|
||||
if not raw.startswith("data:") or "," not in raw:
|
||||
return b"", ""
|
||||
header, encoded = raw.split(",", 1)
|
||||
mime_type = header[5:].split(";", 1)[0].strip()
|
||||
try:
|
||||
return base64.b64decode(encoded), mime_type
|
||||
except (ValueError, binascii.Error):
|
||||
return b"", mime_type
|
||||
|
||||
def generate(
|
||||
self,
|
||||
prompt: str = "",
|
||||
|
||||
Reference in New Issue
Block a user