|
| 1 | +import Instructor from "@/index" |
| 2 | +import { describe, expect, test } from "bun:test" |
| 3 | +import OpenAI from "openai" |
| 4 | +import { z } from "zod" |
| 5 | + |
| 6 | +const textBlock = ` |
| 7 | +In our recent online meeting, participants from various backgrounds joined to discuss the upcoming tech conference. The names and contact details of the participants were as follows: |
| 8 | +
|
| 9 | +- Name: John Doe, Email: [email protected], Twitter: @TechGuru44 |
| 10 | +- Name: Jane Smith, Email: [email protected], Twitter: @DigitalDiva88 |
| 11 | +- Name: Alex Johnson, Email: [email protected], Twitter: @CodeMaster2023 |
| 12 | +
|
| 13 | +During the meeting, we agreed on several key points. The conference will be held on March 15th, 2024, at the Grand Tech Arena located at 4521 Innovation Drive. Dr. Emily Johnson, a renowned AI researcher, will be our keynote speaker. |
| 14 | +
|
| 15 | +The budget for the event is set at $50,000, covering venue costs, speaker fees, and promotional activities. Each participant is expected to contribute an article to the conference blog by February 20th. |
| 16 | +
|
| 17 | +A follow-up meeting is scheduled for January 25th at 3 PM GMT to finalize the agenda and confirm the list of speakers. |
| 18 | +` |
| 19 | + |
| 20 | +const ExtractionValuesSchema = z.object({ |
| 21 | + users: z |
| 22 | + .array( |
| 23 | + z.object({ |
| 24 | + name: z.string(), |
| 25 | + email: z.string(), |
| 26 | + twitter: z.string() |
| 27 | + }) |
| 28 | + ) |
| 29 | + .min(3), |
| 30 | + conference: z.object({ |
| 31 | + date: z.string(), |
| 32 | + venue: z.string(), |
| 33 | + budget: z.number(), |
| 34 | + keynoteSpeaker: z.string() |
| 35 | + }), |
| 36 | + nextMeeting: z.object({ |
| 37 | + date: z.string(), |
| 38 | + time: z.string(), |
| 39 | + timezone: z.string() |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +describe("thinking parser - live tests", () => { |
| 44 | + test("should parse r1 response with thinking tags", async () => { |
| 45 | + const groq = new OpenAI({ |
| 46 | + apiKey: process.env["GROQ_API_KEY"] ?? undefined, |
| 47 | + baseURL: "https://api.groq.com/openai/v1" |
| 48 | + }) |
| 49 | + |
| 50 | + const client = Instructor({ |
| 51 | + client: groq, |
| 52 | + mode: "THINKING_MD_JSON", |
| 53 | + debug: true |
| 54 | + }) |
| 55 | + |
| 56 | + const result = await client.chat.completions.create({ |
| 57 | + messages: [{ role: "user", content: textBlock }], |
| 58 | + model: "deepseek-r1-distill-llama-70b", |
| 59 | + response_model: { schema: ExtractionValuesSchema, name: "Extract" }, |
| 60 | + max_retries: 4 |
| 61 | + }) |
| 62 | + |
| 63 | + console.log("result", result) |
| 64 | + |
| 65 | + expect(result._meta?.thinking).toBeDefined() |
| 66 | + expect(typeof result._meta?.thinking).toBe("string") |
| 67 | + |
| 68 | + expect(result.users).toHaveLength(3) |
| 69 | + expect(result.users[0]).toHaveProperty("name") |
| 70 | + expect(result.users[0]).toHaveProperty("email") |
| 71 | + expect(result.users[0]).toHaveProperty("twitter") |
| 72 | + |
| 73 | + expect(result.conference).toBeDefined() |
| 74 | + expect(result.conference.budget).toBe(50000) |
| 75 | + expect(result.conference.keynoteSpeaker).toBe("Dr. Emily Johnson") |
| 76 | + |
| 77 | + expect(result.nextMeeting).toBeDefined() |
| 78 | + expect(result.nextMeeting.timezone).toBe("GMT") |
| 79 | + }) |
| 80 | +}) |
0 commit comments