from dotenv import load_dotenv
import os
# Load the .env file
load_dotenv(dotenv_path=".env") # or just load_dotenv() if file is named `.env`
os.environ["OPENAI_API_KEY"]=os.getenv("OPENAI_API_KEY")
LLMmodel="gpt-4.1-nano" #provide the LLM Model name
from langchain_openai import ChatOpenAI
#temperature denotes the level of accuracy in the output, 0.0 is the most accurate form and higher the number return will be random accordingly
llmmodel = ChatOpenAI(model=LLMmodel,temperature=0.0)
llmmodel2 = ChatOpenAI(model=LLMmodel,temperature=1.0)
article="One Piece, created by Eiichiro Oda, is a global anime phenomenon that has captivated audiences for over two decades. It follows Monkey D. Luffy, a rubber-bodied boy, and his diverse crew of Straw Hat Pirates, as they journey across the Grand Line in search of the legendary One Piece treasure. This treasure, left by the Pirate King Gol D. Roger, promises ultimate freedom and the title of the next Pirate King. The series is celebrated for its vibrant characters, epic battles, profound themes of friendship, freedom, and justice, and its intricate world-building. With its blend of humor, emotion, and thrilling adventure, One Piece continues to inspire and entertain millions worldwide."
from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate
# Defining the system prompt (how the AI should act)
system_prompt = SystemMessagePromptTemplate.from_template(
"You are a very helpful AI Assistant whose name is {name} ",input_variable=["name"]
)
# the user prompt is provided by the user, in this case however the only dynamic
# input is the article
user_prompt=HumanMessagePromptTemplate.from_template("""
Provide a suitable title for this article :
---
{article}
---
Dont give any other text or explanation apart from the title
""",input_variable=["article"]
)
from langchain.prompts import ChatPromptTemplate
first_prompt = ChatPromptTemplate.from_messages([system_prompt,user_prompt])
chain_one = (
{"article": lambda x: x["article"],
"name": lambda x: x["name"]
}
| first_prompt
| llmmodel2
| {"article_title": lambda x: x.content}
)
article_title_msg = chain_one.invoke(
{
"article": article,
"name":"Joe"
})
article_title_msg
{'article_title': 'The Epic Adventure of One Piece: A Cultural Phenomenon'}
langchain.prompts
: This is a module that provides tools for structuring and managing prompts for various language models.SystemMessagePromptTemplate
: This is a class used to define a prompt that sets the AI's persona, instructions, or contextual rules for the conversation.HumanMessagePromptTemplate
: This is a class used to define a prompt that represents input or questions from the human user.ChatOpenAI
: This is a class that serves as LangChain's interface for interacting with OpenAI's chat-based language models (like GPT-3.5 Turbo or GPT-4).langchain_openai
: This is a module that provides the specific integration components for OpenAI models within the LangChain ecosystem.ChatPromptTemplate
: This is a class that combines different types of messages (system, human, AI) into a structured prompt suitable for chat models.ChatPromptTemplate.from_messages
: This is a class method (a method called directly on the class, not an instance) used to create a ChatPromptTemplate
by providing it a list of individual message templates.chain_one.invoke
: This is an instance method (a method called on an object instance, like chain_one
) used to execute a LangChain "chain" by passing input and getting the output.