structured llm outputs
Project description
struct-gpt
Get structured output from LLM's.
from struct_gpt import OpenAiBase
from pydantic import Field
class SentimentSchema(OpenAiBase):
"""
Determine the sentiment of the given text:
{content}
"""
# 👆this becomes the prompt
sentiment: str = Field(description="Either -1, 0, or 1.")
result = SentimentSchema.from_openai(content="I love pizza!").json()
# 👆this ends up in the prompt
print(result)
outputs:
{
"sentiment": "1"
}
Features
- Easy creation of custom models using the OpenAI API
- Integration with Pydantic for model validation and serialization
- Flexible configuration with retries and temperature settings
Installation
pip install struct-gpt
Usage
- Template variables in the class' docstring are replaced with the keyword arguments passed to
from_openai - Classes can reference one another
- You can use the
OpenAiMixinto add functionality to existing Pydantic classes
from struct_gpt import OpenAiBase, OpenAiMixin
from pydantic import Field, BaseModel
from typing import Mapping
class SentimentSchema(OpenAiBase):
sentiment: str = Field(description="Either -1, 0, or 1.")
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
print(
SentimentAnalysis.from_openai(
text="As president, I loved the beautiful scenery, but the long hike was exhausting."
).json(indent=2)
)
See Output
{
"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
from_openai 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.
output should be an instance of the model, a dictionary, or its json string representation,.
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": "I love the beach!",
"output": {"sentiment": "1"},
},
{
"input": "don't touch that",
"output": '{"sentiment": "-1"}',
},
{
"input": "this library is neat!",
"output": SentimentSchema(sentiment="1"),
},
]
print(SentimentSchema.from_openai(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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file struct_gpt-2.2.7.tar.gz.
File metadata
- Download URL: struct_gpt-2.2.7.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.6.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abcf3d53b8b0282346128101cccd27a52c5425b1bdc42f8c9b148e16a295d076
|
|
| MD5 |
c70eb2d5829708f78b029ffecda0cb4e
|
|
| BLAKE2b-256 |
0b6493c08229160fe7f8aa7d189d98ec4dbf0f83a240903a59071f5f31314c81
|
File details
Details for the file struct_gpt-2.2.7-py3-none-any.whl.
File metadata
- Download URL: struct_gpt-2.2.7-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.6.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8545dbca48ae5113748493e0f060505a8b51b88fe933277f2e9ba3d01ff843c0
|
|
| MD5 |
ba985db61c6e9f440af8bec073d17d85
|
|
| BLAKE2b-256 |
fdc619e13a7c35c41b0a066bbebd89ad85f20ca67ae9e1a0360f6e73b2eb28b0
|