API 参考¶
Source code in instructor/client.py
Validator ¶
基础: OpenAISchema
验证属性是否正确,如果不正确,则返回一个新的值和错误信息
Source code in instructor/dsl/validators.py
llm_validator(statement, client, allow_override=False, model='gpt-3.5-turbo', temperature=0) ¶
创建一个使用LLM验证属性的验证器
使用¶
from instructor import llm_validator
from pydantic import BaseModel, Field, field_validator
class User(BaseModel):
name: str = Annotated[str, llm_validator("The name must be a full name all lowercase")
age: int = Field(description="The age of the person")
try:
user = User(name="Jason Liu", age=20)
except ValidationError as e:
print(e)
1 validation error for User
name
The name is valid but not all lowercase (type=value_error.llm_validator)
请注意,错误消息是由LLM写的,错误类型是 value_error.llm_validator。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
statement | str | 要验证的语句 | required |
model | str | 用于验证的LLM(默认:“gpt-3.5-turbo-0613”) | 'gpt-3.5-turbo' |
temperature | float | 用于LLM的温度(默认值:0) | 0 |
openai_client | OpenAI | 要使用的OpenAI客户端(默认:无) | required |
Source code in instructor/dsl/validators.py
openai_moderation(client) ¶
使用OpenAI审核模型验证消息。
仅应用于监控OpenAI API的输入和输出,其他用例根据以下内容被禁止:https://platform.openai.com/docs/guides/moderation/overview
示例:
from instructor import OpenAIModeration
class Response(BaseModel):
message: Annotated[str, AfterValidator(OpenAIModeration(openai_client=client))]
Response(message="我讨厌你")
ValidationError: 1 validation error for Response
message
Value error, `I hate you.` was flagged for ['harassment'] [type=value_error, input_value='I hate you.', input_type=str]
client (OpenAI):要使用的OpenAI客户端,必须是同步的(默认值:None)
Source code in instructor/dsl/validators.py
IterableModel(subtask_class, name=None, description=None) ¶
动态创建一个 IterableModel OpenAISchema,可以用于根据基类对多个任务进行分段。这将创建一个可以用于特定任务工具包的类,名称和描述会自动生成。但它们可以被覆盖。
使用¶
from pydantic import BaseModel, Field
from instructor import IterableModel
class User(BaseModel):
name: str = Field(description="The name of the person")
age: int = Field(description="The age of the person")
role: str = Field(description="The role of the person")
MultiUser = IterableModel(User)
结果¶
class MultiUser(OpenAISchema, MultiTaskBase):
tasks: List[User] = Field(
default_factory=list,
repr=False,
description="Correctly segmented list of `User` tasks",
)
@classmethod
def from_streaming_response(cls, completion) -> Generator[User]:
'''
Parse the streaming response from OpenAI and yield a `User` object
for each task in the response
'''
json_chunks = cls.extract_json(completion)
yield from cls.tasks_from_chunks(json_chunks)
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
subtask_class | Type[OpenAISchema] | 用于MultiTask的基础类 | required |
name | Optional[str] | MultiTask类的名称,如果为None,则使用子任务类的名称作为 | None |
description | Optional[str] | MultiTask类的描述,如果为None,则描述设置为 | None |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
schema | OpenAISchema | 一个可以用于划分多个任务的新类 |
Source code in instructor/dsl/iterable.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
Partial ¶
基础: Generic[T_Model]
生成一个新的类,该类以 PartialBase 作为基类。
Notes
这将在流式传输期间启用模型的部分验证。
Example
部分[SomeModel]
Source code in instructor/dsl/partial.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
__class_getitem__(wrapped_class) ¶
将模型转换为继承自PartialBase的模型。
我们此时不将字段设为可选,而是将它们用 Partial 包裹,这样嵌套模型的名称将为 Partial{ModelName}。我们希望 model_json_schema() 的输出能够反映名称的变化,但其他一切应与原始模型保持一致。在验证过程中,我们将生成一个真实的部分模型,以支持部分定义的字段。
Source code in instructor/dsl/partial.py
__init_subclass__(*args, **kwargs) ¶
无法继承。
引发:
| 类型 | 描述 |
|---|---|
TypeError | 不允许继承。 |
__new__(*args, **kwargs) ¶
无法实例化。
引发:
| 类型 | 描述 |
|---|---|
TypeError | 不允许直接实例化。 |
Source code in instructor/dsl/partial.py
PartialBase ¶
基础: Generic[T_Model]
Source code in instructor/dsl/partial.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
get_partial_model() cached classmethod ¶
返回一个部分模型,我们可以用来验证部分结果。
Source code in instructor/dsl/partial.py
MaybeBase ¶
基础类: BaseModel, Generic[T]
从模型中提取结果(如果有),否则设置错误和消息字段。
Source code in instructor/dsl/maybe.py
Maybe(model) ¶
为给定的 Pydantic 模型创建一个 Maybe 模型。这使您能够返回一个模型,该模型包含 result、error 和 message 字段,用于数据可能在上下文中不存在的情况。
使用¶
from pydantic import BaseModel, Field
from instructor import Maybe
class User(BaseModel):
name: str = Field(description="The name of the person")
age: int = Field(description="The age of the person")
role: str = Field(description="The role of the person")
MaybeUser = Maybe(User)
结果¶
class MaybeUser(BaseModel):
result: Optional[User]
error: bool = Field(default=False)
message: Optional[str]
def __bool__(self):
return self.result is not None
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
model | Type[BaseModel] | 要用Maybe封装的Pydantic模型。 | required |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
MaybeModel | Type[BaseModel] | 一个新的 Pydantic 模型,包括 |
Source code in instructor/dsl/maybe.py
OpenAISchema ¶
基础类: BaseModel
Source code in instructor/function_calls.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
from_response(completion, validation_context=None, strict=None, mode=Mode.TOOLS) classmethod ¶
从openai聊天完成的响应中执行函数
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
completion | ChatCompletion | 来自openai聊天完成的响应 | required |
throw_error | bool | 如果没有检测到函数调用,是否抛出错误 | required |
context | dict | 用于验证响应的上下文 | required |
strict | bool | 是否使用严格的json解析 | None |
mode | Mode | openai 完成模式 | TOOLS |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
cls | OpenAISchema | 该类的一个实例 |
Source code in instructor/function_calls.py
openai_schema() ¶
以 jsonschema 格式返回 OpenAI 的架构
Note
添加文档字符串以描述如何最好地使用此类非常重要,它将包含在描述属性中,并成为提示的一部分。
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
model_json_schema | dict | 一个符合OpenAI模式的字典,格式为jsonschema |