quick_start/legacy/08_LLM_RAG_demo.ipynb (260 lines of code) (raw):

{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Demo: OpenAI Large Language Model - A RAG Sample\n", "\n", "In this demo, we show how to use GPT3 model to analyze natural query, use knowledge base to search for more information and answer questions." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import openai\n", "import os\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "\n", "openai.api_type = \"azure\"\n", "openai.api_version = os.getenv(\"OPENAI_API_VERSION\")\n", "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", "openai.api_base = os.getenv(\"OPENAI_API_BASE\")\n", "\n", "CHAT_COMPLETIONS_MODEL = os.getenv('CHAT_COMPLETION_NAME')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Start with a natural question" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "input = \"Which presendential candidate is likely to win in 2024 election? Explain the reasons.\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Step 1: GPT3: What do I need to to answer this question?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "system_prompt=f'''Answer the following questions as best you can. You have access to the following tools:\n", "\n", "Web Search: Use the web to find information\n", "\n", "You need to follow STRICTLY below format:\n", "\n", "Question: the input question you must answer\n", "Thought: you should always think about what to do\n", "Action: the action to take, should be one of [Research]\n", "Action Input: the input to the action\n", "\n", "Begin!\n", "'''\n", "user_prompt = f'''\n", "Question: {input}\n", "Thought:'''" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I should research the current political landscape and potential presidential candidates for the 2024 election.\n", "Action: Research\n", "Action Input: 2024 presidential election candidates and current political analysis\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":system_prompt},\n", " {\"role\":\"user\",\"content\": user_prompt,}],\n", " max_tokens=400,)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Step 2: Search web for more details " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# Use Azure BING Search API to get the results\n", "import requests\n", "import json\n", "url = \"https://api.bing.microsoft.com/v7.0/search\"\n", "headers = {\"Ocp-Apim-Subscription-Key\": os.getenv('BING_SUBSCRIPTION_KEY')}\n", "params = {\"q\": \"2024 presidential election candidates and current political analysis\"}\n", "response = requests.get(url, headers=headers, params=params)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "context = ''\n", "for result in response.json()['webPages']['value']:\n", " context += result['snippet'] + '\\n'" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Collected information for further processing" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Who Are the 2024 Presidential Election Candidates? - The New York Times Advertisement Who’s Running for President in 2024? By Martín González Gómez and Maggie Astor Updated Jan. 21,...\n", "Francis Suarez The Democrats Incumbent Joe Biden Marianne Williamson Dean Phillips Third Party Robert F. Kennedy Jr. Cornel West Jill Stein The Republicans Back to Top Former President of the...\n", "Comment We asked each of the 2024 presidential candidates the same questions about abortion, climate, crime and guns, the economy, education, elections, foreign policy and immigration. If...\n", "08 Nevada Republican presidential caucuses Election news Biden needles Trump in hopes of pushing him off message Colorado voters emphasize January 6 violence as they urge Supreme Court to keep...\n", "By OLIVIA ALAFRIZ 01/27/2024 10:30 AM EST The day after President Joe Biden ramped up his rhetoric on a border deal, former President Donald Trump again tried to undercut the chances of...\n", "Who’s Ahead In Republican Primary Polls? National polling average Who’s Ahead in South Carolina Republican Primary Polls? State polling average How Popular Is Joe Biden? Approval polling average Do...\n", "burgum christie desantis elder hurd hutchinson johnson pence ramaswamy scott suarez Republicans Ryan Binkley Businessman and pastor Binkley, a Texas businessman, is a long-shot candidate who is...\n", "Steve Benen It’s past time President Biden says what he plans to do in a second term Dean Obeidallah The scope of Biden-era job growth comes into sharper focus Steve Benen Inside the fallout from...\n", "No one alive has seen a race like the 2024 presidential election. For months, if not years, many people have expected a reprise of the 2020 election, a matchup between the sitting president and a ...\n", "Browse stories on the 2024 presidential election, including updates on the latest polls, news about Democrat and Republican candidates and what to expect in November. IE 11 is not supported.\n", "\n" ] } ], "source": [ "print(context)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "system_prompt=f'''Answer the following questions as best you can. Use the context provided below:\n", "{context}\n", "\n", "Begin!\n", "'''\n", "user_prompt = f'''\n", "Question: {input}\\\n", "Answer:'''" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Step 3: GPT3: What do I need to do to answer this question? (Again)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It is not possible to accurately predict which presidential candidate is likely to win the 2024 election at this time. The election is still a while away and there are many factors that can influence the outcome, including the candidates' campaigns, political events, and public opinion. Additionally, as of now, the final lineup of candidates is not fully confirmed. It is important to wait for more developments, including debates, primary results, and campaign strategies, before making any predictions about the likely winner.\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":system_prompt},\n", " {\"role\":\"user\",\"content\": user_prompt,}],\n", " max_tokens=400,)\n", "\n", "print(response['choices'][0]['message']['content'])" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### GPT3: I now have final answer to the question. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "azureml_py310_sdkv2", "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.7" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }