fix: use requests instead of openai SDK for API compatibility
This commit is contained in:
parent
362c9585f3
commit
cf8aaecece
BIN
langgraph-tutorial/__pycache__/config.cpython-313.pyc
Normal file
BIN
langgraph-tutorial/__pycache__/config.cpython-313.pyc
Normal file
Binary file not shown.
@ -1,13 +1,12 @@
|
|||||||
"""
|
"""
|
||||||
API 配置文件
|
API 配置文件 - 修改这里来配置你的 API
|
||||||
修改这里来配置你的 API
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# API 配置
|
# API 配置
|
||||||
API_KEY = "sk-..." # 你的 API Key
|
API_KEY = "sk-f5ba1acb1d61c39f754c28f19d0e6e1b7d8d9fda43ef812ce4e335b6c26eff01"
|
||||||
BASE_URL = "https://api.openai.com/v1" # API 地址(OpenAI 兼容)
|
BASE_URL = "https://gw2.oops.asia/v1"
|
||||||
MODEL = "gpt-4o-mini" # 模型名称
|
MODEL = "gpt-5.4-mini" # 可用模型: gpt-5.2, gpt-5.4, gpt-5.4-mini, gpt-5.5 等
|
||||||
|
|
||||||
# 智能体配置
|
# 智能体配置
|
||||||
MAX_ITERATIONS = 4 # 最大迭代次数
|
MAX_ITERATIONS = 4
|
||||||
TEMPERATURE = 0.3 # 温度参数(越低越确定)
|
TEMPERATURE = 0.3
|
||||||
47
langgraph-tutorial/debug_api.py
Normal file
47
langgraph-tutorial/debug_api.py
Normal 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}")
|
||||||
12
langgraph-tutorial/list_models.py
Normal file
12
langgraph-tutorial/list_models.py
Normal 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}")
|
||||||
@ -4,6 +4,7 @@ ReAct 模式:思考(Reason) - 行动(Act) - 观察(Observe)
|
|||||||
配置见 config.py
|
配置见 config.py
|
||||||
"""
|
"""
|
||||||
import sys
|
import sys
|
||||||
|
import requests
|
||||||
from langgraph.graph import StateGraph, START, END
|
from langgraph.graph import StateGraph, START, END
|
||||||
from typing import TypedDict
|
from typing import TypedDict
|
||||||
|
|
||||||
@ -14,9 +15,6 @@ if API_KEY == "sk-...":
|
|||||||
print("请先在 config.py 中配置 API_KEY")
|
print("请先在 config.py 中配置 API_KEY")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
from openai import OpenAI
|
|
||||||
client = OpenAI(api_key=API_KEY, base_url=BASE_URL)
|
|
||||||
|
|
||||||
# 2️⃣ 定义状态
|
# 2️⃣ 定义状态
|
||||||
class AgentState(TypedDict):
|
class AgentState(TypedDict):
|
||||||
question: str
|
question: str
|
||||||
@ -56,7 +54,27 @@ tools = {
|
|||||||
"search": search_knowledge,
|
"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):
|
def think_node(state: AgentState):
|
||||||
"""思考节点 - 让 LLM 决定下一步"""
|
"""思考节点 - 让 LLM 决定下一步"""
|
||||||
state = state.copy()
|
state = state.copy()
|
||||||
@ -85,14 +103,7 @@ def think_node(state: AgentState):
|
|||||||
if state.get('observation'):
|
if state.get('observation'):
|
||||||
messages.append({"role": "assistant", "content": f"[观察] {state['observation']}"})
|
messages.append({"role": "assistant", "content": f"[观察] {state['observation']}"})
|
||||||
|
|
||||||
response = client.chat.completions.create(
|
thought_text = call_llm(messages)
|
||||||
model=MODEL,
|
|
||||||
messages=messages,
|
|
||||||
max_tokens=300,
|
|
||||||
temperature=TEMPERATURE,
|
|
||||||
)
|
|
||||||
|
|
||||||
thought_text = response.choices[0].message.content
|
|
||||||
state['current_thought'] = thought_text
|
state['current_thought'] = thought_text
|
||||||
state['thoughts'] = state.get('thoughts', []) + [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', []))}
|
{"role": "user", "content": f"问题: {state['question']}\n\n思考过程:\n" + "\n".join(state.get('thoughts', []))}
|
||||||
]
|
]
|
||||||
|
|
||||||
response = client.chat.completions.create(
|
state['final_answer'] = call_llm(messages, max_tokens=200)
|
||||||
model=MODEL,
|
|
||||||
messages=messages,
|
|
||||||
max_tokens=200,
|
|
||||||
)
|
|
||||||
|
|
||||||
state['final_answer'] = response.choices[0].message.content
|
|
||||||
print(f"\n[回答] {state['final_answer']}")
|
print(f"\n[回答] {state['final_answer']}")
|
||||||
return state
|
return state
|
||||||
|
|
||||||
# 5️⃣ 路由函数
|
# 6️⃣ 路由函数
|
||||||
def route(state: AgentState):
|
def route(state: AgentState):
|
||||||
"""决定下一步"""
|
"""决定下一步"""
|
||||||
if state.get('final_answer'):
|
if state.get('final_answer'):
|
||||||
@ -167,7 +172,7 @@ def route(state: AgentState):
|
|||||||
return "act"
|
return "act"
|
||||||
return "answer"
|
return "answer"
|
||||||
|
|
||||||
# 6️⃣ 构建图
|
# 7️⃣ 构建图
|
||||||
graph = StateGraph(AgentState)
|
graph = StateGraph(AgentState)
|
||||||
graph.add_node("think", think_node)
|
graph.add_node("think", think_node)
|
||||||
graph.add_node("act", act_node)
|
graph.add_node("act", act_node)
|
||||||
@ -180,7 +185,7 @@ graph.add_edge("answer", END)
|
|||||||
|
|
||||||
app = graph.compile()
|
app = graph.compile()
|
||||||
|
|
||||||
# 7️⃣ 运行
|
# 8️⃣ 运行
|
||||||
print("=" * 50)
|
print("=" * 50)
|
||||||
print("LangGraph ReAct 智能体")
|
print("LangGraph ReAct 智能体")
|
||||||
print("=" * 50)
|
print("=" * 50)
|
||||||
|
|||||||
13
langgraph-tutorial/test_api.py
Normal file
13
langgraph-tutorial/test_api.py
Normal 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)
|
||||||
23
langgraph-tutorial/test_models.py
Normal file
23
langgraph-tutorial/test_models.py
Normal 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}")
|
||||||
Loading…
x
Reference in New Issue
Block a user