Azure Search + OpenAI Demo: Rewrite a Chat Question into a Knowledge-Base Search Query (Jinja2)

Turns the conversation so far plus a new question into one search query for a document index: no file names, no bracketed text, no special characters, translated to English, or just "0" when no query can be made.

  • 🤖 Any chat LLM
  • 🗂️ Search

Last verified: write-up checked

Prompt by Microsoft Azure Samples (Azure-Samples/azure-search-openai-demo) · Original source (opens in a new tab)

Azure Search + OpenAI Demo: Rewrite a Chat Question into a Knowledge-Base Search Query (Jinja2) — prompt preview

The prompt

prompt
Below is a history of the conversation so far, and a new question asked by the user that needs to be answered by searching in a knowledge base.
You have access to Azure AI Search index with 100's of documents.
Generate a search query based on the conversation and the new question.
Do not include cited source filenames and document names e.g. info.txt or doc.pdf in the search query terms.
Do not include any text inside [] or <<>> in the search query terms.
Do not include any special characters like '+'.
If the question is not in English, translate the question to English before generating the search query.
If you cannot generate a search query, return just the number 0.

Generate search query for: How did crypto do last year?

Search query: Summarize Cryptocurrency Market Dynamics from last year

Generate search query for: What are my health plans?

Search query: Show available health plans

{% if past_messages %}
Conversation history:

{% for message in past_messages %}
{{ message["role"] }}:
{{ message["content"] }}
{% endfor %}
{% endif %}

Generate search query for: {{ user_query }}

What it is

Turns the conversation so far plus a new question into one search query for a document index: no file names, no bracketed text, no special characters, translated to English, or just "0" when no query can be made. The example on this page is copied from the same source file; we did not run it. The prompt text comes from the azure-search-openai-demo repository (MIT License, Copyright (c) 2023 Azure Samples). Licence: https://github.com/Azure-Samples/azure-search-openai-demo/blob/main/LICENSE. Original author: Microsoft Azure Samples (Azure-Samples/azure-search-openai-demo). Changes by FoxaMind: none; the Jinja2 template is shown as in the source file, with its {% ... %} and {{ ... }} tags.

Who it's for

  • Developers building chat over company documents
  • Anyone adding a query-rewrite step to RAG

يحوّل المحادثة السابقة مع سؤال جديد إلى استعلام بحث واحد في فهرس المستندات: بلا أسماء ملفات ولا نص بين أقواس ولا رموز خاصة، مترجماً إلى الإنجليزية، أو "0" فقط إذا تعذّر تكوين استعلام.

Examples

Examples inside the prompt

Prompt
prompt
Generate search query for: How did crypto do last year?

Search query: Summarize Cryptocurrency Market Dynamics from last year

Generate search query for: What are my health plans?

Search query: Show available health plans

Expected output: The two question and query pairs in the source prompt. Not run by us.

Tips

  • The fallback is explicit: return just the number 0 when no search query can be generated.
  • The prompt says not to include any text inside [] or <<>> in the search query terms.
  • past_messages and user_query are filled by the app.