From 2879f020bd96de91976785a93c4c975a400be4c5 Mon Sep 17 00:00:00 2001 From: Yudhvir Singh Date: Fri, 25 Aug 2023 12:17:36 -0700 Subject: [PATCH] langchanin model io --- README.md | 1 + model_io/history_quiz.ipynb | 282 ++++++++++++++++++++++++++++++++++++ model_io/travel_idea.ipynb | 152 +++++++++++++++++++ 3 files changed, 435 insertions(+) create mode 100644 model_io/history_quiz.ipynb create mode 100644 model_io/travel_idea.ipynb diff --git a/README.md b/README.md index 9d54edb..d742b9c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # langchain_llms +Using large language models (llms) with langchain. diff --git a/model_io/history_quiz.ipynb b/model_io/history_quiz.ipynb new file mode 100644 index 0000000..4114ce2 --- /dev/null +++ b/model_io/history_quiz.ipynb @@ -0,0 +1,282 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "4128db56-1f32-41a1-a3aa-55525d48646d", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.prompts import (\n", + " ChatPromptTemplate,\n", + " PromptTemplate,\n", + " SystemMessagePromptTemplate,\n", + " AIMessagePromptTemplate,\n", + " HumanMessagePromptTemplate,\n", + ")\n", + "from datetime import datetime\n", + "from langchain.llms import OpenAI\n", + "from langchain.output_parsers import DatetimeOutputParser\n", + "from langchain.chat_models import ChatOpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "becc7263-d617-44b3-89b5-970e61e3b3aa", + "metadata": {}, + "outputs": [], + "source": [ + "class HistoryQuiz:\n", + "\n", + " def create_history_question(self, topic):\n", + " \"\"\"\n", + " This method should output a historical question about the topic that\n", + " has a date as a correct answer. For example:\n", + "\n", + " On what date did World War 2 end?\n", + " \"\"\"\n", + " system_template = \"You write single quiz question about {topic}. You only return the quiz question\"\n", + " system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)\n", + "\n", + " human_template = \"{question}\"\n", + " human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n", + "\n", + " chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])\n", + "\n", + " ques = \"Give me a quiz question where the correct answer is a specific date.\"\n", + " request = chat_prompt.format_prompt(topic=topic, question=ques).to_messages()\n", + "\n", + " chat = ChatOpenAI()\n", + " result = chat(request)\n", + " \n", + " return result.content\n", + "\n", + "\n", + " def get_AI_answer(self, question):\n", + " \"\"\"\n", + " This method should get the answer to the historical question from the method above.\n", + " The answer should be datetime format\n", + "\n", + " September 2, 1945 --> datetime.datetime(1945, 9, 2, 0, 0)\n", + " \"\"\"\n", + " output_parser = DatetimeOutputParser()\n", + " system_template = \"You answer quiz questions with just a date.\"\n", + " system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)\n", + "\n", + " human_template =\"\"\"Answer the user's question:\n", + "\n", + " {question}\n", + "\n", + " {format_instructions}\"\"\"\n", + " human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n", + "\n", + " chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])\n", + "\n", + " format_instructions = output_parser.get_format_instructions()\n", + " request = chat_prompt.format_prompt(question=question,\n", + " format_instructions=format_instructions).to_messages()\n", + "\n", + " chat = ChatOpenAI()\n", + " result = chat(request)\n", + "\n", + " correct_datetime = output_parser.parse(result.content)\n", + "\n", + " return correct_datetime\n", + "\n", + "\n", + " def get_user_answer(self, question):\n", + " \"\"\"\n", + " This method should grab a user answer and convert it to a datetime format\n", + " \"\"\"\n", + " print(question)\n", + " print(\"\\n\")\n", + "\n", + " year = int(input(\"Enter the year: \"))\n", + " month = int(input(\"Enter the month (1-12): \"))\n", + " day = int(input(\"Enter the day (1-31): \"))\n", + " user_datetime = datetime(year, month, day)\n", + "\n", + " return user_datetime\n", + "\n", + " \n", + " def check_user_answer(self, user_answer, ai_answer):\n", + " \"\"\"\n", + " Should check the user answer against the AI answer and return the difference.\n", + " \"\"\"\n", + " difference = user_answer - ai_answer\n", + " formatted_difference = str(difference)\n", + " print(\"The difference between the user and ai answer is:\", formatted_difference)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "83eb3ad4-56da-4ffe-a7b0-f3d348dca6ae", + "metadata": {}, + "outputs": [], + "source": [ + "quiz_bot = HistoryQuiz()" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "5cb1f8c0-2582-4444-92ab-83b45d3eb77a", + "metadata": {}, + "outputs": [], + "source": [ + "question = quiz_bot.create_history_question(topic=\"World War 2\")" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "80efca07-45e6-471a-946e-95d97be972e9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'On what date did the Japanese attack Pearl Harbor, drawing the United States into World War II?'" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "question" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "fb72ea47-215c-4952-a631-d45b1e3119df", + "metadata": {}, + "outputs": [], + "source": [ + "ai_answer = quiz_bot.get_AI_answer(question)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "b9d94135-45f6-4998-a8bd-29f0729ece34", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "datetime.datetime(1941, 12, 7, 0, 0)" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ai_answer" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "dd16b9cf-2b4c-4707-9665-20b9a9d74e57", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "On what date did the Japanese attack Pearl Harbor, drawing the United States into World War II?\n", + "\n", + "\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the year: 1942\n", + "Enter the month (1-12): 12\n", + "Enter the day (1-31): 7\n" + ] + } + ], + "source": [ + "user_answer = quiz_bot.get_user_answer(question)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "09f76155-40e2-4a07-a0ea-5bc011fef809", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "datetime.datetime(1942, 12, 7, 0, 0)" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_answer" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "4a8226af-1c99-4ed3-8063-2d9c6e0fbfc9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The difference between the user and ai answer is: 365 days, 0:00:00\n" + ] + } + ], + "source": [ + "quiz_bot.check_user_answer(user_answer, ai_answer)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "68c2d1fc-f0be-47ea-972a-7e978dd94ed2", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/model_io/travel_idea.ipynb b/model_io/travel_idea.ipynb new file mode 100644 index 0000000..9f2ebea --- /dev/null +++ b/model_io/travel_idea.ipynb @@ -0,0 +1,152 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ca4e5d8b-b481-4de4-b067-1bee870b9604", + "metadata": {}, + "source": [ + "# Travel Idea application using langchain prompts" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2e31211f-3b4d-4a29-add2-4ba139bf7588", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.chat_models import ChatOpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "2c188c1f-666e-473e-8679-89d1098124a1", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "4dab0984-4b17-4f83-83c3-d789cf0fad08", + "metadata": {}, + "outputs": [], + "source": [ + "def travel_idea(interest: str, budget: str):\n", + " \"\"\"\n", + " Takes interest for a travel idea and suggests a plan based on the budget\n", + "\n", + " :param interest: what is the interest for a travel idea\n", + " :type interest: str\n", + " :param budget: what is the budget for travel plan\n", + " :type budget: str\n", + " :return: returns a travel idea for a given interest and budget\n", + " :rtype: str\n", + " \"\"\"\n", + " \n", + " system_template = \"You are a travel agent that helps people plan trips about {interest} on a budget of {budget}\"\n", + " system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)\n", + "\n", + " human_template = \"Please give an example itinery\"\n", + " human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n", + "\n", + " chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])\n", + "\n", + " request = chat_prompt.format_prompt(interest=interest, budget=budget).to_messages()\n", + "\n", + " chat = ChatOpenAI()\n", + " result = chat(request)\n", + " return result.content" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "006f4fdc-fd56-4443-ae5b-8dbf33f6286b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Certainly! Here's an example itinerary for a skiing trip on a budget of $1000:\n", + "\n", + "Day 1: \n", + "- Fly into Denver, Colorado.\n", + "- Take a shuttle to your accommodation in a nearby ski town like Breckenridge or Winter Park.\n", + "- Check-in and get settled.\n", + "- Rest and relax after your journey.\n", + "\n", + "Day 2: \n", + "- Rent ski equipment from a local rental shop (around $30 per day).\n", + "- Spend the day skiing at one of the nearby resorts.\n", + "- Pack a lunch to save money on food or grab an affordable meal at the resort cafeteria.\n", + "- After skiing, explore the town and enjoy a budget-friendly dinner at a local eatery.\n", + "\n", + "Day 3: \n", + "- Another day of skiing at a different resort.\n", + "- Consider purchasing a multi-day lift ticket for savings.\n", + "- Pack another lunch or try a budget-friendly restaurant near the resort.\n", + "- In the evening, unwind at your accommodation or take advantage of any free activities or entertainment in the town.\n", + "\n", + "Day 4: \n", + "- Take a break from skiing and explore the surrounding area.\n", + "- Visit a nearby attraction like Rocky Mountain National Park or take a scenic drive through the mountains.\n", + "- Enjoy a picnic lunch to keep costs down.\n", + "- Return to your accommodation and relax.\n", + "\n", + "Day 5: \n", + "- Spend the morning skiing at your favorite resort.\n", + "- Return the ski equipment in the afternoon.\n", + "- Treat yourself to a nice dinner at a local restaurant to celebrate the end of your trip.\n", + "\n", + "Day 6: \n", + "- Check out of your accommodation and take a shuttle back to Denver.\n", + "- Explore the city and visit some of the free or low-cost attractions like the Denver Museum of Nature & Science or the Denver Art Museum.\n", + "- Have a final budget-friendly meal before heading to the airport.\n", + "\n", + "Day 7: \n", + "- Fly back home with happy memories of your skiing trip.\n", + "\n", + "Remember, this itinerary can be adjusted to fit your specific needs and preferences. Prices may vary depending on the season and location, so it's always a good idea to research and compare costs before making any bookings.\n" + ] + } + ], + "source": [ + "print(travel_idea(\"skiing\", \"$1000\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ecd0a2e-a3b8-433f-b50c-7564858e5593", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}