Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Add tool call id in TS and example for v3 tokenization
Browse files Browse the repository at this point in the history
  • Loading branch information
fuegoio committed May 23, 2024
1 parent 2fcc564 commit 133146d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 44 deletions.
100 changes: 59 additions & 41 deletions examples/function_calling.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import MistralClient from '@mistralai/mistralai';
import MistralClient from "@mistralai/mistralai";

Check failure on line 1 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 1 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote

const apiKey = process.env.MISTRAL_API_KEY;

// Assuming we have the following data
const data = {
transactionId: ['T1001', 'T1002', 'T1003', 'T1004', 'T1005'],
customerId: ['C001', 'C002', 'C003', 'C002', 'C001'],
paymentAmount: [125.50, 89.99, 120.00, 54.30, 210.20],
transactionId: ["T1001", "T1002", "T1003", "T1004", "T1005"],

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote

Check failure on line 7 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote
customerId: ["C001", "C002", "C003", "C002", "C001"],

Check failure on line 8 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 8 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 8 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 8 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (20)

Strings must use singlequote

Check failure on line 8 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote

Check failure on line 8 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote

Check failure on line 8 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote

Check failure on line 8 in examples/function_calling.js

View workflow job for this annotation

GitHub Actions / lint_and_test (22)

Strings must use singlequote
paymentAmount: [125.5, 89.99, 120.0, 54.3, 210.2],
paymentDate: [
'2021-10-05', '2021-10-06', '2021-10-07', '2021-10-05', '2021-10-08',
"2021-10-05",
"2021-10-06",
"2021-10-07",
"2021-10-05",
"2021-10-08",
],
paymentStatus: ['Paid', 'Unpaid', 'Paid', 'Paid', 'Pending'],
paymentStatus: ["Paid", "Unpaid", "Paid", "Paid", "Pending"],
};

/**
Expand All @@ -19,12 +23,12 @@ const data = {
* @param {string} transactionId - The transaction id.
* @return {string} - The payment status.
*/
function retrievePaymentStatus({data, transactionId}) {
function retrievePaymentStatus({ data, transactionId }) {
const transactionIndex = data.transactionId.indexOf(transactionId);
if (transactionIndex != -1) {
return JSON.stringify({status: data.payment_status[transactionIndex]});
return JSON.stringify({ status: data.payment_status[transactionIndex] });
} else {
return JSON.stringify({status: 'error - transaction id not found.'});
return JSON.stringify({ status: "error - transaction id not found." });
}
}

Expand All @@ -35,75 +39,80 @@ function retrievePaymentStatus({data, transactionId}) {
* @return {string} - The payment date.
*
*/
function retrievePaymentDate({data, transactionId}) {
function retrievePaymentDate({ data, transactionId }) {
const transactionIndex = data.transactionId.indexOf(transactionId);
if (transactionIndex != -1) {
return JSON.stringify({status: data.payment_date[transactionIndex]});
return JSON.stringify({ status: data.payment_date[transactionIndex] });
} else {
return JSON.stringify({status: 'error - transaction id not found.'});
return JSON.stringify({ status: "error - transaction id not found." });
}
}

const namesToFunctions = {
retrievePaymentStatus: (transactionId) =>
retrievePaymentStatus({data, ...transactionId}),
retrievePaymentStatus({ data, ...transactionId }),
retrievePaymentDate: (transactionId) =>
retrievePaymentDate({data, ...transactionId}),
retrievePaymentDate({ data, ...transactionId }),
};

const tools = [
{
type: 'function',
type: "function",
function: {
name: 'retrievePaymentStatus',
description: 'Get payment status of a transaction id',
name: "retrievePaymentStatus",
description: "Get payment status of a transaction id",
parameters: {
type: 'object',
required: ['transactionId'],
properties: {transactionId:
{type: 'string', description: 'The transaction id.'},
type: "object",
required: ["transactionId"],
properties: {
transactionId: { type: "string", description: "The transaction id." },
},
},
},
},
{
type: 'function',
type: "function",
function: {
name: 'retrievePaymentDate',
description: 'Get payment date of a transaction id',
name: "retrievePaymentDate",
description: "Get payment date of a transaction id",
parameters: {
type: 'object',
required: ['transactionId'],
properties: {transactionId:
{type: 'string', description: 'The transaction id.'},
type: "object",
required: ["transactionId"],
properties: {
transactionId: { type: "string", description: "The transaction id." },
},
},
},
},
];

const model = "mistral-small-latest";

const model = 'mistral-large';

const client = new MistralClient(apiKey, 'https://api-2.aurocloud.net');
const client = new MistralClient(apiKey);

const messages = [
{role: 'user', content: 'What\'s the status of my transaction?'},
{ role: "user", content: "What's the status of my transaction?" },
];

let response = await client.chat({
model: model, messages: messages, tools: tools,
model: model,
messages: messages,
tools: tools,
});


console.log(response.choices[0].message.content);

messages.push(
{role: 'assistant', content: response.choices[0].message.content},
);
messages.push({role: 'user', content: 'My transaction ID is T1001.'});
messages.push({
role: "assistant",
content: response.choices[0].message.content,
});
messages.push({ role: "user", content: "My transaction ID is T1001." });

response = await client.chat({model: model, messages: messages, tools: tools});
response = await client.chat({
model: model,
messages: messages,
tools: tools,
});

const toolCall = response.choices[0].message.toolCalls[0];
const functionName = toolCall.function.name;
Expand All @@ -115,8 +124,17 @@ console.log(`functionParams: ${toolCall.function.arguments}`);
const functionResult = namesToFunctions[functionName](functionParams);

messages.push(response.choices[0].message);
messages.push({role: 'tool', name: functionName, content: functionResult});
messages.push({
role: "tool",
name: functionName,
content: functionResult,
tol_call_id: toolCall.id,
});

response = await client.chat({model: model, messages: messages, tools: tools});
response = await client.chat({
model: model,
messages: messages,
tools: tools,
});

console.log(response.choices[0].message.content);
11 changes: 8 additions & 3 deletions src/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,15 @@ declare module "@mistralai/mistralai" {
usage: TokenUsage;
}

export interface Message {
role: string;
export type Message = {
role: "system" | "user" | "assistant";
content: string | string[];
}
} & {
role: "tool";
content: string | string[];
name: string;
tool_call_id: string;
};

export interface Tool {
type: "function";
Expand Down

0 comments on commit 133146d

Please sign in to comment.