-
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.
Update provider validation and add groq + examples (#162)
- Loading branch information
Showing
5 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"@instructor-ai/instructor": patch | ||
--- | ||
|
||
add groq to supported providers - remove error on validation and warn instead so we dont fail if we are out of date on the mappings |
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,50 @@ | ||
import Instructor from "@/instructor" | ||
import OpenAI from "openai" | ||
import { z } from "zod" | ||
|
||
const property = z | ||
.object({ | ||
name: z.string(), | ||
value: z.string() | ||
}) | ||
.describe("A property defined by a name and value") | ||
|
||
const UserSchema = z.object({ | ||
age: z.number(), | ||
name: z.string(), | ||
properties: z.array(property) | ||
}) | ||
|
||
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" | ||
}) | ||
|
||
const user = await client.chat.completions.create({ | ||
messages: [{ role: "user", content: "Harry Potter" }], | ||
model: "llama3-70b-8192", | ||
response_model: { schema: UserSchema, name: "User" }, | ||
max_retries: 3 | ||
}) | ||
|
||
console.log(user) | ||
/** | ||
* { | ||
age: 17, | ||
name: "Harry Potter", | ||
properties: [ | ||
{ | ||
name: "House", | ||
value: "Gryffindor", | ||
}, { | ||
name: "Wand", | ||
value: "Holly and Phoenix feather", | ||
} | ||
], | ||
} | ||
*/ |
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,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: johndoe@email.com, Twitter: @TechGuru44 | ||
- Name: Jane Smith, Email: janesmith@email.com, Twitter: @DigitalDiva88 | ||
- Name: Alex Johnson, Email: alexj@email.com, Twitter: @CodeMaster2023 | ||
- Name: Emily Clark, Email: emilyc@email.com, Twitter: @InnovateQueen | ||
- Name: Ron Stewart, Email: ronstewart@email.com, Twitter: @RoboticsRon5 | ||
- Name: Sarah Lee, Email: sarahlee@email.com, Twitter: @AI_Aficionado | ||
- Name: Mike Brown, Email: mikeb@email.com, Twitter: @FutureTechLeader | ||
- Name: Lisa Green, Email: lisag@email.com, Twitter: @CyberSavvy101 | ||
- Name: David Wilson, Email: davidw@email.com, Twitter: @GadgetGeek77 | ||
- Name: Daniel Kim, Email: danielk@email.com, 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) |
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