Files
skill-template/llm-manager/scripts/engines/api_engine.py
2026-04-04 10:35:02 +08:00

29 lines
1001 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
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}"