structured llm outputs
Project description
struct-gpt
structured llm outputs
Usage
Template variables in the class' docstring are replaced with the keyword arguments passed to create
.
from struct_gpt import OpenAiBase
from pydantic import Field
class SentimentSchema(OpenAiBase):
"""
Determine the sentiment of the given text:
{content}
"""
sentiment: str = Field(description="Either -1, 0, or 1.")
print(SentimentSchema.create(content="I love pizza!").json())
outputs:
{
"sentiment": "1"
}
Your classes can reference one another. You can also use the OpenAiMixin
to add functionality to your own classes.
from struct_gpt import OpenAiBase, OpenAiMixin
from pydantic import Field, BaseModel
from typing import Mapping
class SentimentSchema(OpenAiBase):
"""
Determine the sentiment of the given text:
{content}
"""
sentiment: str = Field(description="Either -1, 0, or 1.")
# you can use the OpenAiMixin to add functionality to your own classes
class SentimentAnalysis(BaseModel, OpenAiMixin):
"""
Determine the sentiment of each word in the following: {text}
Also determine the overall sentiment of the text and who the narrator is.
"""
words_to_sentiment: Mapping[str, SentimentSchema]
overall_sentiment: SentimentSchema
narrator: str = Field(description="The narrator of the text.")
print(
SentimentAnalysis.create(
text="As president, I loved the beautiful scenery, but the long hike was exhausting."
).json(indent=2)
)
outputs
{
"words_to_sentiment": {
"As": {
"sentiment": "0"
},
"president,": {
"sentiment": "1"
},
"I": {
"sentiment": "0"
},
"loved": {
"sentiment": "1"
},
"the": {
"sentiment": "0"
},
"beautiful": {
"sentiment": "1"
},
"scenery,": {
"sentiment": "1"
},
"but": {
"sentiment": "-1"
},
"long": {
"sentiment": "-1"
},
"hike": {
"sentiment": "-1"
},
"was": {
"sentiment": "0"
},
"exhausting.": {
"sentiment": "-1"
}
},
"overall_sentiment": {
"sentiment": "0"
},
"narrator": "president"
}
Improving reliability with examples
create
can accept a list of examples to guide the model and improve its accuracy. Each example is a dictionary containing an input
and output
key. The input
is the user message and the output
is the expected assistant message, which should be a valid instance of the schema serialized as a string.
In this example, we are providing the model with examples of positive and negative sentiments:
from struct_gpt import OpenAiBase
from pydantic import Field
class SentimentSchema(OpenAiBase):
"""
Determine the sentiment of the given text:
{content}
"""
sentiment: str = Field(description="Either -1, 0, or 1.")
examples = [
{
"input": "this library is neat!",
"output": SentimentSchema(sentiment="1").json(),
},
{
"input": "don't touch that",
"output": SentimentSchema(sentiment="-1").json(),
},
]
print(SentimentSchema.create(content="I love pizza!", examples=examples).json())
outputs:
{
"sentiment": "1"
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for struct_gpt-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39f050d9ddf2fdbcac9ec70969ff2bcbc833a10c8e8fecbecc3d1ace91bebea9 |
|
MD5 | 67bdb95a54301db0d3103752efe08a72 |
|
BLAKE2b-256 | 9683bb0443c340469668f73a65966690a198c4e7c3c99eba8078c0c861aa7e38 |