|
| 1 | +# Context |
| 2 | + |
| 3 | +GPTScript provides a mechanism to share prompt information across many tools using the tool parameter `context`. It is used to provide additional information to the calling tool on when to use a specific tool by prepending the `context` to the instruction of the calling tool. |
| 4 | + |
| 5 | +- Context can point to a static text or a GPTScript. |
| 6 | +- Context tools are just regular GPTScript tools, and any valid gptscript field can be used. |
| 7 | +- Exported tools from a context tool are made available to the calling tool. |
| 8 | +- When context points to a GPTScript tool, output from the context tool gets prepended to the instruction of the calling tool. |
| 9 | + |
| 10 | +## Writing a Context Provider Tool as static text |
| 11 | + |
| 12 | +```yaml |
| 13 | +# my-search-context.txt |
| 14 | + |
| 15 | +You are an expert web researcher with access to the Search tool.If the search tool fails to return any information stop execution of the script with message "Sorry! Search did not return any results". Feel free to get the contents of the returned URLs in order to get more information. Provide as much detail as you can. Also return the source of the search results. |
| 16 | + |
| 17 | +``` |
| 18 | + |
| 19 | +## Using a Context Provider Tool |
| 20 | + |
| 21 | +Continuing with the above example, this is how you can use the same context in tools that uses different search providers: |
| 22 | + |
| 23 | +```yaml |
| 24 | +# my-search-duduckgo.gpt |
| 25 | +context: ./my-search-context.txt |
| 26 | +tools: github.com/gptscript-ai/search/duckduckgo,sys.http.html2text |
| 27 | + |
| 28 | +What are some of the most popular tourist destinations in Scotland, and how many people visit them each year? |
| 29 | + |
| 30 | +``` |
| 31 | + |
| 32 | +```yaml |
| 33 | +# my-search-brave.gpt |
| 34 | +context: ./my-search-context.txt |
| 35 | +tools: github.com/gptscript-ai/search/brave,sys.http.html2text |
| 36 | + |
| 37 | +List out some of the main actors in the Christopher Nolan movie Inception, as well as the names of the other Christopher Nolan movies they have appeared in. |
| 38 | + |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +## Context Provider Tool with exported tools |
| 43 | + |
| 44 | +Here is a simple example of a context provider tool that provides additional context to search tool: |
| 45 | + |
| 46 | +```yaml |
| 47 | +# my-search-context-tool.gpt |
| 48 | +export: sys.http.html2text? |
| 49 | + |
| 50 | +#!/bin/bash |
| 51 | +echo You are an expert web researcher with access to the Search tool.If the search tool fails to return any information stop execution of the script with message "Sorry! Search did not return any results". Feel free to get the contents of the returned URLs in order to get more information. Provide as much detail as you can. Also return the source of the search results. |
| 52 | + |
| 53 | +``` |
| 54 | + |
| 55 | +Continuing with the above example, this is how you can use it in a script: |
| 56 | + |
| 57 | +```yaml |
| 58 | +context: ./my-search-context-tool.gpt |
| 59 | +tools: github.com/gptscript-ai/search/duckduckgo |
| 60 | + |
| 61 | +What are some of the most popular tourist destinations in Scotland, and how many people visit them each year? |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +When you run this script, GPTScript will use the output from the context tool and add it to the user message along with the |
| 66 | +existing prompt in this tool to provide additional context to LLM. |
| 67 | + |
| 68 | +## Context Provider Tool with args |
| 69 | + |
| 70 | +Here is an example of a context provider tool that uses args to decide which search tool to use when answering the user provided queries: |
| 71 | + |
| 72 | +```yaml |
| 73 | +# context_with_arg.gpt |
| 74 | +export: github.com/gptscript-ai/search/duckduckgo, github.com/gptscript-ai/search/brave, sys.http.html2text? |
| 75 | +args: search_tool: tool to search with |
| 76 | + |
| 77 | +#!/bin/bash |
| 78 | +echo You are an expert web researcher with access to the ${search_tool} Search tool.If the search tool fails to return any information stop execution of the script with message "Sorry! Search did not return any results". Feel free to get the contents of the returned URLs in order to get more information. Provide as much detail as you can. Also return the source of the search results. |
| 79 | + |
| 80 | +``` |
| 81 | + |
| 82 | +Continuing with the above example, this is how you can use it in a script: |
| 83 | + |
| 84 | +```yaml |
| 85 | +# my_context_with_arg.gpt |
| 86 | +context: ./context_with_arg.gpt with ${search} as search_tool |
| 87 | +Args: search: Search tool to use |
| 88 | + |
| 89 | +What are some of the most popular tourist destinations in Scotland, and how many people visit them each year? |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +This script can be used to search with `brave` or `duckduckdb` tools depending on the search parameter passed to the tool. |
| 94 | +Example usage for using brave search tool: |
| 95 | +```yaml |
| 96 | +gptscript --disable-cache my_context_with_arg.gpt '{"search": "brave"}' |
| 97 | +``` |
0 commit comments