Skip to main content

structured llm outputs

Project description

struct-gpt

structured llm outputs

Usage

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.")

positive = SentimentSchema.create(content="I love pizza!")
print(f"{positive = }") # positive = SentimentSchema(sentiment='1')
class SentimentAnalysis(OpenAiBase):
    """
    Determine the sentiment of each word in the following: {text}
    """

    sentiment: Mapping[str, SentimentSchema]


analysis = SentimentAnalysis.create(text="I love the beautiful scenery, but the long hike was exhausting.")
print(analysis.json(indent=2))

outputs:

{
  "sentiment": {
    "I": {
      "sentiment": "1"
    },
    "love": {
      "sentiment": "1"
    },
    "the": {
      "sentiment": "0"
    },
    "beautiful": {
      "sentiment": "1"
    },
    "scenery,": {
      "sentiment": "1"
    },
    "but": {
      "sentiment": "0"
    },
    "long": {
      "sentiment": "-1"
    },
    "hike": {
      "sentiment": "-1"
    },
    "was": {
      "sentiment": "0"
    },
    "exhausting.": {
      "sentiment": "-1"
    }
  }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

struct_gpt-0.1.0.tar.gz (2.7 kB view hashes)

Uploaded Source

Built Distribution

struct_gpt-0.1.0-py3-none-any.whl (3.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page