fix: Update mistral package for mistral API 1.5.1 (#1741)

This commit is contained in:
Marcus Schiesser
2025-03-12 11:23:42 +07:00
committed by GitHub
parent a654f580cf
commit 387a19284d
5 changed files with 50 additions and 24 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@llamaindex/mistral": patch
---
Update mistral package for mistral API 1.5.1
+1 -1
View File
@@ -35,6 +35,6 @@
"dependencies": {
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*",
"@mistralai/mistralai": "^1.3.4"
"@mistralai/mistralai": "^1.5.1"
}
}
+3 -3
View File
@@ -19,12 +19,12 @@ export class MistralAIEmbedding extends BaseEmbedding {
private async getMistralAIEmbedding(input: string) {
const client = await this.session.getClient();
const { data } = await client.embeddings({
const { data } = await client.embeddings.create({
model: this.model,
input: [input],
inputs: [input],
});
return data[0].embedding;
return data[0]?.embedding ?? [];
}
async getTextEmbedding(text: string): Promise<number[]> {
+36 -15
View File
@@ -7,6 +7,8 @@ import {
type LLMChatParamsStreaming,
} from "@llamaindex/core/llms";
import { getEnv } from "@llamaindex/env";
import { type Mistral } from "@mistralai/mistralai";
import type { ContentChunk } from "@mistralai/mistralai/models/components";
export const ALL_AVAILABLE_MISTRAL_MODELS = {
"mistral-tiny": { contextWindow: 32000 },
@@ -30,7 +32,7 @@ export class MistralAISession {
}
}
async getClient() {
async getClient(): Promise<Mistral> {
const { Mistral } = await import("@mistralai/mistralai");
if (!this.client) {
this.client = new Mistral({
@@ -105,11 +107,21 @@ export class MistralAI extends BaseLLM {
}
// Non-streaming
const client = await this.session.getClient();
const response = await client.chat(this.buildParams(messages));
const message = response.choices[0].message;
const response = await client.chat.complete(this.buildParams(messages));
if (!response || !response.choices || !response.choices[0]) {
throw new Error("Unexpected response format from Mistral API");
}
// Extract the content from the message response
const content = response.choices[0].message.content;
return {
raw: response,
message,
message: {
role: "assistant",
content: this.extractContentAsString(content),
},
};
}
@@ -117,23 +129,32 @@ export class MistralAI extends BaseLLM {
messages,
}: LLMChatParamsStreaming): AsyncIterable<ChatResponseChunk> {
const client = await this.session.getClient();
const chunkStream = await client.chatStream(this.buildParams(messages));
const chunkStream = await client.chat.stream(this.buildParams(messages));
//Indices
let idx_counter: number = 0;
for await (const part of chunkStream) {
if (!part.choices.length) continue;
for await (const chunk of chunkStream) {
if (!chunk.data || !chunk.data.choices || !chunk.data.choices.length)
continue;
part.choices[0].index = idx_counter;
idx_counter++;
const choice = chunk.data.choices[0];
if (!choice) continue;
yield {
raw: part,
delta: part.choices[0].delta.content ?? "",
raw: chunk.data,
delta: this.extractContentAsString(choice.delta.content),
};
}
return;
}
private extractContentAsString(
content: string | ContentChunk[] | null | undefined,
): string {
if (Array.isArray(content)) {
return content
.map((chunk) => (chunk.type === "text" ? chunk.text : undefined))
.filter(Boolean)
.join("");
}
return content ?? "";
}
}
+5 -5
View File
@@ -1260,8 +1260,8 @@ importers:
specifier: workspace:*
version: link:../../env
'@mistralai/mistralai':
specifier: ^1.3.4
version: 1.5.0(zod@3.24.2)
specifier: ^1.5.1
version: 1.5.1(zod@3.24.2)
devDependencies:
bunchee:
specifier: 6.4.0
@@ -3503,8 +3503,8 @@ packages:
'@mdx-js/mdx@3.1.0':
resolution: {integrity: sha512-/QxEhPAvGwbQmy1Px8F899L5Uc2KZ6JtXwlCgJmjSTBedwOZkByYcBG4GceIGPXRDsmfxhHazuS+hlOShRLeDw==}
'@mistralai/mistralai@1.5.0':
resolution: {integrity: sha512-AIn8pwAwA/fDvEUvmkt+40zH1ZmfaG3Q7oUWl17GUEC1tU7ZPwYz8Cv9P59lyS1SisHdDSu81oknO7f1ywkz8Q==}
'@mistralai/mistralai@1.5.1':
resolution: {integrity: sha512-Ie0EH4dAO11MEXR5N2kS2cgr+ycTWvqN/yP9bKrtmUEqjdcF4i7DLxtrFMUw5l2dOPhrkX93G4SziFiATPWu2w==}
peerDependencies:
zod: '>= 3'
@@ -14292,7 +14292,7 @@ snapshots:
- acorn
- supports-color
'@mistralai/mistralai@1.5.0(zod@3.24.2)':
'@mistralai/mistralai@1.5.1(zod@3.24.2)':
dependencies:
zod: 3.24.2
zod-to-json-schema: 3.24.1(zod@3.24.2)