* 示例4: 完整的工具调用工作流
(qwenLLM: QwenLLM, toolManager: any)
| 145 | * 示例4: 完整的工具调用工作流 |
| 146 | */ |
| 147 | async function demonstrateToolWorkflow(qwenLLM: QwenLLM, toolManager: any) { |
| 148 | console.log('=== 示例4: 完整工具调用工作流 ==='); |
| 149 | |
| 150 | try { |
| 151 | const availableTools = toolManager.getAllTools().slice(0, 8); |
| 152 | console.log('工作流使用的工具:', availableTools.map(t => t.name).join(', ')); |
| 153 | |
| 154 | const messages = [ |
| 155 | { |
| 156 | role: 'user', |
| 157 | content: '请告诉我现在的时间,然后生成一个随机UUID,最后计算一下字符串长度', |
| 158 | }, |
| 159 | ]; |
| 160 | |
| 161 | // 工具执行器 |
| 162 | const toolExecutor = async (toolName: string, args: any) => { |
| 163 | console.log(`执行工具: ${toolName},参数:`, args); |
| 164 | const response = await toolManager.callTool({ |
| 165 | toolName, |
| 166 | parameters: args, |
| 167 | }); |
| 168 | return response.result.data; |
| 169 | }; |
| 170 | |
| 171 | // 执行完整工作流 |
| 172 | const result = await qwenLLM.executeToolWorkflow(messages, availableTools, toolExecutor); |
| 173 | |
| 174 | console.log('\n=== 工作流结果 ==='); |
| 175 | console.log('最终回复:', result.finalResponse); |
| 176 | console.log(`\n执行了 ${result.toolExecutions.length} 个工具:`); |
| 177 | |
| 178 | for (const execution of result.toolExecutions) { |
| 179 | const status = execution.success ? '✅' : '❌'; |
| 180 | console.log( |
| 181 | `${status} ${execution.toolName}: ${execution.success ? '成功' : execution.error}` |
| 182 | ); |
| 183 | if (execution.success && execution.result) { |
| 184 | console.log(` 结果: ${JSON.stringify(execution.result)}`); |
| 185 | } |
| 186 | } |
| 187 | console.log(''); |
| 188 | } catch (error) { |
| 189 | console.error('工具工作流失败:', error); |
| 190 | console.log(''); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * 工具格式转换示例 |
no test coverage detected