* 示例3: 智能选择最佳格式
(qwenLLM: QwenLLM, availableTools: any[])
| 107 | * 示例3: 智能选择最佳格式 |
| 108 | */ |
| 109 | async function demonstrateSmartFunctionCall(qwenLLM: QwenLLM, availableTools: any[]) { |
| 110 | console.log('=== 示例3: 智能格式选择 ==='); |
| 111 | |
| 112 | try { |
| 113 | const selectedTools = availableTools.slice(0, 5); |
| 114 | |
| 115 | console.log('使用智能格式选择,支持的工具:', selectedTools.map(t => t.name).join(', ')); |
| 116 | |
| 117 | const messages = [ |
| 118 | { |
| 119 | role: 'user', |
| 120 | content: '帮我查看当前目录下的文件列表', |
| 121 | }, |
| 122 | ]; |
| 123 | |
| 124 | // 智能选择最佳格式 |
| 125 | const response = await qwenLLM.smartFunctionCall(messages, selectedTools); |
| 126 | const result = qwenLLM.parseToolCallResult(response); |
| 127 | |
| 128 | console.log('LLM 回复:', result.content); |
| 129 | if (result.hasToolCalls) { |
| 130 | console.log('选择的工具:', result.toolCalls.map(t => t.function.name).join(', ')); |
| 131 | |
| 132 | // 显示工具调用参数 |
| 133 | for (const toolCall of result.toolCalls) { |
| 134 | console.log(`${toolCall.function.name} 参数:`, toolCall.function.arguments); |
| 135 | } |
| 136 | } |
| 137 | console.log(''); |
| 138 | } catch (error) { |
| 139 | console.error('智能格式选择失败:', error); |
| 140 | console.log(''); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * 示例4: 完整的工具调用工作流 |
no test coverage detected