Files
wehub-resource-sync a0c8464e58
Build Package / build (ubuntu-latest) (push) Failing after 1s
CodeQL / Analyze (python) (push) Failing after 1s
Core Typecheck / core-typecheck (push) Failing after 1s
Linting / lint (push) Failing after 1s
llama-dev tests / test-llama-dev (push) Failing after 1s
Publish Sub-Package to PyPI if Needed / publish_subpackage_if_needed (push) Has been skipped
Sync Docs to Developer Hub / sync-docs (push) Failing after 0s
Build Package / build (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:26:52 +08:00

197 lines
6.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "9e22fa55",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/evaluation/guideline_eval.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"id": "da1a2754-e6c0-45ec-8bd5-b080673fb26d",
"metadata": {},
"source": [
"# Guideline Evaluator"
]
},
{
"cell_type": "markdown",
"id": "51074c30-8e39-4f30-8125-f1fedb28c679",
"metadata": {},
"source": [
"This notebook shows how to use `GuidelineEvaluator` to evaluate a question answer system given user specified guidelines."
]
},
{
"cell_type": "markdown",
"id": "b48d62d4",
"metadata": {},
"source": [
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ef35dac",
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-llms-openai"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b2316a6",
"metadata": {},
"outputs": [],
"source": [
"!pip install llama-index"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f647fa2-f007-4242-8c5b-5dbdbb3ad345",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core.evaluation import GuidelineEvaluator\n",
"from llama_index.llms.openai import OpenAI\n",
"\n",
"# Needed for running async functions in Jupyter Notebook\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5726c6cf-f9e8-489a-80cb-3272737d2b6f",
"metadata": {},
"outputs": [],
"source": [
"GUIDELINES = [\n",
" \"The response should fully answer the query.\",\n",
" \"The response should avoid being vague or ambiguous.\",\n",
" (\n",
" \"The response should be specific and use statistics or numbers when\"\n",
" \" possible.\"\n",
" ),\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca96df17-1c4e-4474-8579-a27d53ac82b3",
"metadata": {},
"outputs": [],
"source": [
"llm = OpenAI(model=\"gpt-4\")\n",
"\n",
"evaluators = [\n",
" GuidelineEvaluator(llm=llm, guidelines=guideline)\n",
" for guideline in GUIDELINES\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "07ce9b34-73b2-4be7-a1da-3f9103988c9a",
"metadata": {},
"outputs": [],
"source": [
"sample_data = {\n",
" \"query\": \"Tell me about global warming.\",\n",
" \"contexts\": [\n",
" (\n",
" \"Global warming refers to the long-term increase in Earth's\"\n",
" \" average surface temperature due to human activities such as the\"\n",
" \" burning of fossil fuels and deforestation.\"\n",
" ),\n",
" (\n",
" \"It is a major environmental issue with consequences such as\"\n",
" \" rising sea levels, extreme weather events, and disruptions to\"\n",
" \" ecosystems.\"\n",
" ),\n",
" (\n",
" \"Efforts to combat global warming include reducing carbon\"\n",
" \" emissions, transitioning to renewable energy sources, and\"\n",
" \" promoting sustainable practices.\"\n",
" ),\n",
" ],\n",
" \"response\": (\n",
" \"Global warming is a critical environmental issue caused by human\"\n",
" \" activities that lead to a rise in Earth's temperature. It has\"\n",
" \" various adverse effects on the planet.\"\n",
" ),\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69f66574-87dd-4aec-8d73-efe347b701e6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=====\n",
"Guideline: The response should fully answer the query.\n",
"Pass: False\n",
"Feedback: The response does not fully answer the query. While it does provide a brief overview of global warming, it does not delve into the specifics of the causes, effects, or potential solutions to the problem. The response should be more detailed and comprehensive to fully answer the query.\n",
"=====\n",
"Guideline: The response should avoid being vague or ambiguous.\n",
"Pass: False\n",
"Feedback: The response is too vague and does not provide specific details about global warming. It should include more information about the causes, effects, and potential solutions to global warming.\n",
"=====\n",
"Guideline: The response should be specific and use statistics or numbers when possible.\n",
"Pass: False\n",
"Feedback: The response is too general and lacks specific details or statistics about global warming. It would be more informative if it included data such as the rate at which the Earth's temperature is rising, the main human activities contributing to global warming, or the specific adverse effects on the planet.\n"
]
}
],
"source": [
"for guideline, evaluator in zip(GUIDELINES, evaluators):\n",
" eval_result = evaluator.evaluate(\n",
" query=sample_data[\"query\"],\n",
" contexts=sample_data[\"contexts\"],\n",
" response=sample_data[\"response\"],\n",
" )\n",
" print(\"=====\")\n",
" print(f\"Guideline: {guideline}\")\n",
" print(f\"Pass: {eval_result.passing}\")\n",
" print(f\"Feedback: {eval_result.feedback}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}