跳到主要内容

instructor-js

基于 Typescript 的结构化提取,由大型语言模型 (LLMs) 提供支持,旨在实现简洁、透明和可控。


Twitter Follow Twitter Follow Documentation GitHub issues Discord

深入探索基于 Typescript 的结构化提取世界,它由 OpenAI 的函数调用 API 和 Zod(支持静态类型推断的 Typescript 优先模式验证库)提供支持。Instructor 以其简洁性、透明性和以用户为中心的设计脱颖而出。无论您是经验丰富的开发者还是新手,您都会发现 Instructor 的方法直观且易于掌控。

查看我们的 PythonElixirPHP 版本。

如果您想将 Instructor 移植到其他语言,请通过 Twitter 联系我们,我们很乐意帮助您入门!

用法

要了解所有关于提示和数据提取的技巧,请查看文档

import Instructor from "@instructor-ai/instructor";
import OpenAI from "openai"
import { z } from "zod"

const oai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY ?? undefined,
  organization: process.env.OPENAI_ORG_ID ?? undefined
})

const client = Instructor({
  client: oai,
  mode: "FUNCTIONS"
})

const UserSchema = z.object({
  // Description will be used in the prompt
  age: z.number().describe("The age of the user"), 
  name: z.string()
})


// User will be of type z.infer<typeof UserSchema>
const user = await client.chat.completions.create({
  messages: [{ role: "user", content: "Jason Liu is 30 years old" }],
  model: "gpt-3.5-turbo",
  response_model: { 
    schema: UserSchema, 
    name: "User"
  }
})

console.log(user)
// { age: 30, name: "Jason Liu" }

为什么使用 Instructor?

使用 Instructor 的问题,本质上就是为什么使用 Zod 的问题。

  1. 由 OpenAI 提供支持 — Instructor 由 OpenAI 的函数调用 API 提供支持。这意味着您可以使用同一个 API 进行提示和提取。

  2. 可定制 — Zod 具有高度可定制性。您可以定义自己的验证器、自定义错误消息等等。

  3. 生态系统 Zod 是 Typescript 中使用最广泛的数据验证库。

  4. 经过实战检验 — Zod 每月下载量超过 2400 万次,并得到了庞大社区贡献者的支持。

更多示例

如果您想查看更多示例,请查看我们的使用指南

安装 Instructor 轻而易举。

贡献

如果您想提供帮助,可以查看一些标记为 good-first-issuehelp-wanted 的问题。点此处查找。这些问题可能包括代码改进、客座博客文章或新的使用指南。

许可证

本项目根据 MIT 许可证的条款获得许可。