* 将工具定义转换为 OpenAI Tools 格式
(toolsOrFunctions: any[])
| 378 | * 将工具定义转换为 OpenAI Tools 格式 |
| 379 | */ |
| 380 | private convertToToolsFormat(toolsOrFunctions: any[]): any[] { |
| 381 | return toolsOrFunctions.map(item => { |
| 382 | // 如果已经是 tools 格式,直接返回 |
| 383 | if (item.type === 'function' && item.function) { |
| 384 | return item; |
| 385 | } |
| 386 | |
| 387 | // 如果是 functions 格式,转换为 tools 格式 |
| 388 | if (item.name && item.description && item.parameters) { |
| 389 | return { |
| 390 | type: 'function', |
| 391 | function: { |
| 392 | name: item.name, |
| 393 | description: item.description, |
| 394 | parameters: item.parameters, |
| 395 | }, |
| 396 | }; |
| 397 | } |
| 398 | |
| 399 | // 如果是项目内部的 ToolDefinition 格式,转换为 tools 格式 |
| 400 | if (item.name && item.description && item.parameters) { |
| 401 | return { |
| 402 | type: 'function', |
| 403 | function: { |
| 404 | name: item.name, |
| 405 | description: item.description, |
| 406 | parameters: { |
| 407 | type: 'object', |
| 408 | properties: item.parameters, |
| 409 | required: item.required || [], |
| 410 | }, |
| 411 | }, |
| 412 | }; |
| 413 | } |
| 414 | |
| 415 | return item; |
| 416 | }); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * 将工具定义转换为旧的 Functions 格式(向后兼容) |