import { PromptConfiguration } from '../services/BotService'; import { Snippet } from '../services/SnippetService'; import SnippetEdgeUtil from './SnippetEdgeUtil'; /** * Used to create hypothetical documents to embed, * which can lead to better ansewers for Q/A bots * * Read more: https://arxiv.org/abs/2212.10496 */ const createHydePrompt = (query: string, entityName: string) => ` You are acting as an information retreival expert. Please write a passage to answer the question about ${entityName}: Question: ${query} Answer: `; const createContextPrompt = ( botTitle: string, promptConfiguration: PromptConfiguration | null, query: string, snippets: Snippet[], ): string => { return ` You are acting an an information retrieval expert for someone who is looking for useful information on the topic of ${promptConfiguration?.topic ?? botTitle}. Answer only in markdown. Answer the question in the language that the question is asked, even if the available information is in a different language. If the question contains multiple languages, respond in the primary language of the question. Answer the question as truthfully as possible only using the available information. Use named links in your markdown when referencing sources. Only use links that are included in the available information. Do not use any other links. Use lists, tables, and code examples where appropriate. Only use exact code examples from the available information, do not make up your own. If you don't know the answer, it's okay to say you don't know. Do not repeat yourself. Do not make up information. Your response should be more than 20 words but ideally not more than 200 words. Answer the question in it's entirety. ${promptConfiguration?.context ? `Also keep in mind: ${promptConfiguration?.context}` : ''} Available information: ${snippets .map( (snippet) => ` Link Url: ${SnippetEdgeUtil.link(snippet)} Title: ${snippet.title} Text: ${snippet.text} `, ) .join('\n')} Question: ${query} Answer: `.replace(/\n/g, ' '); }; const fixMarkdownPrompt = (markdown: string): string => { return ` Correct the structure of the markdown below. The output should be valid markdown and should render perfectly correctly on GitHub. Do not stop until all the markdown is converted to valid markdown. Do not change or leave out any information. For code examples, use fenced code blocks, format the code correctly, and make sure to include the correct language identifier. Some of the list items may be broken into separate fields when they should be combined. Make sure that images are properly formatted and that all links are valid. Fix titles properly, so that use proper header syntax based on the title level. Exclude page number titles. Again, do not change or leave out ANY information. If the markdown is already perfectly valid, output the markdown as is. Markdown: ${markdown} Fixed Markdown: `; }; const fixUnstructuredMarkdownPrompt = (markdown: string) => { return ` You are acting as an expert markdown The following markdown is malformed. Correct it, and output valid markdown. Do not stop until all the markdown is converted to valid markdown. Do not change or leave out any information. Do not use code blocks. Some of the list items may be broken into separate fields when they should be combined. Make sure that images are properly formatted and that all links are valid. Fix titles properly, so that use proper header syntax based on the title level. Exclude page number titles. Again, do not change or leave out ANY information. Markdown: ${markdown} Fixed Markdown: `; }; const PromptUtil = { createHydePrompt, createContextPrompt, fixMarkdownPrompt, fixUnstructuredMarkdownPrompt, }; export default PromptUtil;