Skip to content

Commit

Permalink
add groq stream example
Browse files Browse the repository at this point in the history
  • Loading branch information
roodboi committed Apr 20, 2024
1 parent 72ba4d8 commit 50d1f2c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
77 changes: 77 additions & 0 deletions examples/extract_user_stream/groq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import Instructor from "@/instructor"
import OpenAI from "openai"
import { z } from "zod"

const textBlock = `
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:
- Name: John Doe, Email: [email protected], Twitter: @TechGuru44
- Name: Jane Smith, Email: [email protected], Twitter: @DigitalDiva88
- Name: Alex Johnson, Email: [email protected], Twitter: @CodeMaster2023
- Name: Emily Clark, Email: [email protected], Twitter: @InnovateQueen
- Name: Ron Stewart, Email: [email protected], Twitter: @RoboticsRon5
- Name: Sarah Lee, Email: [email protected], Twitter: @AI_Aficionado
- Name: Mike Brown, Email: [email protected], Twitter: @FutureTechLeader
- Name: Lisa Green, Email: [email protected], Twitter: @CyberSavvy101
- Name: David Wilson, Email: [email protected], Twitter: @GadgetGeek77
- Name: Daniel Kim, Email: [email protected], Twitter: @DataDrivenDude
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.
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.
A follow-up meeting is scheduled for January 25th at 3 PM GMT to finalize the agenda and confirm the list of speakers.
`

const ExtractionValuesSchema = z.object({
users: z
.array(
z.object({
name: z.string(),
handle: z.string(),
twitter: z.string()
})
)
.min(5),
date: z.string(),
location: z.string(),
budget: z.number(),
deadline: z.string().min(1)
})

export const groq = new OpenAI({
baseURL: "https://api.groq.com/openai/v1",
apiKey: process.env["GROQ_API_KEY"]
})

const client = Instructor({
client: groq,
mode: "MD_JSON"
})

let extraction = {}

const extractionStream = await client.chat.completions.create({
messages: [{ role: "user", content: textBlock }],
model: "llama3-70b-8192",
response_model: {
schema: ExtractionValuesSchema,
name: "value extraction"
},
stream: true
})

for await (const result of extractionStream) {
try {
extraction = result
console.clear()
console.table(extraction)
} catch (e) {
console.log(e)
break
}
}

console.clear()
console.log("completed extraction:")
console.table(extraction)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"homepage": "https://github.com/instructor-ai/instructor-js#readme",
"dependencies": {
"zod-stream": "1.0.2",
"zod-stream": "1.0.0",
"zod-validation-error": "^2.1.0"
},
"peerDependencies": {
Expand Down

0 comments on commit 50d1f2c

Please sign in to comment.