Find answers from the community

Updated 3 months ago

qa_prompt = PromptTemplate(

qa_prompt = PromptTemplate(
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the context information and not prior knowledge, "
"answer the query.\n"
"Query: {query_str}\n"
"Answer: "
)


class RAGStringQueryEngine(CustomQueryEngine):
"""RAG String Query Engine."""

retriever: BaseRetriever
response_synthesizer: BaseSynthesizer
llm: OpenAI
qa_prompt: PromptTemplate

def custom_query(self, query_str: str):
nodes = self.retriever.retrieve(query_str)

context_str = "\n\n".join([n.node.get_content() for n in nodes])
response = self.llm.complete(
qa_prompt.format(context_str=context_str, query_str=query_str)
)

return str(response)


configure retriever

retriever = VectorIndexRetriever(
index=index,
similarity_top_k=2,
)

configure response synthesizer

response_synthesizer = get_response_synthesizer(
streaming=True,
response_mode="tree_summarize",
)
llm = OpenAI(model="gpt-3.5-turbo")

assemble query engine

query_engine = RAGStringQueryEngine(
retriever=retriever,
response_synthesizer=response_synthesizer,
llm=llm,
qa_prompt=qa_prompt,
)

query

start_time = time.perf_counter()
streaming_response = query_engine.query('''''')
elapsed_time = time.perf_counter() - start_time

print(f"{elapsed_time:0.3f}s")

Or iterate over the tokens as they arrive

for text in streaming_response.response_gen:
print(text, end="")

I am unable to stream response here
L
3 comments
You aren't using the response synthesizer in RAGStringQueryEngine , you are just calling llm.complete
Probably you should call llm.stream_complete and wrap that in the expected response object
Plain Text
from llama_index.response.schema import StreamingResponse

response = llm.stream_complete(...)

# source nodes and metadata optional
response_obj = StreamingResponse(response, source_nodes=[], metadata={})
return response_obj


or if you actually use the response synthesize you passed in

Plain Text
return self.response_synthesizer.syntheszie(query_str, nodes)
Add a reply
Sign up and join the conversation on Discord