-
Notifications
You must be signed in to change notification settings - Fork 331
/
mistral.ts
146 lines (129 loc) · 3.86 KB
/
mistral.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { getEnv } from "@llamaindex/env";
import { Settings } from "../Settings.js";
import { type StreamCallbackResponse } from "../callbacks/CallbackManager.js";
import { BaseLLM } from "./base.js";
import type {
ChatMessage,
ChatResponse,
ChatResponseChunk,
LLMChatParamsNonStreaming,
LLMChatParamsStreaming,
} from "./types.js";
export const ALL_AVAILABLE_MISTRAL_MODELS = {
"mistral-tiny": { contextWindow: 32000 },
"mistral-small": { contextWindow: 32000 },
"mistral-medium": { contextWindow: 32000 },
};
export class MistralAISession {
apiKey?: string;
private client: any;
constructor(init?: Partial<MistralAISession>) {
if (init?.apiKey) {
this.apiKey = init?.apiKey;
} else {
this.apiKey = getEnv("MISTRAL_API_KEY");
}
if (!this.apiKey) {
throw new Error("Set Mistral API key in MISTRAL_API_KEY env variable"); // Overriding MistralAI package's error message
}
}
async getClient() {
const { default: MistralClient } = await import("@mistralai/mistralai");
if (!this.client) {
this.client = new MistralClient(this.apiKey);
}
return this.client;
}
}
/**
* MistralAI LLM implementation
*/
export class MistralAI extends BaseLLM {
// Per completion MistralAI params
model: keyof typeof ALL_AVAILABLE_MISTRAL_MODELS;
temperature: number;
topP: number;
maxTokens?: number;
apiKey?: string;
safeMode: boolean;
randomSeed?: number;
private session: MistralAISession;
constructor(init?: Partial<MistralAI>) {
super();
this.model = init?.model ?? "mistral-small";
this.temperature = init?.temperature ?? 0.1;
this.topP = init?.topP ?? 1;
this.maxTokens = init?.maxTokens ?? undefined;
this.safeMode = init?.safeMode ?? false;
this.randomSeed = init?.randomSeed ?? undefined;
this.session = new MistralAISession(init);
}
get metadata() {
return {
model: this.model,
temperature: this.temperature,
topP: this.topP,
maxTokens: this.maxTokens,
contextWindow: ALL_AVAILABLE_MISTRAL_MODELS[this.model].contextWindow,
tokenizer: undefined,
};
}
private buildParams(messages: ChatMessage[]): any {
return {
model: this.model,
temperature: this.temperature,
maxTokens: this.maxTokens,
topP: this.topP,
safeMode: this.safeMode,
randomSeed: this.randomSeed,
messages,
};
}
chat(
params: LLMChatParamsStreaming,
): Promise<AsyncIterable<ChatResponseChunk>>;
chat(params: LLMChatParamsNonStreaming): Promise<ChatResponse>;
async chat(
params: LLMChatParamsNonStreaming | LLMChatParamsStreaming,
): Promise<ChatResponse | AsyncIterable<ChatResponseChunk>> {
const { messages, stream } = params;
// Streaming
if (stream) {
return this.streamChat(params);
}
// Non-streaming
const client = await this.session.getClient();
const response = await client.chat(this.buildParams(messages));
const message = response.choices[0].message;
return {
raw: response,
message,
};
}
protected async *streamChat({
messages,
}: LLMChatParamsStreaming): AsyncIterable<ChatResponseChunk> {
const client = await this.session.getClient();
const chunkStream = await client.chatStream(this.buildParams(messages));
//Indices
let idx_counter: number = 0;
for await (const part of chunkStream) {
if (!part.choices.length) continue;
part.choices[0].index = idx_counter;
const isDone: boolean =
part.choices[0].finish_reason === "stop" ? true : false;
const stream_callback: StreamCallbackResponse = {
index: idx_counter,
isDone: isDone,
token: part,
};
Settings.callbackManager.dispatchEvent("stream", stream_callback);
idx_counter++;
yield {
raw: part,
delta: part.choices[0].delta.content ?? "",
};
}
return;
}
}