-
Notifications
You must be signed in to change notification settings - Fork 55
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
9 changed files
with
190 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
import Instructor from "@/instructor" | ||
import OpenAI from "openai" | ||
import { z } from "zod" | ||
|
||
const UserSchema = z.object({ | ||
age: z.number(), | ||
name: z.string().refine(name => name.includes(" "), { | ||
message: "Name must contain a space" | ||
}) | ||
}) | ||
|
||
type User = z.infer<typeof UserSchema> | ||
|
||
const oai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY ?? undefined, | ||
organization: process.env.OPENAI_ORG_ID ?? undefined | ||
}) | ||
|
||
const client = Instructor({ | ||
client: oai, | ||
mode: "FUNCTIONS" | ||
}) | ||
|
||
const user: User = await client.chat.completions.create({ | ||
messages: [{ role: "user", content: "Jason Liu is 30 years old" }], | ||
model: "gpt-3.5-turbo", | ||
response_model: UserSchema, | ||
max_retries: 3 | ||
}) | ||
|
||
console.log(user) |
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,53 @@ | ||
import Instructor from "@/instructor" | ||
import OpenAI from "openai" | ||
import { z } from "zod" | ||
|
||
const UserSchema = z.object({ | ||
age: z.number(), | ||
name: z.string() | ||
}) | ||
|
||
type User = z.infer<typeof UserSchema> | ||
|
||
const oai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY ?? undefined, | ||
organization: process.env.OPENAI_ORG_ID ?? undefined | ||
}) | ||
|
||
const client = Instructor({ | ||
client: oai, | ||
mode: "FUNCTIONS" | ||
}) | ||
|
||
const userStream = await client.chat.completions.create({ | ||
messages: [{ role: "user", content: "Jason Liu is 30 years old" }], | ||
model: "gpt-3.5-turbo", | ||
response_model: UserSchema, | ||
max_retries: 3, | ||
stream: true | ||
}) | ||
|
||
const reader = userStream.readable.getReader() | ||
const decoder = new TextDecoder() | ||
|
||
let result = {} | ||
let done = false | ||
|
||
while (!done) { | ||
try { | ||
const { value, done: doneReading } = await reader.read() | ||
done = doneReading | ||
|
||
if (done) { | ||
break | ||
} | ||
|
||
const chunkValue = decoder.decode(value) | ||
result = JSON.parse(chunkValue) | ||
console.log(result) | ||
} catch (e) { | ||
done = true | ||
console.log(e) | ||
break | ||
} | ||
} |
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
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,51 @@ | ||
import OpenAI from "openai" | ||
import { Stream } from "openai/streaming" | ||
|
||
interface OaiStreamArgs { | ||
res: Stream<OpenAI.Chat.Completions.ChatCompletionChunk> | ||
parser | ||
} | ||
|
||
/** | ||
* `OaiStream` creates a ReadableStream that parses the SSE response from OAI | ||
* and returns a parsed string from the response. | ||
* | ||
* @param {OaiStreamArgs} args - The arguments for the function. | ||
* @returns {ReadableStream<string>} - The created ReadableStream. | ||
*/ | ||
export function OAIStream({ res, parser }: OaiStreamArgs): ReadableStream<Uint8Array> { | ||
const encoder = new TextEncoder() | ||
let cancelGenerator: () => void | ||
|
||
async function* generateStream(res): AsyncGenerator<string> { | ||
cancelGenerator = () => { | ||
return | ||
} | ||
|
||
for await (const part of res) { | ||
if (part?.choices?.[0]?.finish_reason === "stop") { | ||
cancelGenerator() | ||
break | ||
} | ||
|
||
yield parser(part) | ||
} | ||
} | ||
|
||
const generator = generateStream(res) | ||
|
||
return new ReadableStream({ | ||
async start(controller) { | ||
for await (const parsedData of generator) { | ||
controller.enqueue(encoder.encode(parsedData)) | ||
} | ||
|
||
controller.close() | ||
}, | ||
cancel() { | ||
if (cancelGenerator) { | ||
cancelGenerator() | ||
} | ||
} | ||
}) | ||
} |
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