Structured Outputs with Writer now supported¶
We're excited to announce that instructor
now supports Writer's enterprise-grade LLMs, including their latest Palmyra X 004 model. This integration enables structured outputs and enterprise AI workflows with Writer's powerful language models.
Getting Started¶
First, make sure that you've signed up for an account on Writer and obtained an API key using this quickstart guide. Once you've done so, install instructor
with Writer support by running pip install instructor[writer]
in your terminal.
Make sure to set the WRITER_API_KEY
environment variable with your Writer API key or pass it as an argument to the Writer
constructor.
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
Note
If you'd like to use the Async version of the Writer client, you can do so by using instructor.from_writer(AsyncWriter())
.
We also support streaming with the Writer client using our create_partial
method. This allows you to process responses incrementally as they arrive.
This is particularly valuable for maintaining responsive applications and delivering a smooth user experience, especially when dealing with larger responses so that users can see immediate results.
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: [email protected], Twitter: @TechGuru44
- Name: Jane Smith, Email: [email protected], Twitter: @DigitalDiva88
- Name: Alex Johnson, Email: [email protected], 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'
As with all our integrations, instructor
ships with the ability to automatically retry requests that happen due to schema validation without you having to do anything.
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
This was a sneak peek into the things that you can do with Writer and instructor
- from classification of text to sentimen analysis and more.
We're excited to see what you build with instructor
and Writer. If you have any other questions about writer, do check out the Writer Documentation for the API sdk.