1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
function getWeather(cityName) { return `天气总是晴朗的,位于 ${cityName}!`; }
const toolSchema = { type: "function", function: { name: "get_weather", description: "查询指定城市的天气情况。", parameters: { type: "object", properties: { cityName: { type: "string", description: "要查询天气的城市名称", }, }, required: ["cityName"], }, }, };
async function callDeepSeekApi(messages, tools) { const apiKey = "xxxx"; const apiUrl = "https://api.deepseek.com/v1/chat/completions"; const model = "deepseek-chat";
const payload = { model: model, messages: messages, tools: tools.length > 0 ? tools : undefined, temperature: 0.0, };
try { const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}`, }, body: JSON.stringify(payload), });
if (!response.ok) { const errorText = await response.text(); throw new Error(`API 调用失败: ${response.status} - ${errorText}`); }
const jsonResponse = await response.json();
return jsonResponse.choices[0].message;
} catch (error) { console.error("DeepSeek API 调用出错:", error.message); throw error; } }
async function runAgent(userQuery) { let messages = [ { role: "system", content: "你是一个乐于助人的助手,擅长使用工具来获取信息并用中文回复。" }, { role: "user", content: userQuery }, ];
const availableTools = { get_weather: getWeather, };
let maxSteps = 3; let step = 0; while (step < maxSteps) { step++; console.log(`\n--- Agent 步骤 ${step} ---`);
let apiResponse; try { apiResponse = await callDeepSeekApi(messages, [toolSchema]); } catch (e) { console.error("Agent 运行中断。", e.message); break; }
if (apiResponse.tool_calls && apiResponse.tool_calls.length > 0) {
messages.push(apiResponse);
const toolCall = apiResponse.tool_calls[0]; const toolName = toolCall.function.name;
try { const args = JSON.parse(toolCall.function.arguments); const toolFunc = availableTools[toolName];
if (toolFunc) { console.log(`LLM 请求工具: ${toolName}, 参数: ${JSON.stringify(args)}`);
const toolResult = toolFunc(args.cityName);
console.log(`工具执行结果: ${toolResult}`);
messages.push({ role: "tool", tool_call_id: toolCall.id, name: toolName, content: toolResult, });
} else { console.error(`错误:找不到工具 ${toolName}`); break; } } catch (e) { console.error(`解析参数或工具执行出错: ${e.message}`); break; }
} else { messages.push(apiResponse); console.log("\n--- Agent 最终回复 ---"); console.log(apiResponse.content); break; } } }
runAgent("东京的天气怎么样?");
|