Function calling for Groq

This commit is contained in:
Cohee 2024-05-29 01:10:18 +03:00
parent 7d983adc6e
commit b833f36c75
3 changed files with 22 additions and 2 deletions

View File

@ -1739,7 +1739,7 @@
</span>
</div>
</div>
<div class="range-block" data-source="openai,cohere,mistralai,custom,claude,openrouter">
<div class="range-block" data-source="openai,cohere,mistralai,custom,claude,openrouter,groq">
<label for="openai_function_calling" class="checkbox_label flexWrap widthFreeExpand">
<input id="openai_function_calling" type="checkbox" />
<span data-i18n="Enable function calling">Enable function calling</span>

View File

@ -1968,7 +1968,14 @@ async function registerFunctionTools(type, data) {
}
async function checkFunctionToolCalls(data) {
if ([chat_completion_sources.OPENAI, chat_completion_sources.CUSTOM, chat_completion_sources.MISTRALAI, chat_completion_sources.OPENROUTER].includes(oai_settings.chat_completion_source)) {
const oaiCompat = [
chat_completion_sources.OPENAI,
chat_completion_sources.CUSTOM,
chat_completion_sources.MISTRALAI,
chat_completion_sources.OPENROUTER,
chat_completion_sources.GROQ,
];
if (oaiCompat.includes(oai_settings.chat_completion_source)) {
if (!Array.isArray(data?.choices)) {
return;
}
@ -2045,6 +2052,7 @@ export function isFunctionCallingSupported() {
chat_completion_sources.MISTRALAI,
chat_completion_sources.CLAUDE,
chat_completion_sources.OPENROUTER,
chat_completion_sources.GROQ,
];
return supportedSources.includes(oai_settings.chat_completion_source);
}

View File

@ -915,6 +915,18 @@ router.post('/generate', jsonParser, function (request, response) {
apiKey = readSecret(request.user.directories, SECRET_KEYS.GROQ);
headers = {};
bodyParams = {};
// 'required' tool choice is not supported by Groq
if (request.body.tool_choice === 'required') {
if (Array.isArray(request.body.tools) && request.body.tools.length > 0) {
request.body.tool_choice = request.body.tools.length > 1
? 'auto' :
{ type: 'function', function: { name: request.body.tools[0]?.function?.name } };
} else {
request.body.tool_choice = 'none';
}
}
} else {
console.log('This chat completion source is not supported yet.');
return response.status(400).send({ error: true });