stream = client.chat.completions.create(
model="claude-sonnet-5-5",
messages=[{"role": "user", "content": "Write a short deployment checklist."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
函数调用(工具调用)
支持标准的两步流程:模型返回工具调用,你执行工具,再把结果发回去获取最终回答。
import json
from openai import OpenAI
client = OpenAI(base_url="https://api.apivai.com/v1", api_key="YOUR_APIVAI_API_KEY")
MODEL = "claude-sonnet-5-5"
TOOLS = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
messages = [{"role": "user", "content": "What's the weather in Tokyo?"}]
first = client.chat.completions.create(model=MODEL, messages=messages, tools=TOOLS)
choice = first.choices[0].message
messages.append(choice)
for call in choice.tool_calls or []:
args = json.loads(call.function.arguments)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps({"city": args["city"], "temp_c": 21}),
})
final = client.chat.completions.create(model=MODEL, messages=messages, tools=TOOLS)
print(final.choices[0].message.content)
常用框架
LangChain
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://api.apivai.com/v1",
api_key="YOUR_APIVAI_API_KEY",
model="claude-sonnet-5-5",
)
print(llm.invoke("Explain API gateways in 2 sentences.").content)
LlamaIndex
from llama_index.llms.openai_like import OpenAILike
llm = OpenAILike(
model="claude-sonnet-5-5",
api_base="https://api.apivai.com/v1",
api_key="YOUR_APIVAI_API_KEY",
is_chat_model=True,
)
print(llm.complete("Explain API gateways in 2 sentences."))