From 0d550facc9d88b5a836545df94f835aa27b99e03 Mon Sep 17 00:00:00 2001 From: autumnlight Date: Tue, 4 Mar 2025 08:05:24 +0100 Subject: [PATCH 1/3] Fix typescript issue for batch file upload --- docs/capabilities/batch.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/capabilities/batch.md b/docs/capabilities/batch.md index 95ff53f1..03b0a8e6 100644 --- a/docs/capabilities/batch.md +++ b/docs/capabilities/batch.md @@ -55,7 +55,8 @@ const batchData = await client.files.upload({ file: { fileName: "batch_input_file.jsonl", content: batchFile, - } + }, + purpose: "batch" }); ``` From eec12ebbed001f11204fb04ac7a86acd9b31998b Mon Sep 17 00:00:00 2001 From: autumnlight Date: Tue, 4 Mar 2025 08:53:42 +0100 Subject: [PATCH 2/3] feat: add ts vision example for base64 --- docs/capabilities/vision.md | 61 ++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/docs/capabilities/vision.md b/docs/capabilities/vision.md index e0e96932..1f7e299c 100644 --- a/docs/capabilities/vision.md +++ b/docs/capabilities/vision.md @@ -13,7 +13,7 @@ If the image is hosted online, you can simply provide the URL of the image in th - + ```python @@ -117,6 +117,8 @@ curl https://api.mistral.ai/v1/chat/completions \ ## Passing a Base64 Encoded Image If you have an image or a set of images stored locally, you can pass them to the model in base64 encoded format. Base64 encoding is a common method for converting binary data into a text format that can be easily transmitted over the internet. This is particularly useful when you need to include images in API requests. + + ```py import base64 @@ -178,6 +180,63 @@ chat_response = client.chat.complete( print(chat_response.choices[0].message.content) ``` + + + +```typescript +import { Mistral } from "@mistralai/mistralai"; +import type { ChatCompletionRequest } from "@mistralai/mistralai/models/components"; +import fs from "fs"; +import path from "path"; + +// Retrieving the API key from our environment +const apiKey = process.env["MISTRAL_API_KEY"]; +// path to the image we want to encode to base64 +const imagePath = path.join(__dirname, "path_to_your_image.jpeg"); + +// Encoding the image to Base 64 +const base64 = base64_encode(imagePath); + +// Initializing a Mistral Client with our API Key +const client = new Mistral({ apiKey: apiKey }); + +// Specifying the model we want to use +const model = "pixtral-12b-2409"; + +function base64_encode(file: string) { + var bitmap = fs.readFileSync(file); + // convert binary data to base64 encoded string + return new Buffer(bitmap).toString("base64"); +} + +// Defining our Messages +const messages: ChatCompletionRequest["messages"] = [ + { + role: "user", + content: [ + { type: "text", text: "What's in this image?" }, + { + type: "image_url", + // Important here, we pass on "data:image/jpeg;base64," so that Mistral knows what we are encoding. + imageUrl: "data:image/jpeg;base64," + base64, + }, + ], + }, +]; + +// Getting the text response +const chatResponse = await client.chat.complete({ + model: model, + messages: messages, +}); + +// Logging the response +console.log("Response: ", chatResponse.choices?.[0].message.content); +``` + + + + ## Use cases
Understand charts From bd873d2a5296a99633aa77f97a09fbc08b9eb542 Mon Sep 17 00:00:00 2001 From: autumnlight Date: Tue, 4 Mar 2025 09:03:16 +0100 Subject: [PATCH 3/3] improve const --- docs/capabilities/vision.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/capabilities/vision.md b/docs/capabilities/vision.md index 1f7e299c..062c3de8 100644 --- a/docs/capabilities/vision.md +++ b/docs/capabilities/vision.md @@ -204,7 +204,7 @@ const client = new Mistral({ apiKey: apiKey }); const model = "pixtral-12b-2409"; function base64_encode(file: string) { - var bitmap = fs.readFileSync(file); + const bitmap = fs.readFileSync(file); // convert binary data to base64 encoded string return new Buffer(bitmap).toString("base64"); }