-
Notifications
You must be signed in to change notification settings - Fork 29
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
Showing
11 changed files
with
207 additions
and
77 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,42 @@ | ||
import { OpenAIStream, StreamingTextResponse } from "ai"; | ||
import { ChatCompletionCreateParamsBase, OpenAI } from "openai"; | ||
|
||
export async function queryOpenAI(parameters: { | ||
messages: ChatCompletionCreateParamsBase["messages"]; | ||
functions?: ChatCompletionCreateParamsBase["functions"]; | ||
function_key?: ChatCompletionCreateParamsBase["function_key"]; | ||
}) { | ||
// get key from localstorage | ||
let key = localStorage.getItem("oai-key"); | ||
if (!key) { | ||
key = prompt( | ||
"Please enter your OpenAI key (not shared with TypeCell, stored in your browser):", | ||
); | ||
if (!key) { | ||
return { | ||
status: "error", | ||
error: "no-key", | ||
} as const; | ||
} | ||
localStorage.setItem("oai-key", key); | ||
} | ||
|
||
const openai = new OpenAI({ | ||
apiKey: key, | ||
dangerouslyAllowBrowser: true, | ||
}); | ||
|
||
const response = await openai.chat.completions.create({ | ||
model: "gpt-4", | ||
stream: true, | ||
...parameters, | ||
}); | ||
const stream = OpenAIStream(response); | ||
// Respond with the stream | ||
const ret = new StreamingTextResponse(stream); | ||
const data = await ret.text(); | ||
return { | ||
status: "ok", | ||
result: data, | ||
} as const; | ||
} |
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
Oops, something went wrong.