Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📦 NEW: Groq tools support #82

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions packages/baseai/src/data/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,43 +421,71 @@ export const modelsByProvider: ModelsByProviderInclCosts = {
id: 'llama-3.1-70b-versatile',
provider: GROQ,
promptCost: 0.59,
completionCost: 0.79
completionCost: 0.79,
toolSupport: {
toolChoice: true,
parallelToolCalls: true
}
},
{
id: 'llama-3.1-8b-instant',
provider: GROQ,
promptCost: 0.59,
completionCost: 0.79
completionCost: 0.79,
toolSupport: {
toolChoice: true,
parallelToolCalls: true
}
},
{
id: 'llama3-70b-8192',
provider: GROQ,
promptCost: 0.59,
completionCost: 0.79
completionCost: 0.79,
toolSupport: {
toolChoice: true,
parallelToolCalls: true
}
},
{
id: 'llama3-8b-8192',
provider: GROQ,
promptCost: 0.05,
completionCost: 0.1
completionCost: 0.1,
toolSupport: {
toolChoice: true,
parallelToolCalls: true
}
},
{
id: 'mixtral-8x7b-32768',
provider: GROQ,
promptCost: 0.27,
completionCost: 0.27
completionCost: 0.27,
toolSupport: {
toolChoice: true,
parallelToolCalls: false
}
},
{
id: 'gemma2-9b-it',
provider: GROQ,
promptCost: 0.2,
completionCost: 0.2
completionCost: 0.2,
toolSupport: {
toolChoice: true,
parallelToolCalls: false
}
},
{
id: 'gemma-7b-it',
provider: GROQ,
promptCost: 0.07,
completionCost: 0.07
completionCost: 0.07,
toolSupport: {
toolChoice: true,
parallelToolCalls: false
}
}
],
[GOOGLE]: [
Expand Down
2 changes: 2 additions & 0 deletions packages/baseai/src/dev/llms/call-groq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import transformToProviderRequest from '../utils/provider-handlers/transfrom-to-
import { applyJsonModeIfEnabled, handleLlmError } from './utils';
import type { ModelParams } from 'types/providers';
import type { Message } from 'types/pipe';
import { addToolsToParams } from '../utils/add-tools-to-params';

export async function callGroq({
pipe,
Expand All @@ -24,6 +25,7 @@ export async function callGroq({
baseURL: 'https://api.groq.com/openai/v1'
});
applyJsonModeIfEnabled(modelParams, pipe);
addToolsToParams(modelParams, pipe);

// Transform params according to provider's format
const transformedRequestParams = transformToProviderRequest({
Expand Down
12 changes: 12 additions & 0 deletions packages/baseai/src/dev/providers/groq/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,17 @@ export const GroqChatCompleteConfig: ProviderConfig = {
default: 1,
max: 1,
min: 1
},
parallel_tool_calls: {
param: 'parallel_tool_calls',
default: false
},
tool_choice: {
param: 'tool_choice',
default: 'none'
},
tools: {
param: 'tools',
default: []
}
};
19 changes: 8 additions & 11 deletions packages/baseai/src/dev/utils/add-tools-to-params.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { getSupportedToolSettings, hasToolSupport } from './has-tool-support';
import { hasModelToolSupport } from './has-tool-support';
import type { ModelParams } from 'types/providers';

export function addToolsToParams(modelParams: ModelParams, pipe: any) {
if (!pipe.functions.length) return;

// Check if the model supports tool calls
const hasToolCallSupport = hasToolSupport({
modelName: pipe.model.name,
provider: pipe.model.provider
});
const { hasToolChoiceSupport, hasParallelToolCallSupport } =
hasModelToolSupport({
modelName: pipe.model.name,
provider: pipe.model.provider
});

if (hasToolCallSupport) {
const { hasParallelToolCallSupport, hasToolChoiceSupport } =
getSupportedToolSettings({
modelName: pipe.model.name,
provider: pipe.model.provider
});
const hasToolSupport = hasToolChoiceSupport || hasParallelToolCallSupport;

if (hasToolSupport) {
if (hasParallelToolCallSupport) {
modelParams.parallel_tool_calls = pipe.model.parallel_tool_calls;
}
Expand Down
18 changes: 1 addition & 17 deletions packages/baseai/src/dev/utils/has-tool-support.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { modelsByProvider } from '@/data/models';

export function hasToolSupport({
export function hasModelToolSupport({
provider,
modelName
}: {
Expand All @@ -10,23 +10,7 @@ export function hasToolSupport({
const toolSupportedModels = modelsByProvider[provider].filter(
model => model.toolSupport
);
const hasToolCallSupport = toolSupportedModels
.flatMap(model => model.id)
.includes(modelName);

return hasToolCallSupport;
}

export function getSupportedToolSettings({
provider,
modelName
}: {
modelName: string;
provider: string;
}) {
const toolSupportedModels = modelsByProvider[provider].filter(
model => model.toolSupport
);
const providerModel = toolSupportedModels.find(
model => model.id === modelName
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ModelParams } from '@/types/providers';
import {
handleNonStreamingMode,
handleStreamingMode
} from './response-handler-utils';
import Providers from '@/dev/providers';
import { dlog } from '../dlog';
import type { ModelParams } from 'types/providers';

/**
* Handles various types of responses based on the specified parameters
Expand Down