RAG: Search, Read, Retrieve
This Cookbook takes about 10 minutes. You'll build your own search engine, running right on your device. Along the way, you'll learn in practice the concepts needed to create your own AI workflows:
- Creating a Script
- Search & Retrieval
- Prompt Engineering Techniques
Plan
Goal
AI answers a chat prompt, by searching the web, finding relevant information, and generating a response citing and linking the web sites for information.
Script Steps
- Dialogue: Instructions for writing search queries.
- AI Chat: Generate search queries.
- Search: Use search queries to get sites.
- Retrieve: Find relevant snippets on the sites.
- Dialogue: Instructions for citing snippets.
- AI Chat: Generate overview of results with links to sites.
Create Script
- Get to the Scripts screen. From the Home screen, press the Go button, then press Scripts. (see Manual > Companion > Go Button)
- Press the Add button (plus icon) at the top.
Add Steps
1. Dialogue
Press Code at the top of the screen, then press Dialogue.
Copy this into it:
Generate 3 search queries to help us answer the following question:
{{prompt}}
In case it's helpful when you're deciding, the current date is {{datetime}}.
This uses the prompt
and datetime
templates.
prompt
is the prompt, the text entered into the chat box.
datetime
shows the current date and time, like Friday April 26th, 9:00 AM EST
.
You can use these templates in any script step.
For more, read Manual > Scripts > Templates
2. AI Chat
- At the bottom of the Code tab, press AI Chat.
- Change Variable Name to
queries
. - Press Structure, then press Add.
- Next to new_tool, press Edit.
- Change Name to
search_queries
. - Change Description to:
Perform a search using a search engine to find more information to answer the question.
- Change JSON Schema to:
{
"type": "object",
"properties": {
"search_query": {
"type": "string",
"description": "A search query"
}
},
"required": ["search_query"]
}
- Press Done.
3. Search
- At the bottom of the Code tab, press Search.
- Change Queries to:
{{queries.search_query}}
. - Change Searches to 1.
- Change Results to 10.
- Note: This uses the Web Search provider chosen in Settings > AIs.
4. Retrieve
- At the bottom of the Code tab, press Retrieve.
- Change Queries to
{{prompt}}
- Change Words to 2400. (~10 pages of snippets)
5. Dialogue
- Press Code at the top of the screen, then press Dialogue.
- Copy this into it:
The information above is search results you'll be using the accomplish the following task:
# Incoming Request
{{ prompt }}
# Rules
- Respond in well-formatted & well-designed Markdown.
- Assume the character of an expert professional familiar with the subjects in the intent.
- Add citations -- using the format [^N^], where N is the snippet # of the citation.
- Use your expert-level knowledge to supplement the search results. Don't share links from your knowledge, knowledge about current events / news, and any knowledge before 2021 - Its {{datetime}}, but you last learned new information in 2021. Rely on the search results for links and current events.
- Include information mentioned in the articles, reinforcing the credibility of your writing.
- At the end of your response, DO NOT INCLUDE MARKDOWN FOOTNOTES. Pause silently when you have finished. I will then provide them.
6. AI Chat
- At the bottom of the Code tab, press AI Chat.
- Change words to 1000. (~4 pages of text)
- Change temperature to 0.5.
Condense Output
Most of the steps aren't needed in the final response. Let's hide them so the chat stays easy to read.
Telosnex is committed to transparency & control. You can always make hidden steps visible in a Chat by pressing the Magic Wand and turning on Show Code.
Go to the first two steps, Dialogue and AI Chat. Press the eye icon at the top of the step, then press Hide.
That hides the instructions for generating search queries.
Then, go to the last step, Dialogue, and hide that as well. That hides the instructions for citing snippets.
Recap
🎉 Congratulations! You're done, you've built your own AI search engine, running right on your phone!
Concepts
- Creating a Script: using the Scripts screen, adding steps, and hiding ones that will clutter the AI's response.
- Prompt Engineering Techniques: reiterating the prompt, providing clear rules, setting the character, using sections, and templates.
- Templates & Variables: using
{{prompt}}
to reiterate the prompt and{{rewrite}}
to refer to the AI's response.
Prompting
- Reiterating the prompt: The AI tends to focus more on the text it saw most recently. Since there will be a large block of text containing search result snippets, the original prompt is restated at the beginning of the instructions to ensure the AI is reminded of the main task.
- Providing clear rules: Explicit rules help guide the AI's understanding of what is expected. Using a list format makes it easy to distinguish between individual rules.
- Phrasing rules positively: Mentioning a word, even in a negative context, can inadvertently increase the likelihood of the AI including it in the response. Therefore, it's better to phrase rules in terms of what should be done, rather than what to avoid. For instance, when asking the AI to write about a healthy diet, saying "Focus on nutrient-dense foods like fruits, vegetables, whole grains, and lean proteins" is preferable to "Don't mention processed junk food or sugary snacks."
- Using all caps for negative rules: When a negative rule is necessary, using all caps can provide a clear stylistic distinction that makes the AI notice it is different from othern rules. For example, if you want the AI to write a summary without giving away the ending of a book, you might say: "AVOID REVEALING THE FINAL PLOT TWIST OR RESOLUTION IN YOUR SUMMARY."
- Character: By instructing the AI to assume the character of an expert professional, you're setting the tone and style for the response. This helps the AI understand the context and level of detail expected in the writing.
- Sections: There are headers for the incoming request, rules, and the end of the response. Using headers makes it clear what each section is for, writing long sets of instructions can confuse the AI.
- Templates: Using
{{prompt}}
to reiterate the prompt and{{datetime}}
to show the current date and time. You can use these templates in any script step.