-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from zya/add-mistral
Feature: Add support for Mistral API
- Loading branch information
Showing
5 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { ChatCompletion } from 'openai/resources/chat'; | ||
|
||
import { | ||
HandlerParams, | ||
HandlerParamsNotStreaming, | ||
HandlerParamsStreaming, | ||
Message, | ||
ResultNotStreaming, | ||
ResultStreaming, | ||
StreamingChunk, | ||
} from '../types'; | ||
|
||
async function* iterateResponse( | ||
response: Response, | ||
): AsyncIterable<StreamingChunk> { | ||
const reader = response.body?.getReader(); | ||
let done = false; | ||
|
||
while (!done) { | ||
const next = await reader?.read(); | ||
if (next?.value) { | ||
done = next.done; | ||
const decoded = new TextDecoder().decode(next.value); | ||
if (decoded.startsWith('data: [DONE]')) { | ||
done = true; | ||
} else { | ||
const [, value] = decoded.split('data: '); | ||
yield JSON.parse(value); | ||
} | ||
} else { | ||
done = true; | ||
} | ||
} | ||
} | ||
|
||
async function getMistralResponse( | ||
model: string, | ||
messages: Message[], | ||
baseUrl: string, | ||
stream: boolean, | ||
): Promise<Response> { | ||
return fetch(`${baseUrl}/v1/chat/completions`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${process.env.MISTRAL_API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
messages, | ||
model, | ||
stream, | ||
}), | ||
}); | ||
} | ||
|
||
export async function MistralHandler( | ||
params: HandlerParamsNotStreaming, | ||
): Promise<ResultNotStreaming>; | ||
|
||
export async function MistralHandler( | ||
params: HandlerParamsStreaming, | ||
): Promise<ResultStreaming>; | ||
|
||
export async function MistralHandler( | ||
params: HandlerParams, | ||
): Promise<ResultNotStreaming | ResultStreaming>; | ||
|
||
export async function MistralHandler( | ||
params: HandlerParams, | ||
): Promise<ResultNotStreaming | ResultStreaming> { | ||
const baseUrl = params.baseUrl ?? 'https://api.mistral.ai'; | ||
const model = params.model.split('mistral/')[1]; | ||
|
||
const res = await getMistralResponse( | ||
model, | ||
params.messages, | ||
baseUrl, | ||
params.stream ?? false, | ||
); | ||
|
||
if (params.stream) { | ||
return iterateResponse(res); | ||
} | ||
|
||
return res.json() as Promise<ChatCompletion>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters