-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
createExtractFunction
for data extraction
Introduce `createExtractFunction` using OpenAI's structured output feature, leveraging a Zod schema for data validation and extraction. This is a better and more modern alternative to the `createAIExtractFunction` utility, which is now deprecated.
- Loading branch information
1 parent
3eb2fa8
commit 9a26060
Showing
6 changed files
with
128 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,52 @@ | ||
import { zodToJsonSchema } from 'openai-zod-to-json-schema'; | ||
import { type z } from 'zod'; | ||
|
||
import { type Model, type Msg, MsgUtil } from '../model/index.js'; | ||
|
||
/** | ||
* Extract data using OpenAI structured outputs. | ||
* | ||
* Always returns an object satisfying the provided Zod schema. | ||
*/ | ||
export function createExtractFunction<Schema extends z.ZodObject<any>>(args: { | ||
/** The ChatModel used to make API calls. */ | ||
chatModel: Model.Chat.Model; | ||
/** A descriptive name for the object to extract. */ | ||
name: string; | ||
/** The Zod schema for the data to extract. */ | ||
schema: Schema; | ||
/** Add a system message to the beginning of the messages array. */ | ||
systemMessage: string; | ||
}): (input: string | Msg) => Promise<z.infer<Schema>> { | ||
const { chatModel, schema, systemMessage } = args; | ||
|
||
async function runExtract(input: string | Msg): Promise<z.infer<Schema>> { | ||
const inputVal = typeof input === 'string' ? input : (input.content ?? ''); | ||
const messages: Msg[] = [ | ||
MsgUtil.system(systemMessage), | ||
MsgUtil.user(inputVal), | ||
]; | ||
|
||
const { message } = await chatModel.run({ | ||
messages, | ||
response_format: { | ||
type: 'json_schema', | ||
json_schema: { | ||
name: args.name, | ||
strict: true, | ||
schema: zodToJsonSchema(schema, { | ||
$refStrategy: 'none', | ||
openaiStrictMode: true, | ||
}), | ||
}, | ||
}, | ||
}); | ||
|
||
MsgUtil.assertAssistant(message); | ||
|
||
const json = JSON.parse(message.content); | ||
return schema.parse(json); | ||
} | ||
|
||
return runExtract; | ||
} |