-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'dotenv/config'; | ||
import { z } from 'zod'; | ||
import { ChatModel, createAIExtractFunction } from '@dexaai/dexter'; | ||
|
||
/** A function to extract people names from text. */ | ||
const extractPeopleNamesRunner = createAIExtractFunction({ | ||
chatModel: new ChatModel({ params: { model: 'gpt-4-1106-preview' } }), | ||
systemMessage: `You use functions to extract people names from a message.`, | ||
name: 'log_people_names', | ||
description: `Use this to log the full names of people from a message. Don't include duplicate names.`, | ||
schema: z.object({ | ||
names: z.array( | ||
z | ||
.string() | ||
.describe( | ||
`The name of a person from the message. Normalize the name by removing suffixes, prefixes, and fixing capitalization` | ||
) | ||
), | ||
}), | ||
}); | ||
|
||
/** | ||
* npx tsx examples/extract-people-names.ts | ||
*/ | ||
async function main() { | ||
// Use OpenAI functions to extract data adhering to a Zod schema | ||
const peopleNames = await extractPeopleNamesRunner( | ||
`Dr. Andrew Huberman interviewed Tony Hawk, an idol of Andrew Hubermans.` | ||
); | ||
console.log('peopleNames', peopleNames); | ||
} | ||
|
||
main().catch(console.error); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,6 @@ import { Msg, getErrorMsg } from '../index.js'; | |
import type { Prompt } from '../types.js'; | ||
import type { Model } from '../../index.js'; | ||
|
||
type RunnerModelParams = Partial< | ||
Omit<Model.Chat.Run & Model.Chat.Config, 'messages' | 'functions' | 'tools'> | ||
>; | ||
|
||
/** | ||
* Creates a function to run a chat model in a loop | ||
* - Handles parsing, running, and inserting responses for function & tool call messages | ||
|
@@ -31,7 +27,9 @@ export function createAIRunner<Content extends any = string>(args: { | |
/** Add a system message to the beginning of the messages array. */ | ||
systemMessage?: string; | ||
/** Model params to use for each API call (optional). */ | ||
params?: RunnerModelParams; | ||
params?: Prompt.Runner.ModelParams; | ||
/** Optional context to pass to ChatModel.run calls */ | ||
context?: Model.Ctx; | ||
}): Prompt.Runner<Content> { | ||
/** Return the content string or an empty string if null. */ | ||
function defaultValidateContent(content: string | null): Content { | ||
|
@@ -53,10 +51,16 @@ export function createAIRunner<Content extends any = string>(args: { | |
functionCallConcurrency, | ||
systemMessage, | ||
params: runnerModelParams, | ||
context: runnerContext, | ||
validateContent = defaultValidateContent, | ||
shouldBreakLoop = defaultShouldBreakLoop, | ||
} = args; | ||
|
||
const mergedContext = { | ||
...runnerContext, | ||
...context, | ||
}; | ||
|
||
// Add the functions/tools to the model params | ||
const additonalParams = getParams({ functions, mode }); | ||
|
||
|
@@ -86,7 +90,7 @@ export function createAIRunner<Content extends any = string>(args: { | |
...additonalParams, | ||
messages, | ||
}; | ||
const { message } = await chatModel.run(runParams, context); | ||
const { message } = await chatModel.run(runParams, mergedContext); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
transitive-bullshit
Author
Collaborator
|
||
messages.push(message); | ||
|
||
// Run functions from tool/function call messages and append the new messages | ||
|
@transitive-bullshit The
ChatModel
already stores context and params state, andchatModel.run()
will merge the new context with the existing model context.I'm really not a fan of having so many ways to do the same thing and increasing the amount of args passed to functions/classes like this. Can we remove this duplicate code path for passing params/context to keep things cleaner.