How To Call Functions For Knowledge Retrieval
How to use functions with a knowledge base
This notebook builds on the concepts in the argument generation notebook, by creating an agent with access to a knowledge base and two functions that it can call based on the user requirement.
We'll create an agent that uses data from arXiv to answer questions about academic subjects. It has two functions at its disposal:
- get_articles: A function that gets arXiv articles on a subject and summarizes them for the user with links.
- 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.
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.
Walkthrough
This cookbook takes you through the following workflow:
- Search utilities: Creating the two functions that access arXiv for answers.
- 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.
- arXiv conversation: Put all of this together in live conversation.
Search utilities
We'll first set up some utilities that will underpin our two functions.
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.
Directory './data/papers' already exists.
{'title': 'Proximal Policy Optimization and its Dynamic Version for Sequence Generation',
, '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.',
, 'article_url': 'http://arxiv.org/abs/1808.07982v1',
, 'pdf_url': 'http://arxiv.org/pdf/1808.07982v1'} Existing papers found... Articles: 10 Chunking text from paper Summarizing each chunk of text
100%|███████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:05<00:00, 1.40s/it]
Summarizing into overall summary
Configure Agent
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.
arXiv conversation
Let's put this all together by testing our functions out in conversation.
Function generation requested, calling function Getting search results Got search results, summarizing content
Function generation requested, calling function Finding and reading paper Existing papers found... Articles: 20 Chunking text from paper Summarizing each chunk of text
100%|███████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:04<00:00, 1.21s/it]
Summarizing into overall summary