Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alvesvaren/zod-to-openai-tool
Easily create tools from zod schemas to use with OpenAI Assistants and Chat Completions, inspired by tRPC
https://github.com/alvesvaren/zod-to-openai-tool
json-schema openai openai-assistants-api openai-chat openai-functions openai-tools zod
Last synced: about 2 months ago
JSON representation
Easily create tools from zod schemas to use with OpenAI Assistants and Chat Completions, inspired by tRPC
- Host: GitHub
- URL: https://github.com/alvesvaren/zod-to-openai-tool
- Owner: alvesvaren
- License: isc
- Created: 2023-11-15T19:02:17.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-09T20:23:36.000Z (3 months ago)
- Last Synced: 2024-10-14T11:48:37.154Z (2 months ago)
- Topics: json-schema, openai, openai-assistants-api, openai-chat, openai-functions, openai-tools, zod
- Language: TypeScript
- Homepage: https://npmjs.com/package/zod-to-openai-tool
- Size: 358 KB
- Stars: 23
- Watchers: 1
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# zod-to-openai-tool
![npm](https://img.shields.io/npm/v/zod-to-openai-tool)
![npm](https://img.shields.io/npm/dw/zod-to-openai-tool)Easily create tools from zod schemas to use with OpenAI Assistants and Chat Completions.
## Usage
```ts
import { t, createTools } from "zod-to-openai-tool";const { tools, processAssistantActions } = createTools({
getWeather: t
.input(z.object({ city: z.string() }))
.describe("Get the weather in a city")
.run(async ({ city }) => {
const weatherData = await getWeather(city);
return { weather: weatherData };
}),
});const assistant = await openai.beta.assistants.create({
tools,
// ...
});// Then, when the assistant uses the tools and responds with `requires_action`:
const actions = run.required_action.submit_tool_outputs.tool_calls;
const tool_outputs = processAssistantActions(actions);
await openai.beta.threads.runs.submitToolOutputs(thread.id, run.id, {
tool_outputs,
});
```> See the examples folder and the JSDocs for more examples and information.
>
> See [homeassistant-chatbot-zod](https://github.com/alvesvaren/homeassistant-chatbot-zod) for another more useful example :)This package exports the following functions:
- `t` - Used to create a new tool
- `createTools(tools)` - Used to convert tools to the openai format (and give them a name)
- `combineTools(...tools)` - Used to combine multiple tools into one (including Code Interpreter and File Search)## Installation
`npm install zod-to-openai-tool`
## Tools
A tool is created using the t object. They can then have the following properties added:
- `input` - The input to the function (optional). This needs to be an object (`z.object({})`)
- `describe` - A description of the function (optional)
- `run` - The function to run using the input (required)You'll need to define the input first in order for it to be inferred all the way when using the function.