-
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 #6 from zya/deepinfra
Feature: Add support for deep infra
- Loading branch information
Showing
6 changed files
with
101 additions
and
6 deletions.
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,85 @@ | ||
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 getDeepInfraResponse( | ||
model: string, | ||
messages: Message[], | ||
baseUrl: string, | ||
stream: boolean, | ||
): Promise<Response> { | ||
return fetch(`${baseUrl}/v1/openai/chat/completions`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${process.env.DEEPINFRA_API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
messages, | ||
model, | ||
stream, | ||
}), | ||
}); | ||
} | ||
|
||
export async function DeepInfraHandler( | ||
params: HandlerParamsNotStreaming, | ||
): Promise<ResultNotStreaming>; | ||
|
||
export async function DeepInfraHandler( | ||
params: HandlerParamsStreaming, | ||
): Promise<ResultStreaming>; | ||
|
||
export async function DeepInfraHandler( | ||
params: HandlerParams, | ||
): Promise<ResultNotStreaming | ResultStreaming>; | ||
|
||
export async function DeepInfraHandler( | ||
params: HandlerParams, | ||
): Promise<ResultNotStreaming | ResultStreaming> { | ||
const baseUrl = params.baseUrl ?? 'https://api.deepinfra.com'; | ||
const model = params.model.split('deepinfra/')[1]; | ||
|
||
const res = await getDeepInfraResponse( | ||
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
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