@mistralai/mistralai 0.1.3 → 0.3.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 CHANGED
@@ -4,6 +4,7 @@ env:
4
4
  extends: google
5
5
  ignorePatterns:
6
6
  - examples/chat-react/
7
+ - src/client.d.ts
7
8
  parserOptions:
8
9
  ecmaVersion: latest
9
10
  sourceType: module
@@ -19,7 +19,7 @@ jobs:
19
19
 
20
20
  strategy:
21
21
  matrix:
22
- node-version: [18, 20]
22
+ node-version: [18, 20, 22]
23
23
 
24
24
  steps:
25
25
  # Checkout the repository
@@ -47,6 +47,13 @@ jobs:
47
47
  run: |
48
48
  npm run test
49
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
+
50
57
  publish:
51
58
  needs: lint_and_test
52
59
  runs-on: ubuntu-latest
@@ -67,68 +74,4 @@ jobs:
67
74
  - name: Publish
68
75
  run: |
69
76
  echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" >> .npmrc
70
- npm version ${{ github.ref_name }}
71
- sed -i 's/VERSION = '\''0.0.1'\''/VERSION = '\''${{ github.ref_name }}'\''/g' src/client.js
72
77
  npm publish
73
-
74
- create_pr_on_public:
75
- if: startsWith(github.ref, 'refs/tags')
76
- runs-on: ubuntu-latest
77
- needs: lint_and_test
78
- steps:
79
- - name: Checkout
80
- uses: actions/checkout@v4
81
- with:
82
- token: ${{ secrets.PUBLIC_CLIENT_WRITE_TOKEN }}
83
-
84
- - name: Pull public updates
85
- env: # We cannot use the github bot token to push to the public repo, we have to use one with more permissions
86
- GITHUB_TOKEN: ${{ secrets.PUBLIC_CLIENT_WRITE_TOKEN }}
87
- run: |
88
-
89
- set -x
90
- git config --global user.name "GitHub Actions"
91
- git config --global user.email "mayo@mistral.ai"
92
-
93
- git remote add public https://github.com/mistralai/client-js.git
94
- git remote update
95
-
96
- # Create a diff of the changes, ignoring the ci workflow
97
- git merge public/main --no-commit --no-ff --no-edit --allow-unrelated-histories --strategy-option ours
98
-
99
- # If there are changes, commit them
100
- if ! git diff --quiet; then
101
- git commit -m "Update from public repo"
102
- git push origin ${{github.ref}}
103
- else
104
- echo "No changes to apply"
105
- fi
106
-
107
- - name: Push to public repo
108
- env:
109
- GITHUB_TOKEN: ${{ secrets.PUBLIC_CLIENT_WRITE_TOKEN }}
110
- run: |
111
- git checkout public/main
112
- git checkout -b update/${{github.ref_name}}
113
-
114
- # write version number to version file
115
- echo ${{github.ref_name}} > version.txt
116
-
117
- git add .
118
- git commit -m "Bump version file"
119
-
120
- # create a diff of this ref and the public repo
121
- git diff update/${{github.ref_name}} ${{github.ref_name}} --binary -- . ':!.github' > changes.diff
122
-
123
- # apply the diff to the current branch
124
- git apply changes.diff
125
-
126
- # commit the changes
127
- git add .
128
- git commit -m "Update version to ${{github.ref_name}}"
129
-
130
- # push the changes
131
- git push public update/${{github.ref_name}}
132
-
133
- # Create a PR from this branch to the public repo
134
- gh pr create --title "Update client to ${{github.ref_name}}" --body "This PR was automatically created by a GitHub Action" --base main --head update/${{github.ref_name}} --repo mistralai/client-js
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
- This javascript client is inspired from [cohere-typescript](https://github.com/cohere-ai/cohere-typescript)
1
+ This JavaScript client is inspired from [cohere-typescript](https://github.com/cohere-ai/cohere-typescript)
2
2
 
3
- # Mistral Javascript Client
3
+ # Mistral JavaScript Client
4
4
 
5
- You can use the Mistral Javascript client to interact with the Mistral AI API.
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 = await client.chatStream({
7
+ const chatStreamResponse = client.chatStream({
8
8
  model: 'mistral-tiny',
9
9
  messages: [{role: 'user', content: 'What is the best French cheese?'}],
10
10
  });
@@ -6,9 +6,13 @@ const apiKey = process.env.MISTRAL_API_KEY;
6
6
  const data = {
7
7
  transactionId: ['T1001', 'T1002', 'T1003', 'T1004', 'T1005'],
8
8
  customerId: ['C001', 'C002', 'C003', 'C002', 'C001'],
9
- paymentAmount: [125.50, 89.99, 120.00, 54.30, 210.20],
9
+ paymentAmount: [125.5, 89.99, 120.0, 54.3, 210.2],
10
10
  paymentDate: [
11
- '2021-10-05', '2021-10-06', '2021-10-07', '2021-10-05', '2021-10-08',
11
+ '2021-10-05',
12
+ '2021-10-06',
13
+ '2021-10-07',
14
+ '2021-10-05',
15
+ '2021-10-08',
12
16
  ],
13
17
  paymentStatus: ['Paid', 'Unpaid', 'Paid', 'Paid', 'Pending'],
14
18
  };
@@ -22,7 +26,7 @@ const data = {
22
26
  function retrievePaymentStatus({data, transactionId}) {
23
27
  const transactionIndex = data.transactionId.indexOf(transactionId);
24
28
  if (transactionIndex != -1) {
25
- return JSON.stringify({status: data.payment_status[transactionIndex]});
29
+ return JSON.stringify({status: data.paymentStatus[transactionIndex]});
26
30
  } else {
27
31
  return JSON.stringify({status: 'error - transaction id not found.'});
28
32
  }
@@ -60,8 +64,8 @@ const tools = [
60
64
  parameters: {
61
65
  type: 'object',
62
66
  required: ['transactionId'],
63
- properties: {transactionId:
64
- {type: 'string', description: 'The transaction id.'},
67
+ properties: {
68
+ transactionId: {type: 'string', description: 'The transaction id.'},
65
69
  },
66
70
  },
67
71
  },
@@ -74,38 +78,43 @@ const tools = [
74
78
  parameters: {
75
79
  type: 'object',
76
80
  required: ['transactionId'],
77
- properties: {transactionId:
78
- {type: 'string', description: 'The transaction id.'},
81
+ properties: {
82
+ transactionId: {type: 'string', description: 'The transaction id.'},
79
83
  },
80
84
  },
81
85
  },
82
86
  },
83
87
  ];
84
88
 
89
+ const model = 'mistral-small-latest';
85
90
 
86
- const model = 'mistral-large';
87
-
88
- const client = new MistralClient(apiKey, 'https://api-2.aurocloud.net');
91
+ const client = new MistralClient(apiKey);
89
92
 
90
93
  const messages = [
91
94
  {role: 'user', content: 'What\'s the status of my transaction?'},
92
95
  ];
93
96
 
94
97
  let response = await client.chat({
95
- model: model, messages: messages, tools: tools,
98
+ model: model,
99
+ messages: messages,
100
+ tools: tools,
96
101
  });
97
102
 
98
-
99
103
  console.log(response.choices[0].message.content);
100
104
 
101
- messages.push(
102
- {role: 'assistant', content: response.choices[0].message.content},
103
- );
105
+ messages.push({
106
+ role: 'assistant',
107
+ content: response.choices[0].message.content,
108
+ });
104
109
  messages.push({role: 'user', content: 'My transaction ID is T1001.'});
105
110
 
106
- response = await client.chat({model: model, messages: messages, tools: tools});
111
+ response = await client.chat({
112
+ model: model,
113
+ messages: messages,
114
+ tools: tools,
115
+ });
107
116
 
108
- const toolCall = response.choices[0].message.toolCalls[0];
117
+ const toolCall = response.choices[0].message.tool_calls[0];
109
118
  const functionName = toolCall.function.name;
110
119
  const functionParams = JSON.parse(toolCall.function.arguments);
111
120
 
@@ -115,8 +124,17 @@ console.log(`functionParams: ${toolCall.function.arguments}`);
115
124
  const functionResult = namesToFunctions[functionName](functionParams);
116
125
 
117
126
  messages.push(response.choices[0].message);
118
- messages.push({role: 'tool', name: functionName, content: functionResult});
127
+ messages.push({
128
+ role: 'tool',
129
+ name: functionName,
130
+ content: functionResult,
131
+ tool_call_id: toolCall.id,
132
+ });
119
133
 
120
- response = await client.chat({model: model, messages: messages, tools: tools});
134
+ response = await client.chat({
135
+ model: model,
136
+ messages: messages,
137
+ tools: tools,
138
+ });
121
139
 
122
140
  console.log(response.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
+ }