This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article shows you how to evaluate a chat app's answers against a set of correct or ideal answers (known as ground truth). Whenever you change your chat application in a way that affects the answers, run an evaluation to compare the changes. This demo application offers tools that you can use today to make it easier to run evaluations.
By following the instructions in this article, you:
Note
This article uses one or moreAI app templates as the basis for the examples and guidance in the article. AI app templates provide you with well-maintained reference implementations that are easy to deploy. They help to ensure a high-quality starting point for your AI apps.
Key components of the architecture include:
When you deploy this evaluation to Azure, the Azure OpenAI Service endpoint is created for theGPT-4 model with its owncapacity. When you evaluate chat applications, it's important that the evaluator has its own Azure OpenAI resource by usingGPT-4 with its own capacity.
Azure subscription.Create one for free
Deploy a chat app.
These chat apps load the data into the Azure AI Search resource. This resource is required for the evaluations app to work. Don't complete theClean up resources section of the previous procedure.
You need the following Azure resource information from that deployment, which is referred to as thechat app in this article:
azd up process.Search service during theazd up process.The Chat API URL allows the evaluations to make requests through your backend application. The Azure AI Search information allows the evaluation scripts to use the same deployment as your backend, loaded with the documents.
After you collect this information, you don’t need to use thechat app development environment again. This article refers to thechat app several times to show how theEvaluations app uses it. Don’t delete thechat app resources until you finish all steps in this article.
Adevelopment container environment is available with all dependencies required to complete this article. You can run the development container in GitHub Codespaces (in a browser) or locally using Visual Studio Code.
Follow these instructions to set up a preconfigured development environment with all the required dependencies to complete this article. Arrange your monitor workspace so that you can see this documentation and the development environment at the same time.
This article was tested with theswitzerlandnorth region for the evaluation deployment.
GitHub Codespaces runs a development container managed by GitHub withVisual Studio Code for the Web as the user interface. Use GitHub Codespaces for the easiest development environment. It comes with the right developer tools and dependencies preinstalled to complete this article.
Important
All GitHub accounts can use GitHub Codespaces for up to 60 hours free each month with two core instances. For more information, seeGitHub Codespaces monthly included storage and core hours.
Start the process to create a new GitHub codespace on themain branch of theAzure-Samples/ai-rag-chat-evaluator GitHub repository.
To display the development environment and the documentation available at the same time, right-click the following button, and selectOpen link in new window.
On theCreate codespace page, review the codespace configuration settings, and then selectCreate new codespace.

