对简单类型的支持¶
我们的 InstructMacro
宏现在已原生支持大多数基本类型。以下是快速了解如何使用我们宏的概述。请注意,您提供的结构体名称和描述将馈送给 OpenAI 调用 - 我们在下面展示的只是我们提供的函数参数。
导入宏¶
您需要使用以下导入来使用该宏
use instruct_macros::{InstructMacro};
use instruct_macros_types::{
InstructMacro, InstructMacroResult, Parameter, ParameterInfo, StructInfo,
};
#[derive(InstructMacro, Debug)]
struct User {
pub name: String,
pub age: String
}
/*
{
"type": "object",
"properties": {
"age": {
"type": "string",
"description": ""
},
"name": {
"type": "string",
"description": ""
}
},
"required": [
"name",
"age"
]
}
*/
添加描述¶
我们提供一个 #[description( Description goes here )]
注解,您可以将其添加到您的结构体中。这将包含在我们发送给 OpenAI/其他推理提供者的函数调用中。
对于单个字段或整个结构体,这同样适用,并且很容易实现多行注释。
#[derive(InstructMacro, Debug)]
#[description("This is a user object")]
struct User {
#[description("This is the name of the user")]
pub name: String,
#[description(
"This is\
a multi-line description\
which can be used"
)]
pub age: String,
}
/*
{
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "This is the name of the user"
},
"age": {
"type": "string",
"description": "This isa multi-line descriptionwhich can be used"
}
},
"required": [
"name",
"age"
]
}
*/
高级类型¶
枚举¶
枚举也以同样的方式支持。只需像声明普通 Serde
对象一样声明它,它就会原生无缝工作。
#[derive(InstructMacro, Debug)]
#[description("This is an enum representing the status of a person")]
pub enum Status {
Active,
Inactive,
Pending,
}
#[derive(InstructMacro, Debug)]
pub struct User {
name: String,
status: Status,
}
/*
{
"type": "object",
"properties": {
"name": {
"type": "string",
"description": ""
},
"status": {
"type": "string",
"description": "This is an enum representing the status of a person",
"enum_values": [
"Active",
"Inactive",
"Pending"
]
}
},
"required": [
"name",
"status"
]
}
*/
如果您想为结构体中的枚举字段提供自定义描述,只需使用 description
注解,我们在生成函数参数时将覆盖枚举的默认描述。
#[derive(InstructMacro, Debug)]
#[description("This is an enum representing the status of a person")]
pub enum Status {
Active,
Inactive,
Pending,
}
#[derive(InstructMacro, Debug)]
pub struct User {
name: String,
#[description("This is the person's status")]
status: Status,
}
/*
{
"type": "object",
"properties": {
"status": {
"type": "string",
"description": "This is the person's status",
"enum_values": [
"Active",
"Inactive",
"Pending"
]
},
"name": {
"type": "string",
"description": ""
}
},
"required": [
"name",
"status"
]
}
*/
向量¶
有时您可能想提取对象列表(例如用户)。为此,您可以简单地使用一个 Vec
对象。
#[derive(InstructMacro, Debug)]
#[description("This is a struct with Option types")]
struct Numbers {
#[description("This is a list of numbers")]
pub numbers: Vec<i32>,
}
/*
{
"type": "object",
"properties": {
"users": {
"type": "array",
"description": "A list of users",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": ""
}
}
}
}
},
"required": [
"users"
]
}
*/
可选类型¶
我们也支持 Option 类型。这在使用 Maybe
模式时最为常见,在这种模式下,我们有一些可能想要提取的数据。
#[derive(InstructMacro, Debug)]
#[allow(dead_code)]
#[description("This is a user struct")]
struct User {
#[description("This is the user's name")]
pub name: String,
#[description("This is the user's age")]
pub age: i32,
}
#[derive(InstructMacro, Debug)]
#[allow(dead_code)]
#[description("This is a struct with Option<user> type")]
struct MaybeUser {
#[description("This is an optional user field")]
pub user: Option<User>,
error_message: Option<String>
}
/*
{
"type": "object",
"properties": {
"user": {
"type": "object",
"description": "This is an optional user field. If the user is not present, the field will be null",
"properties": {
"age": {
"type": "number",
"description": ""
},
"name": {
"type": "string",
"description": ""
}
}
},
"error_message": {
"type": "string",
"description": ""
}
},
"required": []
}
*/
嵌套结构体¶
我们也原生支持嵌套结构体 - 请参阅下面的示例
#[derive(InstructMacro, Debug, Serialize, Deserialize)]
struct Address {
location: String,
distance: i32,
}
#[derive(InstructMacro, Debug, Serialize, Deserialize)]
struct User {
name: String,
age: u8,
address: Address,
}
/*
{
"type": "object",
"properties": {
"address": {
"type": "object",
"description": "",
"properties": {
"location": {
"type": "string",
"description": ""
},
"distance": {
"type": "number",
"description": ""
}
}
},
"name": {
"type": "string",
"description": ""
},
"age": {
"type": "number",
"description": ""
}
},
"required": [
"name",
"age",
"address"
]
}
*/