跳到内容

Instructor

Instructor 使从 GPT-3.5, GPT-4, GPT-4-Vision 等大型语言模型(LLMs)以及包括 Mistral/Mixtral, Anyscale, Ollama, 和 llama-cpp-python 在内的开源模型中获取 JSON 等结构化数据变得容易。

Instructor 的 Rust 客户端正在积极开发中。这意味着 API 和包未来可能会发生变化。与此同时,我们正在寻找积极的代码贡献者来帮助充实更多功能。

路线图

以下是我们希望实现的初步功能路线图

结构体 -> JSON 解析

  • 字符串
  • 处理布尔值
  • 整数
  • 处理字符串枚举
  • 列表
  • 嵌套结构体
  • 联合类型(例如:结构体1 | 结构体2)

验证器

  • 支持不同类型的整数(例如:u8, u32, u64 -> 自动添加一个验证器,检查值是否 > 0 且 < 最大值)
  • 验证上下文(例如:我们可以通过传入原始段落来验证引用)

客户端

  • OpenAI
  • Anthropic
  • Cohere
  • Gemini
  • Mistral
  • Llama-cpp

CLI

  • 支持使用 Instructor 进行批量作业
  • 支持使用 instructor 进行微调作业
  • 监控使用情况

入门

要安装 instructor-ai,您需要在 cargo.toml 文件中添加以下内容

instructor-ai = "0.1.8"
instruct-macros = "0.1.8"
openai-api-rs = "4.1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
instruct-macros-types = "0.1.8"

然后,开始结构化提取就像使用 InstructMacro 声明一个新的结构体并导入 ParamterInfoStructInfo 类型一样简单。

use std::env;
use instruct_macros::InstructMacro;
use instruct_macros_types::{ParameterInfo, StructInfo};
use instructor_ai::from_openai;
use openai_api_rs::v1::{
    api::Client,
    chat_completion::{self, ChatCompletionRequest},
    common::GPT3_5_TURBO,
};
use serde::{Deserialize, Serialize};

fn main() {
    let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
    let instructor_client = from_openai(client);

    #[derive(InstructMacro, Debug, Serialize, Deserialize)]
    // This represents a single user
    struct UserInfo {
        // This represents the name of the user
        name: String,
        // This represents the age of the user
        age: u8,
    }

    let req = ChatCompletionRequest::new(
        GPT3_5_TURBO.to_string(),
        vec![chat_completion::ChatCompletionMessage {
            role: chat_completion::MessageRole::user,
            content: chat_completion::Content::Text(String::from(
                "John Doe is a 30 year old software engineer",
            )),
            name: None,
        }],
    );

    let result = instructor_client
        .chat_completion::<UserInfo>(req, 3)
        .unwrap();

    println!("{}", result.name); // John Doe
    println!("{}", result.age); // 30
}