-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Leon
committed
Jan 26, 2024
1 parent
6258e78
commit 47933d9
Showing
3 changed files
with
52 additions
and
51 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,43 @@ | ||
import { log } from "./utils" | ||
|
||
export const request = async (endpoint: string, headers: Record<string, string>, body: any) => { | ||
log("sending completion request", JSON.stringify(body)) | ||
|
||
const response = await fetch(endpoint, { | ||
method: "POST", | ||
headers, | ||
body: JSON.stringify(body), | ||
}) | ||
|
||
if (!response.ok) { | ||
log("completion error", response.status, await response.text()) | ||
throw new Error("request error with status code " + response.status) | ||
} | ||
|
||
return response | ||
} | ||
|
||
export const standard = async (endpoint: string, headers: Record<string, string>, body: any) => { | ||
const response = await request(endpoint, headers, body) | ||
const data = await response.json() | ||
return data?.choices?.map(i => i.message?.content) | ||
} | ||
|
||
export const stream = async (endpoint: string, headers: Record<string, string>, body: any) => { | ||
const response = await request(endpoint, headers, body) | ||
const text = await response.text() | ||
|
||
const data = text.split('\n').map(i => i.slice(5)).map((i) => { | ||
try { | ||
return JSON.parse(i).choices[0] | ||
} catch (e) { return null } | ||
}).filter(i => i).reduce(function(r, a) { | ||
r[a.index] = r[a.index] || []; | ||
r[a.index].push(a); | ||
return r; | ||
}, Object.create(null)) | ||
|
||
return Object.values(data).map((i) => i.map(i => i.text).join('')) | ||
} | ||
|
||
export default { stream, standard } |