This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
GitHub Actions
committed
Feb 26, 2024
1 parent
18fbf66
commit 72ab604
Showing
6 changed files
with
226 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,4 +127,6 @@ dist | |
.yarn/unplugged | ||
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
.pnp.* | ||
|
||
changes.diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import MistralClient from '@mistralai/mistralai'; | ||
|
||
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], | ||
paymentDate: [ | ||
'2021-10-05', '2021-10-06', '2021-10-07', '2021-10-05', '2021-10-08', | ||
], | ||
paymentStatus: ['Paid', 'Unpaid', 'Paid', 'Paid', 'Pending'], | ||
}; | ||
|
||
/** | ||
* This function retrieves the payment status of a transaction id. | ||
* @param {object} data - The data object. | ||
* @param {string} transactionId - The transaction id. | ||
* @return {string} - The payment status. | ||
*/ | ||
function retrievePaymentStatus({data, transactionId}) { | ||
const transactionIndex = data.transactionId.indexOf(transactionId); | ||
if (transactionIndex != -1) { | ||
return JSON.stringify({status: data.payment_status[transactionIndex]}); | ||
} else { | ||
return JSON.stringify({status: 'error - transaction id not found.'}); | ||
} | ||
} | ||
|
||
/** | ||
* This function retrieves the payment date of a transaction id. | ||
* @param {object} data - The data object. | ||
* @param {string} transactionId - The transaction id. | ||
* @return {string} - The payment date. | ||
* | ||
*/ | ||
function retrievePaymentDate({data, transactionId}) { | ||
const transactionIndex = data.transactionId.indexOf(transactionId); | ||
if (transactionIndex != -1) { | ||
return JSON.stringify({status: data.payment_date[transactionIndex]}); | ||
} else { | ||
return JSON.stringify({status: 'error - transaction id not found.'}); | ||
} | ||
} | ||
|
||
const namesToFunctions = { | ||
retrievePaymentStatus: (transactionId) => | ||
retrievePaymentStatus({data, ...transactionId}), | ||
retrievePaymentDate: (transactionId) => | ||
retrievePaymentDate({data, ...transactionId}), | ||
}; | ||
|
||
const tools = [ | ||
{ | ||
type: 'function', | ||
function: { | ||
name: 'retrievePaymentStatus', | ||
description: 'Get payment status of a transaction id', | ||
parameters: { | ||
type: 'object', | ||
required: ['transactionId'], | ||
properties: {transactionId: | ||
{type: 'string', description: 'The transaction id.'}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
type: 'function', | ||
function: { | ||
name: 'retrievePaymentDate', | ||
description: 'Get payment date of a transaction id', | ||
parameters: { | ||
type: 'object', | ||
required: ['transactionId'], | ||
properties: {transactionId: | ||
{type: 'string', description: 'The transaction id.'}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
|
||
const model = 'mistral-large'; | ||
|
||
const client = new MistralClient(apiKey, 'https://api-2.aurocloud.net'); | ||
|
||
const messages = [ | ||
{role: 'user', content: 'What\'s the status of my transaction?'}, | ||
]; | ||
|
||
let response = await client.chat({ | ||
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.'}); | ||
|
||
response = await client.chat({model: model, messages: messages, tools: tools}); | ||
|
||
const toolCall = response.choices[0].message.toolCalls[0]; | ||
const functionName = toolCall.function.name; | ||
const functionParams = JSON.parse(toolCall.function.arguments); | ||
|
||
console.log(`calling functionName: ${functionName}`); | ||
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}); | ||
|
||
response = await client.chat({model: model, messages: messages, tools: tools}); | ||
|
||
console.log(response.choices[0].message.content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import MistralClient from '@mistralai/mistralai'; | ||
|
||
const apiKey = process.env.MISTRAL_API_KEY; | ||
|
||
const client = new MistralClient(apiKey); | ||
|
||
const chatResponse = await client.chat({ | ||
model: 'mistral-large', | ||
messages: [{role: 'user', content: 'What is the best French cheese?'}], | ||
responseFormat: {type: 'json_object'}, | ||
}); | ||
|
||
console.log('Chat:', chatResponse.choices[0].message.content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.