{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from langchain_openai import OpenAI\n", "import os" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "llm=OpenAI(openai_api_key=os.getenv(\"OPENAI_API_KEY\"), temperature=0.6)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " from the Sun\n", "\n", "The average distance of Mars from the Sun is about 142 million miles (228 million kilometers). However, because the orbits of both Mars and Earth are elliptical, the distance between the two planets can vary from about 33.9 million miles (54.6 million kilometers) at their closest approach (known as \"opposition\") to about 250 million miles (401 million kilometers) at their farthest. \n" ] } ], "source": [ "text = \"What is the distance of Mars\"\n", "print(llm.invoke(text))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/ng/workspace/beautifulcode/llm/freecode/venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "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": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "365.9 astronomical units\n" ] } ], "source": [ "output=llm_huggingface.invoke(\"Can you tell me distance of Moon from Earth\")\n", "print(output)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Prompt Templates" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Q: What is the distance of Mars\\nA:'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.prompts import PromptTemplate\n", "\n", "prompt_template = PromptTemplate(input_variables=[\"question\"],\n", " template=\"Q: {question}\\nA:\")\n", "\n", "prompt_template.format(question=\"What is the distance of Mars\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'question': 'What is the distance of Mars',\n", " 'text': ' The average distance of Mars from the Sun is about 142 million miles (228 million kilometers). However, its distance varies due to its elliptical orbit, ranging from about 128 million miles (206 million kilometers) at its closest approach (perihelion) to about 154 million miles (249 million kilometers) at its farthest point (aphelion).'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains import LLMChain\n", "\n", "chain = LLMChain(llm=llm, prompt=prompt_template)\n", "chain.invoke(\"What is the distance of Mars\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combining Multiple Chains Using simple Squential Chains" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "capital_prompt = PromptTemplate(input_variables=[\"country\"],\n", " template=\"Please tell me the capital of the country {country}\")\n", "\n", "capital_chain = LLMChain(llm=llm, prompt=capital_prompt)\n", "\n", "famous_template = PromptTemplate(input_variables=[\"capital\"],\n", " template=\"Suggest me some amazing places to visit in {capital}\")\n", "\n", "famous_chain = LLMChain(llm=llm, prompt=famous_template)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", "\u001b[36;1m\u001b[1;3m\n", "\n", "The capital of India is New Delhi.\u001b[0m\n", "\u001b[33;1m\u001b[1;3m Here are some amazing places to visit in New Delhi:\n", "\n", "1. Red Fort - A UNESCO World Heritage Site and a symbol of India's rich history and culture.\n", "\n", "2. India Gate - A war memorial and a popular picnic spot with beautiful gardens and fountains.\n", "\n", "3. Qutub Minar - Another UNESCO World Heritage Site, this 73-meter tall minaret is a must-visit for its stunning architecture and history.\n", "\n", "4. Humayun's Tomb - A magnificent mausoleum and garden complex built in the 16th century, also a UNESCO World Heritage Site.\n", "\n", "5. Lotus Temple - A Bahá'í House of Worship known for its unique lotus-shaped architecture and peaceful atmosphere.\n", "\n", "6. Akshardham Temple - A modern Hindu temple complex known for its intricate carvings, exhibitions, and light and sound show.\n", "\n", "7. Chandni Chowk - One of the oldest and busiest markets in Delhi, known for its narrow lanes, street food, and traditional bazaars.\n", "\n", "8. Jama Masjid - The largest mosque in India, known for its stunning architecture and peaceful courtyard.\n", "\n", "9. Rashtrapati Bhavan - The official residence of the President of India, this grand building is a blend of Indian and European architectural styles.\n", "\n", "10.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "{'input': 'India',\n", " 'output': \" Here are some amazing places to visit in New Delhi:\\n\\n1. Red Fort - A UNESCO World Heritage Site and a symbol of India's rich history and culture.\\n\\n2. India Gate - A war memorial and a popular picnic spot with beautiful gardens and fountains.\\n\\n3. Qutub Minar - Another UNESCO World Heritage Site, this 73-meter tall minaret is a must-visit for its stunning architecture and history.\\n\\n4. Humayun's Tomb - A magnificent mausoleum and garden complex built in the 16th century, also a UNESCO World Heritage Site.\\n\\n5. Lotus Temple - A Bahá'í House of Worship known for its unique lotus-shaped architecture and peaceful atmosphere.\\n\\n6. Akshardham Temple - A modern Hindu temple complex known for its intricate carvings, exhibitions, and light and sound show.\\n\\n7. Chandni Chowk - One of the oldest and busiest markets in Delhi, known for its narrow lanes, street food, and traditional bazaars.\\n\\n8. Jama Masjid - The largest mosque in India, known for its stunning architecture and peaceful courtyard.\\n\\n9. Rashtrapati Bhavan - The official residence of the President of India, this grand building is a blend of Indian and European architectural styles.\\n\\n10.\"}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains import SimpleSequentialChain\n", "chain = SimpleSequentialChain(chains=[capital_chain, famous_chain], verbose=True)\n", "\n", "chain.invoke(\"India\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sequential Chain" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "capital_prompt = PromptTemplate(input_variables=[\"country\"],\n", " template=\"Please tell me the capital of the country {country}\")\n", "\n", "capital_chain = LLMChain(llm=llm, prompt=capital_prompt, output_key=\"capital\")\n", "\n", "famous_template = PromptTemplate(input_variables=[\"capital\"],\n", " template=\"Suggest me some amazing places to visit in {capital}\")\n", "\n", "famous_chain = LLMChain(llm=llm, prompt=famous_template, output_key=\"places\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "{'country': 'India',\n", " 'capital': '\\n\\nThe capital of India is New Delhi.',\n", " 'places': \" Here are some amazing places to visit in New Delhi:\\n\\n1. Red Fort - A historical fort built in the 17th century by Mughal emperor Shah Jahan, it is a UNESCO World Heritage Site and a must-visit for its stunning architecture and rich history.\\n\\n2. Qutub Minar - Another UNESCO World Heritage Site, this 73-meter tall tower is a symbol of Delhi's rich past and is surrounded by beautiful gardens and other historical structures.\\n\\n3. India Gate - This iconic war memorial is a prominent landmark of Delhi and a popular spot for picnics and evening walks. It is also a tribute to the soldiers who lost their lives in World War I.\\n\\n4. Humayun's Tomb - Built in the 16th century, this stunning mausoleum is the resting place of Mughal emperor Humayun and is known for its intricate Mughal architecture.\\n\\n5. Lotus Temple - This stunning temple is a Bahá'í House of Worship and is shaped like a lotus flower. It is a peaceful and serene place to visit and is open to people of all religions.\\n\\n6. Chandni Chowk - One of the oldest and busiest markets in Old Delhi, Chandni Chowk is a must-visit for its\"}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.chains import SequentialChain\n", "\n", "chain = SequentialChain(chains=[capital_chain, famous_chain], verbose=True, input_variables=[\"country\"], output_variables=[\"capital\", \"places\"])\n", "\n", "chain.invoke({\"country\": \"India\"})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Chatmodels with ChatOpenAI" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI\n", "from langchain.schema import HumanMessage, SystemMessage, AIMessage" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/ng/workspace/beautifulcode/llm/freecode/venv/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.chat_models.openai.ChatOpenAI` was deprecated in langchain-community 0.0.10 and will be removed in 0.2.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.\n", " warn_deprecated(\n" ] } ], "source": [ "chatllm = ChatOpenAI(openai_api_key=os.environ[\"OPENAI_API_KEY\"], model_name=\"gpt-3.5-turbo\", temperature=0.6)\n", "\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content='1. \"They say AI is the future, but I just hope they don\\'t replace stand-up comedians. I mean, who else is going to tell their jokes in a monotone voice and never understand sarcasm?\"\\n\\n2. \"AI is like that friend who always has an answer for everything, except it\\'s usually wrong and makes you question their sanity.\"\\n\\n3. \"AI can do a lot of incredible things, but I\\'m still waiting for the day they create an AI that can laugh at my jokes. I guess they haven\\'t cracked the code for bad humor yet.\"\\n\\n4. \"You know you\\'re living in the age of AI when even your fridge has a better sense of humor than you. At least it\\'s cold enough to handle all the ice-breaking jokes.\"\\n\\n5. \"AI is like a magic trick gone wrong. It\\'s supposed to make our lives easier, but instead, it just keeps asking us to prove we\\'re not robots.\"\\n\\n6. \"I asked Siri to tell me a joke, and she said, \\'Why did the robot go on a diet? Because it had too many bytes!\\' I guess AI comedy is just as cheesy as regular comedy.\"\\n\\n7. \"AI is great at giving advice, but sometimes I think it takes things a little too literally. I asked for relationship advice, and it told me to \\'upgrade to a newer model.\\'\"\\n\\n8. \"AI is like that friend who\\'s always watching you. Not in a creepy way, but more like a judgmental robot who constantly reminds you to drink more water and get more sleep.\"\\n\\n9. \"AI is the ultimate multitasker. It can play chess, write poetry, and predict the weather, but it still can\\'t figure out how to fold a fitted sheet. Priorities, AI!\"\\n\\n10. \"AI might be able to beat humans at chess, but can it handle a game of rock-paper-scissors? I mean, it\\'s got a 33.3% chance of winning, so it\\'s not exactly rocket science.\"')" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chatllm.invoke([\n", " SystemMessage(content=\"You are a comedian AI assitant\"),\n", " HumanMessage(content=\"Please provide some comedy punchlines on AI\")\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Prompt Temlate + LLM + Output Parsers" ] }, { "cell_type": "code", "execution_count": 15, "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": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['smart', ' clever', ' brilliant', ' knowledgeable', ' sharp']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class CommaSeperatedOutputParser(BaseOutputParser):\n", " def parse(self, text: str):\n", " return text.strip().split(\",\")\n", "\n", "\n", "template=\"You are a helpful assistant. When the user gives any input, you should generate 5 synonmes in a comma separated list.\"\n", "human_template=\"{text}\"\n", "chatprompt = ChatPromptTemplate.from_messages([\n", " (\"system\", template), (\"human\", human_template)\n", "])\n", "\n", "\n", "chain = chatprompt|chatllm|CommaSeperatedOutputParser()\n", "\n", "chain.invoke({\"text\": \"Intelligent\"})" ] } ], "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.9.7" } }, "nbformat": 4, "nbformat_minor": 2 }