llama_index 更改累积响应合成器的提示,

px9o7tmv  于 9个月前  发布在  其他
关注(0)|答案(5)|浏览(119)

问题验证

  • 我已在文档和Discord中寻找答案。

问题

我正在使用累积响应合成器,想知道是否可以更改相同的提示。基本上,累积为检索到的数据块生成许多响应。如果文档没有提供编写的查询的上下文,那么它会写成“在文档[文档名称或其他内容]中没有提供上下文”。现在我们能否更改函数,使其不在最终输出中写入这些内容,并在未提供上下文的情况下跳过该文档以及该文档的响应?

c8ib6hqw

c8ib6hqw1#

要修改 Accumulate 响应合成器,如果没有提供上下文,则完全跳过文档。您可以在 Accumulate 类中添加一个检查,以确保它只处理具有上下文的文档。如果没有提供上下文,它应该跳过这些文档,并在最终输出中不包含它们的任何响应。
以下是修改 Accumulate 类的示例:

  1. from llama_index.legacy.response_synthesizers.base import BaseSynthesizer
  2. class Accumulate(BaseSynthesizer):
  3. def __init__(self, text_qa_template=None, service_context=None, output_cls=None, streaming=False, use_async=False):
  4. super().__init__(service_context=service_context, streaming=streaming)
  5. self._text_qa_template = text_qa_template or DEFAULT_TEXT_QA_PROMPT_SEL
  6. self._use_async = use_async
  7. self._output_cls = output_cls
  8. def synthesize(self, documents):
  9. responses = []
  10. for doc in documents:
  11. if not doc.context: # Check if context is provided
  12. continue # Skip the document if no context is provided
  13. response = self._service_context.llm.predict(self._text_qa_template, context_str=doc.context)
  14. responses.append(response)
  15. return self._accumulate_responses(responses)
  16. def _accumulate_responses(self, responses):
  17. # Implement the logic to accumulate responses
  18. return " ".join(responses)

在此示例中,修改了 Accumulate 类的 synthesize 方法,以检查每个文档的 context 属性是否已提供。如果没有提供上下文( if not doc.context ),则跳过该文档( continue )。这确保只处理并包含具有上下文的文档在最终输出 1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/response_synthesizers/accumulate.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/response_synthesizers/factory.py) 中。

  • 要继续与 Dosu 交谈,请提及 @dosu。*
展开查看全部
pgccezyw

pgccezyw2#

我们能否改变提示而不是做这件事?

30byixjq

30byixjq3#

是的,您可以在不修改 Accumulate 类的情况下更改累积响应合成器使用的提示,以跳过为不提供查询上下文的文档生成响应。您可以通过更新提示模板来实现这一点,将包含检查文档是否为查询提供上下文并在没有提供上下文时跳过生成响应的逻辑。
以下是如何修改提示模板的示例:

  1. from llama_index.core.prompts.base import PromptTemplate
  2. from llama_index.core.response_synthesizers import get_response_synthesizer, ResponseMode
  3. from llama_index.core.service_context import ServiceContext
  4. class CustomPromptTemplate(PromptTemplate):
  5. def partial_format(self, query_str: str) -> str:
  6. # Custom logic to check if the document provides context for the query
  7. return f"""
  8. {{% if context_str contains '{query_str}' %}}
  9. Answer the following question based on the context: {query_str}
  10. {{% else %}}
  11. No relevant context found.
  12. {{% endif %}}
  13. """
  14. # Use the custom prompt template in the Accumulate synthesizer
  15. custom_prompt_template = CustomPromptTemplate("{context_str}{query_str}", prompt_type="QUESTION_ANSWER")
  16. service_context = ServiceContext.from_defaults()
  17. accumulate_synthesizer = get_response_synthesizer(
  18. service_context=service_context,
  19. text_qa_template=custom_prompt_template,
  20. response_mode=ResponseMode.ACCUMULATE,
  21. )
  22. # Example usage
  23. query_str = "What is?"
  24. texts = [
  25. "This is bar",
  26. "This is foo",
  27. ]
  28. response = accumulate_synthesizer.get_response(text_chunks=texts, query_str=query_str)
  29. print(response)

此自定义提示模板使用条件语句检查 context_str 是否包含 query_str 。如果包含,则生成响应;否则,返回“未找到相关上下文”。这样,您可以跳过为不提供查询上下文的文档生成响应。

  • 要继续与 Dosu 交谈,请提及 @dosu。*
展开查看全部
ao218c7q

ao218c7q4#

如何调用update_prompts方法并将提示传递给Accumulate?

f0brbegy

f0brbegy5#

要调用 update_prompts 方法并在 Accumulate 响应合成器中传递自定义提示,请按照以下步骤操作:

  1. 定义自定义提示模板。
  2. 使用 update_prompts 方法更新 Accumulate 响应合成器中的提示。
    以下是一个示例:
  1. from llama_index.core.prompts.base import PromptTemplate
  2. from llama_index.core.response_synthesizers.accumulate import Accumulate
  3. # Define your custom prompt template
  4. custom_prompt_str = (
  5. "Context information is below.\n"
  6. "---------------------\n"
  7. "{context_str}\n"
  8. "---------------------\n"
  9. "Given the context information and not prior knowledge, "
  10. "answer the query in a detailed manner.\n"
  11. "Query: {query_str}\n"
  12. "Answer: "
  13. )
  14. custom_prompt = PromptTemplate(custom_prompt_str)
  15. # Update the prompt in the Accumulate response synthesizer
  16. accumulate_synthesizer = Accumulate()
  17. accumulate_synthesizer.update_prompts(
  18. {"text_qa_template": custom_prompt}
  19. )

此代码片段定义了一个自定义提示模板,并使用 update_prompts 方法 1(https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/prompts/prompt_mixin.ipynb)2(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/response_synthesizers/accumulate.py)3(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/response_synthesizers/accumulate.py) 更新了 Accumulate 响应合成器中的 text_qa_template

  • 要继续与 Dosu 对话,请提及 @dosu。*
展开查看全部

相关问题