Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKQA over structured dataSQLRetrieval-augmented generation (RAG)Interacting with APIsChatbotsExtractionSummarizationTaggingWeb scrapingSynthetic data generationGraph queryingTaggingOn this pageTaggingUse case‚ÄãTagging means labeling a document with classes such as:sentimentlanguagestyle (formal, informal etc.)covered topicspolitical tendencyOverview‚ÄãTagging has a few components:function: Like extraction, tagging uses functions to specify how the model should tag a documentschema: defines how we want to tag the documentQuickstart‚ÄãLet's see a very straightforward example of how we can use OpenAI functions for tagging in LangChain.pip install langchain openai # Set env var OPENAI_API_KEY or load from a .env file:# import dotenv# dotenv.load_dotenv()from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplatefrom langchain.chains import create_tagging_chain, create_tagging_chain_pydanticWe specify a few properties with their expected type in our schema.# Schemaschema = {    "properties": {        "sentiment": {"type": "string"},        "aggressiveness": {"type": "integer"},        "language": {"type": "string"},    }}# LLMllm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")chain = create_tagging_chain(schema, llm)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)    {'sentiment': 'enojado', 'aggressiveness': 1, 'language': 'es'}As we can see in the examples, it correctly interprets what we want.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.Finer control‚ÄãCareful schema definition gives us more control over
Open In Colab
Open In Colab ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKQA over structured dataSQLRetrieval-augmented generation (RAG)Interacting with APIsChatbotsExtractionSummarizationTaggingWeb scrapingSynthetic data generationGraph queryingTaggingOn this pageTaggingUse case‚ÄãTagging means labeling a document with classes such as:sentimentlanguagestyle (formal, informal etc.)covered topicspolitical tendencyOverview‚ÄãTagging has a few components:function: Like extraction, tagging uses functions to specify how the model should tag a documentschema: defines how we want to tag the documentQuickstart‚ÄãLet's see a very straightforward example of how we can use OpenAI functions for tagging in LangChain.pip install langchain openai # Set env var OPENAI_API_KEY or load from a .env file:# import dotenv# dotenv.load_dotenv()from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplatefrom langchain.chains import create_tagging_chain, create_tagging_chain_pydanticWe specify a few properties with their expected type in our schema.# Schemaschema = {    "properties": {        "sentiment": {"type": "string"},        "aggressiveness": {"type": "integer"},        "language": {"type": "string"},    }}# LLMllm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")chain = create_tagging_chain(schema, llm)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)    {'sentiment': 'enojado', 'aggressiveness': 1, 'language': 'es'}As we can see in the examples, it correctly interprets what we want.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.Finer control‚ÄãCareful schema definition gives us more control over