chain.run(inp)
{'sentiment': 'sad', 'aggressiveness': 10, 'language': 'spanish'}
inp = "Weather is ok here, I can go outside without much more than a coat"
chain.run(inp)
{'sentiment': 'neutral', 'aggressiveness': 0, 'language': 'english'}
Specifying schema with 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’ as can be seen in the example below.
By using the create_tagging_chain_pydantic function, we can send a Pydantic schema as input and the output will be an instantiated object that respects our desired schema.
In this way, we can specify our schema in the same manner that we would a new class or function in Python - with purely Pythonic types.
from enum import Enum
from pydantic import BaseModel, Field
class Tags(BaseModel):
    sentiment: str = Field(..., enum=["happy", "neutral", "sad"])
    aggressiveness: int = Field(..., description="describes how aggressive the statement is, the higher the number the more aggressive", enum=[1, 2, 3, 4, 5])
    language: str = Field(..., enum=["spanish", "english", "french", "german", "italian"])
chain = create_tagging_chain_pydantic(Tags, llm)
inp = "Estoy muy enojado con vos! Te voy a dar tu merecido!"
res = chain.run(inp)
res
Tags(sentiment='sad', aggressiveness=10, language='spanish')
previous
SQL Chain example
next
Chains
 Contents
  
Simplest approach, only specifying type
More control
rtdocs_stable/api.python.langchain.com/en/stable/modules/chains/examples/tagging.html