|
| 1 | +import { OpenAI } from "openai"; |
| 2 | +import { z } from "zod"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +const system_prompt = fs.readFileSync( |
| 6 | + path.resolve(import.meta.dirname, "system-prompt.en.md"), |
| 7 | + "utf-8" |
| 8 | +); |
| 9 | +export const getOpenaiOutput = async <T>( |
| 10 | + user_content: string, |
| 11 | + schema: z.ZodType<T> |
| 12 | +) => { |
| 13 | + const openai = new OpenAI({ |
| 14 | + baseURL: z |
| 15 | + .string({ required_error: "required env.AI_GATEWAY_ENDPOINT" }) |
| 16 | + .parse(process.env.AI_GATEWAY_ENDPOINT), |
| 17 | + apiKey: z |
| 18 | + .string({ required_error: "required env.OPENAI_API_KEY" }) |
| 19 | + .parse(process.env.OPENAI_API_KEY), |
| 20 | + }); |
| 21 | + |
| 22 | + const stream = await openai.chat.completions.create({ |
| 23 | + messages: [ |
| 24 | + { |
| 25 | + role: "system", |
| 26 | + content: system_prompt, |
| 27 | + }, |
| 28 | + { |
| 29 | + role: "user", |
| 30 | + content: user_content, |
| 31 | + }, |
| 32 | + ], |
| 33 | + model: "deepseek-chat", |
| 34 | + response_format: { |
| 35 | + type: "json_object", |
| 36 | + }, |
| 37 | + stream: true, |
| 38 | + }); |
| 39 | + |
| 40 | + let res = ""; |
| 41 | + for await (const chunk of stream) { |
| 42 | + const part = chunk.choices[0].delta.content ?? ""; |
| 43 | + res += part; |
| 44 | + process.stdout.write(part); |
| 45 | + } |
| 46 | + return schema.parse(JSON.parse(res)); |
| 47 | +}; |
| 48 | +import { type LanguageModelV1, streamObject } from "ai"; |
| 49 | + |
| 50 | +export const getAiOutput = async <T>( |
| 51 | + model: LanguageModelV1, |
| 52 | + schema: z.ZodType<T>, |
| 53 | + user_content: string |
| 54 | +) => { |
| 55 | + const stream = streamObject({ |
| 56 | + model, |
| 57 | + schema, |
| 58 | + system: system_prompt, |
| 59 | + prompt: user_content, |
| 60 | + }); |
| 61 | + for await (const partialObject of stream.partialObjectStream) { |
| 62 | + console.clear(); |
| 63 | + console.log(partialObject); |
| 64 | + } |
| 65 | + |
| 66 | + return await stream.object; |
| 67 | +}; |
0 commit comments