#!/usr/bin/env python # coding: utf-8 # 在 Colab 中打开 # # # 指南评估器 # # 这个笔记本展示了如何使用`GuidelineEvaluator`来评估一个问答系统,根据用户指定的准则。 # # 如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex 🦙。 # # In[ ]: get_ipython().run_line_magic('pip', 'install llama-index-llms-openai') # In[ ]: get_ipython().system('pip install llama-index') # In[ ]: from llama_index.core.evaluation import GuidelineEvaluatorfrom llama_index.llms.openai import OpenAI# 在Jupyter Notebook中运行异步函数所需import nest_asyncionest_asyncio.apply() # In[ ]: GUIDELINES = [ "The response should fully answer the query.", "The response should avoid being vague or ambiguous.", ( "The response should be specific and use statistics or numbers when" " possible." ), ] # In[ ]: llm = OpenAI(model="gpt-4") evaluators = [ GuidelineEvaluator(llm=llm, guidelines=guideline) for guideline in GUIDELINES ] # In[ ]: sample_data = { "query": "Tell me about global warming.", "contexts": [ ( "Global warming refers to the long-term increase in Earth's" " average surface temperature due to human activities such as the" " burning of fossil fuels and deforestation." ), ( "It is a major environmental issue with consequences such as" " rising sea levels, extreme weather events, and disruptions to" " ecosystems." ), ( "Efforts to combat global warming include reducing carbon" " emissions, transitioning to renewable energy sources, and" " promoting sustainable practices." ), ], "response": ( "Global warming is a critical environmental issue caused by human" " activities that lead to a rise in Earth's temperature. It has" " various adverse effects on the planet." ), } # In[ ]: for guideline, evaluator in zip(GUIDELINES, evaluators): eval_result = evaluator.evaluate( query=sample_data["query"], contexts=sample_data["contexts"], response=sample_data["response"], ) print("=====") print(f"Guideline: {guideline}") print(f"Pass: {eval_result.passing}") print(f"Feedback: {eval_result.feedback}")