Remove tool calling for Cohere v1
| @@ -461,19 +461,6 @@ export class ToolManager { | |||
| 461 | } | 461 | } |
| 462 | */ | 462 | */ |
| 463 | 463 | ||
| 464 | /* | ||
| 465 | if ([chat_completion_sources.COHERE].includes(oai_settings.chat_completion_source)) { | ||
| 466 | if (!Array.isArray(data?.tool_calls)) { | ||
| 467 | return; | ||
| 468 | } | ||
| 469 | |||
| 470 | for (const toolCall of data.tool_calls) { | ||
| 471 | const args = { name: toolCall.name, arguments: JSON.stringify(toolCall.parameters) }; | ||
| 472 | console.log('Function tool call:', toolCall); | ||
| 473 | } | ||
| 474 | } | ||
| 475 | */ | ||
| 476 | |||
| 477 | return result; | 464 | return result; |
| 478 | } | 465 | } |
| 479 | 466 | ||
| @@ -4,7 +4,7 @@ const fetch = require('node-fetch').default; | |||
| 4 | const { jsonParser } = require('../../express-common'); | 4 | const { jsonParser } = require('../../express-common'); |
| 5 | const { CHAT_COMPLETION_SOURCES, GEMINI_SAFETY, BISON_SAFETY, OPENROUTER_HEADERS } = require('../../constants'); | 5 | const { CHAT_COMPLETION_SOURCES, GEMINI_SAFETY, BISON_SAFETY, OPENROUTER_HEADERS } = require('../../constants'); |
| 6 | const { forwardFetchResponse, getConfigValue, tryParse, uuidv4, mergeObjectWithYaml, excludeKeysByYaml, color } = require('../../util'); | 6 | const { forwardFetchResponse, getConfigValue, tryParse, uuidv4, mergeObjectWithYaml, excludeKeysByYaml, color } = require('../../util'); |
| 7 | const { convertClaudeMessages, convertGooglePrompt, convertTextCompletionPrompt, convertCohereMessages, convertMistralMessages, convertCohereTools, convertAI21Messages } = require('../../prompt-converters'); | 7 | const { convertClaudeMessages, convertGooglePrompt, convertTextCompletionPrompt, convertCohereMessages, convertMistralMessages, convertAI21Messages } = require('../../prompt-converters'); |
| 8 | const CohereStream = require('../../cohere-stream'); | 8 | const CohereStream = require('../../cohere-stream'); |
| 9 | 9 | ||
| 10 | const { readSecret, SECRET_KEYS } = require('../secrets'); | 10 | const { readSecret, SECRET_KEYS } = require('../secrets'); |
| @@ -555,14 +555,6 @@ async function sendCohereRequest(request, response) { | |||
| 555 | }); | 555 | }); |
| 556 | } | 556 | } |
| 557 | 557 | ||
| 558 | /* | ||
| 559 | if (Array.isArray(request.body.tools) && request.body.tools.length > 0) { | ||
| 560 | tools.push(...convertCohereTools(request.body.tools)); | ||
| 561 | // Can't have both connectors and tools in the same request | ||
| 562 | connectors.splice(0, connectors.length); | ||
| 563 | } | ||
| 564 | */ | ||
| 565 | |||
| 566 | // https://docs.cohere.com/reference/chat | 558 | // https://docs.cohere.com/reference/chat |
| 567 | const requestBody = { | 559 | const requestBody = { |
| 568 | stream: Boolean(request.body.stream), | 560 | stream: Boolean(request.body.stream), |
| @@ -522,76 +522,6 @@ function convertTextCompletionPrompt(messages) { | |||
| 522 | return messageStrings.join('\n') + '\nassistant:'; | 522 | return messageStrings.join('\n') + '\nassistant:'; |
| 523 | } | 523 | } |
| 524 | 524 | ||
| 525 | /** | ||
| 526 | * Convert OpenAI Chat Completion tools to the format used by Cohere. | ||
| 527 | * @param {object[]} tools OpenAI Chat Completion tool definitions | ||
| 528 | */ | ||
| 529 | function convertCohereTools(tools) { | ||
| 530 | if (!Array.isArray(tools) || tools.length === 0) { | ||
| 531 | return []; | ||
| 532 | } | ||
| 533 | |||
| 534 | const jsonSchemaToPythonTypes = { | ||
| 535 | 'string': 'str', | ||
| 536 | 'number': 'float', | ||
| 537 | 'integer': 'int', | ||
| 538 | 'boolean': 'bool', | ||
| 539 | 'array': 'list', | ||
| 540 | 'object': 'dict', | ||
| 541 | }; | ||
| 542 | |||
| 543 | const cohereTools = []; | ||
| 544 | |||
| 545 | for (const tool of tools) { | ||
| 546 | if (tool?.type !== 'function') { | ||
| 547 | console.log(`Unsupported tool type: ${tool.type}`); | ||
| 548 | continue; | ||
| 549 | } | ||
| 550 | |||
| 551 | const name = tool?.function?.name; | ||
| 552 | const description = tool?.function?.description; | ||
| 553 | const properties = tool?.function?.parameters?.properties; | ||
| 554 | const required = tool?.function?.parameters?.required; | ||
| 555 | const parameters = {}; | ||
| 556 | |||
| 557 | if (!name) { | ||
| 558 | console.log('Tool name is missing'); | ||
| 559 | continue; | ||
| 560 | } | ||
| 561 | |||
| 562 | if (!description) { | ||
| 563 | console.log('Tool description is missing'); | ||
| 564 | } | ||
| 565 | |||
| 566 | if (!properties || typeof properties !== 'object') { | ||
| 567 | console.log(`No properties found for tool: ${tool?.function?.name}`); | ||
| 568 | continue; | ||
| 569 | } | ||
| 570 | |||
| 571 | for (const property in properties) { | ||
| 572 | const parameterDefinition = properties[property]; | ||
| 573 | const description = parameterDefinition.description || (parameterDefinition.enum ? JSON.stringify(parameterDefinition.enum) : ''); | ||
| 574 | const type = jsonSchemaToPythonTypes[parameterDefinition.type] || 'str'; | ||
| 575 | const isRequired = Array.isArray(required) && required.includes(property); | ||
| 576 | parameters[property] = { | ||
| 577 | description: description, | ||
| 578 | type: type, | ||
| 579 | required: isRequired, | ||
| 580 | }; | ||
| 581 | } | ||
| 582 | |||
| 583 | const cohereTool = { | ||
| 584 | name: tool.function.name, | ||
| 585 | description: tool.function.description, | ||
| 586 | parameter_definitions: parameters, | ||
| 587 | }; | ||
| 588 | |||
| 589 | cohereTools.push(cohereTool); | ||
| 590 | } | ||
| 591 | |||
| 592 | return cohereTools; | ||
| 593 | } | ||
| 594 | |||
| 595 | module.exports = { | 525 | module.exports = { |
| 596 | convertClaudePrompt, | 526 | convertClaudePrompt, |
| 597 | convertClaudeMessages, | 527 | convertClaudeMessages, |
| @@ -599,6 +529,5 @@ module.exports = { | |||
| 599 | convertTextCompletionPrompt, | 529 | convertTextCompletionPrompt, |
| 600 | convertCohereMessages, | 530 | convertCohereMessages, |
| 601 | convertMistralMessages, | 531 | convertMistralMessages, |
| 602 | convertCohereTools, | ||
| 603 | convertAI21Messages, | 532 | convertAI21Messages, |
| 604 | }; | 533 | }; |