3 个版本
0.1.8 | 2024年7月8日 |
---|---|
0.1.7 | 2024年7月8日 |
0.1.0 | 2024年6月23日 |
#4 in #outputs
每月下载量 43
43KB
871 行
instructor-rs
Instructor 是一个 Rust 库,使得处理大型语言模型(LLMs)的结构化输出变得非常容易。它提供了一个简单易用的 API,通过抽象验证、重试和流式响应来帮助管理 LLM 工作流程。
现在,让我们通过一个简单的例子看看 Instructor 的使用方法。
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
结构化验证
我们可以使用内建的 serde 函数来处理特定值的验证。
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
#[validate(custom = "validate_uppercase")]
name: String,
// This represents the age of the user
age: u8,
}
#[validate]
fn validate_uppercase(name: &String) -> Result<String, String> {
if name.chars().any(|c| c.is_lowercase()) {
return Err(format!(
"Name '{}' should be entirely in uppercase. Examples: 'TIMOTHY', 'JANE SMITH'",
name
));
}
Ok(name.to_uppercase())
}
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
依赖项
~9–17MB
~314K SLoC