Skip to content

Commit

Permalink
Groq tool calling (#304)
Browse files Browse the repository at this point in the history
* Support for Groq's tool calling

* removed console.log and unused code
  • Loading branch information
Luisotee authored Sep 16, 2024
1 parent 8ccd660 commit 0f6097f
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ GOOGLE_API_KEY="" # AIz...
# You can get this at https://anthropic.com/
ANTHROPIC_API_KEY="" # sk-...

# ------------------------------
# Obligatory if you're using Groq's models and want to use tool calling:
# ------------------------------
# You can get this at https://console.groq.com/keys
GROQ_API_KEY="" # gsk-...

# ------------------------------
# Obligatory if you're using one of OpenRouter models:
# ------------------------------
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@langchain/core": "0.2.32",
"@langchain/google-genai": "^0.1.0",
"@langchain/google-vertexai-web": "0.0.19",
"@langchain/groq": "^0.1.1",
"@langchain/openai": "0.2.1",
"@prisma/client": "5.3.1",
"@types/common-tags": "^1.8.4",
Expand Down
81 changes: 81 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions src/clients/open-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ANTHROPIC_API_KEY,
DEFAULT_MODEL,
GOOGLE_API_KEY,
GROQ_API_KEY,
MODEL_TEMPERATURE,
OPENAI_API_KEY,
OPENROUTER_API_KEY,
Expand All @@ -32,10 +33,12 @@ import {
import {
anthropicToolCallingModels,
googleToolCallingModels,
groqToolCallingModels,
openAIToolCallingModels,
} from "./tools/tool-calling-models";
import { tools } from "./tools/tools-openrouter";
import { ChatAnthropic } from "@langchain/anthropic";
import { ChatGroq } from "@langchain/groq";

function parseMessageHistory(
rawHistory: { [key: string]: string }[]
Expand Down Expand Up @@ -122,7 +125,6 @@ export async function createExecutorForOpenRouter(

// OpenAI LLM with Tool Calling Agent
if (openAIToolCallingModels.includes(llmModel) && OPENAI_API_KEY !== "") {
console.log("Using OpenAI LLM");
prompt = await pull<ChatPromptTemplate>(
"luisotee/wa-assistant-tool-calling"
);
Expand All @@ -145,7 +147,6 @@ export async function createExecutorForOpenRouter(
googleToolCallingModels.includes(llmModel) &&
GOOGLE_API_KEY !== ""
) {
console.log("Using Google Generative AI");
prompt = await pull<ChatPromptTemplate>(
"luisotee/wa-assistant-tool-calling"
);
Expand All @@ -168,7 +169,6 @@ export async function createExecutorForOpenRouter(
anthropicToolCallingModels.includes(llmModel) &&
ANTHROPIC_API_KEY !== ""
) {
console.log("Using Anthropics LLM");
prompt = await pull<ChatPromptTemplate>(
"luisotee/wa-assistant-tool-calling"
);
Expand All @@ -186,9 +186,27 @@ export async function createExecutorForOpenRouter(
prompt,
});
}
// Groq LLM with Tool Calling Agent
else if (groqToolCallingModels.includes(llmModel) && GROQ_API_KEY !== "") {
prompt = await pull<ChatPromptTemplate>(
"luisotee/wa-assistant-tool-calling"
);

llm = new ChatGroq({
modelName: llmModel,
streaming: true,
temperature: MODEL_TEMPERATURE,
apiKey: GROQ_API_KEY,
});

agent = await createToolCallingAgent({
llm,
tools,
prompt,
});
}
// OpenRouter LLMs without Tool Calling Agent, with Structured Agent
else {
console.log("Using OpenRouter LLM");
prompt = await pull<ChatPromptTemplate>("luisotee/wa-assistant");

llm = new ChatOpenAI(
Expand All @@ -211,7 +229,6 @@ export async function createExecutorForOpenRouter(
}

const executor = new AgentExecutor({
// @ts-ignore
agent,
tools,
memory,
Expand Down
5 changes: 5 additions & 0 deletions src/clients/tools/tool-calling-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export const anthropicToolCallingModels = [
"claude-3-sonnet-20240229",
"claude-3-haiku-20240307",
];

export const groqToolCallingModels = [
"llama3-groq-70b-8192-tool-use-preview",
"llava-v1.5-7b-4096-preview",
];
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ export const MODEL_TEMPERATURE = parseFloat(
export const ENABLE_GOOGLE_ROUTES = process.env.ENABLE_GOOGLE_ROUTES as string;
export const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY as string;
export const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY as string;
export const GROQ_API_KEY = process.env.GROQ_API_KEY as string;
4 changes: 4 additions & 0 deletions src/handlers/command/change-llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const LLM_OPTIONS = {
"8": "claude-3-5-sonnet-20240620",
"9": "claude-3-opus-20240229",
"10": "claude-3-haiku-20240307",
"11": "llama3-groq-70b-8192-tool-use-preview",
"12": "llava-v1.5-7b-4096-preview",
};

export async function handleChangeLLM(message: Message, args: string) {
Expand Down Expand Up @@ -45,6 +47,8 @@ export async function handleChangeLLM(message: Message, args: string) {
*${CMD_PREFIX}change 8* for _claude-3-5-sonnet_ (Anthropic API)
*${CMD_PREFIX}change 9* for _claude-3-opus_ (Anthropic API)
*${CMD_PREFIX}change 10* for _claude-3-haiku_ (Anthropic API)
*${CMD_PREFIX}change 11* for _llama3-groq-70b_ (Groq API)
*${CMD_PREFIX}change 12* for _llava-v1.5-7b_ (Groq API)
You can also type the name of your desired model supported by OpenRouter, like *${CMD_PREFIX}change mistralai/mixtral-8x7b-instruct*
Expand Down

0 comments on commit 0f6097f

Please sign in to comment.