Skip to content

Commit 287aa27

Browse files
authored
Update provider validation and add groq + examples (#162)
1 parent 625b543 commit 287aa27

File tree

5 files changed

+144
-3
lines changed

5 files changed

+144
-3
lines changed

.changeset/mighty-chairs-speak.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@instructor-ai/instructor": patch
3+
---
4+
5+
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

examples/extract_user/groq.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Instructor from "@/instructor"
2+
import OpenAI from "openai"
3+
import { z } from "zod"
4+
5+
const property = z
6+
.object({
7+
name: z.string(),
8+
value: z.string()
9+
})
10+
.describe("A property defined by a name and value")
11+
12+
const UserSchema = z.object({
13+
age: z.number(),
14+
name: z.string(),
15+
properties: z.array(property)
16+
})
17+
18+
export const groq = new OpenAI({
19+
baseURL: "https://api.groq.com/openai/v1",
20+
apiKey: process.env["GROQ_API_KEY"]
21+
})
22+
23+
const client = Instructor({
24+
client: groq,
25+
mode: "MD_JSON"
26+
})
27+
28+
const user = await client.chat.completions.create({
29+
messages: [{ role: "user", content: "Harry Potter" }],
30+
model: "llama3-70b-8192",
31+
response_model: { schema: UserSchema, name: "User" },
32+
max_retries: 3
33+
})
34+
35+
console.log(user)
36+
/**
37+
* {
38+
age: 17,
39+
name: "Harry Potter",
40+
properties: [
41+
{
42+
name: "House",
43+
value: "Gryffindor",
44+
}, {
45+
name: "Wand",
46+
value: "Holly and Phoenix feather",
47+
}
48+
],
49+
}
50+
*/

examples/extract_user_stream/groq.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import Instructor from "@/instructor"
2+
import OpenAI from "openai"
3+
import { z } from "zod"
4+
5+
const textBlock = `
6+
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:
7+
8+
- Name: John Doe, Email: [email protected], Twitter: @TechGuru44
9+
- Name: Jane Smith, Email: [email protected], Twitter: @DigitalDiva88
10+
- Name: Alex Johnson, Email: [email protected], Twitter: @CodeMaster2023
11+
- Name: Emily Clark, Email: [email protected], Twitter: @InnovateQueen
12+
- Name: Ron Stewart, Email: [email protected], Twitter: @RoboticsRon5
13+
- Name: Sarah Lee, Email: [email protected], Twitter: @AI_Aficionado
14+
- Name: Mike Brown, Email: [email protected], Twitter: @FutureTechLeader
15+
- Name: Lisa Green, Email: [email protected], Twitter: @CyberSavvy101
16+
- Name: David Wilson, Email: [email protected], Twitter: @GadgetGeek77
17+
- Name: Daniel Kim, Email: [email protected], Twitter: @DataDrivenDude
18+
19+
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.
20+
21+
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.
22+
23+
A follow-up meeting is scheduled for January 25th at 3 PM GMT to finalize the agenda and confirm the list of speakers.
24+
`
25+
26+
const ExtractionValuesSchema = z.object({
27+
users: z
28+
.array(
29+
z.object({
30+
name: z.string(),
31+
handle: z.string(),
32+
twitter: z.string()
33+
})
34+
)
35+
.min(5),
36+
date: z.string(),
37+
location: z.string(),
38+
budget: z.number(),
39+
deadline: z.string().min(1)
40+
})
41+
42+
export const groq = new OpenAI({
43+
baseURL: "https://api.groq.com/openai/v1",
44+
apiKey: process.env["GROQ_API_KEY"]
45+
})
46+
47+
const client = Instructor({
48+
client: groq,
49+
mode: "MD_JSON"
50+
})
51+
52+
let extraction = {}
53+
54+
const extractionStream = await client.chat.completions.create({
55+
messages: [{ role: "user", content: textBlock }],
56+
model: "llama3-70b-8192",
57+
response_model: {
58+
schema: ExtractionValuesSchema,
59+
name: "value extraction"
60+
},
61+
stream: true
62+
})
63+
64+
for await (const result of extractionStream) {
65+
try {
66+
extraction = result
67+
console.clear()
68+
console.table(extraction)
69+
} catch (e) {
70+
console.log(e)
71+
break
72+
}
73+
}
74+
75+
console.clear()
76+
console.log("completed extraction:")
77+
console.table(extraction)

src/constants/providers.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const PROVIDERS = {
88
ANYSCALE: "ANYSCALE",
99
TOGETHER: "TOGETHER",
1010
ANTHROPIC: "ANTHROPIC",
11+
GROQ: "GROQ",
1112
OTHER: "OTHER"
1213
} as const
1314

@@ -20,14 +21,16 @@ export const PROVIDER_SUPPORTED_MODES: {
2021
[PROVIDERS.OAI]: [MODE.FUNCTIONS, MODE.TOOLS, MODE.JSON, MODE.MD_JSON],
2122
[PROVIDERS.ANYSCALE]: [MODE.TOOLS, MODE.JSON, MODE.JSON_SCHEMA, MODE.MD_JSON],
2223
[PROVIDERS.TOGETHER]: [MODE.TOOLS, MODE.JSON, MODE.JSON_SCHEMA, MODE.MD_JSON],
23-
[PROVIDERS.ANTHROPIC]: [MODE.MD_JSON, MODE.TOOLS]
24+
[PROVIDERS.ANTHROPIC]: [MODE.MD_JSON, MODE.TOOLS],
25+
[PROVIDERS.GROQ]: [MODE.TOOLS, MODE.FUNCTIONS, MODE.MD_JSON]
2426
} as const
2527

2628
export const NON_OAI_PROVIDER_URLS = {
2729
[PROVIDERS.ANYSCALE]: "api.endpoints.anyscale",
2830
[PROVIDERS.TOGETHER]: "api.together.xyz",
2931
[PROVIDERS.OAI]: "api.openai.com",
30-
[PROVIDERS.ANTHROPIC]: "api.anthropic.com"
32+
[PROVIDERS.ANTHROPIC]: "api.anthropic.com",
33+
[PROVIDERS.GROQ]: "api.groq.com"
3134
} as const
3235

3336
export const PROVIDER_PARAMS_TRANSFORMERS = {
@@ -96,6 +99,7 @@ export const PROVIDER_SUPPORTED_MODES_BY_MODEL = {
9699
[MODE.MD_JSON]: ["*"]
97100
},
98101
[PROVIDERS.TOGETHER]: {
102+
[MODE.MD_JSON]: ["*"],
99103
[MODE.JSON_SCHEMA]: [
100104
"mistralai/Mixtral-8x7B-Instruct-v0.1",
101105
"mistralai/Mistral-7B-Instruct-v0.1",
@@ -108,6 +112,7 @@ export const PROVIDER_SUPPORTED_MODES_BY_MODEL = {
108112
]
109113
},
110114
[PROVIDERS.ANYSCALE]: {
115+
[MODE.MD_JSON]: ["*"],
111116
[MODE.JSON_SCHEMA]: [
112117
"mistralai/Mistral-7B-Instruct-v0.1",
113118
"mistralai/Mixtral-8x7B-Instruct-v0.1"
@@ -117,5 +122,9 @@ export const PROVIDER_SUPPORTED_MODES_BY_MODEL = {
117122
[PROVIDERS.ANTHROPIC]: {
118123
[MODE.MD_JSON]: ["*"],
119124
[MODE.TOOLS]: ["*"]
125+
},
126+
[PROVIDERS.GROQ]: {
127+
[MODE.TOOLS]: ["llama2-70b-4096", "mixtral-8x7b-32768", "gemma-7b-it"],
128+
[MODE.MD_JSON]: ["*"]
120129
}
121130
}

src/instructor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Instructor<C extends GenericClient | OpenAI> {
6262
}
6363

6464
if (!isModeSupported) {
65-
throw new Error(`Mode ${this.mode} is not supported by provider ${this.provider}`)
65+
this.log("warn", `Mode ${this.mode} may not be supported by provider ${this.provider}`)
6666
}
6767
}
6868

0 commit comments

Comments
 (0)