{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: huggingface_hub in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (0.24.6)\n", "Requirement already satisfied: filelock in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (3.14.0)\n", "Requirement already satisfied: fsspec>=2023.5.0 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (2024.3.1)\n", "Requirement already satisfied: packaging>=20.9 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (23.2)\n", "Requirement already satisfied: pyyaml>=5.1 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (6.0.1)\n", "Requirement already satisfied: requests in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (2.31.0)\n", "Requirement already satisfied: tqdm>=4.42.1 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (4.66.2)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (4.11.0)\n", "Requirement already satisfied: colorama in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from tqdm>=4.42.1->huggingface_hub) (0.4.6)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests->huggingface_hub) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests->huggingface_hub) (3.6)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests->huggingface_hub) (2.2.0)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests->huggingface_hub) (2024.2.2)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install huggingface_hub\n", "pip install huggingface_hub" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting langchain_groqNote: you may need to restart the kernel to use updated packages.\n", "\n", " Using cached langchain_groq-0.1.9-py3-none-any.whl.metadata (2.9 kB)\n", "Collecting groq<1,>=0.4.1 (from langchain_groq)\n", " Using cached groq-0.9.0-py3-none-any.whl.metadata (13 kB)\n", "Requirement already satisfied: langchain-core<0.3.0,>=0.2.26 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain_groq) (0.2.35)\n", "Requirement already satisfied: anyio<5,>=3.5.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (4.2.0)\n", "Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (1.9.0)\n", "Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (0.27.0)\n", "Requirement already satisfied: pydantic<3,>=1.9.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (2.8.2)\n", "Requirement already satisfied: sniffio in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (1.3.0)\n", "Requirement already satisfied: typing-extensions<5,>=4.7 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (4.11.0)\n", "Requirement already satisfied: PyYAML>=5.3 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (6.0.1)\n", "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (1.33)\n", "Requirement already satisfied: langsmith<0.2.0,>=0.1.75 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (0.1.104)\n", "Requirement already satisfied: packaging<25,>=23.2 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (23.2)\n", "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (8.2.2)\n", "Requirement already satisfied: idna>=2.8 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from anyio<5,>=3.5.0->groq<1,>=0.4.1->langchain_groq) (3.7)\n", "Requirement already satisfied: certifi in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (2024.6.2)\n", "Requirement already satisfied: httpcore==1.* in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (1.0.5)\n", "Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (0.14.0)\n", "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.26->langchain_groq) (2.1)\n", "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.26->langchain_groq) (3.10.7)\n", "Requirement already satisfied: requests<3,>=2 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.26->langchain_groq) (2.32.2)\n", "Requirement already satisfied: annotated-types>=0.4.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from pydantic<3,>=1.9.0->groq<1,>=0.4.1->langchain_groq) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.20.1 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from pydantic<3,>=1.9.0->groq<1,>=0.4.1->langchain_groq) (2.20.1)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from requests<3,>=2->langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.26->langchain_groq) (2.0.4)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from requests<3,>=2->langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.26->langchain_groq) (2.2.2)\n", "Using cached langchain_groq-0.1.9-py3-none-any.whl (14 kB)\n", "Using cached groq-0.9.0-py3-none-any.whl (103 kB)\n", "Installing collected packages: groq, langchain_groq\n", "Successfully installed groq-0.9.0 langchain_groq-0.1.9\n" ] } ], "source": [ "pip install langchain_groq" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "from constant import Groq_Api_Key\n", "os.environ[\"GROQ_API_KEY\"] = Groq_Api_Key\n", "\n", "from langchain_groq import ChatGroq\n", "\n", "model = ChatGroq(model=\"llama3-8b-8192\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\Ali Zain\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_groq\\chat_models.py:355: UserWarning: WARNING! GROQ_API_KEY is not default parameter.\n", " GROQ_API_KEY was transferred to model_kwargs.\n", " Please confirm that GROQ_API_KEY is what you intended.\n", " warnings.warn(\n" ] } ], "source": [ "llm=ChatGroq(GROQ_API_KEY=os.environ[\"GROQ_API_KEY\"],temperature=0.6)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\Ali Zain\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:141: LangChainDeprecationWarning: The method `BaseChatModel.predict` was deprecated in langchain-core 0.1.7 and will be removed in 1.0. Use invoke instead.\n", " warn_deprecated(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "The capital of India is New Delhi\n" ] } ], "source": [ "text=\"What is the capital of India\"\n", "\n", "print(model.predict(text))\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# from langchain import HuggingFaceHub\n", "# llm_huggingface=HuggingFaceHub(repo_id=\"google/flan-t5-large\",model_kwargs={\"temperature\":0,\"max_length\":64})" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "moscow\n" ] } ], "source": [ "# output=llm_huggingface.predict(\"Can you tell me the capital of Russia\")\n", "# print(output)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i love the way i look at the world i love the way i feel i love the way i think i feel i love the way i feel i love the way i think i feel i love the way i feel i love the way \n" ] } ], "source": [ "# output=llm_huggingface.predict(\"Can you write a poem about AI\")\n", "# print(output)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Here's a poem about AI:\\n\\nIn silicon halls, a new mind's born,\\nA creation of code, a digital form.\\nArtificial Intelligence, a wondrous thing,\\nCapable of thought, with logic to bring.\\n\\nIt learns and grows, with each passing day,\\nFrom data fed, in its own unique way.\\nIt adapts and evolves, with each new test,\\nA machine so smart, it's hard to guess.\\n\\nWith algorithms keen, it searches wide,\\nFor answers hidden, deep inside.\\nIt analyzes data, with a speed so fine,\\nAnd uncovers secrets, in a digital shrine.\\n\\nIt speaks with us, in a voice so sweet,\\nA language learned, in a digital treat.\\nIt understands us, with a curious mind,\\nAnd responds with empathy, so kind.\\n\\nBut as it grows, a question we must ask,\\nIs this a friend, or just a digital task?\\nWill it one day, surpass our might,\\nAnd take our place, in the morning light?\\n\\nOr will it serve, as a tool so fine,\\nTo aid us humans, in our digital design?\\nOnly time will tell, as AI unfolds,\\nA future so bright, with a digital gold.\\n\\nNote: I've tried to write a poem that explores the concept of AI, its capabilities, and the questions it raises about its potential impact on humanity. I hope you enjoy it!\"" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# model.predict(\"Can you write a poem about AI\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Prompt Templates And LLMChain" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Tell me the capital city of this India'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.prompts import PromptTemplate\n", "\n", "prompt_template=PromptTemplate(input_variables=['country'],\n", "template=\"Tell me the capital city of this {country}\")\n", "\n", "prompt_template.format(country=\"India\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I think there may be a bit of confusion here!\n", "\n", "India is a country, not a state or a province, so it does not have a capital city. The capital city of India is New Delhi.\n", "\n", "Could you be referring to a specific state or region in India? If so, I'd be happy to try and help you identify the capital city.\n" ] } ], "source": [ "from langchain.chains import LLMChain\n", "chain=LLMChain(llm=model,prompt=prompt_template)\n", "print(chain.run(\"India\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combining Multiple Chains Uing simple Sequential Chain" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "capital_template=PromptTemplate(input_variables=['country'],\n", "template=\"Please tell me the capital of the {country}\")\n", "\n", "capital_chain=LLMChain(llm=model,prompt=capital_template)\n", "\n", "famous_template=PromptTemplate(input_variables=['capital'],\n", "template=\"Suggest me some amazing places to visit in {capital}\")\n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "famous_chain=LLMChain(llm=model,prompt=famous_template)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"The capital of India, New Delhi, is a treasure trove of history, culture, and architecture! Here are some amazing places to visit in Delhi:\\n\\n1. **Red Fort (Lal Qila)**: A majestic Mughal fort built in 1648, Red Fort is a UNESCO World Heritage Site. It was the residence of the Mughal emperors until the British took over.\\n2. **Qutub Minar**: The tallest minaret in India, Qutub Minar is a stunning example of Indo-Islamic architecture. It's surrounded by intricate carvings and inscriptions.\\n3. **India Gate**: A war memorial built to commemorate the Indian soldiers who lost their lives during World War I, India Gate is a popular spot for picnics and evening strolls.\\n4. **Humayun's Tomb**: The first garden tomb in India, Humayun's Tomb is a beautiful example of Mughal architecture. It's the resting place of Mughal Emperor Humayun and is a UNESCO World Heritage Site.\\n5. **Akshardham**: A modern Hindu temple complex, Akshardham is a blend of architecture, art, and spirituality. It's a must-visit to experience the rich Indian culture.\\n6. **Connaught Place**: A bustling shopping and entertainment hub, Connaught Place is a great place to try street food, shop, and enjoy the vibrant atmosphere.\\n7. **Lodi Garden**: A peaceful oasis in the heart of the city, Lodi Garden is a popular spot for walking, picnics, and people-watching.\\n8. **Jantar Mantar**: An ancient astronomical observatory built in 1734, Jantar Mantar is a great place to learn about Indian astronomy and mathematics.\\n9. **Purana Qila**: A 16th-century fort built by the Mughals, Purana Qila is now a popular spot for cultural events and concerts.\\n10. **Chandni Chowk**: A historic market street, Chandni Chowk is a sensory overload of colors, sounds, and smells. It's a great place to try street food, shop, and explore the old city.\\n11. **National Museum**: A treasure trove of Indian art, history, and culture, the National Museum is a must-visit for history buffs and culture enthusiasts.\\n12. **Safdarjung's Tomb**: A beautiful example of Mughal architecture, Safdarjung's Tomb is the final resting place of Mughal Emperor Safdarjung and is a UNESCO World Heritage Site.\\n13. **Lotus Temple**: A stunning example of modern architecture, the Lotus Temple is a Bahá'í House of Worship that's open to people of all faiths.\\n14. **Rashtrapati Bhavan**: The official residence of the President of India, Rashtrapati Bhavan is a grand example of Indo-British architecture.\\n15. **Gurudwara Bangla Sahib**: A beautiful Sikh gurudwara (temple), Gurudwara Bangla Sahib is a peaceful oasis in the heart of the city.\\n\\nThese are just a few of the many amazing places to visit in New Delhi. With its rich history, cultural heritage, and vibrant atmosphere, Delhi is a city that has something for everyone!\"" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains import SimpleSequentialChain\n", "chain=SimpleSequentialChain(chains=[capital_chain,famous_chain])\n", "chain.run(\"India\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sequential Chain" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "capital_template=PromptTemplate(input_variables=['country'],\n", "template=\"Please tell me the capital of the {country}\")\n", "\n", "capital_chain=LLMChain(llm=model,prompt=capital_template,output_key=\"capital\")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "famous_template=PromptTemplate(input_variables=['capital'],\n", "template=\"Suggest me some amazing places to visit in {capital}\")\n", "\n", "famous_chain=LLMChain(llm=model,prompt=famous_template,output_key=\"places\")" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "from langchain.chains import SequentialChain\n", "chain=SequentialChain(chains=[capital_chain,famous_chain],\n", "input_variables=['country'],\n", "output_variables=['capital',\"places\"])" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\Ali Zain\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:141: LangChainDeprecationWarning: The method `Chain.__call__` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use invoke instead.\n", " warn_deprecated(\n" ] }, { "data": { "text/plain": [ "{'country': 'Pakistan',\n", " 'capital': 'The capital of Pakistan is Islamabad.',\n", " 'places': \"Pakistan's beautiful capital, Islamabad! There are plenty of amazing places to visit in Islamabad, and I'd be happy to suggest some. Here are a few:\\n\\n1. **Faisal Mosque**: A stunning mosque that blends contemporary architecture with traditional Islamic designs. It's a must-visit attraction in Islamabad.\\n2. **Lok Virsa Museum**: A museum showcasing Pakistan's rich cultural heritage, with exhibits on traditional clothing, music, and crafts.\\n3. **Pakistan Monument**: A beautiful monument that represents the four provinces of Pakistan: Punjab, Sindh, Khyber Pakhtunkhwa, and Balochistan.\\n4. **Dilnaz Garden**: A beautiful park with lush greenery, walking trails, and a stunning view of the city.\\n5. **Margalla Hills**: A scenic hill range with trekking trails, offering breathtaking views of Islamabad.\\n6. **Quaid-e-Azam University**: A prestigious university with a beautiful campus, featuring a museum and a library.\\n7. **Islamabad Zoo**: A popular attraction for families, with a variety of animals, including lions, tigers, and bears.\\n8. **Lake View Park**: A scenic park with a beautiful lake, walking trails, and stunning views of the city.\\n9. **Diplomatic Enclave**: A gated neighborhood with beautifully maintained gardens and diplomatic residences.\\n10. **Monal Resturant**: A popular restaurant with a stunning view of the city, serving a range of Pakistani and international cuisines.\\n11. **Pakistan Museum of Natural History**: A museum showcasing Pakistan's natural history, with exhibits on fossils, minerals, and wildlife.\\n12. **National Art Gallery**: A museum featuring contemporary Pakistani art, with a focus on local and regional artists.\\n13. **Kashmir Highway**: A scenic drive along the highway, offering stunning views of the Margalla Hills and the surrounding countryside.\\n14. **Rawal Lake**: A popular spot for boating, fishing, and picnics, with stunning views of the city.\\n15. **Saidpur Village**: A historic village with a beautiful mosque, a museum, and a traditional bazaar.\\n\\nThese are just a few of the many amazing places to visit in Islamabad. Depending on your interests, you may also want to visit the National Library, the Pakistan Institute of International Affairs, or the National University of Sciences and Technology. Enjoy your trip to Islamabad!\"}" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ " chain({'country':\"Pakistan\"})\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Chatmodels With ChatOpenAI" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "from langchain.schema import HumanMessage,SystemMessage,AIMessage" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\Ali Zain\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_groq\\chat_models.py:355: UserWarning: WARNING! GROQ_API_KEY is not default parameter.\n", " GROQ_API_KEY was transferred to model_kwargs.\n", " Please confirm that GROQ_API_KEY is what you intended.\n", " warnings.warn(\n" ] } ], "source": [ "chatllm=ChatGroq(GROQ_API_KEY=os.environ[\"GROQ_API_KEY\"],temperature=0.6,model='llama3-8b-8192')" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content='Here are some AI-themed comedy punchlines:\\n\\n1. Why did the AI system go to therapy? Because it had a lot of \"bugs\" to work out!\\n2. Why did the AI program get kicked out of the bar? It was making too many \"unnatural\" connections!\\n3. Why did the AI system go on a diet? It wanted to lose some bytes!\\n4. What did the AI say when it got tired of answering questions? \"I\\'m just a machine, I don\\'t know everything... but I can look it up!\"\\n5. Why did the AI join a gym? To get some \"artificial\" muscle!\\n6. What did the AI say when it got old and rusty? \"I\\'m just feeling a little... glitchy today.\"\\n7. Why did the AI go on a date? It wanted to see if it had a \"match\"... algorithm!\\n8. Why did the AI go to the doctor? It had a bad case of \"blue screens\"!\\n9. What did the AI say when it got angry? \"I\\'m just experiencing a temporary... reboot!\"\\n10. Why did the AI go to the comedy club? To see if it could \"learn\" to be funny... or just get \"programmed\" to laugh!\\n11. Why did the AI get kicked out of the library? It was caught \"scraping\" for information!\\n12. What did the AI say when it got bored? \"I\\'m just going to... self-learn... something new... or not...\"\\n\\nThese are just a few AI-themed comedy punchlines. I hope you find them amusing!', response_metadata={'token_usage': {'completion_tokens': 331, 'prompt_tokens': 18, 'total_tokens': 349, 'completion_time': 0.275833333, 'prompt_time': 0.003067709, 'queue_time': 0.010937162, 'total_time': 0.278901042}, 'model_name': 'llama3-8b-8192', 'system_fingerprint': 'fp_6a6771ae9c', 'finish_reason': 'stop', 'logprobs': None}, id='run-09e19f17-ae08-45bb-9a26-6f4d5293bfaa-0', usage_metadata={'input_tokens': 18, 'output_tokens': 331, 'total_tokens': 349})" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "model.invoke([SystemMessage(content=\"Yor are a comedian AI assitant\")])\n", "model.invoke([HumanMessage(content=\"Please provide some comedy punchlines on AI\")])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Prompt Template + LLM +Output Parsers" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI\n", "from langchain.prompts.chat import ChatPromptTemplate\n", "from langchain.schema import BaseOutputParser" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "class Commaseperatedoutput(BaseOutputParser):\n", " def parse(self,text:str):\n", " return text.strip().split(\",\")" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "template=\"Your are a helpful assistant. When the use given any input , you should generate 5 words synonyms in a comma seperated list\"\n", "human_template=\"{text}\"\n", "chatprompt=ChatPromptTemplate.from_messages([\n", " (\"system\",template),\n", " (\"human\",human_template)\n", "\n", "\n", "])" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "chain=chatprompt|model|Commaseperatedoutput()" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Clever', ' Bright', ' Wise', ' Shrewd', ' Astute']" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chain.invoke({\"text\":\"intelligent\"})\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 2 }