fix: use requests instead of openai SDK for API compatibility

This commit is contained in:
redbotu 2026-05-25 22:03:00 +08:00
parent 362c9585f3
commit cf8aaecece
7 changed files with 128 additions and 29 deletions

Binary file not shown.

View File

@ -1,13 +1,12 @@
"""
API 配置文件
修改这里来配置你的 API
API 配置文件 - 修改这里来配置你的 API
"""
# API 配置
API_KEY = "sk-..." # 你的 API Key
BASE_URL = "https://api.openai.com/v1" # API 地址OpenAI 兼容)
MODEL = "gpt-4o-mini" # 模型名称
API_KEY = "sk-f5ba1acb1d61c39f754c28f19d0e6e1b7d8d9fda43ef812ce4e335b6c26eff01"
BASE_URL = "https://gw2.oops.asia/v1"
MODEL = "gpt-5.4-mini" # 可用模型: gpt-5.2, gpt-5.4, gpt-5.4-mini, gpt-5.5 等
# 智能体配置
MAX_ITERATIONS = 4 # 最大迭代次数
TEMPERATURE = 0.3 # 温度参数(越低越确定)
MAX_ITERATIONS = 4
TEMPERATURE = 0.3

View File

@ -0,0 +1,47 @@
import requests
import json
url = "https://gw2.oops.asia/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer sk-f5ba1acb1d61c39f754c28f19d0e6e1b7d8d9fda43ef812ce4e335b6c26eff01"
}
# 测试不同的请求格式
test_cases = [
{
"name": "标准格式",
"body": {
"model": "gpt-5.4-mini",
"messages": [{"role": "user", "content": "Say hi"}],
"max_tokens": 20
}
},
{
"name": "带 temperature",
"body": {
"model": "gpt-5.4-mini",
"messages": [{"role": "user", "content": "Say hi"}],
"max_tokens": 20,
"temperature": 0.3
}
},
{
"name": "带 stream",
"body": {
"model": "gpt-5.4-mini",
"messages": [{"role": "user", "content": "Say hi"}],
"max_tokens": 20,
"stream": False
}
}
]
for test in test_cases:
print(f"\n测试: {test['name']}")
try:
response = requests.post(url, headers=headers, json=test["body"], timeout=10)
print(f" 状态码: {response.status_code}")
print(f" 响应: {response.text[:200]}")
except Exception as e:
print(f" 错误: {e}")

View File

@ -0,0 +1,12 @@
from openai import OpenAI
client = OpenAI(
api_key="sk-f5ba1acb1d61c39f754c28f19d0e6e1b7d8d9fda43ef812ce4e335b6c26eff01",
base_url="https://gw2.oops.asia/v1"
)
models = client.models.list()
print("支持的模型列表:")
print("-" * 50)
for m in models:
print(f" {m.id}")

View File

@ -4,6 +4,7 @@ ReAct 模式:思考(Reason) - 行动(Act) - 观察(Observe)
配置见 config.py
"""
import sys
import requests
from langgraph.graph import StateGraph, START, END
from typing import TypedDict
@ -14,9 +15,6 @@ if API_KEY == "sk-...":
print("请先在 config.py 中配置 API_KEY")
exit(1)
from openai import OpenAI
client = OpenAI(api_key=API_KEY, base_url=BASE_URL)
# 2⃣ 定义状态
class AgentState(TypedDict):
question: str
@ -56,7 +54,27 @@ tools = {
"search": search_knowledge,
}
# 4⃣ 定义节点
# 4⃣ LLM 调用函数
def call_llm(messages, max_tokens=300):
"""调用 LLM API"""
url = f"{BASE_URL}/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
body = {
"model": MODEL,
"messages": messages,
"max_tokens": max_tokens,
"temperature": TEMPERATURE,
}
response = requests.post(url, headers=headers, json=body, timeout=30)
response.raise_for_status()
data = response.json()
return data["choices"][0]["message"]["content"]
# 5⃣ 定义节点
def think_node(state: AgentState):
"""思考节点 - 让 LLM 决定下一步"""
state = state.copy()
@ -85,14 +103,7 @@ def think_node(state: AgentState):
if state.get('observation'):
messages.append({"role": "assistant", "content": f"[观察] {state['observation']}"})
response = client.chat.completions.create(
model=MODEL,
messages=messages,
max_tokens=300,
temperature=TEMPERATURE,
)
thought_text = response.choices[0].message.content
thought_text = call_llm(messages)
state['current_thought'] = thought_text
state['thoughts'] = state.get('thoughts', []) + [thought_text]
@ -146,17 +157,11 @@ def answer_node(state: AgentState):
{"role": "user", "content": f"问题: {state['question']}\n\n思考过程:\n" + "\n".join(state.get('thoughts', []))}
]
response = client.chat.completions.create(
model=MODEL,
messages=messages,
max_tokens=200,
)
state['final_answer'] = response.choices[0].message.content
state['final_answer'] = call_llm(messages, max_tokens=200)
print(f"\n[回答] {state['final_answer']}")
return state
# 5️⃣ 路由函数
# 6⃣ 路由函数
def route(state: AgentState):
"""决定下一步"""
if state.get('final_answer'):
@ -167,7 +172,7 @@ def route(state: AgentState):
return "act"
return "answer"
# 6️⃣ 构建图
# 7️⃣ 构建图
graph = StateGraph(AgentState)
graph.add_node("think", think_node)
graph.add_node("act", act_node)
@ -180,7 +185,7 @@ graph.add_edge("answer", END)
app = graph.compile()
# 7️⃣ 运行
# 8️⃣ 运行
print("=" * 50)
print("LangGraph ReAct 智能体")
print("=" * 50)

View File

@ -0,0 +1,13 @@
from openai import OpenAI
client = OpenAI(
api_key="sk-f5ba1acb1d61c39f754c28f19d0e6e1b7d8d9fda43ef812ce4e335b6c26eff01",
base_url="https://gw2.oops.asia/v1"
)
response = client.chat.completions.create(
model="gpt-5.4-mini",
messages=[{"role": "user", "content": "Say hi in Chinese"}],
max_tokens=20
)
print(response.choices[0].message.content)

View File

@ -0,0 +1,23 @@
from openai import OpenAI
client = OpenAI(
api_key="sk-f5ba1acb1d61c39f754c28f19d0e6e1b7d8d9fda43ef812ce4e335b6c26eff01",
base_url="https://gw2.oops.asia/v1"
)
models_to_test = ["gpt-4o-mini", "gpt-4o", "gpt-3.5-turbo", "gpt-5", "gpt-5.5", "claude-sonnet-4-20250514"]
print("测试可用模型:")
print("-" * 50)
for model in models_to_test:
try:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "Say hi"}],
max_tokens=20
)
print(f"{model} - OK")
except Exception as e:
error_msg = str(e)[:50]
print(f"{model} - {error_msg}")