跳到内容

Writer 现在支持结构化输出

我们很高兴宣布,instructor 现在支持 Writer 的企业级 LLM,包括他们最新的 Palmyra X 004 模型。此次集成使得 Writer 强大的语言模型能够实现结构化输出和企业级 AI 工作流程。

入门

首先,请确保您已在 Writer 注册账户,并使用此快速入门指南获取 API 密钥。完成此步骤后,在终端运行 pip install instructor[writer] 安装支持 Writer 的 instructor

务必将 WRITER_API_KEY 环境变量设置为您的 Writer API 密钥,或将其作为参数传递给 Writer 构造函数。

import instructor
from writerai import Writer
from pydantic import BaseModel

# Initialize Writer client
client = instructor.from_writer(Writer(api_key="your API key"))


class User(BaseModel):
    name: str
    age: int


# Extract structured data
user = client.chat.completions.create(
    model="palmyra-x-004",
    messages=[{"role": "user", "content": "Extract: John is 30 years old"}],
    response_model=User,
)

print(user)
#> name='John' age=30

注意

如果您想使用 Writer 客户端的异步版本,可以通过使用 instructor.from_writer(AsyncWriter()) 来实现。

我们还支持使用 create_partial 方法进行 Writer 客户端的流式传输。这允许您在响应到达时增量处理。

这对于保持应用程序的响应性和提供流畅的用户体验特别有价值,尤其是在处理大型响应时,用户可以立即看到结果。

import instructor
from writerai import Writer
from pydantic import BaseModel

# Initialize Writer client
client = instructor.from_writer(Writer())


text_block = """
In our recent online meeting, participants from various backgrounds joined to discuss the upcoming tech conference. The names and contact details of the participants were as follows:

- Name: John Doe, Email: johndoe@email.com, Twitter: @TechGuru44
- Name: Jane Smith, Email: janesmith@email.com, Twitter: @DigitalDiva88
- Name: Alex Johnson, Email: alexj@email.com, Twitter: @CodeMaster2023

During the meeting, we agreed on several key points. The conference will be held on March 15th, 2024, at the Grand Tech Arena located at 4521 Innovation Drive. Dr. Emily Johnson, a renowned AI researcher, will be our keynote speaker.

The budget for the event is set at $50,000, covering venue costs, speaker fees, and promotional activities. Each participant is expected to contribute an article to the conference blog by February 20th.

A follow-up meetingis scheduled for January 25th at 3 PM GMT to finalize the agenda and confirm the list of speakers.
"""


class User(BaseModel):
    name: str
    email: str
    twitter: str


class MeetingInfo(BaseModel):
    date: str
    location: str
    budget: int
    deadline: str


PartialMeetingInfo = instructor.Partial[MeetingInfo]


extraction_stream = client.chat.completions.create(
    model="palmyra-x-004",
    messages=[
        {
            "role": "user",
            "content": f"Get the information about the meeting and the users {text_block}",
        },
    ],
    response_model=PartialMeetingInfo,
    stream=True,
)  # type: ignore


for obj in extraction_stream:
    print(obj)
    #> date='March 15th, 2024' location='' budget=None deadline=None
    #> date='March 15th, 2024' location='Grand Tech Arena, 4521 Innovation' budget=None deadline=None
    #> date='March 15th, 2024' location='Grand Tech Arena, 4521 Innovation Drive' budget=50000 eadline='February 20th'

与我们所有的集成一样,instructor 具有自动重试因模式验证失败而发生的请求的能力,您无需进行任何操作。

import instructor
from typing import Annotated
from writerai import Writer
from pydantic import BaseModel, AfterValidator, Field

# Initialize Writer client
client = instructor.from_writer(Writer())


# Example of model, that may require usage of retries
def uppercase_validator(v):
    if v.islower():
        raise ValueError("Name must be in uppercase")
    return v


class User(BaseModel):
    name: Annotated[str, AfterValidator(uppercase_validator)] = Field(
        ..., description="The name of the user"
    )
    age: int


user = client.chat.completions.create(
    model="palmyra-x-004",
    messages=[{"role": "user", "content": "Extract: jason is 12"}],
    response_model=User,
    max_retries=3,
)

print(user)
#> name='JASON' age=12

这只是一个关于 Writer 和 instructor 功能的初步介绍——从文本分类到情感分析等等。

我们很高兴看到您使用 instructor 和 Writer 构建的应用。如果您对 Writer 还有其他疑问,请查阅Writer 文档以了解 API SDK。