.ipynb
.pdf
Tagging
 Contents 
Simplest approach, only specifying type
More control
Specifying schema with Pydantic
Tagging#
The tagging chain uses the OpenAI functions parameter to specify a schema to tag a document with. This helps us make sure that the model outputs exactly tags that we want, with their appropriate types.
The tagging chain is to be used when we want to tag a passage with a specific attribute (i.e. what is the sentiment of this message?)
from langchain.chat_models import ChatOpenAI
from langchain.chains import create_tagging_chain, create_tagging_chain_pydantic
from langchain.prompts import ChatPromptTemplate
llm = ChatOpenAI(
    temperature=0, 
    model="gpt-3.5-turbo-0613"
)
Simplest approach, only specifying type#
We can start by specifying a few properties with their expected type in our schema
schema = {
      "properties": {
          "sentiment": {"type": "string"}, 
          "aggressiveness": {"type": "integer"},
          "language": {"type": "string"},
              }
    }
chain = create_tagging_chain(schema, llm)
As we can see in the examples, it correctly interprets what we want but the results vary so that we get, for example, sentiments in different languages (‘positive’, ‘enojado’ etc.).
We will see how to control these results in the next section.
inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"
chain.run(inp)
{'sentiment': 'positive', 'language': 'Spanish'}
inp = "Estoy muy enojado con vos! Te voy a dar tu merecido!"
chain.run(inp)
rtdocs_stable/api.python.langchain.com/en/stable/modules/chains/examples/tagging.html