第一章 大模型配置
from openai import OpenAI
client = OpenAI(
api_key="**-***************************************",
base_url="https://api.moonshot.cn/v1"
)
response = client.responses.create(
model="kimi-k2.6",
instructions="你是西安**天气查询助手...",
input="你是谁",
tool_choice="auto",
tools=weather_tool,
max_tool_calls=5
)
print(response.output_text)
第二章 大模型函数调用机制

import json
from openai import OpenAI
from tools.weather_query import get_weather
tools = [{
"type": "function",
"name": "get_weather",
"description": "查询指定地点的当前天气。支持国家、省、市、区、县和地点名称。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "要查询的地点,例如:北京市、深圳市南山区、四川省成都市郫都区、Tokyo"
}
},
"required": ["location"],
"additionalProperties": False
},
"strict": True
}]
FUNCTIONS = {"get_weather": get_weather}
client = OpenAI(
api_key="**-*********************************",
base_url="https://api.moonshot.cn/v1"
)
response = client.responses.create(
model="kimi-k2.6",
instructions="你是西安**贾维斯...",
input="你是谁",
tool_choice="auto",
tools=tools,
max_tool_calls=5
)
for item in response.output:
if item.type != "function_call": continue
arguments = json.loads(item.arguments)
function = FUNCTIONS[item.name]
result = function(**arguments)
print(json.dumps(result, ensure_ascii=False))