Skip to content

Commit

Permalink
Update provider validation and add groq + examples (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
roodboi committed Apr 20, 2024
1 parent 625b543 commit 287aa27
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-chairs-speak.md
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
50 changes: 50 additions & 0 deletions examples/extract_user/groq.ts
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",
}
],
}
*/
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: 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)
13 changes: 11 additions & 2 deletions src/constants/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const PROVIDERS = {
ANYSCALE: "ANYSCALE",
TOGETHER: "TOGETHER",
ANTHROPIC: "ANTHROPIC",
GROQ: "GROQ",
OTHER: "OTHER"
} as const

Expand All @@ -20,14 +21,16 @@ export const PROVIDER_SUPPORTED_MODES: {
[PROVIDERS.OAI]: [MODE.FUNCTIONS, MODE.TOOLS, MODE.JSON, MODE.MD_JSON],
[PROVIDERS.ANYSCALE]: [MODE.TOOLS, MODE.JSON, MODE.JSON_SCHEMA, MODE.MD_JSON],
[PROVIDERS.TOGETHER]: [MODE.TOOLS, MODE.JSON, MODE.JSON_SCHEMA, MODE.MD_JSON],
[PROVIDERS.ANTHROPIC]: [MODE.MD_JSON, MODE.TOOLS]
[PROVIDERS.ANTHROPIC]: [MODE.MD_JSON, MODE.TOOLS],
[PROVIDERS.GROQ]: [MODE.TOOLS, MODE.FUNCTIONS, MODE.MD_JSON]
} as const

export const NON_OAI_PROVIDER_URLS = {
[PROVIDERS.ANYSCALE]: "api.endpoints.anyscale",
[PROVIDERS.TOGETHER]: "api.together.xyz",
[PROVIDERS.OAI]: "api.openai.com",
[PROVIDERS.ANTHROPIC]: "api.anthropic.com"
[PROVIDERS.ANTHROPIC]: "api.anthropic.com",
[PROVIDERS.GROQ]: "api.groq.com"
} as const

export const PROVIDER_PARAMS_TRANSFORMERS = {
Expand Down Expand Up @@ -96,6 +99,7 @@ export const PROVIDER_SUPPORTED_MODES_BY_MODEL = {
[MODE.MD_JSON]: ["*"]
},
[PROVIDERS.TOGETHER]: {
[MODE.MD_JSON]: ["*"],
[MODE.JSON_SCHEMA]: [
"mistralai/Mixtral-8x7B-Instruct-v0.1",
"mistralai/Mistral-7B-Instruct-v0.1",
Expand All @@ -108,6 +112,7 @@ export const PROVIDER_SUPPORTED_MODES_BY_MODEL = {
]
},
[PROVIDERS.ANYSCALE]: {
[MODE.MD_JSON]: ["*"],
[MODE.JSON_SCHEMA]: [
"mistralai/Mistral-7B-Instruct-v0.1",
"mistralai/Mixtral-8x7B-Instruct-v0.1"
Expand All @@ -117,5 +122,9 @@ export const PROVIDER_SUPPORTED_MODES_BY_MODEL = {
[PROVIDERS.ANTHROPIC]: {
[MODE.MD_JSON]: ["*"],
[MODE.TOOLS]: ["*"]
},
[PROVIDERS.GROQ]: {
[MODE.TOOLS]: ["llama2-70b-4096", "mixtral-8x7b-32768", "gemma-7b-it"],
[MODE.MD_JSON]: ["*"]
}
}
2 changes: 1 addition & 1 deletion src/instructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Instructor<C extends GenericClient | OpenAI> {
}

if (!isModeSupported) {
throw new Error(`Mode ${this.mode} is not supported by provider ${this.provider}`)
this.log("warn", `Mode ${this.mode} may not be supported by provider ${this.provider}`)
}
}

Expand Down

0 comments on commit 287aa27

Please sign in to comment.