0.13.1 • Published 11 months ago

zod-to-openai-tool v0.13.1

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

zod-to-openai-tool

npm npm

Easily create tools from zod schemas to use with OpenAI Assistants and Chat Completions.

Usage

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 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.

0.12.0

12 months ago

0.13.0

11 months ago

0.13.1

11 months ago

0.11.25

1 year ago

0.11.24

1 year ago

0.11.23

1 year ago

0.11.22

1 year ago

0.11.21

1 year ago

0.11.20

1 year ago

0.11.19

1 year ago

0.11.18

1 year ago

0.11.17

1 year ago

0.11.16

2 years ago

0.11.15

2 years ago

0.11.14

2 years ago

0.11.13

2 years ago

0.11.12

2 years ago

0.11.11

2 years ago

0.11.9

2 years ago

0.11.10

2 years ago

0.11.8

2 years ago

0.11.6

2 years ago

0.11.4

2 years ago

0.11.3

2 years ago

0.11.2

2 years ago

0.11.1

2 years ago

0.11.0

2 years ago

0.10.0

2 years ago

0.9.0

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago