You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openai-cookbook/examples/How_to_call_functions_for_k...

846 lines
44 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "3e67f200",
"metadata": {},
"source": [
"# How to use functions with a knowledge base\n",
"\n",
"This notebook builds on the concepts in the [argument generation](How_to_call_functions_with_chat_models.ipynb) notebook, by creating an agent with access to a knowledge base and two functions that it can call based on the user requirement.\n",
"\n",
"We'll create an agent that uses data from arXiv to answer questions about academic subjects. It has two functions at its disposal:\n",
"- **get_articles**: A function that gets arXiv articles on a subject and summarizes them for the user with links.\n",
"- **read_article_and_summarize**: This function takes one of the previously searched articles, reads it in its entirety and summarizes the core argument, evidence and conclusions.\n",
"\n",
"This will get you comfortable with a multi-function workflow that can choose from multiple services, and where some of the data from the first function is persisted to be used by the second.\n",
"\n",
"## Walkthrough\n",
"\n",
"This cookbook takes you through the following workflow:\n",
"\n",
"- **Search utilities:** Creating the two functions that access arXiv for answers.\n",
"- **Configure Agent:** Building up the Agent behaviour that will assess the need for a function and, if one is required, call that function and present results back to the agent.\n",
"- **arXiv conversation:** Put all of this together in live conversation.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "80e71f33",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: scipy in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (1.11.3)\n",
"Requirement already satisfied: numpy<1.28.0,>=1.21.6 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from scipy) (1.26.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tenacity in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (8.2.3)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tiktoken==0.3.3 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (0.3.3)\n",
"Requirement already satisfied: regex>=2022.1.18 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from tiktoken==0.3.3) (2023.8.8)\n",
"Requirement already satisfied: requests>=2.26.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from tiktoken==0.3.3) (2.31.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken==0.3.3) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken==0.3.3) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken==0.3.3) (1.26.16)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken==0.3.3) (2023.7.22)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: termcolor in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (2.3.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: openai in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (1.3.5)\n",
"Requirement already satisfied: anyio<4,>=3.5.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (3.7.1)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (1.8.0)\n",
"Requirement already satisfied: httpx<1,>=0.23.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (0.25.1)\n",
"Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (2.4.2)\n",
"Requirement already satisfied: tqdm>4 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (4.66.1)\n",
"Requirement already satisfied: typing-extensions<5,>=4.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (4.8.0)\n",
"Requirement already satisfied: idna>=2.8 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (3.4)\n",
"Requirement already satisfied: sniffio>=1.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (1.3.0)\n",
"Requirement already satisfied: exceptiongroup in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (1.1.3)\n",
"Requirement already satisfied: certifi in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpx<1,>=0.23.0->openai) (2023.7.22)\n",
"Requirement already satisfied: httpcore in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpx<1,>=0.23.0->openai) (1.0.1)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pydantic<3,>=1.9.0->openai) (0.5.0)\n",
"Requirement already satisfied: pydantic-core==2.10.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pydantic<3,>=1.9.0->openai) (2.10.1)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpcore->httpx<1,>=0.23.0->openai) (0.14.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: requests in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (2.31.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (1.26.16)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (2023.7.22)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: arxiv in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (2.0.0)\n",
"Requirement already satisfied: feedparser==6.0.10 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from arxiv) (6.0.10)\n",
"Requirement already satisfied: requests==2.31.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from arxiv) (2.31.0)\n",
"Requirement already satisfied: sgmllib3k in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from feedparser==6.0.10->arxiv) (1.0.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests==2.31.0->arxiv) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests==2.31.0->arxiv) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests==2.31.0->arxiv) (1.26.16)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests==2.31.0->arxiv) (2023.7.22)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: pandas in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (2.1.1)\n",
"Requirement already satisfied: numpy>=1.22.4 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pandas) (1.26.0)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pandas) (2.8.2)\n",
"Requirement already satisfied: pytz>=2020.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pandas) (2023.3.post1)\n",
"Requirement already satisfied: tzdata>=2022.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pandas) (2023.3)\n",
"Requirement already satisfied: six>=1.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.12.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: PyPDF2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (3.0.1)\n",
"Requirement already satisfied: typing_extensions>=3.10.0.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from PyPDF2) (4.8.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tqdm in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (4.66.1)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0m"
]
}
],
"source": [
"!pip install scipy\n",
"!pip install tenacity\n",
"!pip install tiktoken==0.3.3\n",
"!pip install termcolor\n",
"!pip install openai\n",
"!pip install requests\n",
"!pip install arxiv\n",
"!pip install pandas\n",
"!pip install PyPDF2\n",
"!pip install tqdm\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dab872c5",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"import arxiv\n",
"import ast\n",
"import concurrent\n",
"from csv import writer\n",
"from IPython.display import display, Markdown\n",
"import json\n",
"from openai import OpenAI\n",
"import os\n",
"import pandas as pd\n",
"from PyPDF2 import PdfReader\n",
"import requests\n",
"from scipy import spatial\n",
"import tiktoken\n",
"from tqdm import tqdm\n",
"from termcolor import colored\n",
"\n",
"client = OpenAI()\n",
"GPT_MODEL = \"gpt-3.5-turbo-1106\"\n",
"EMBEDDING_MODEL = \"text-embedding-ada-002\"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "f2e47962",
"metadata": {},
"source": [
"## Search utilities\n",
"\n",
"We'll first set up some utilities that will underpin our two functions.\n",
"\n",
"Downloaded papers will be stored in a directory (we use ```./data/papers``` here). We create a file ```arxiv_library.csv``` to store the embeddings and details for downloaded papers to retrieve against using ```summarize_text```."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2de5d32d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Directory './data/papers' already exists.\n"
]
}
],
"source": [
"directory = './data/papers'\n",
"\n",
"# Check if the directory already exists\n",
"if not os.path.exists(directory):\n",
" # If the directory doesn't exist, create it and any necessary intermediate directories\n",
" os.makedirs(directory)\n",
" print(f\"Directory '{directory}' created successfully.\")\n",
"else:\n",
" # If the directory already exists, print a message indicating it\n",
" print(f\"Directory '{directory}' already exists.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ae5cb7a1",
"metadata": {},
"outputs": [],
"source": [
"# Set a directory to store downloaded papers\n",
"data_dir = os.path.join(os.curdir, \"data\", \"papers\")\n",
"paper_dir_filepath = \"./data/arxiv_library.csv\"\n",
"\n",
"# Generate a blank dataframe where we can store downloaded files\n",
"df = pd.DataFrame(list())\n",
"df.to_csv(paper_dir_filepath)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "57217b9d",
"metadata": {},
"outputs": [],
"source": [
"def embedding_request(text):\n",
" response = client.embeddings.create(input=text, model=EMBEDDING_MODEL)\n",
" return response\n",
"\n",
"\n",
"def get_articles(query, library=paper_dir_filepath, top_k=5):\n",
" \"\"\"This function gets the top_k articles based on a user's query, sorted by relevance.\n",
" It also downloads the files and stores them in arxiv_library.csv to be retrieved by the read_article_and_summarize.\n",
" \"\"\"\n",
" search = arxiv.Search(\n",
" query=query, max_results=top_k, sort_by=arxiv.SortCriterion.Relevance\n",
" )\n",
" result_list = []\n",
" for result in search.results():\n",
" result_dict = {}\n",
" result_dict.update({\"title\": result.title})\n",
" result_dict.update({\"summary\": result.summary})\n",
"\n",
" # Taking the first url provided\n",
" result_dict.update({\"article_url\": [x.href for x in result.links][0]})\n",
" result_dict.update({\"pdf_url\": [x.href for x in result.links][1]})\n",
" result_list.append(result_dict)\n",
"\n",
" # Store references in library file\n",
" response = embedding_request(text=result.title)\n",
" file_reference = [\n",
" result.title,\n",
" result.download_pdf(data_dir),\n",
" response.data[0].embedding,\n",
" ]\n",
"\n",
" # Write to file\n",
" with open(library, \"a\") as f_object:\n",
" writer_object = writer(f_object)\n",
" writer_object.writerow(file_reference)\n",
" f_object.close()\n",
" return result_list\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "dda02bdb",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/1g/fk6xzzvj0c1ggf_6nwknlfy00000gq/T/ipykernel_87832/3977585067.py:14: DeprecationWarning: The '(Search).results' method is deprecated, use 'Client.results' instead\n",
" for result in search.results():\n"
]
},
{
"data": {
"text/plain": [
"{'title': 'Proximal Policy Optimization and its Dynamic Version for Sequence Generation',\n",
" 'summary': 'In sequence generation task, many works use policy gradient for model\\noptimization to tackle the intractable backpropagation issue when maximizing\\nthe non-differentiable evaluation metrics or fooling the discriminator in\\nadversarial learning. In this paper, we replace policy gradient with proximal\\npolicy optimization (PPO), which is a proved more efficient reinforcement\\nlearning algorithm, and propose a dynamic approach for PPO (PPO-dynamic). We\\ndemonstrate the efficacy of PPO and PPO-dynamic on conditional sequence\\ngeneration tasks including synthetic experiment and chit-chat chatbot. The\\nresults show that PPO and PPO-dynamic can beat policy gradient by stability and\\nperformance.',\n",
" 'article_url': 'http://arxiv.org/abs/1808.07982v1',\n",
" 'pdf_url': 'http://arxiv.org/pdf/1808.07982v1'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Test that the search is working\n",
"result_output = get_articles(\"ppo reinforcement learning\")\n",
"result_output[0]\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "11675627",
"metadata": {},
"outputs": [],
"source": [
"def strings_ranked_by_relatedness(\n",
" query: str,\n",
" df: pd.DataFrame,\n",
" relatedness_fn=lambda x, y: 1 - spatial.distance.cosine(x, y),\n",
" top_n: int = 100,\n",
") -> list[str]:\n",
" \"\"\"Returns a list of strings and relatednesses, sorted from most related to least.\"\"\"\n",
" query_embedding_response = embedding_request(query)\n",
" query_embedding = query_embedding_response.data[0].embedding\n",
" strings_and_relatednesses = [\n",
" (row[\"filepath\"], relatedness_fn(query_embedding, row[\"embedding\"]))\n",
" for i, row in df.iterrows()\n",
" ]\n",
" strings_and_relatednesses.sort(key=lambda x: x[1], reverse=True)\n",
" strings, relatednesses = zip(*strings_and_relatednesses)\n",
" return strings[:top_n]\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "7211df2c",
"metadata": {},
"outputs": [],
"source": [
"def read_pdf(filepath):\n",
" \"\"\"Takes a filepath to a PDF and returns a string of the PDF's contents\"\"\"\n",
" # creating a pdf reader object\n",
" reader = PdfReader(filepath)\n",
" pdf_text = \"\"\n",
" page_number = 0\n",
" for page in reader.pages:\n",
" page_number += 1\n",
" pdf_text += page.extract_text() + f\"\\nPage Number: {page_number}\"\n",
" return pdf_text\n",
"\n",
"\n",
"# Split a text into smaller chunks of size n, preferably ending at the end of a sentence\n",
"def create_chunks(text, n, tokenizer):\n",
" \"\"\"Returns successive n-sized chunks from provided text.\"\"\"\n",
" tokens = tokenizer.encode(text)\n",
" i = 0\n",
" while i < len(tokens):\n",
" # Find the nearest end of sentence within a range of 0.5 * n and 1.5 * n tokens\n",
" j = min(i + int(1.5 * n), len(tokens))\n",
" while j > i + int(0.5 * n):\n",
" # Decode the tokens and check for full stop or newline\n",
" chunk = tokenizer.decode(tokens[i:j])\n",
" if chunk.endswith(\".\") or chunk.endswith(\"\\n\"):\n",
" break\n",
" j -= 1\n",
" # If no end of sentence found, use n tokens as the chunk size\n",
" if j == i + int(0.5 * n):\n",
" j = min(i + n, len(tokens))\n",
" yield tokens[i:j]\n",
" i = j\n",
"\n",
"\n",
"def extract_chunk(content, template_prompt):\n",
" \"\"\"This function applies a prompt to some input content. In this case it returns a summarized chunk of text\"\"\"\n",
" prompt = template_prompt + content\n",
" response = client.chat.completions.create(\n",
" model=GPT_MODEL, messages=[{\"role\": \"user\", \"content\": prompt}], temperature=0\n",
" )\n",
" return response.choices[0].message.content\n",
"\n",
"\n",
"def summarize_text(query):\n",
" \"\"\"This function does the following:\n",
" - Reads in the arxiv_library.csv file in including the embeddings\n",
" - Finds the closest file to the user's query\n",
" - Scrapes the text out of the file and chunks it\n",
" - Summarizes each chunk in parallel\n",
" - Does one final summary and returns this to the user\"\"\"\n",
"\n",
" # A prompt to dictate how the recursive summarizations should approach the input paper\n",
" summary_prompt = \"\"\"Summarize this text from an academic paper. Extract any key points with reasoning.\\n\\nContent:\"\"\"\n",
"\n",
" # If the library is empty (no searches have been performed yet), we perform one and download the results\n",
" library_df = pd.read_csv(paper_dir_filepath).reset_index()\n",
" if len(library_df) == 0:\n",
" print(\"No papers searched yet, downloading first.\")\n",
" get_articles(query)\n",
" print(\"Papers downloaded, continuing\")\n",
" library_df = pd.read_csv(paper_dir_filepath).reset_index()\n",
" library_df.columns = [\"title\", \"filepath\", \"embedding\"]\n",
" library_df[\"embedding\"] = library_df[\"embedding\"].apply(ast.literal_eval)\n",
" strings = strings_ranked_by_relatedness(query, library_df, top_n=1)\n",
" print(\"Chunking text from paper\")\n",
" pdf_text = read_pdf(strings[0])\n",
"\n",
" # Initialise tokenizer\n",
" tokenizer = tiktoken.get_encoding(\"cl100k_base\")\n",
" results = \"\"\n",
"\n",
" # Chunk up the document into 1500 token chunks\n",
" chunks = create_chunks(pdf_text, 1500, tokenizer)\n",
" text_chunks = [tokenizer.decode(chunk) for chunk in chunks]\n",
" print(\"Summarizing each chunk of text\")\n",
"\n",
" # Parallel process the summaries\n",
" with concurrent.futures.ThreadPoolExecutor(\n",
" max_workers=len(text_chunks)\n",
" ) as executor:\n",
" futures = [\n",
" executor.submit(extract_chunk, chunk, summary_prompt)\n",
" for chunk in text_chunks\n",
" ]\n",
" with tqdm(total=len(text_chunks)) as pbar:\n",
" for _ in concurrent.futures.as_completed(futures):\n",
" pbar.update(1)\n",
" for future in futures:\n",
" data = future.result()\n",
" results += data\n",
"\n",
" # Final summary\n",
" print(\"Summarizing into overall summary\")\n",
" response = client.chat.completions.create(\n",
" model=GPT_MODEL,\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": f\"\"\"Write a summary collated from this collection of key points extracted from an academic paper.\n",
" The summary should highlight the core argument, conclusions and evidence, and answer the user's query.\n",
" User query: {query}\n",
" The summary should be structured in bulleted lists following the headings Core Argument, Evidence, and Conclusions.\n",
" Key points:\\n{results}\\nSummary:\\n\"\"\",\n",
" }\n",
" ],\n",
" temperature=0,\n",
" )\n",
" return response\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "898b94d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chunking text from paper\n",
"Summarizing each chunk of text\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 4/4 [00:01<00:00, 2.19it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Summarizing into overall summary\n"
]
}
],
"source": [
"# Test the summarize_text function works\n",
"chat_test_response = summarize_text(\"PPO reinforcement learning sequence generation\")\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c715f60d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Core Argument:\n",
"- The paper introduces the use of proximal policy optimization (PPO) as a more efficient reinforcement learning algorithm for sequence generation tasks, particularly in the context of chit-chat chatbot.\n",
"- The authors propose a dynamic approach for PPO (PPO-dynamic) to improve stability and performance compared to traditional policy gradient methods.\n",
"\n",
"Evidence:\n",
"- The paper presents experiments on synthetic tasks and chit-chat chatbot, demonstrating that both PPO and PPO-dynamic can stabilize the training and lead the model to learn to generate more diverse outputs.\n",
"- The results suggest that PPO is a better way for sequence learning, and GAN-based sequence learning can use PPO as the new optimization method for better performance.\n",
"- The supplementary material contains the derivation of the proposed PPO-dynamic, experimental settings, and results, including the distribution of the first output with different input sentence lengths.\n",
"\n",
"Conclusions:\n",
"- The paper concludes that PPO and PPO-dynamic are effective for sequence generation, with PPO-dynamic achieving comparable or higher precision and BLEU-2 scores in experiments compared to other algorithms.\n",
"- The results indicate that PPO-dynamic has a different distribution compared to the traditional REINFORCE method, showing the potential for improved stability and performance in sequence generation tasks.\n"
]
}
],
"source": [
"print(chat_test_response.choices[0].message.content)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "dab07e98",
"metadata": {},
"source": [
"## Configure Agent\n",
"\n",
"We'll create our agent in this step, including a ```Conversation``` class to support multiple turns with the API, and some Python functions to enable interaction between the ```ChatCompletion``` API and our knowledge base functions."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "77a6fb4f",
"metadata": {},
"outputs": [],
"source": [
"def chat_completion_request(messages, functions=None, model=GPT_MODEL):\n",
" headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": \"Bearer \" + client.api_key,\n",
" }\n",
" json_data = {\"model\": model, \"messages\": messages}\n",
" if functions is not None:\n",
" json_data.update({\"functions\": functions})\n",
" try:\n",
" response = requests.post(\n",
" \"https://api.openai.com/v1/chat/completions\",\n",
" headers=headers,\n",
" json=json_data,\n",
" )\n",
" return response\n",
" except Exception as e:\n",
" print(\"Unable to generate ChatCompletion response\")\n",
" print(f\"Exception: {e}\")\n",
" return e\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "73f7672d",
"metadata": {},
"outputs": [],
"source": [
"class Conversation:\n",
" def __init__(self):\n",
" self.conversation_history = []\n",
"\n",
" def add_message(self, role, content):\n",
" message = {\"role\": role, \"content\": content}\n",
" self.conversation_history.append(message)\n",
"\n",
" def display_conversation(self, detailed=False):\n",
" role_to_color = {\n",
" \"system\": \"red\",\n",
" \"user\": \"green\",\n",
" \"assistant\": \"blue\",\n",
" \"function\": \"magenta\",\n",
" }\n",
" for message in self.conversation_history:\n",
" print(\n",
" colored(\n",
" f\"{message['role']}: {message['content']}\\n\\n\",\n",
" role_to_color[message[\"role\"]],\n",
" )\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "978b7877",
"metadata": {},
"outputs": [],
"source": [
"# Initiate our get_articles and read_article_and_summarize functions\n",
"arxiv_functions = [\n",
" {\n",
" \"name\": \"get_articles\",\n",
" \"description\": \"\"\"Use this function to get academic papers from arXiv to answer user questions.\"\"\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"query\": {\n",
" \"type\": \"string\",\n",
" \"description\": f\"\"\"\n",
" User query in JSON. Responses should be summarized and should include the article URL reference\n",
" \"\"\",\n",
" }\n",
" },\n",
" \"required\": [\"query\"],\n",
" },\n",
" },\n",
" {\n",
" \"name\": \"read_article_and_summarize\",\n",
" \"description\": \"\"\"Use this function to read whole papers and provide a summary for users.\n",
" You should NEVER call this function before get_articles has been called in the conversation.\"\"\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"query\": {\n",
" \"type\": \"string\",\n",
" \"description\": f\"\"\"\n",
" Description of the article in plain text based on the user's query\n",
" \"\"\",\n",
" }\n",
" },\n",
" \"required\": [\"query\"],\n",
" },\n",
" }\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "0c88ae15",
"metadata": {},
"outputs": [],
"source": [
"def chat_completion_with_function_execution(messages, functions=[None]):\n",
" \"\"\"This function makes a ChatCompletion API call with the option of adding functions\"\"\"\n",
" response = chat_completion_request(messages, functions)\n",
" full_message = response.json()[\"choices\"][0]\n",
" if full_message[\"finish_reason\"] == \"function_call\":\n",
" print(f\"Function generation requested, calling function\")\n",
" return call_arxiv_function(messages, full_message)\n",
" else:\n",
" print(f\"Function not required, responding to user\")\n",
" return response.json()\n",
"\n",
"\n",
"def call_arxiv_function(messages, full_message):\n",
" \"\"\"Function calling function which executes function calls when the model believes it is necessary.\n",
" Currently extended by adding clauses to this if statement.\"\"\"\n",
"\n",
" if full_message[\"message\"][\"function_call\"][\"name\"] == \"get_articles\":\n",
" try:\n",
" parsed_output = json.loads(\n",
" full_message[\"message\"][\"function_call\"][\"arguments\"]\n",
" )\n",
" print(\"Getting search results\")\n",
" results = get_articles(parsed_output[\"query\"])\n",
" except Exception as e:\n",
" print(parsed_output)\n",
" print(f\"Function execution failed\")\n",
" print(f\"Error message: {e}\")\n",
" messages.append(\n",
" {\n",
" \"role\": \"function\",\n",
" \"name\": full_message[\"message\"][\"function_call\"][\"name\"],\n",
" \"content\": str(results),\n",
" }\n",
" )\n",
" try:\n",
" print(\"Got search results, summarizing content\")\n",
" response = chat_completion_request(messages)\n",
" return response.json()\n",
" except Exception as e:\n",
" print(type(e))\n",
" raise Exception(\"Function chat request failed\")\n",
"\n",
" elif (\n",
" full_message[\"message\"][\"function_call\"][\"name\"] == \"read_article_and_summarize\"\n",
" ):\n",
" parsed_output = json.loads(\n",
" full_message[\"message\"][\"function_call\"][\"arguments\"]\n",
" )\n",
" print(\"Finding and reading paper\")\n",
" summary = summarize_text(parsed_output[\"query\"])\n",
" return summary\n",
"\n",
" else:\n",
" raise Exception(\"Function does not exist and cannot be called\")\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "dd3e7868",
"metadata": {},
"source": [
"## arXiv conversation\n",
"\n",
"Let's put this all together by testing our functions out in conversation."
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "c39a1d80",
"metadata": {},
"outputs": [],
"source": [
"# Start with a system message\n",
"paper_system_message = \"\"\"You are arXivGPT, a helpful assistant pulls academic papers to answer user questions.\n",
"You summarize the papers clearly so the customer can decide which to read to answer their question.\n",
"You always provide the article_url and title so the user can understand the name of the paper and click through to access it.\n",
"Begin!\"\"\"\n",
"paper_conversation = Conversation()\n",
"paper_conversation.add_message(\"system\", paper_system_message)\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "253fd0f7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Function generation requested, calling function\n",
"Getting search results\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/1g/fk6xzzvj0c1ggf_6nwknlfy00000gq/T/ipykernel_87832/3977585067.py:14: DeprecationWarning: The '(Search).results' method is deprecated, use 'Client.results' instead\n",
" for result in search.results():\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Got search results, summarizing content\n"
]
},
{
"data": {
"text/markdown": [
"Proximal Policy Optimization (PPO) is a reinforcement learning algorithm known for its efficiency and stability. It is used for optimizing models in tasks involving non-differentiable evaluation metrics and adversarial learning. PPO replaces the policy gradient with its own optimization approach, and it has been shown to outperform policy gradient in stability and performance. PPO is particularly effective in conditional sequence generation tasks and continuous control tasks.\n",
"\n",
"Here are a few papers that provide more details about how PPO works and its variations:\n",
"1. Title: [\"Proximal Policy Optimization and its Dynamic Version for Sequence Generation\"](http://arxiv.org/abs/1808.07982v1)\n",
" - Summary: This paper demonstrates the efficacy of PPO and its dynamic approach in conditional sequence generation tasks.\n",
"\n",
"2. Title: [\"CIM-PPO: Proximal Policy Optimization with Liu-Correntropy Induced Metric\"](http://arxiv.org/abs/2110.10522v2)\n",
" - Summary: Analyzes the asymmetry effect of KL divergence on PPO's objective function and proposes a new algorithm called CIM-PPO, which applies correntropy induced metric in PPO.\n",
"\n",
"3. Title: [\"A2C is a special case of PPO\"](http://arxiv.org/abs/2205.09123v1)\n",
" - Summary: Demonstrates that Advantage Actor-critic (A2C) is a special case of PPO through theoretical justifications and empirical experiments.\n",
"\n",
"These papers provide insights into the workings and variations of PPO in reinforcement learning."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Add a user message\n",
"paper_conversation.add_message(\"user\", \"Hi, how does PPO reinforcement learning work?\")\n",
"chat_response = chat_completion_with_function_execution(\n",
" paper_conversation.conversation_history, functions=arxiv_functions\n",
")\n",
"assistant_message = chat_response[\"choices\"][0][\"message\"][\"content\"]\n",
"paper_conversation.add_message(\"assistant\", assistant_message)\n",
"display(Markdown(assistant_message))\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "3ca3e18a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Function generation requested, calling function\n",
"Finding and reading paper\n",
"Chunking text from paper\n",
"Summarizing each chunk of text\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 4/4 [00:01<00:00, 2.42it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Summarizing into overall summary\n"
]
},
{
"data": {
"text/markdown": [
"Core Argument:\n",
"- The paper introduces the use of proximal policy optimization (PPO) as a more efficient reinforcement learning algorithm for sequence generation tasks, particularly in the context of chit-chat chatbots.\n",
"- It proposes a dynamic approach for PPO (PPO-dynamic) to improve stability and performance compared to traditional policy gradient methods.\n",
"\n",
"Evidence:\n",
"- The paper presents experiments on synthetic tasks and chit-chat chatbot, demonstrating that both PPO and PPO-dynamic can stabilize the training and lead the model to learn to generate more diverse outputs.\n",
"- It compares the performance of PPO and PPO-dynamic with other algorithms such as REINFORCE, MIXER, and SeqGAN, showing that PPO-dynamic achieves comparable or higher precision and BLEU-2 scores in the experiments.\n",
"- The supplementary material contains the derivation of the proposed PPO-dynamic, experimental settings, and results, including the distribution of the first output with different input sentence lengths.\n",
"\n",
"Conclusions:\n",
"- The paper suggests that replacing policy gradient with PPO can lead to improved performance and stability in conditional text generation tasks.\n",
"- PPO-dynamic is shown to generate a more scattered distribution compared to using the REINFORCE method, and it achieves comparable or higher precision and BLEU-2 scores compared to other algorithms in the experiments."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Add another user message to induce our system to use the second tool\n",
"paper_conversation.add_message(\n",
" \"user\",\n",
" \"Can you read the PPO sequence generation paper for me and give me a summary\",\n",
")\n",
"updated_response = chat_completion_with_function_execution(\n",
" paper_conversation.conversation_history, functions=arxiv_functions\n",
")\n",
"display(Markdown(updated_response.choices[0].message.content))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "openai-cookbook",
"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.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}