langchain/docs/extras/guides/evaluation/string/exact_match.ipynb

175 lines
5.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "2da95378",
"metadata": {},
"source": [
"# Exact Match\n",
"[![Open In Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/langchain-ai/langchain/blob/master/docs/extras/guides/evaluation/string/exact_match.ipynb)\n",
"\n",
"Probably the simplest ways to evaluate an LLM or runnable's string output against a reference label is by a simple string equivalence.\n",
"\n",
"This can be accessed using the `exact_match` evaluator."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "0de44d01-1fea-4701-b941-c4fb74e521e7",
"metadata": {},
"outputs": [],
"source": [
"from langchain.evaluation import ExactMatchStringEvaluator\n",
"\n",
"evaluator = ExactMatchStringEvaluator()"
]
},
{
"cell_type": "markdown",
"id": "fe3baf5f-bfee-4745-bcd6-1a9b422ed46f",
"metadata": {},
"source": [
"Alternatively via the loader:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f6790c46",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.evaluation import load_evaluator\n",
"\n",
"evaluator = load_evaluator(\"exact_match\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "49ad9139",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"{'score': 0}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"evaluator.evaluate_strings(\n",
" prediction=\"1 LLM.\",\n",
" reference=\"2 llm\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1f5e82a3-247e-45a8-85fc-6af53bf7ff82",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'score': 0}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"evaluator.evaluate_strings(\n",
" prediction=\"LangChain\",\n",
" reference=\"langchain\",\n",
")"
]
},
{
"cell_type": "markdown",
"id": "b8ed1f12-09a6-4e90-a69d-c8df525ff293",
"metadata": {},
"source": [
"## Configure the ExactMatchStringEvaluator\n",
"\n",
"You can relax the \"exactness\" when comparing strings."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0c079864-0175-4d06-9d3f-a0e51dd3977c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"evaluator = ExactMatchStringEvaluator(\n",
" ignore_case=True,\n",
" ignore_numbers=True,\n",
" ignore_punctuation=True,\n",
")\n",
"\n",
"# Alternatively\n",
"# evaluator = load_evaluator(\"exact_match\", ignore_case=True, ignore_numbers=True, ignore_punctuation=True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a8dfb900-14f3-4a1f-8736-dd1d86a1264c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'score': 1}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"evaluator.evaluate_strings(\n",
" prediction=\"1 LLM.\",\n",
" reference=\"2 llm\",\n",
")"
]
}
],
"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",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}