Skip to content

Commit f10ef43

Browse files
authored
chore(ci): run examples in the ci #132
2 parents 189638f + 75af60f commit f10ef43

12 files changed

Lines changed: 136 additions & 37 deletions
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Running the examples
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
lint:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v3
17+
18+
- name: Set up Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: '22'
22+
23+
- name: Install dependencies
24+
run: npm install && npm install --prefix examples && npm install tsx
25+
26+
- name: Build the project
27+
run: npm run build
28+
29+
- name: Run the example script
30+
run: ./scripts/run_examples.sh
31+
env:
32+
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}

examples/package-lock.json

Lines changed: 15 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/src/async_audio_transcription_stream.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ const stream = await mistral.audio.transcriptions.stream({
1313
});
1414

1515
for await (const event of stream) {
16-
process.stdout.write(event);
16+
process.stdout.write(JSON.stringify(event));
17+
process.stdout.write("\n");
1718
}

examples/src/async_chat_with_image_no_streaming.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ const chatResponse = await client.chat.complete({
1616
{ type: "text", text: "What's in this image?" },
1717
{
1818
type: "image_url",
19-
imageUrl: "https://tripfixers.com/wp-content/uploads/2019/11/eiffel-tower-with-snow.jpeg",
19+
imageUrl:
20+
"https://mistral.ai/_next/image?url=https%3A%2F%2Fcms.mistral.ai%2Fassets%2Fce1514ab-62f9-4825-a20b-298f2497c536&w=3840&q=75",
2021
},
21-
]
22-
}
22+
],
23+
},
2324
],
2425
});
2526

26-
27-
console.log('JSON:', chatResponse.choices[0].message.content)
27+
console.log("JSON:", chatResponse.choices[0].message.content);

examples/src/async_files.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import * as fs from "fs";
22
import { Mistral } from "@mistralai/mistralai";
3+
import path from 'path';
4+
import { fileURLToPath } from "url";
35

46
const apiKey = process.env["MISTRAL_API_KEY"];
57
if (!apiKey) {
68
throw new Error("missing MISTRAL_API_KEY environment variable");
79
}
810

11+
// Get the absolute directory path
12+
const __filename = fileURLToPath(import.meta.url);
13+
const __dirname = path.dirname(__filename);
14+
915
const client = new Mistral({ apiKey: apiKey });
1016

1117
// Create a new file
12-
const blob = new Blob([fs.readFileSync("./src/file.jsonl")], {
18+
const filePath = path.join(__dirname, "file.jsonl");
19+
const blob = new Blob([fs.readFileSync(filePath)], {
1320
type: "application/json",
1421
});
1522
const createdFile = await client.files.upload({ file: blob, });
16-
// const createdFile = await client.files.upload(
17-
// {
18-
// file: {
19-
// fileName: "./src/file.jsonl",
20-
// content: fs.readFileSync("./src/file.jsonl")
21-
// },
22-
// });
2323

2424
console.log(createdFile);
2525

examples/src/async_function_calling_streaming.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const tools = [
9090
},
9191
];
9292

93-
const model = "mistral-large-latest";
93+
const model = "mistral-small-latest";
9494

9595
const client = new Mistral({ apiKey: apiKey });
9696

examples/src/async_jobs.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as fs from "fs";
22
import { Mistral } from "@mistralai/mistralai";
3+
import path from 'path';
4+
import { fileURLToPath } from "url";
35

46
const apiKey = process.env["MISTRAL_API_KEY"];
57
if (!apiKey) {
@@ -8,8 +10,13 @@ if (!apiKey) {
810

911
const mistral = new Mistral();
1012

13+
// Get the absolute directory path
14+
const __filename = fileURLToPath(import.meta.url);
15+
const __dirname = path.dirname(__filename);
16+
const filePath = path.join(__dirname, "file.jsonl");
17+
1118
// Create a new file
12-
const blob = new Blob([fs.readFileSync("src/file.jsonl")], {
19+
const blob = new Blob([fs.readFileSync(filePath)], {
1320
type: "application/json",
1421
});
1522
const createdFile = await mistral.files.upload({ file: blob });

examples/src/async_json_format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (!apiKey) {
88
const mistral = new Mistral({ apiKey: apiKey });
99

1010
const chatResponse = await mistral.chat.complete({
11-
model: "mistral-large-latest",
11+
model: "mistral-small-latest",
1212
messages: [{ role: "user", content: "What is the best French cheese?" }],
1313
responseFormat: { type: "json_object" },
1414
});

examples/src/async_ocr_process_from_file.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { Mistral } from '@mistralai/mistralai';
22
import fs from 'fs';
3+
import path from 'path';
4+
import { fileURLToPath } from "url";
35

46
const apiKey = process.env.MISTRAL_API_KEY;
57

68
const client = new Mistral({ apiKey: apiKey });
79

810

9-
const file_path = "/path/to/uploaded_file.pdf"
10-
const uploaded_file = fs.readFileSync(file_path);
11+
// Get the absolute directory path
12+
const __filename = fileURLToPath(import.meta.url);
13+
const __dirname = path.dirname(__filename);
14+
const filePath = path.join(__dirname, "ocr.pdf")
15+
16+
// Upload the file and run ocr
17+
const uploaded_file = fs.readFileSync(filePath);
1118
const uploaded_pdf = await client.files.upload({
1219
file: {
1320
fileName: "uploaded_file.pdf",

examples/src/error_handling.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {Mistral} from "@mistralai/mistralai";
2+
import {SDKError} from "../../models/errors/index.js";
3+
4+
const apiKey = process.env["MISTRAL_API_KEY"];
5+
if (!apiKey) {
6+
throw new Error("missing MISTRAL_API_KEY environment variable");
7+
}
8+
9+
const client = new Mistral({apiKey: apiKey});
10+
11+
try{
12+
const chatResponse = await client.chat.complete({
13+
model: "pixtral-12b",
14+
messages: [
15+
{
16+
role: "user",
17+
content: "hello"
18+
},
19+
{
20+
role: "assistant",
21+
content: "hey"
22+
}
23+
],
24+
});
25+
} catch (e){
26+
if (e instanceof SDKError) {
27+
process.stdout.write(e.message)
28+
} else {
29+
throw e
30+
}
31+
}

0 commit comments

Comments
 (0)