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'}