diff --git a/examples/openai-vNext/index.tsx b/examples/openai-vNext/index.tsx index b81afe34..7315a6e1 100644 --- a/examples/openai-vNext/index.tsx +++ b/examples/openai-vNext/index.tsx @@ -1,4 +1,9 @@ -import { CompositionCompletion, GSXTool, OpenAIProvider } from "@gensx/openai"; +import { + CompositionCompletion, + GSXStructuredOutput, + GSXTool, + OpenAIProvider, +} from "@gensx/openai"; import { gsx } from "gensx"; import { ChatCompletion as ChatCompletionOutput, @@ -151,26 +156,97 @@ async function streamingCompletion() { return results; } -// async function toolsSync() {} +async function structuredOutput() { + // Define a schema for rating trash bins + const trashRatingSchema = z.object({ + bins: z.array( + z.object({ + location: z.string().describe("Location of the trash bin"), + rating: z.number().describe("Rating from 1-10"), + review: z.string().describe("A sassy review of the trash bin"), + bestFinds: z + .array(z.string()) + .describe("List of the best items found in this bin"), + }), + ), + overallVerdict: z + .string() + .describe("Overall verdict on the neighborhood's trash quality"), + }); -// async function toolsStreaming() {} + type TrashRating = z.infer; -async function main() { - // const results = await basicCompletion(); - // console.log(results.choices[0].message.content); + // Create a structured output wrapper + const structuredOutput = new GSXStructuredOutput(trashRatingSchema, { + description: "Rate and review different trash bins in a neighborhood", + examples: [ + { + bins: [ + { + location: "Behind the fancy restaurant", + rating: 9, + review: "Michelin star garbage, simply exquisite!", + bestFinds: ["day-old croissants", "barely touched sushi"], + }, + ], + overallVerdict: + "High-class neighborhood with refined taste in leftovers", + }, + ], + }); + + const results = await gsx.execute( + + + , + ); - // const stream = await streamingCompletion(); - // for await (const chunk of stream) { - // process.stdout.write(chunk.choices[0].delta.content ?? ""); - // } + return results; +} - // const results = await tools(); - // console.log(results.choices[0].message.content); +async function main() { + console.log("basic completion 🔥"); + const r = await basicCompletion(); + console.log(r.choices[0].message.content); - const stream = await toolsStreaming(); + console.log("streaming completion 🔥"); + const stream = await streamingCompletion(); for await (const chunk of stream) { process.stdout.write(chunk.choices[0].delta.content ?? ""); } + console.log("\n"); + + console.log("tools completion 🔥"); + const results = await tools(); + console.log(results.choices[0].message.content); + + console.log("tools streaming completion 🔥"); + const s2 = await toolsStreaming(); + for await (const chunk of s2) { + process.stdout.write(chunk.choices[0].delta.content ?? ""); + } + console.log("\n"); + + console.log("structured output completion 🔥"); + const structured = await structuredOutput(); + console.log(structured.overallVerdict); + console.log(structured); } main().catch(console.error); diff --git a/packages/gensx-openai/src/composition.tsx b/packages/gensx-openai/src/composition.tsx index 3cb93669..76db7c35 100644 --- a/packages/gensx-openai/src/composition.tsx +++ b/packages/gensx-openai/src/composition.tsx @@ -245,8 +245,8 @@ export const ToolTransform = gsx.Component< return completion; } - // Execute tools and get final completion - return gsx.execute( + // Execute tools + const toolResponses = await gsx.execute( , ); + + // Make final completion with tool results + return gsx.execute( + , + ); }); // Structured output transform component