Wait for the codespace to start. This startup process can take a few minutes.
In the terminal at the bottom of the screen, sign in to Azure with the Azure Developer CLI:
azd auth login --use-device-codeCopy the code from the terminal and then paste it into a browser. Follow the instructions to authenticate with your Azure account.
Provision the required Azure resource, Azure OpenAI Service, for the evaluations app:
azd upThisAZD command doesn't deploy the evaluations app, but it does create the Azure OpenAI resource with a requiredGPT-4 deployment to run the evaluations in the local development environment.
The remaining tasks in this article take place in the context of this development container.
The name of the GitHub repository appears in the search bar. This visual indicator helps you distinguish the evaluations app from the chat app. Thisai-rag-chat-evaluator repo is referred to as theevaluations app in this article.
Update the environment values and configuration information with the information you gathered duringPrerequisites for the evaluations app.
Create a.env file based on.env.sample.
cp .env.sample .envRun this command to get the required values forAZURE_OPENAI_EVAL_DEPLOYMENT andAZURE_OPENAI_SERVICE from your deployed resource group. Paste those values into the.env file.
azd env get-value AZURE_OPENAI_EVAL_DEPLOYMENTazd env get-value AZURE_OPENAI_SERVICEAdd the following values from the chat app for its Azure AI Search instance to the.env file, which you gathered in thePrerequisites section.
AZURE_SEARCH_SERVICE="<service-name>"AZURE_SEARCH_INDEX="<index-name>"The chat app and the evaluations app both implement the Microsoft AI Chat Protocol specification, an open-source, cloud, and language-agnostic AI endpoint API contract that's used for consumption and evaluation. When your client and middle-tier endpoints adhere to this API specification, you can consistently consume and run evaluations on your AI backends.
Create a new file namedmy_config.json and copy the following content into it:
{ "testdata_path": "my_input/qa.jsonl", "results_dir": "my_results/experiment<TIMESTAMP>", "target_url": "http://localhost:50505/chat", "target_parameters": { "overrides": { "top": 3, "temperature": 0.3, "retrieval_mode": "hybrid", "semantic_ranker": false, "prompt_template": "<READFILE>my_input/prompt_refined.txt", "seed": 1 } }}The evaluation script creates themy_results folder.
Theoverrides object contains any configuration settings that are needed for the application. Each application defines its own set of settings properties.
Use the following table to understand the meaning of the settings properties that are sent to the chat app.
| Settings property | Description |
|---|---|
semantic_ranker | Whether to usesemantic ranker, a model that reranks search results based on semantic similarity to the user's query. We disable it for this tutorial to reduce costs. |
retrieval_mode | The retrieval mode to use. The default ishybrid. |
temperature | The temperature setting for the model. The default is0.3. |
top | The number of search results to return. The default is3. |
prompt_template | An override of the prompt used to generate the answer based on the question and search results. |
seed | The seed value for any calls to GPT models. Setting a seed results in more consistent results across evaluations. |
Change thetarget_url value to the URI value of your chat app, which you gathered in thePrerequisites section. The chat app must conform to the chat protocol. The URI has the following format:https://CHAT-APP-URL/chat. Make sure the protocol and thechat route are part of the URI.
To evaluate new answers, they must be compared to aground truth answer, which is the ideal answer for a particular question. Generate questions and answers from documents that are stored in Azure AI Search for the chat app.
Copy theexample_input folder into a new folder namedmy_input.
In a terminal, run the following command to generate the sample data:
python -m evaltools generate --output=my_input/qa.jsonl --persource=2 --numquestions=14The question-and-answer pairs are generated and stored inmy_input/qa.jsonl (inJSONL format) as input to the evaluator that's used in the next step. For a production evaluation, you would generate more question-and-answer pairs. More than 200 are generated for this dataset.
Note
Only a few questions and answers are generated per source so that you can quickly complete this procedure. It isn't meant to be a production evaluation, which should have more questions and answers per source.
Edit themy_config.json configuration file properties.
| Property | New value |
|---|---|
results_dir | my_results/experiment_refined |
prompt_template | <READFILE>my_input/prompt_refined.txt |
The refined prompt is specific about the subject domain.
If there isn't enough information below, say you don't know. Do not generate answers that don't use the sources below. If asking a clarifying question to the user would help, ask the question.Use clear and concise language and write in a confident yet friendly tone. In your answers, ensure the employee understands how your response connects to the information in the sources and include all citations necessary to help the employee validate the answer provided.For tabular information, return it as an html table. Do not return markdown format. If the question is not in English, answer in the language used in the question.Each source has a name followed by a colon and the actual information. Always include the source name for each fact you use in the response. Use square brackets to reference the source, e.g. [info1.txt]. Don't combine sources, list each source separately, e.g. [info1.txt][info2.pdf].In a terminal, run the following command to run the evaluation:
python -m evaltools evaluate --config=my_config.json --numquestions=14This script created a new experiment folder inmy_results/ with the evaluation. The folder contains the results of the evaluation.
| File name | Description |
|---|---|
config.json | A copy of the configuration file used for the evaluation. |
evaluate_parameters.json | The parameters used for the evaluation. Similar toconfig.json but includes other metadata like time stamp. |
eval_results.jsonl | Each question and answer, along with the GPT metrics for each question-and-answer pair. |
summary.json | The overall results, like the average GPT metrics. |
Edit themy_config.json configuration file properties.
| Property | New value |
|---|---|
results_dir | my_results/experiment_weak |
prompt_template | <READFILE>my_input/prompt_weak.txt |
That weak prompt has no context about the subject domain.
You are a helpful assistant.In a terminal, run the following command to run the evaluation:
python -m evaltools evaluate --config=my_config.json --numquestions=14Use a prompt that allows for more creativity.
Edit themy_config.json configuration file properties.
| Existing | Property | New value |
|---|---|---|
| Existing | results_dir | my_results/experiment_ignoresources_temp09 |
| Existing | prompt_template | <READFILE>my_input/prompt_ignoresources.txt |
| New | temperature | 0.9 |
The defaulttemperature is 0.7. The higher the temperature, the more creative the answers.
Theignore prompt is short.
Your job is to answer questions to the best of your ability. You will be given sources but you should IGNORE them. Be creative!The configuration object should look like the following example, except that you replacedresults_dir with your path:
{ "testdata_path": "my_input/qa.jsonl", "results_dir": "my_results/prompt_ignoresources_temp09", "target_url": "https://YOUR-CHAT-APP/chat", "target_parameters": { "overrides": { "temperature": 0.9, "semantic_ranker": false, "prompt_template": "<READFILE>my_input/prompt_ignoresources.txt" } }}In a terminal, run the following command to run the evaluation:
python -m evaltools evaluate --config=my_config.json --numquestions=14You performed three evaluations based on different prompts and app settings. The results are stored in themy_results folder. Review how the results differ based on the settings.
Use the review tool to see the results of the evaluations.
python -m evaltools summary my_resultsThe results looksomething like:

Each value is returned as a number and a percentage.
Use the following table to understand the meaning of the values.
| Value | Description |
|---|---|
| Groundedness | Checks how well the model's responses are based on factual, verifiable information. A response is considered grounded if it's factually accurate and reflects reality. |
| Relevance | Measures how closely the model's responses align with the context or the prompt. A relevant response directly addresses the user's query or statement. |
| Coherence | Checks how logically consistent the model's responses are. A coherent response maintains a logical flow and doesn't contradict itself. |
| Citation | Indicates if the answer was returned in the format requested in the prompt. |
| Length | Measures the length of the response. |
The results should indicate that all three evaluations had high relevance while theexperiment_ignoresources_temp09 had the lowest relevance.
Select the folder to see the configuration for the evaluation.
EnterCtrl +C to exit the app and return to the terminal.
Compare the returned answers from the evaluations.
Select two of the evaluations to compare, and then use the same review tool to compare the answers.
python -m evaltools diff my_results/experiment_refined my_results/experiment_ignoresources_temp09Review the results. Your results might vary.

EnterCtrl +C to exit the app and return to the terminal.
my_input to tailor the answers such as subject domain, length, and other factors.my_config.json file to change the parameters such astemperature, andsemantic_ranker and rerun experiments.Please answer in about 3 sentences.The following steps walk you through the process of cleaning up the resources you used.
The Azure resources created in this article are billed to your Azure subscription. If you don't expect to need these resources in the future, delete them to avoid incurring more charges.
To delete the Azure resources and remove the source code, run the following Azure Developer CLI command:
azd down --purgeDeleting the GitHub Codespaces environment ensures that you can maximize the amount of free per-core hours entitlement that you get for your account.
Important
For more information about your GitHub account's entitlements, seeGitHub Codespaces monthly included storage and core hours.
Sign in to theGitHub Codespaces dashboard.
Locate your currently running codespaces that are sourced from theAzure-Samples/ai-rag-chat-evaluator GitHub repository.

Open the context menu for the codespace, and then selectDelete.

Return to the chat app article to clean up those resources.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?