@mistralai/mistralai 0.0.10 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.yml +1 -0
- package/.github/workflows/build_publish.yaml +10 -8
- package/README.md +5 -3
- package/examples/chat_with_streaming.js +1 -1
- package/examples/function_calling.js +122 -0
- package/examples/json_format.js +13 -0
- package/examples/typescript/chat_with_streaming.ts +24 -0
- package/examples/typescript/package-lock.json +480 -0
- package/examples/typescript/package.json +11 -0
- package/examples/typescript/tsconfig.json +10 -0
- package/package.json +2 -2
- package/src/client.d.ts +172 -147
- package/src/client.js +162 -67
- package/tests/client.test.js +9 -9
package/.eslintrc.yml
CHANGED
|
@@ -3,7 +3,7 @@ name: Build and Publish
|
|
|
3
3
|
on:
|
|
4
4
|
push:
|
|
5
5
|
branches: ["main"]
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
# We only deploy on tags and main branch
|
|
8
8
|
tags:
|
|
9
9
|
# Only run on tags that match the following regex
|
|
@@ -14,13 +14,12 @@ on:
|
|
|
14
14
|
pull_request:
|
|
15
15
|
|
|
16
16
|
jobs:
|
|
17
|
-
|
|
18
17
|
lint_and_test:
|
|
19
18
|
runs-on: ubuntu-latest
|
|
20
19
|
|
|
21
20
|
strategy:
|
|
22
21
|
matrix:
|
|
23
|
-
node-version: [18, 20]
|
|
22
|
+
node-version: [18, 20, 22]
|
|
24
23
|
|
|
25
24
|
steps:
|
|
26
25
|
# Checkout the repository
|
|
@@ -48,6 +47,13 @@ jobs:
|
|
|
48
47
|
run: |
|
|
49
48
|
npm run test
|
|
50
49
|
|
|
50
|
+
# Build TypeScript Examples
|
|
51
|
+
- name: Build typescript examples
|
|
52
|
+
run: |
|
|
53
|
+
cd examples/typescript
|
|
54
|
+
npm install
|
|
55
|
+
npx tsc --build --verbose tsconfig.json
|
|
56
|
+
|
|
51
57
|
publish:
|
|
52
58
|
needs: lint_and_test
|
|
53
59
|
runs-on: ubuntu-latest
|
|
@@ -68,8 +74,4 @@ jobs:
|
|
|
68
74
|
- name: Publish
|
|
69
75
|
run: |
|
|
70
76
|
echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" >> .npmrc
|
|
71
|
-
npm
|
|
72
|
-
sed -i 's/VERSION = '\''0.0.1'\''/VERSION = '\''${{ github.ref_name }}'\''/g' src/client.js
|
|
73
|
-
npm publish
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
npm publish
|
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
This
|
|
1
|
+
This JavaScript client is inspired from [cohere-typescript](https://github.com/cohere-ai/cohere-typescript)
|
|
2
2
|
|
|
3
|
-
# Mistral
|
|
3
|
+
# Mistral JavaScript Client
|
|
4
4
|
|
|
5
|
-
You can use the Mistral
|
|
5
|
+
You can use the Mistral JavaScript client to interact with the Mistral AI API.
|
|
6
6
|
|
|
7
7
|
## Installing
|
|
8
8
|
|
|
@@ -12,6 +12,8 @@ You can install the library in your project using:
|
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
|
+
You can watch a free course on using the Mistral JavaScript client [here.](https://scrimba.com/links/mistral)
|
|
16
|
+
|
|
15
17
|
### Set up
|
|
16
18
|
|
|
17
19
|
```typescript
|
|
@@ -4,7 +4,7 @@ const apiKey = process.env.MISTRAL_API_KEY;
|
|
|
4
4
|
|
|
5
5
|
const client = new MistralClient(apiKey);
|
|
6
6
|
|
|
7
|
-
const chatStreamResponse =
|
|
7
|
+
const chatStreamResponse = client.chatStream({
|
|
8
8
|
model: 'mistral-tiny',
|
|
9
9
|
messages: [{role: 'user', content: 'What is the best French cheese?'}],
|
|
10
10
|
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import MistralClient from '@mistralai/mistralai';
|
|
2
|
+
|
|
3
|
+
const apiKey = process.env.MISTRAL_API_KEY;
|
|
4
|
+
|
|
5
|
+
// Assuming we have the following data
|
|
6
|
+
const data = {
|
|
7
|
+
transactionId: ['T1001', 'T1002', 'T1003', 'T1004', 'T1005'],
|
|
8
|
+
customerId: ['C001', 'C002', 'C003', 'C002', 'C001'],
|
|
9
|
+
paymentAmount: [125.50, 89.99, 120.00, 54.30, 210.20],
|
|
10
|
+
paymentDate: [
|
|
11
|
+
'2021-10-05', '2021-10-06', '2021-10-07', '2021-10-05', '2021-10-08',
|
|
12
|
+
],
|
|
13
|
+
paymentStatus: ['Paid', 'Unpaid', 'Paid', 'Paid', 'Pending'],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This function retrieves the payment status of a transaction id.
|
|
18
|
+
* @param {object} data - The data object.
|
|
19
|
+
* @param {string} transactionId - The transaction id.
|
|
20
|
+
* @return {string} - The payment status.
|
|
21
|
+
*/
|
|
22
|
+
function retrievePaymentStatus({data, transactionId}) {
|
|
23
|
+
const transactionIndex = data.transactionId.indexOf(transactionId);
|
|
24
|
+
if (transactionIndex != -1) {
|
|
25
|
+
return JSON.stringify({status: data.payment_status[transactionIndex]});
|
|
26
|
+
} else {
|
|
27
|
+
return JSON.stringify({status: 'error - transaction id not found.'});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* This function retrieves the payment date of a transaction id.
|
|
33
|
+
* @param {object} data - The data object.
|
|
34
|
+
* @param {string} transactionId - The transaction id.
|
|
35
|
+
* @return {string} - The payment date.
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
function retrievePaymentDate({data, transactionId}) {
|
|
39
|
+
const transactionIndex = data.transactionId.indexOf(transactionId);
|
|
40
|
+
if (transactionIndex != -1) {
|
|
41
|
+
return JSON.stringify({status: data.payment_date[transactionIndex]});
|
|
42
|
+
} else {
|
|
43
|
+
return JSON.stringify({status: 'error - transaction id not found.'});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const namesToFunctions = {
|
|
48
|
+
retrievePaymentStatus: (transactionId) =>
|
|
49
|
+
retrievePaymentStatus({data, ...transactionId}),
|
|
50
|
+
retrievePaymentDate: (transactionId) =>
|
|
51
|
+
retrievePaymentDate({data, ...transactionId}),
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const tools = [
|
|
55
|
+
{
|
|
56
|
+
type: 'function',
|
|
57
|
+
function: {
|
|
58
|
+
name: 'retrievePaymentStatus',
|
|
59
|
+
description: 'Get payment status of a transaction id',
|
|
60
|
+
parameters: {
|
|
61
|
+
type: 'object',
|
|
62
|
+
required: ['transactionId'],
|
|
63
|
+
properties: {transactionId:
|
|
64
|
+
{type: 'string', description: 'The transaction id.'},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
type: 'function',
|
|
71
|
+
function: {
|
|
72
|
+
name: 'retrievePaymentDate',
|
|
73
|
+
description: 'Get payment date of a transaction id',
|
|
74
|
+
parameters: {
|
|
75
|
+
type: 'object',
|
|
76
|
+
required: ['transactionId'],
|
|
77
|
+
properties: {transactionId:
|
|
78
|
+
{type: 'string', description: 'The transaction id.'},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
const model = 'mistral-large';
|
|
87
|
+
|
|
88
|
+
const client = new MistralClient(apiKey, 'https://api-2.aurocloud.net');
|
|
89
|
+
|
|
90
|
+
const messages = [
|
|
91
|
+
{role: 'user', content: 'What\'s the status of my transaction?'},
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
let response = await client.chat({
|
|
95
|
+
model: model, messages: messages, tools: tools,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
console.log(response.choices[0].message.content);
|
|
100
|
+
|
|
101
|
+
messages.push(
|
|
102
|
+
{role: 'assistant', content: response.choices[0].message.content},
|
|
103
|
+
);
|
|
104
|
+
messages.push({role: 'user', content: 'My transaction ID is T1001.'});
|
|
105
|
+
|
|
106
|
+
response = await client.chat({model: model, messages: messages, tools: tools});
|
|
107
|
+
|
|
108
|
+
const toolCall = response.choices[0].message.toolCalls[0];
|
|
109
|
+
const functionName = toolCall.function.name;
|
|
110
|
+
const functionParams = JSON.parse(toolCall.function.arguments);
|
|
111
|
+
|
|
112
|
+
console.log(`calling functionName: ${functionName}`);
|
|
113
|
+
console.log(`functionParams: ${toolCall.function.arguments}`);
|
|
114
|
+
|
|
115
|
+
const functionResult = namesToFunctions[functionName](functionParams);
|
|
116
|
+
|
|
117
|
+
messages.push(response.choices[0].message);
|
|
118
|
+
messages.push({role: 'tool', name: functionName, content: functionResult});
|
|
119
|
+
|
|
120
|
+
response = await client.chat({model: model, messages: messages, tools: tools});
|
|
121
|
+
|
|
122
|
+
console.log(response.choices[0].message.content);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import MistralClient from '@mistralai/mistralai';
|
|
2
|
+
|
|
3
|
+
const apiKey = process.env.MISTRAL_API_KEY;
|
|
4
|
+
|
|
5
|
+
const client = new MistralClient(apiKey);
|
|
6
|
+
|
|
7
|
+
const chatResponse = await client.chat({
|
|
8
|
+
model: 'mistral-large',
|
|
9
|
+
messages: [{role: 'user', content: 'What is the best French cheese?'}],
|
|
10
|
+
responseFormat: {type: 'json_object'},
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
console.log('Chat:', chatResponse.choices[0].message.content);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import MistralClient from '@mistralai/mistralai';
|
|
2
|
+
|
|
3
|
+
const apiKey = process.env.MISTRAL_API_KEY;
|
|
4
|
+
|
|
5
|
+
const client = new MistralClient(apiKey);
|
|
6
|
+
|
|
7
|
+
const responseInterface = '{"best": string, "reasoning": string}';
|
|
8
|
+
const chatStreamResponse = client.chatStream({
|
|
9
|
+
model: 'open-mistral-7b',
|
|
10
|
+
responseFormat: {type: 'json_object'},
|
|
11
|
+
messages: [{
|
|
12
|
+
role: 'user', content: `
|
|
13
|
+
What is the best French cheese?
|
|
14
|
+
Answer in ${responseInterface} format`,
|
|
15
|
+
}],
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
console.log('Chat Stream:');
|
|
19
|
+
for await (const chunk of chatStreamResponse) {
|
|
20
|
+
if (chunk.choices[0].delta.content !== undefined) {
|
|
21
|
+
const streamText = chunk.choices[0].delta.content;
|
|
22
|
+
process.stdout.write(streamText);
|
|
23
|
+
}
|
|
24
|
+
}
|