mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-13 05:09:22 +03:00
Edited various notebooks in the tutorial section to fix: * Grammatical Errors * Improve Readability by changing the sentence structure or reducing repeated words which bears the same meaning * Edited a code block to follow the PEP 8 Standard * Added more information in some sentences to make the concept more clear and reduce ambiguity --------- Co-authored-by: Eugene Yurtsev <eugene@langchain.dev>
9.3 KiB
9.3 KiB
Cell:
[Cell type raw - unsupported, skipped]
In [ ]:
%pip install --upgrade --quiet langchain langchain-openai
# Set env var OPENAI_API_KEY or load from a .env file:
# import dotenv
# dotenv.load_dotenv()In [4]:
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field
tagging_prompt = ChatPromptTemplate.from_template(
"""
Extract the desired information from the following passage.
Only extract the properties mentioned in the 'Classification' function.
Passage:
{input}
"""
)
class Classification(BaseModel):
sentiment: str = Field(description="The sentiment of the text")
aggressiveness: int = Field(
description="How aggressive the text is on a scale from 1 to 10"
)
language: str = Field(description="The language the text is written in")
# LLM
llm = ChatOpenAI(temperature=0, model="gpt-4o-mini").with_structured_output(
Classification
)
tagging_chain = tagging_prompt | llmIn [6]:
inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"
tagging_chain.invoke({"input": inp})Out [6]:
Classification(sentiment='positive', aggressiveness=1, language='Spanish')
In [13]:
inp = "Estoy muy enojado con vos! Te voy a dar tu merecido!"
res = tagging_chain.invoke({"input": inp})
res.dict()Out [13]:
{'sentiment': 'negative', 'aggressiveness': 8, 'language': 'Spanish'}In [14]:
class Classification(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"]
)In [15]:
tagging_prompt = ChatPromptTemplate.from_template(
"""
Extract the desired information from the following passage.
Only extract the properties mentioned in the 'Classification' function.
Passage:
{input}
"""
)
llm = ChatOpenAI(temperature=0, model="gpt-4o-mini").with_structured_output(
Classification
)
chain = tagging_prompt | llmIn [17]:
inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"
chain.invoke({"input": inp})Out [17]:
Classification(sentiment='happy', aggressiveness=1, language='spanish')
In [18]:
inp = "Estoy muy enojado con vos! Te voy a dar tu merecido!"
chain.invoke({"input": inp})Out [18]:
Classification(sentiment='sad', aggressiveness=5, language='spanish')
In [19]:
inp = "Weather is ok here, I can go outside without much more than a coat"
chain.invoke({"input": inp})Out [19]:
Classification(sentiment='neutral', aggressiveness=2, language='english')

