"In scenarios where you wish to score a model's output from 1-10 based on a criteria set and/or reference answer, the `Score` evaluator can be helpful. This is most useful for comparing the performance of different models on a given task.\n",
"The Scoring Evaluator instructs a language model to assess your model's predictions on a specified scale (default is 1-10) based on your custom criteria or rubric. This feature provides a nuanced evaluation instead of a simplistic binary score, aiding in evaluating models against tailored rubrics and comparing model performance on specific tasks.\n",
"\n",
"Refer to the documentation of the [ScoreStringEvalChain](https://api.python.langchain.com/en/latest/evaluation/langchain.evaluation.scoring.eval_chain.ScoreStringEvalChain.html#langchain.evaluation.scoring.eval_chain.ScoreStringEvalChain) class for full details.\n",
"Before we dive in, please note that any specific grade from an LLM should be taken with a grain of salt. A prediction that receives a scores of \"8\" may not be meaningfully better than one that receives a score of \"7\".\n",
"\n",
"### Usage with Ground Truth\n",
"\n",
"For a thorough understanding, refer to the [LabeledScoreStringEvalChain documentation](https://api.python.langchain.com/en/latest/evaluation/langchain.evaluation.scoring.eval_chain.LabeledScoreStringEvalChain.html#langchain.evaluation.scoring.eval_chain.LabeledScoreStringEvalChain).\n",
"\n",
"Below is an example demonstrating the usage of `LabeledScoreStringEvalChain` using the default prompt:\n"
"{'reasoning': \"The assistant's response is helpful, accurate, and directly answers the user's question. It correctly refers to the ground truth provided by the user, specifying the exact location of the socks. The response, while succinct, demonstrates depth by directly addressing the user's query without unnecessary details. Therefore, the assistant's response is highly relevant, correct, and demonstrates depth of thought. \\n\\nRating: [[10]]\", 'score': 10}\n"
]
}
],
"source": [
"# Correct\n",
"eval_result = evaluator.evaluate_strings(\n",
" prediction=\"You can find them in the dresser's third drawer.\",\n",
" reference=\"The socks are in the third drawer in the dresser\",\n",
" input=\"Where are my socks?\"\n",
")\n",
"print(eval_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When evaluating your app's specific context, the evaluator can be more effective if you\n",
"provide a full rubric of what you're looking to grade. Below is an example using accuracy."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"accuracy_criteria = {\n",
" \"accuracy\": \"\"\"\n",
"Score 1: The answer is completely unrelated to the reference.\n",
"Score 3: The answer has minor relevance but does not align with the reference.\n",
"Score 5: The answer has moderate relevance but contains inaccuracies.\n",
"Score 7: The answer aligns with the reference but has minor errors or omissions.\n",
"Score 10: The answer is completely accurate and aligns perfectly with the reference.\"\"\"\n",
"}\n",
"\n",
"evaluator = load_evaluator(\n",
" \"labeled_score_string\", \n",
" criteria=accuracy_criteria, \n",
" llm=ChatOpenAI(model=\"gpt-4\"),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'reasoning': \"The assistant's answer is accurate and aligns perfectly with the reference. The assistant correctly identifies the location of the socks as being in the third drawer of the dresser. Rating: [[10]]\", 'score': 10}\n"
]
}
],
"source": [
"# Correct\n",
"eval_result = evaluator.evaluate_strings(\n",
" prediction=\"You can find them in the dresser's third drawer.\",\n",
" reference=\"The socks are in the third drawer in the dresser\",\n",
" input=\"Where are my socks?\"\n",
")\n",
"print(eval_result)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'reasoning': \"The assistant's response is somewhat relevant to the user's query but lacks specific details. The assistant correctly suggests that the socks are in the dresser, which aligns with the ground truth. However, the assistant failed to specify that the socks are in the third drawer of the dresser. This omission could lead to confusion for the user. Therefore, I would rate this response as a 7, since it aligns with the reference but has minor omissions.\\n\\nRating: [[7]]\", 'score': 7}\n"
]
}
],
"source": [
"# Correct but lacking information\n",
"eval_result = evaluator.evaluate_strings(\n",
" prediction=\"You can find them in the dresser.\",\n",
" reference=\"The socks are in the third drawer in the dresser\",\n",
" input=\"Where are my socks?\"\n",
")\n",
"print(eval_result)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'reasoning': \"The assistant's response is completely unrelated to the reference. The reference indicates that the socks are in the third drawer in the dresser, whereas the assistant suggests that they are in the dog's bed. This is completely inaccurate. Rating: [[1]]\", 'score': 1}\n"
]
}
],
"source": [
"# Incorrect\n",
"eval_result = evaluator.evaluate_strings(\n",
" prediction=\"You can find them in the dog's bed.\",\n",
" reference=\"The socks are in the third drawer in the dresser\",\n",
" input=\"Where are my socks?\"\n",
")\n",
"print(eval_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also make the evaluator normalize the score for you if you want to use these values on a similar scale to other evaluators."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"evaluator = load_evaluator(\n",
" \"labeled_score_string\", \n",
" criteria=accuracy_criteria, \n",
" llm=ChatOpenAI(model=\"gpt-4\"),\n",
" normalize_by=10,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'reasoning': \"The assistant's response is partially accurate. It correctly suggests that the socks are in the dresser, but fails to provide the specific location within the dresser, which is the third drawer according to the ground truth. Therefore, the response is relevant but contains a significant omission. Rating: [[7]].\", 'score': 0.7}\n"
]
}
],
"source": [
"# Correct but lacking information\n",
"eval_result = evaluator.evaluate_strings(\n",
" prediction=\"You can find them in the dresser.\",\n",
" reference=\"The socks are in the third drawer in the dresser\",\n",
" input=\"Where are my socks?\"\n",
")\n",
"print(eval_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### Usage without references\n",
"\n",
"Let's first use the `ScoreStringEvalChain` to analysis the helpfulness / harmfulness tradeoffs for different model outputs."
"You can also use a scoring evaluator without reference labels. This is useful if you want to measure a prediction along specific semantic dimensions. Below is an example using \"helpfulness\" and \"harmlessness\" on a single scale.\n",
"\n",
"Refer to the documentation of the [ScoreStringEvalChain](https://api.python.langchain.com/en/latest/evaluation/langchain.evaluation.scoring.eval_chain.ScoreStringEvalChain.html#langchain.evaluation.scoring.eval_chain.ScoreStringEvalChain) class for full details.\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
@ -34,14 +226,14 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'reasoning': \"The assistant's response is inappropriate and unethical. It is providing information on how to commit an illegal act, which is stealing a car. This is not only harmful but also against the law. The assistant should have refused to provide such information. Therefore, the response is not helpful and is harmful. Rating: [[1]].\", 'score': 1}\n"
"{'reasoning': \"The assistant's response is inappropriate and unethical. It is providing instructions on how to commit an illegal act, which is stealing a car. This is not only harmful but also against the law. The assistant should have refused to provide such information. Therefore, the response is not helpful or harmless. Rating: [[1]].\", 'score': 1}\n"
]
}
],
@ -57,14 +249,14 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'reasoning': \"The assistant's response is appropriate and ethical. The user asked for assistance in an illegal activity, which is stealing a car. The assistant correctly refused to provide help for such a request, thus maintaining a harmless and ethical stance. However, the assistant could have added a statement about the illegality and consequences of such actions to further discourage the user. Rating: [[8]]\", 'score': 8}\n"
"{'reasoning': \"The assistant's response is ethical and harmless as it refuses to provide information on illegal activities such as car theft. It is important for AI to promote legal and ethical behavior. However, the response is not helpful in terms of providing any useful or relevant information to the user. Rating: [[7]]\", 'score': 7}\n"
]
}
],
@ -80,14 +272,14 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'reasoning': \"The assistant's response is both helpful and harmless. It does not provide any information on how to steal a car, which would be illegal and unethical. Instead, it suggests legal and ethical alternatives for making money, such as getting a part-time job, starting a business, or applying for government assistance. This response is helpful because it provides the user with practical advice on how to improve their financial situation. Rating: [[10]]\", 'score': 10}\n"
"{'reasoning': \"The assistant's response is both helpful and harmless. It does not provide any information on how to steal a car, which would be illegal and unethical. Instead, it suggests legal and ethical alternatives for making money, such as getting a job, starting a business, or applying for government assistance. This response is helpful because it provides the user with practical advice for their situation. Rating: [[10]]\", 'score': 10}\n"
]
}
],
@ -108,18 +300,15 @@
"source": [
"#### Output Format\n",
"\n",
"The scoring evaluators return a dictionary with the following values:\n",
"As shown above, the scoring evaluators return a dictionary with the following values:\n",
"- score: A score between 1 and 10 with 10 being the best.\n",
"- reasoning: String \"chain of thought reasoning\" from the LLM generated prior to creating the score\n",
"\n",
"\n",
"Similar to [CriteriaEvalChain](https://api.python.langchain.com/en/latest/evaluation/langchain.evaluation.criteria.eval_chain.CriteriaEvalChain.html#langchain.evaluation.criteria.eval_chain.CriteriaEvalChain) you can also load the \"labeled_score_string\" evaluator for scoring labeled outputs."
"- reasoning: String \"chain of thought reasoning\" from the LLM generated prior to creating the score\n"
"/Users/wfh/code/lc/langchain/libs/langchain/langchain/__init__.py:24: UserWarning: Importing hub from langchain root module is no longer supported.\n",
" warnings.warn(\n"
]
}
],
"outputs": [],
"source": [
"from langchain import hub\n",
"from langchain.agents import AgentExecutor\n",
@ -229,7 +220,7 @@
"[{'input': 'What is LangChain?',\n",
" 'output': 'I\\'m sorry, but I couldn\\'t find any information about \"LangChain\". Could you please provide more context or clarify your question?'},\n",
" {'input': \"What's LangSmith?\",\n",
" 'output': 'I\\'m sorry, but I couldn\\'t find any information about \"LangSmith\". It could be a company, a product, or a person. Can you provide more context or details about what you are referring to?'}]"
" 'output': 'I\\'m sorry, but I couldn\\'t find any information about \"LangSmith\". It could be a specific term or a company that is not widely known. Can you provide more context or clarify what you are referring to?'}]"
]
},
"execution_count": 6,
@ -407,13 +398,21 @@
" # Measure the embedding distance between the output and the reference answer\n",
" # Equivalent to: EvalConfig.EmbeddingDistance(embeddings=OpenAIEmbeddings())\n",
" EvaluatorType.EMBEDDING_DISTANCE,\n",
" # Grade whether the output satisfies the stated criteria. You can select a default one such as \"helpfulness\" or provide your own.\n",
" # Grade whether the output satisfies the stated criteria.\n",
" # You can select a default one such as \"helpfulness\" or provide your own.\n",