diff --git a/.changeset/mighty-chairs-speak.md b/.changeset/mighty-chairs-speak.md new file mode 100644 index 00000000..ed32a81e --- /dev/null +++ b/.changeset/mighty-chairs-speak.md @@ -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 diff --git a/examples/extract_user/groq.ts b/examples/extract_user/groq.ts new file mode 100644 index 00000000..5bd9dc85 --- /dev/null +++ b/examples/extract_user/groq.ts @@ -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", + } + ], +} + */ diff --git a/examples/extract_user_stream/groq.ts b/examples/extract_user_stream/groq.ts new file mode 100644 index 00000000..fbe854e3 --- /dev/null +++ b/examples/extract_user_stream/groq.ts @@ -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) diff --git a/src/constants/providers.ts b/src/constants/providers.ts index c0c6e381..7b29c7ef 100644 --- a/src/constants/providers.ts +++ b/src/constants/providers.ts @@ -8,6 +8,7 @@ export const PROVIDERS = { ANYSCALE: "ANYSCALE", TOGETHER: "TOGETHER", ANTHROPIC: "ANTHROPIC", + GROQ: "GROQ", OTHER: "OTHER" } as const @@ -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 = { @@ -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", @@ -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" @@ -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]: ["*"] } } diff --git a/src/instructor.ts b/src/instructor.ts index fc4b9a7e..34127db5 100644 --- a/src/instructor.ts +++ b/src/instructor.ts @@ -62,7 +62,7 @@ class Instructor { } 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}`) } }