schema definition gives us more control over the model's output. Specifically, we can define:possible values for each propertydescription to make sure that the model understands the propertyrequired properties to be returnedHere is an example of how we can use _enum_, _description_, and _required_ to control for each of the previously mentioned aspects:schema = {    "properties": {        "aggressiveness": {            "type": "integer",            "enum": [1, 2, 3, 4, 5],            "description": "describes how aggressive the statement is, the higher the number the more aggressive",        },        "language": {            "type": "string",            "enum": ["spanish", "english", "french", "german", "italian"],        },    },    "required": ["language", "sentiment", "aggressiveness"],}chain = create_tagging_chain(schema, llm)Now the answers are much better!inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"chain.run(inp)    {'aggressiveness': 0, 'language': 'spanish'}inp = "Estoy muy enojado con vos! Te voy a dar tu merecido!"chain.run(inp)    {'aggressiveness': 5, 'language': 'spanish'}inp = "Weather is ok here, I can go outside without much more than a coat"chain.run(inp)    {'aggressiveness': 0, 'language': 'english'}The LangSmith trace lets us peek under the hood:As with extraction, we call the information_extraction function here on the input string.This OpenAI function extraction information based upon the provided schema.Pydantic‚ÄãWe can also use a Pydantic schema to specify the required properties and types. We can also send other arguments, such as enum or description, to each field.This lets us specify our schema in the same manner that we would a new class or function in Python with purely Pythonic types.from enum import Enumfrom pydantic import BaseModel, Fieldclass Tags(BaseModel):    sentiment: str = Field(..., enum=["happy", "neutral", "sad"])    aggressiveness: int = Field(        ...,
Open In Colab
Open In Colab ->: schema definition gives us more control over the model's output. Specifically, we can define:possible values for each propertydescription to make sure that the model understands the propertyrequired properties to be returnedHere is an example of how we can use _enum_, _description_, and _required_ to control for each of the previously mentioned aspects:schema = {    "properties": {        "aggressiveness": {            "type": "integer",            "enum": [1, 2, 3, 4, 5],            "description": "describes how aggressive the statement is, the higher the number the more aggressive",        },        "language": {            "type": "string",            "enum": ["spanish", "english", "french", "german", "italian"],        },    },    "required": ["language", "sentiment", "aggressiveness"],}chain = create_tagging_chain(schema, llm)Now the answers are much better!inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"chain.run(inp)    {'aggressiveness': 0, 'language': 'spanish'}inp = "Estoy muy enojado con vos! Te voy a dar tu merecido!"chain.run(inp)    {'aggressiveness': 5, 'language': 'spanish'}inp = "Weather is ok here, I can go outside without much more than a coat"chain.run(inp)    {'aggressiveness': 0, 'language': 'english'}The LangSmith trace lets us peek under the hood:As with extraction, we call the information_extraction function here on the input string.This OpenAI function extraction information based upon the provided schema.Pydantic‚ÄãWe can also use a Pydantic schema to specify the required properties and types. We can also send other arguments, such as enum or description, to each field.This lets us specify our schema in the same manner that we would a new class or function in Python with purely Pythonic types.from enum import Enumfrom pydantic import BaseModel, Fieldclass Tags(BaseModel):    sentiment: str = Field(..., enum=["happy", "neutral", "sad"])    aggressiveness: int = Field(        ...,