29 lines
1001 B
Python
29 lines
1001 B
Python
"""
|
||
OpenAI 兼容 API 引擎:适用于 DeepSeek、通义千问、Kimi、文心一言、豆包(火山方舟)。
|
||
只要平台提供 OpenAI 兼容接口,均可用此引擎,无需 Playwright。
|
||
"""
|
||
|
||
|
||
class ApiEngine:
|
||
def __init__(self, api_base: str, api_key: str, model: str):
|
||
self.api_base = api_base
|
||
self.api_key = api_key
|
||
self.model = model
|
||
|
||
def generate(self, prompt: str) -> str:
|
||
try:
|
||
from openai import OpenAI
|
||
except ImportError:
|
||
return "ERROR:缺少依赖:pip install openai"
|
||
|
||
try:
|
||
client = OpenAI(base_url=self.api_base, api_key=self.api_key)
|
||
response = client.chat.completions.create(
|
||
model=self.model,
|
||
messages=[{"role": "user", "content": prompt}],
|
||
)
|
||
content = response.choices[0].message.content
|
||
return (content or "").strip()
|
||
except Exception as e:
|
||
return f"ERROR:API调用失败:{e}"
|