-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into stream-support
- Loading branch information
Showing
7 changed files
with
196 additions
and
17 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
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 assert from "assert" | ||
import Instructor from "@/instructor" | ||
import OpenAI from "openai" | ||
import { z } from "zod" | ||
|
||
enum MULTI_CLASSIFICATION_LABELS { | ||
"BILLING" = "billing", | ||
"GENERAL_QUERY" = "general_query", | ||
"HARDWARE" = "hardware" | ||
} | ||
|
||
const MultiClassificationSchema = z.object({ | ||
predicted_labels: z.array(z.nativeEnum(MULTI_CLASSIFICATION_LABELS)) | ||
}) | ||
|
||
type MultiClassification = z.infer<typeof MultiClassificationSchema> | ||
|
||
const oai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY ?? undefined, | ||
organization: process.env.OPENAI_ORG_ID ?? undefined | ||
}) | ||
|
||
const client = Instructor({ | ||
client: oai, | ||
mode: "FUNCTIONS" | ||
}) | ||
|
||
const createClassification = async (data: string): Promise<MultiClassification | undefined> => { | ||
const classification: MultiClassification = await client.chat.completions.create({ | ||
messages: [{ role: "user", content: `"Classify the following support ticket: ${data}` }], | ||
model: "gpt-3.5-turbo", | ||
response_model: MultiClassificationSchema, | ||
max_retries: 3 | ||
}) | ||
|
||
return classification || undefined | ||
} | ||
|
||
const classification = await createClassification( | ||
"My account is locked and I can't access my billing info. Phone is also broken" | ||
) | ||
// OUTPUT: { predicted_labels: [ 'billing', 'hardware' ] } | ||
|
||
console.log({ classification }) | ||
|
||
assert( | ||
classification.predicted_labels.includes(MULTI_CLASSIFICATION_LABELS.BILLING) && | ||
classification.predicted_labels.includes(MULTI_CLASSIFICATION_LABELS.HARDWARE), | ||
`Expected ${classification.predicted_labels} to include ${MULTI_CLASSIFICATION_LABELS.BILLING} and ${MULTI_CLASSIFICATION_LABELS.HARDWARE}` | ||
) |
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,48 @@ | ||
import assert from "assert" | ||
import Instructor from "@/instructor" | ||
import OpenAI from "openai" | ||
import { z } from "zod" | ||
|
||
enum CLASSIFICATION_LABELS { | ||
"SPAM" = "SPAM", | ||
"NOT_SPAM" = "NOT_SPAM" | ||
} | ||
|
||
const SimpleClassificationSchema = z.object({ | ||
class_label: z.nativeEnum(CLASSIFICATION_LABELS) | ||
}) | ||
|
||
type SimpleClassification = z.infer<typeof SimpleClassificationSchema> | ||
|
||
const oai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY ?? undefined, | ||
organization: process.env.OPENAI_ORG_ID ?? undefined | ||
}) | ||
|
||
const client = Instructor({ | ||
client: oai, | ||
mode: "FUNCTIONS" | ||
}) | ||
|
||
const createClassification = async (data: string): Promise<SimpleClassification | undefined> => { | ||
const classification: SimpleClassification = await client.chat.completions.create({ | ||
messages: [{ role: "user", content: `"Classify the following text: ${data}` }], | ||
model: "gpt-3.5-turbo", | ||
response_model: SimpleClassificationSchema, | ||
max_retries: 3 | ||
}) | ||
|
||
return classification || undefined | ||
} | ||
|
||
const classification = await createClassification( | ||
"Hello there I'm a nigerian prince and I want to give you money" | ||
) | ||
// OUTPUT: { class_label: 'SPAM' } | ||
|
||
console.log({ classification }) | ||
|
||
assert( | ||
classification.class_label === CLASSIFICATION_LABELS.SPAM, | ||
`Expected ${classification.class_label} to be ${CLASSIFICATION_LABELS.SPAM}` | ||
) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Instructor from "@/instructor" | ||
import { describe, expect, test } from "bun:test" | ||
import OpenAI from "openai" | ||
import { z } from "zod" | ||
|
||
import { MODE } from "@/constants/modes" | ||
|
||
const models_latest = ["gpt-3.5-turbo-1106", "gpt-4-1106-preview"] | ||
const models_old = ["gpt-3.5-turbo", "gpt-4"] | ||
|
||
const createTestCases = (): { model: string; mode: MODE }[] => { | ||
const { FUNCTIONS, ...rest } = MODE | ||
const modes = Object.values(rest) | ||
|
||
return [ | ||
...models_latest.flatMap(model => modes.map(mode => ({ model, mode }))), | ||
...models_old.flatMap(model => ({ model, mode: FUNCTIONS })) | ||
] | ||
} | ||
|
||
const UserSchema = z.object({ | ||
age: z.number(), | ||
name: z.string().refine(name => name.includes(" "), { | ||
message: "Name must contain a space" | ||
}) | ||
}) | ||
|
||
type User = z.infer<typeof UserSchema> | ||
|
||
async function extractUser(model: string, mode: MODE) { | ||
const oai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY ?? undefined, | ||
organization: process.env.OPENAI_ORG_ID ?? undefined | ||
}) | ||
|
||
const client = Instructor({ | ||
client: oai, | ||
mode: mode | ||
}) | ||
|
||
const user: User = await client.chat.completions.create({ | ||
messages: [{ role: "user", content: "Jason Liu is 30 years old" }], | ||
model: model, | ||
response_model: UserSchema, | ||
max_retries: 3 | ||
}) | ||
|
||
return user | ||
} | ||
|
||
describe("Modes", async () => { | ||
const testCases = createTestCases() | ||
|
||
for await (const { model, mode } of testCases) { | ||
test(`Should return extracted name and age for model ${model} and mode ${mode}`, async () => { | ||
const user = await extractUser(model, mode) | ||
|
||
expect(user.name).toEqual("Jason Liu") | ||
expect(user.age).toEqual(30) | ||
}) | ||
} | ||
}) |