自修正查询引擎 - 评估与重试
在本笔记本中,我们展示了几个高级的自校正查询引擎。
它们利用最新大型语言模型评估自身输出的能力,然后通过自我校正来提供更好的响应。
如果您在 Colab 上打开这个笔记本,您可能需要安装 LlamaIndex 🦙。
!pip install llama-index# Uncomment to add your OpenAI API key# import os# os.environ['OPENAI_API_KEY'] = "INSERT OPENAI KEY"# Uncomment for debug level logging# import logging# import sys
# logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)# logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))首先我们摄取文档。
from llama_index.core import VectorStoreIndexfrom llama_index.core import SimpleDirectoryReader
# Needed for running async functions in Jupyter Notebookimport nest_asyncio
nest_asyncio.apply()下载数据
!mkdir -p 'data/paul_graham/'!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'加载数据
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()index = VectorStoreIndex.from_documents(documents)query = "What did the author do growing up?"让我们看看默认查询引擎的响应是什么样的
base_query_engine = index.as_query_engine()response = base_query_engine.query(query)print(response)The author worked on writing and programming outside of school before college. They wrote short stories and tried writing programs on an IBM 1401 computer using an early version of Fortran. They later got a microcomputer and started programming on it, writing simple games and a word processor. They also mentioned their interest in philosophy and AI.重试查询引擎使用评估器来改进基础查询引擎的响应。
它执行以下操作:
- 首先查询基础查询引擎,然后
- 使用评估器来决定响应是否通过。
- 如果响应通过,则返回响应,
- 否则,将原始查询与评估结果(查询、响应和反馈)转换为新的查询,
- 重复最多 max_retries 次
from llama_index.core.query_engine import RetryQueryEnginefrom llama_index.core.evaluation import RelevancyEvaluator
query_response_evaluator = RelevancyEvaluator()retry_query_engine = RetryQueryEngine( base_query_engine, query_response_evaluator)retry_response = retry_query_engine.query(query)print(retry_response)The author worked on writing and programming outside of school before college. They wrote short stories and tried writing programs on an IBM 1401 computer using an early version of Fortran. They later got a microcomputer, a TRS-80, and started programming more extensively, including writing simple games and a word processor.Source Retry 通过基于LLM节点评估筛选查询的现有源节点来修改查询源节点。
from llama_index.core.query_engine import RetrySourceQueryEngine
retry_source_query_engine = RetrySourceQueryEngine( base_query_engine, query_response_evaluator)retry_source_response = retry_source_query_engine.query(query)print(retry_source_response)The author worked on writing and programming outside of school before college. They wrote short stories and tried writing programs on an IBM 1401 computer using an early version of Fortran. They later got a microcomputer and started programming on it, writing simple games and a word processor. They also mentioned their interest in philosophy and AI.本模块尝试使用指导原则来引导评估者的行为。您可以自定义自己的指导原则。
from llama_index.core.evaluation import GuidelineEvaluatorfrom llama_index.core.evaluation.guideline import DEFAULT_GUIDELINESfrom llama_index.core import Responsefrom llama_index.core.indices.query.query_transform.feedback_transform import ( FeedbackQueryTransformation,)from llama_index.core.query_engine import RetryGuidelineQueryEngine
# Guideline evalguideline_eval = GuidelineEvaluator( guidelines=DEFAULT_GUIDELINES + "\nThe response should not be overly long.\n" "The response should try to summarize where possible.\n") # just for example让我们看看幕后发生了什么。
typed_response = ( response if isinstance(response, Response) else response.get_response())eval = guideline_eval.evaluate_response(query, typed_response)print(f"Guideline eval evaluation result: {eval.feedback}")
feedback_query_transform = FeedbackQueryTransformation(resynthesize_query=True)transformed_query = feedback_query_transform.run(query, {"evaluation": eval})print(f"Transformed query: {transformed_query.query_str}")Guideline eval evaluation result: The response partially answers the query but lacks specific statistics or numbers. It provides some details about the author's activities growing up, such as writing short stories and programming on different computers, but it could be more concise and focused. Additionally, the response does not mention any statistics or numbers to support the author's experiences.Transformed query: Here is a previous bad answer.The author worked on writing and programming outside of school before college. They wrote short stories and tried writing programs on an IBM 1401 computer using an early version of Fortran. They later got a microcomputer and started programming on it, writing simple games and a word processor. They also mentioned their interest in philosophy and AI.Here is some feedback from the evaluator about the response given.The response partially answers the query but lacks specific statistics or numbers. It provides some details about the author's activities growing up, such as writing short stories and programming on different computers, but it could be more concise and focused. Additionally, the response does not mention any statistics or numbers to support the author's experiences.Now answer the question.What were the author's activities and interests during their childhood and adolescence?现在让我们运行完整的查询引擎
retry_guideline_query_engine = RetryGuidelineQueryEngine( base_query_engine, guideline_eval, resynthesize_query=True)retry_guideline_response = retry_guideline_query_engine.query(query)print(retry_guideline_response)During their childhood and adolescence, the author worked on writing short stories and programming. They mentioned that their short stories were not very good, lacking plot but focusing on characters with strong feelings. In terms of programming, they tried writing programs on the IBM 1401 computer in 9th grade using an early version of Fortran. However, they mentioned being puzzled by the 1401 and not being able to do much with it due to the limited input options. They also mentioned getting a microcomputer, a TRS-80, and starting to write simple games, a program to predict rocket heights, and a word processor.