-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support to use valibot to define tools
Signed-off-by: Neko Ayaka <[email protected]>
- Loading branch information
1 parent
0de0cf3
commit 8fe325d
Showing
6 changed files
with
142 additions
and
6 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,89 @@ | ||
import { env } from 'node:process' | ||
import OpenAI from 'openai' | ||
import * as v from 'valibot' | ||
|
||
import { | ||
Check failure on line 5 in examples/neuri/weather-query/src/valibot.ts GitHub Actions / Lint - 20.x
|
||
composeAgent, | ||
defineToolFunction, | ||
|
||
resolveFirstTextMessageFromCompletion, | ||
system, | ||
toolFunction, | ||
user, | ||
} from 'neuri/openai' | ||
|
||
async function main() { | ||
const o = new OpenAI({ | ||
baseURL: env.OPENAI_API_BASEURL, | ||
apiKey: env.OPENAI_API_KEY, | ||
}) | ||
|
||
const { call } = composeAgent({ | ||
openAI: o, | ||
tools: [ | ||
defineToolFunction( | ||
toolFunction('getCity', 'Get the user\'s city', {}), | ||
async () => { | ||
return 'New York City' | ||
}, | ||
{ | ||
hooks: { | ||
preInvoke: async () => { | ||
// eslint-disable-next-line no-console | ||
console.log('getCity called') | ||
}, | ||
}, | ||
}, | ||
), | ||
defineToolFunction<{ location: string }, string>( | ||
toolFunction('getCityCode', 'Get the user\'s city code with search', v.object({ | ||
location: v.pipe(v.string(), v.minLength(1), v.description('Get the user\'s city code with search')), | ||
})), | ||
async () => { | ||
return 'NYC' | ||
}, | ||
{ | ||
hooks: { | ||
preInvoke: async () => { | ||
// eslint-disable-next-line no-console | ||
console.log('getCityCode called') | ||
}, | ||
}, | ||
}, | ||
), | ||
defineToolFunction<{ cityCode: string }, { city: string, cityCode: string, weather: string, degreesCelsius: number }>( | ||
toolFunction('getWeather', 'Get the current weather', v.object({ | ||
cityCode: v.pipe(v.string(), v.minLength(1), v.description('Get the user\'s city code with search')), | ||
})), | ||
async ({ parameters: { cityCode } }) => { | ||
return { | ||
city: `New York city`, | ||
cityCode, | ||
weather: 'sunny', | ||
degreesCelsius: 26, | ||
} | ||
}, | ||
{ | ||
hooks: { | ||
preInvoke: async () => { | ||
// eslint-disable-next-line no-console | ||
console.log('getWeather called') | ||
}, | ||
}, | ||
}, | ||
), | ||
], | ||
}) | ||
|
||
const res = await call([ | ||
system('I am a helpful assistant here to provide information of user, user may ask you anything. Please identify the user\'s need, and pick up the right tool to obtain the necessary information.'), | ||
user('What is the weather like today?'), | ||
], { | ||
model: 'openai/gpt-3.5-turbo', | ||
}) | ||
|
||
return resolveFirstTextMessageFromCompletion(res) | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
main().then(console.log).catch(console.error) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.