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

refactor(llm): enhance a string of graph query method #89

Merged
merged 15 commits into from
Oct 14, 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 @@ -91,7 +91,7 @@ def llm_settings(llm_type):
]
else:
llm_config_input = []
llm_config_button = gr.Button("apply configuration")
llm_config_button = gr.Button("Apply Configuration")

def apply_configuration(arg1, arg2, arg3, arg4):
llm_option = settings.llm_type
Expand Down Expand Up @@ -139,7 +139,7 @@ def embedding_settings(embedding_type):
]
else:
embedding_config_input = []
embedding_config_button = gr.Button("apply configuration")
embedding_config_button = gr.Button("Apply Configuration")

def apply_configuration(arg1, arg2, arg3):
embedding_option = settings.embedding_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def create_configs_block():
gr.Textbox(value=settings.graph_pwd, label="pwd", type="password"),
gr.Textbox(value=settings.graph_space, label="graphspace(Optional)"),
]
graph_config_button = gr.Button("Apply config")
graph_config_button = gr.Button("Apply Configuration")
graph_config_button.click(apply_graph_config, inputs=graph_config_input) # pylint: disable=no-member

with gr.Accordion("2. Set up the LLM.", open=False):
Expand Down Expand Up @@ -227,7 +227,7 @@ def llm_settings(llm_type):
]
else:
llm_config_input = []
llm_config_button = gr.Button("apply configuration")
llm_config_button = gr.Button("Apply Configuration")
llm_config_button.click(apply_llm_config, inputs=llm_config_input) # pylint: disable=no-member

with gr.Accordion("3. Set up the Embedding.", open=False):
Expand Down Expand Up @@ -262,7 +262,7 @@ def embedding_settings(embedding_type):
else:
embedding_config_input = []

embedding_config_button = gr.Button("apply configuration")
embedding_config_button = gr.Button("Apply Configuration")

# Call the separate apply_embedding_configuration function here
embedding_config_button.click( # pylint: disable=no-member
Expand Down Expand Up @@ -299,7 +299,7 @@ def reranker_settings(reranker_type):
]
else:
reranker_config_input = []
reranker_config_button = gr.Button("apply configuration")
reranker_config_button = gr.Button("Apply Configuration")

# TODO: use "gr.update()" or other way to update the config in time (refactor the click event)
# Call the separate apply_reranker_configuration function here
Expand Down
17 changes: 13 additions & 4 deletions hugegraph-llm/src/hugegraph_llm/indices/vector_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def from_index_file(dir_path: str) -> "VectorIndex":
if not os.path.exists(index_file) or not os.path.exists(properties_file):
log.warning("No index file found, create a new one.")
return VectorIndex()

faiss_index = faiss.read_index(index_file)
embed_dim = faiss_index.d
with open(properties_file, "rb") as f:
Expand All @@ -54,6 +55,7 @@ def from_index_file(dir_path: str) -> "VectorIndex":
def to_index_file(self, dir_path: str):
if not os.path.exists(dir_path):
os.makedirs(dir_path)

index_file = os.path.join(dir_path, INDEX_FILE_NAME)
properties_file = os.path.join(dir_path, PROPERTIES_FILE_NAME)
faiss.write_index(self.index, index_file)
Expand All @@ -63,6 +65,7 @@ def to_index_file(self, dir_path: str):
def add(self, vectors: List[List[float]], props: List[Any]):
if len(vectors) == 0:
return

if self.index.ntotal == 0 and len(vectors[0]) != self.index.d:
self.index = faiss.IndexFlatL2(len(vectors[0]))
self.index.add(np.array(vectors))
Expand All @@ -73,6 +76,7 @@ def remove(self, props: Union[Set[Any], List[Any]]) -> int:
props = set(props)
indices = []
remove_num = 0

for i, p in enumerate(self.properties):
if p in props:
indices.append(i)
Expand All @@ -81,15 +85,20 @@ def remove(self, props: Union[Set[Any], List[Any]]) -> int:
self.properties = [p for i, p in enumerate(self.properties) if i not in indices]
return remove_num

def search(self, query_vector: List[float], top_k: int) -> List[Dict[str, Any]]:
def search(self, query_vector: List[float], top_k: int, dis_threshold: float = 0.9) -> List[Dict[str, Any]]:
if self.index.ntotal == 0:
return []

if len(query_vector) != self.index.d:
raise ValueError("Query vector dimension does not match index dimension!")
_, indices = self.index.search(np.array([query_vector]), top_k)

distances, indices = self.index.search(np.array([query_vector]), top_k)
results = []
for i in indices[0]:
results.append(deepcopy(self.properties[i]))
for dist, i in zip(distances[0], indices[0]):
if dist < dis_threshold: # Smaller distances indicate higher similarity
results.append(deepcopy(self.properties[i]))
else:
log.debug("Distance %s is larger than threshold %s, ignore this result.", dist, dis_threshold)
return results

@staticmethod
Expand Down
Loading
Loading