|
114 | 114 | "id": "PqoT0ojAcV9P" |
115 | 115 | }, |
116 | 116 | "source": [ |
117 | | - "According to the [research paper](https://arxiv.org/abs/2210.03629), ReAct is a prompting method which allows language models to showcase the trace of thinking processes/steps involved in answering a user's query. This improves human interpretability and trustworthiness. ReAct prompted models generate **Thought-Action-Observation** triplets for every iteration.\n", |
| 117 | + "ReAct ((Yao, et al. 2022)[https://arxiv.org/abs/2210.03629)) is a prompting method that allows language models to trace reasoning steps involved in answering a user's query. This improves human interpretability and trustworthiness. ReAct prompted models generate **Thought-Action-Observation** triplets for every iteration.\n", |
118 | 118 | "\n", |
119 | 119 | "\n", |
120 | | - "- Why to use ReAct when you can simply instruct the model: \"Explain step-by-step\"\n", |
121 | | - "> Large Language models (LLMs) generate responses by extrapolating the prompts they are provided with. This is achieved by using their internal knowledge representation. In short, they are oblivious to the external/updated information, which makes them hallucinate(generate wrong/unreal answers) at times.\n", |
| 120 | + "Instead of instructing the model to \"Explain step-by-step\", using ReAct encourages models to seek factual information by inducing Thought and Action steps, and using external tools to provide Observation steps.\n", |
122 | 121 | "\n", |
123 | | - " - ReAct tries to prevent hallucination by mandating language models to seek factual information strictly from the external environment they are provided with.\n", |
124 | 122 | "\n", |
| 123 | + "It works by adding structure to the prompt that corresponds to specific actions.\n", |
125 | 124 | "\n", |
126 | | - "- How does it work?\n", |
127 | | - "\n", |
128 | | - " - Model receives the query from the user.\n", |
129 | | - " - This query with previous chat history(for continuous context link) if any, will be analyzed by the model.\n", |
130 | | - " - The model will use its internal knowledge to decide which among the following action it needs to take.\n", |
131 | | - " - Search[entity]: By analyzing the query, the model will decide what `entity` to query to the Wikipedia API.\n", |
132 | | - " - Lookup[phrase]: Model will analyze the content of the Wikipedia page returned by `Search[entity]` action and then decide what specific information or `phrase` is needed to be retrieved from that page to answer the user's question.\n", |
133 | | - " - Finish[Answer]: Return the `Answer` to the user." |
| 125 | + " - Search[entity]: Search for a specific `entity` (in this guide, it will query the Wikipedia API).\n", |
| 126 | + " - Lookup[phrase]: Scan the `Search` results for a specific, exact-match phrase (on a Wikipedia page).\n", |
| 127 | + " - Finish[answer]: Return the `answer` to the user." |
134 | 128 | ] |
135 | 129 | }, |
136 | 130 | { |
|
286 | 280 | "id": "lLv9Kuuu5Ffs" |
287 | 281 | }, |
288 | 282 | "source": [ |
289 | | - "The original prompts used in the paper is available at [https://github.com/ysymyth/ReAct/tree/master/prompts](https://github.com/ysymyth/ReAct/tree/master/prompts)\n", |
290 | | - "\n", |
291 | | - "Here, you will be working with the following ReAct prompt with a few minor adjustments." |
| 283 | + "Here, you will be working with the [original ReAct prompt](https://github.com/ysymyth/ReAct/tree/master/prompts) with a few minor adjustments." |
292 | 284 | ] |
293 | 285 | }, |
294 | 286 | { |
|
556 | 548 | "id": "Is8BIVQP3u95" |
557 | 549 | }, |
558 | 550 | "source": [ |
559 | | - "## The Gemini-ReAct pipeline" |
| 551 | + "## Using ReAct with Gemini" |
560 | 552 | ] |
561 | 553 | }, |
562 | 554 | { |
|
574 | 566 | "id": "T4M3lxEoM3k0" |
575 | 567 | }, |
576 | 568 | "source": [ |
577 | | - "You will now build an end-to-end pipeline to facilitate multi-turn chat with the ReAct-prompted Gemini model." |
| 569 | + "You will now build a class to facilitate multi-turn chat with the ReAct-prompted Gemini model." |
578 | 570 | ] |
579 | 571 | }, |
580 | 572 | { |
|
642 | 634 | "As instructed by the prompt, the model will be generating **Thought-Action-Observation** traces, where every **Action** trace could be one of the following tokens:\n", |
643 | 635 | "\n", |
644 | 636 | "\n", |
645 | | - "1. </search/> : Perform a Wikipedia search via external API.\n", |
646 | | - "2. </lookup/> : Lookup for specific information on a page with the Wikipedia API.\n", |
647 | | - "3. </finish/> : Stop the execution of the model and return the answer.\n", |
648 | | - "\n", |
649 | | - "If the model encounters any of these tokens, the model should make use of the `tools` made available to the model. This understanding of the model to leverage acquired toolsets to collect information from the external world is often referred to as **function calling**. Therefore, the next goal is to imitate this function calling technique in order to allow ReAct prompted Gemini model to access the external groundtruth.\n", |
650 | | - "\n", |
651 | | - "The Gemini API supports function calling and you could use this feature to set up your tools. However, for this tutorial, you will learn to simulate it using `stop_sequences` parameter.\n", |
| 637 | + "1. <search/> : Perform a Wikipedia search via external API.\n", |
| 638 | + "2. <lookup/> : Look up specific information on a page with the Wikipedia API.\n", |
| 639 | + "3. <finish/> : Stop the execution of the model and return the answer.\n", |
652 | 640 | "\n", |
| 641 | + "If any of these `Action` tokens are returned, you want to call the relevant tool and update the prompt with an `Observervation`.\n", |
653 | 642 | "\n", |
654 | | - "Define the tools:" |
| 643 | + "Note: The Gemini API supports [function calling](https://ai.google.dev/docs/function_calling) and you could use this feature to set up your tools. However, for this tutorial, you will use `stop_sequences` parameter in a similar manner.\n" |
655 | 644 | ] |
656 | 645 | }, |
657 | 646 | { |
|
790 | 779 | "id": "u9Tl6W98Zhut" |
791 | 780 | }, |
792 | 781 | "source": [ |
793 | | - "### Stop tokens and function calling imitation" |
| 782 | + "### Integrate tools" |
794 | 783 | ] |
795 | 784 | }, |
796 | 785 | { |
|
799 | 788 | "id": "0VnX9zpBcdA0" |
800 | 789 | }, |
801 | 790 | "source": [ |
802 | | - "Now that you are all set with function definitions, the next step is to instruct the model to interrupt its execution upon encountering any of the action tokens. You will make use of the `stop_sequences` parameter from [`genai.GenerativeModel.GenerationConfig`](https://ai.google.dev/api/python/google/generativeai/GenerationConfig) class to instruct the model when to stop. Upon encountering an action token, the pipeline will simply extract what specific token from the `stop_sequences` argument terminated the model's execution thereby calling appropriate **tool**(function).\n", |
| 791 | + "Now that you are all set with function definitions, the next step is to instruct the model to interrupt its execution when it emits any of the action tokens. You will use the `stop_sequences` parameter from the [`genai.GenerativeModel.GenerationConfig`](https://ai.google.dev/api/python/google/generativeai/GenerationConfig) class to instruct the model when to stop. Upon encountering an action token, your system will extract the last action and argument, then call the appropriate tool.\n", |
803 | 792 | "\n", |
804 | | - "Function's response will be added to model's chat history for continuing the context link." |
| 793 | + "The response from the function will be added to prompt to continue the process." |
805 | 794 | ] |
806 | 795 | }, |
807 | 796 | { |
|
0 commit comments