Replies: 4 comments
-
🤖 Hello, Yes, it is possible to retrieve the top 2 nodes of information for each line of multi-line input (A, B, C), combine them, and then pass them all at once to the GPT for a faster response. This can be achieved by using a custom retriever that combines the VectorStoreIndex and the KnowledgeGraphIndex. Here is a sample code snippet that demonstrates how you can achieve this: from llama_index.retrievers import BaseRetriever, VectorIndexRetriever, KGTableRetriever
from typing import List
from llama_index import QueryBundle
from llama_index.schema import NodeWithScore
class CustomRetriever(BaseRetriever):
"""Custom retriever that performs both Vector search and Knowledge Graph search"""
def __init__(
self,
vector_retriever: VectorIndexRetriever,
kg_retriever: KGTableRetriever,
mode: str = "OR",
) -> None:
"""Init params."""
self._vector_retriever = vector_retriever
self._kg_retriever = kg_retriever
if mode not in ("AND", "OR"):
raise ValueError("Invalid mode.")
self._mode = mode
def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
"""Retrieve nodes given query."""
vector_nodes = self._vector_retriever.retrieve(query_bundle)
kg_nodes = self._kg_retriever.retrieve(query_bundle)
vector_ids = {n.node.node_id for n in vector_nodes}
kg_ids = {n.node.node_id for n in kg_nodes}
combined_dict = {n.node.node_id: n for n in vector_nodes}
combined_dict.update({n.node.node_id: n for n in kg_nodes})
if self._mode == "AND":
retrieve_ids = vector_ids.intersection(kg_ids)
else:
retrieve_ids = vector_ids.union(kg_ids)
retrieve_nodes = [combined_dict[rid] for rid in retrieve_ids]
return retrieve_nodes This custom retriever can be used to retrieve the top nodes for each line of multi-line input, combine them, and then pass them all at once to the GPT. This can potentially speed up the response time as it reduces the number of separate queries to the GPT. Please note that I am unable to view the image you attached in your question. If you have any further questions or need additional clarification, feel free to ask. Best, Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
I am not sure how to extract and enter multi line into QueryBundle, it seems like this is only saving one str line of tda |
Beta Was this translation helpful? Give feedback.
-
🤖 Hello, Your However, there are a few improvements that could be made to further optimize your code:
from concurrent.futures import ThreadPoolExecutor
class CustomRetriever(BaseRetriever):
"""Custom retriever that performs both Vector search and Knowledge Graph search"""
def __init__(
self,
vector_retriever: VectorIndexRetriever,
kg_retriever: KGTableRetriever,
mode: str = "OR",
) -> None:
"""Init params."""
self._vector_retriever = vector_retriever
self._kg_retriever = kg_retriever
if mode not in ("AND", "OR"):
raise ValueError("Invalid mode.")
self._mode = mode
def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
"""Retrieve nodes given query."""
with ThreadPoolExecutor(max_workers=2) as executor:
future_vector = executor.submit(self._vector_retriever.retrieve, query_bundle)
future_kg = executor.submit(self._kg_retriever.retrieve, query_bundle)
vector_nodes = future_vector.result()
kg_nodes = future_kg.result()
# rest of the code remains the same
Please note that these are just suggestions and whether they will actually improve performance depends on various factors such as the specific use case, the size of the data, the capabilities of the GPT, etc. Always benchmark your code before and after making these changes to ensure they are having the desired effect. As for your question about the average time taken by the I hope this helps! If you have any more questions, feel free to ask. Best, SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
@ricoyudog You would have to run the retriever for each line in your file Then, pass all the retrieved nodes to a response synthesizer
|
Beta Was this translation helpful? Give feedback.
-
Question Validation
Question
Thank you very much first:
My input is have multi-lines, like A , B , C.
I want each line can get the information from the knowledge base with Top 2 nodes. The combine A, B, C line as the knowledge pass to the GPT to give a answer.
This is due to if I call GPT each time could take 30 second, but it seems like GPT can handle A, B ,C line at the same time which can speed up the whole processes. If I combine A,B,C and pass to the retriever, I am afraid all top 6 similarity could all relevant to one line.
Beta Was this translation helpful? Give feedback.
All reactions