Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Context-Only Response Synthesizer #14439

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Several response synthesizers are implemented already in LlamaIndex:
summarization purposes, but may lose detail due to truncation.
- `no_text`: Only runs the retriever to fetch the nodes that would have been sent to the LLM,
without actually sending them. Then can be inspected by checking `response.source_nodes`.
- `context_only`: Returns a concatenated string of all text chunks.
- `accumulate`: Given a set of text chunks and the query, apply the query to each text
chunk while accumulating the responses into an array. Returns a concatenated string of all
responses. Good for when you need to run the same query separately against each text
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Any, Sequence

from llama_index.core.prompts.mixin import PromptDictType
from llama_index.core.response_synthesizers.base import BaseSynthesizer
from llama_index.core.types import RESPONSE_TEXT_TYPE


class ContextOnly(BaseSynthesizer):
def _get_prompts(self) -> PromptDictType:
"""Get prompts."""
return {}

def _update_prompts(self, prompts: PromptDictType) -> None:
"""Update prompts."""

def get_response(
self,
query_str: str,
text_chunks: Sequence[str],
**response_kwargs: Any,
) -> RESPONSE_TEXT_TYPE:
return "\n\n".join(text_chunks)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically there is a prompt template here, this joiner could maybe be customizable?


async def aget_response(
self,
query_str: str,
text_chunks: Sequence[str],
**response_kwargs: Any,
) -> RESPONSE_TEXT_TYPE:
return "\n\n".join(text_chunks)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from llama_index.core.response_synthesizers.compact_and_refine import (
CompactAndRefine,
)
from llama_index.core.response_synthesizers.context_only import ContextOnly
from llama_index.core.response_synthesizers.generation import Generation
from llama_index.core.response_synthesizers.no_text import NoText
from llama_index.core.response_synthesizers.refine import Refine
Expand Down Expand Up @@ -163,10 +164,15 @@ def get_response_synthesizer(
)
elif response_mode == ResponseMode.NO_TEXT:
return NoText(
llm=llm,
callback_manager=callback_manager,
streaming=streaming,
# deprecated
service_context=service_context,
)
elif response_mode == ResponseMode.CONTEXT_ONLY:
return ContextOnly(
callback_manager=callback_manager,
prompt_helper=prompt_helper,
streaming=streaming,
# deprecated
service_context=service_context,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class ResponseMode(str, Enum):
NO_TEXT = "no_text"
"""Return the retrieved context nodes, without synthesizing a final response."""

CONTEXT_ONLY = "context_only"
"""Returns a concatenated string of all text chunks."""

ACCUMULATE = "accumulate"
"""Synthesize a response for each text chunk, and then return the concatenation."""

Expand Down
Loading