Skip to content

Commit

Permalink
remove global search option
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyaspimpalgaonkar committed Oct 7, 2024
1 parent 282704b commit f76a8cb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 222 deletions.
78 changes: 16 additions & 62 deletions docs/cookbooks/graphrag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,8 @@ Now you can see that the graph is enriched with the following information. We ha

## Search

GraphRAG currently supports two types of searches: `local` and `global`.


### Local search

Local searches are faster and cheaper than global. A local knowledge graph search performs similarity search on the entity, relationship and community description embeddings.

```bash
r2r search --query="Who is Aristotle?" --use-kg-search --kg-search-type=local
r2r search --query="Who is Aristotle?" --use-kg-search
```

The answer will be returned in JSON format and contains results from entities, relationships and communities. Following is a snippet of the output:
Expand All @@ -316,62 +309,23 @@ Vector search results:
]

KG search results:
{
'local_result': {
'query': 'Who is Aristotle?',
'entities': {'0': {'name': 'Aristotle', 'description': 'Aristotle was an ancient Greek philosopher and polymath, recognized as the father of various fields including logic, biology, and political science. He authored significant works such as the *Nicomachean Ethics* and *Politics*, where he explored concepts of virtue, governance, and the nature of reality, while also critiquing Platos ideas. His teachings and observations laid the groundwork for numerous disciplines, influencing thinkers ...'}},
'relationships': {},
'communities': {'0': {'summary': '```json\n{\n "title": "Aristotle and His Contributions",\n "summary": "The community revolves around Aristotle, an ancient Greek philosopher and polymath, who made significant contributions to various fields including logic, biology, political science, and economics. His works, such as 'Politics' and 'Nicomachean Ethics', have influenced numerous disciplines and thinkers from antiquity through the Middle Ages and beyond. The relationships between his various works and the fields he contributed to highlight his profound impact on Western thought.",\n "rating": 9.5,\n "rating_explanation": "The impact severity rating is high due to Aristotle's foundational influence on multiple disciplines and his enduring legacy in Western philosophy and science.",\n "findings": [\n {\n "summary": "Aristotle's Foundational Role in Logic",\n "explanation": "Aristotle is credited with the earliest study of formal logic, and his conception of it was the dominant form of Western logic until the 19th-century advances in mathematical logic. His works compiled into a set of six bo ...}}}}
},
'global_result': None
}
[
{
'method': 'local',
'content':
{
'name': 'NICOMACHUS',
'description': "Nicomachus was the father of the renowned philosopher Aristotle, who passed away during Aristotle's childhood, thereby influencing his early education and career. He served as the personal physician to King Amyntas of Macedon, establishing a connection to the royal family. Additionally, Aristotle named his son Nicomachus in honor of his father, further emphasizing the familial legacy.",
'metadata': None
},
'result_type': 'entity',
'extraction_ids': ['ed3899a1-70fc-58eb-ab28-a0ee74568b8d', 'bc5e26ac-12a7-50c6-9ee8-1ce101d645c2', '3637b7aa-8373-5329-9750-1efc366fa72e', '9df4f6fb-887f-545f-9e3a-f753c122f5cc', '56ebe711-645a-5975-90da-2d6461947d51', 'f6f5cfb6-8654-5e1c-b574-849a8a313452', 'ff8accdb-791e-5b6d-a83a-5adc32c4222c', 'fad10545-6557-576c-89a7-c4fce8d9381c'],
'metadata': {'associated_query': 'Who is Aristotle?'}
}
]
Time taken: 2.39 seconds
```

### Global search

Global searches can be used for queries that require reasoning over the whole dataset. They provide more accurate results, however they use a large amount of queries and are expensive. We recommend checking the number of clusters created in the graph and setting the `max-llm-queries` to be a fraction (> 0.1) of that number.

```bash
r2r search --query="What were Aristotles key contributions to philosophy?" --use-kg-search --kg-search-type=global --max-llm-queries-for-global-search=100
```

Extracting the `global_result` from the payload that is returned above we find the following:

```markdown

### Aristotle's Key Contributions to Philosophy

Aristotle, an ancient Greek philosopher and polymath, made foundational contributions to numerous fields, including philosophy, logic, biology, and political science. His works have had a lasting impact on Western thought and the development of modern science.

#### Foundational Role in Various Fields

Aristotle is recognized as the father of several disciplines. His significant works, such as the *Nicomachean Ethics* and *Politics*, explore concepts of virtue, governance, and the nature of reality. These texts are foundational in ethical theory and political science, examining the best ways to achieve a virtuous and potentially happy character [Data: Reports (2, 51, 52, 53, 54, 55, +more)].

#### Contributions to Logic

Aristotle is credited with the earliest study of formal logic, which remained the dominant form of Western logic until the 19th century. His works, compiled into a set of six books called the *Organon*, are foundational to the field of logic [Data: Reports (21, 22, 23, 24, 25, +more)].

#### Influence on Medieval and Modern Thought

Aristotle's influence extended well beyond his lifetime, impacting medieval scholars and modern thinkers alike. His works were translated into Arabic and studied by Muslim philosophers, contributing to the intellectual heritage of the Islamic Golden Age. In the Middle Ages, Aristotle's ideas were integrated into Christian theology and philosophy, shaping the scholastic tradition [Data: Reports (5)].

#### Classification of the Rational Soul

According to Aristotle's classification, humans possess the rational soul, which is unique to them. This rational soul is responsible for higher-order thinking and reasoning, distinguishing humans from other beings that may only possess vegetative or sensitive souls. The rational soul enables humans to engage in philosophical thought, ethical reasoning, and scientific inquiry [Data: Reports (3)].

#### Exploration of Memory and Cognition

Aristotle's exploration of memory in relation to sensation and perception laid the groundwork for understanding human cognition and perception. His methodological approach to studying memory and other cognitive processes involved systematic observation and logical analysis, influencing the scientific method and the way researchers study human cognition and behavior [Data: Reports (1, 2, 3, 4, 5)].

#### Concept of Aether

Aristotle proposed Aether as a divine substance within the framework of classical elements. It is associated with circular motion in the heavens and is seen as encompassing all classical elements. This concept highlights its central role in ancient philosophical thought and its influence on the conceptualization of the universe [Data: Reports (4)].

#### Influence on Dante

Dante Alighieri, the renowned Italian poet, referred to Aristotle as "the master of those who know," highlighting the profound respect and influence Aristotle had on his work. Dante's philosophy is deeply rooted in Aristotelian thought, particularly in the context of his *Comedy* [Data: Reports (3)].

### Conclusion

Aristotle's contributions to philosophy are vast and multifaceted, spanning ethics, logic, political science, and cognitive science. His works have influenced a wide range of disciplines and continue to be studied and revered for their profound insights into human nature and the world.
Expand All @@ -382,7 +336,7 @@ Aristotle's contributions to philosophy are vast and multifaceted, spanning ethi
You can directly use these search results as part of your RAG pipeline.
```bash
r2r rag --query="What are the key contributions of Aristotle to modern society?" --use-kg-search --kg-search-type=global --max-llm-queries-for-global-search=100
r2r rag --query="What are the key contributions of Aristotle to modern society?" --use-kg-search
```

The output is as follows:
Expand Down
12 changes: 4 additions & 8 deletions docs/cookbooks/walkthrough.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,9 @@ r2r search --query="Who founded Airbnb?" --use-kg-search --kg-search-type=local
```python
client.search("Who founded Airbnb?", kg_search_settings={
"use_kg_search": True,
"kg_search_type": "global",
"kg_search_type": "local",
"kg_search_level": 0, # level of community to search
"max_community_description_length": 65536,
"max_llm_queries_for_global_search": 250,
"local_search_limits": {
"__Entity__": 20,
"__Relationship__": 20,
Expand All @@ -643,10 +642,9 @@ client.search("Who founded Airbnb?", kg_search_settings={
```javascript
await client.search("Who founded Airbnb?", true, {}, 10, false, {}, {
useKgSearch: true,
kgSearchType: "global",
kgSearchType: "local",
kgSearchLevel: "0",
maxCommunityDescriptionLength: 65536,
maxLlmQueriesForGlobalSearch: 250,
localSearchLimits: {
__Entity__: 20,
__Relationship__: 20,
Expand All @@ -664,10 +662,9 @@ curl -X POST http://localhost:7272/v2/search \
"query": "Who founded Airbnb?",
"kg_search_settings": {
"use_kg_search": true,
"kg_search_type": "global",
"kg_search_type": "local",
"kg_search_level": "0",
"max_community_description_length": 65536,
"max_llm_queries_for_global_search": 250,
"local_search_limits": {
"__Entity__": 20,
"__Relationship__": 20,
Expand All @@ -682,12 +679,11 @@ curl -X POST http://localhost:7272/v2/search \
Key configurable parameters for knowledge graph search include:

- `use_kg_search`: Enable knowledge graph search.
- `kg_search_type`: Choose between "global" or "local" search.
- `kg_search_type`: Choose between "local" search. "global" search is no longer supported.
- `kg_search_level`: Specify the level of community to search.
- `entity_types`: List of entity types to include in the search.
- `relationships`: List of relationship types to include in the search.
- `max_community_description_length`: Maximum length of community descriptions.
- `max_llm_queries_for_global_search`: Limit on the number of LLM queries for global search.
- `local_search_limits`: Set limits for different types of local searches.

Knowledge graph search provides structured information about entities and their relationships, complementing the text-based search results and offering a more comprehensive understanding of the data.
Expand Down
156 changes: 9 additions & 147 deletions py/core/pipes/retrieval/kg_search_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,7 @@ async def local_search(
extraction_ids=search_result["extraction_ids"],
metadata={"associated_query": message},
)

# relationship search
# disabled for now. We will check evaluations and see if we need it
# search_type = "__Relationship__"
# async for search_result in self.kg_provider.vector_query( # type: ignore
# input,
# search_type=search_type,
# search_type_limits=kg_search_settings.local_search_limits[
# search_type
# ],
# query_embedding=query_embedding,
# property_names=[
# "name",
# "description",
# "extraction_ids",
# "document_ids",
# ],
# ):
# yield KGSearchResult(
# content=KGRelationshipResult(
# name=search_result["name"],
# description=search_result["description"],
# ),
# method=KGSearchMethod.LOCAL,
# result_type=KGSearchResultType.RELATIONSHIP,
# # extraction_ids=search_result["extraction_ids"],
# # document_ids=search_result["document_ids"],
# metadata={"associated_query": message},
# )


# community search
search_type = "__Community__"
async for search_result in self.kg_provider.vector_query( # type: ignore
Expand Down Expand Up @@ -209,107 +180,6 @@ async def local_search(
},
)

async def global_search(
self,
input: GeneratorPipe.Input,
state: AsyncState,
run_id: UUID,
kg_search_settings: KGSearchSettings,
*args: Any,
**kwargs: Any,
) -> AsyncGenerator[KGSearchResult, None]:
# map reduce
async for message in input.message:
map_responses = []
communities = self.kg_provider.get_communities( # type: ignore
level=kg_search_settings.kg_search_level
)

if len(communities) == 0:
raise R2RException(
"No communities found. Please make sure you have run the KG enrichment step before running the search: r2r create-graph and r2r enrich-graph",
400,
)

async def preprocess_communities(communities):
merged_report = ""
for community in communities:
community_report = community.summary
if (
len(merged_report) + len(community_report)
> kg_search_settings.max_community_description_length
):
yield merged_report.strip()
merged_report = ""
merged_report += community_report + "\n\n"
if merged_report:
yield merged_report.strip()

async def process_community(merged_report):
output = await self.llm_provider.aget_completion(
messages=self.prompt_provider._get_message_payload(
task_prompt_name=self.kg_provider.config.kg_search_settings.graphrag_map_system_prompt,
task_inputs={
"context_data": merged_report,
"input": message,
},
),
generation_config=kg_search_settings.generation_config,
)

return output.choices[0].message.content

preprocessed_reports = [
merged_report
async for merged_report in preprocess_communities(communities)
]

# Use asyncio.gather to process all preprocessed community reports concurrently
logger.info(
f"Processing {len(communities)} communities, {len(preprocessed_reports)} reports, Max LLM queries = {kg_search_settings.max_llm_queries_for_global_search}"
)

map_responses = await asyncio.gather(
*[
process_community(report)
for report in preprocessed_reports[
: kg_search_settings.max_llm_queries_for_global_search
]
]
)
# Filter only the relevant responses
filtered_responses = self.filter_responses(map_responses)

# reducing the outputs
output = await self.llm_provider.aget_completion(
messages=self.prompt_provider._get_message_payload(
task_prompt_name=self.kg_provider.config.kg_search_settings.graphrag_reduce_system_prompt,
task_inputs={
"response_type": "multiple paragraphs",
"report_data": filtered_responses,
"input": message,
},
),
generation_config=kg_search_settings.generation_config,
)

output_text = output.choices[0].message.content

if not output_text:
logger.warning(f"No output generated for query: {message}.")
raise R2RException(
"No output generated for query.",
400,
)

yield KGSearchResult(
content=KGGlobalResult(
name="Global Result", description=output_text
),
method=KGSearchMethod.GLOBAL,
metadata={"associated_query": message},
)

async def _run_logic( # type: ignore
self,
input: GeneratorPipe.Input,
Expand All @@ -319,19 +189,11 @@ async def _run_logic( # type: ignore
*args: Any,
**kwargs: Any,
) -> AsyncGenerator[KGSearchResult, None]:
kg_search_type = kg_search_settings.kg_search_type

# runs local and/or global search
if kg_search_type == "local" or kg_search_type == "local_and_global":
logger.info("Performing KG local search")
async for result in self.local_search(
input, state, run_id, kg_search_settings
):
yield result

if kg_search_type == "global" or kg_search_type == "local_and_global":
logger.info("Performing KG global search")
async for result in self.global_search(
input, state, run_id, kg_search_settings
):
yield result

if kg_search_settings.kg_search_type == "global":
raise NotImplementedError("Global search is no longer supported")

async for result in self.local_search(
input, state, run_id, kg_search_settings
):
yield result
2 changes: 1 addition & 1 deletion py/shared/abstractions/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class KGSearchResultType(str, Enum):

class KGSearchMethod(str, Enum):
LOCAL = "local"
GLOBAL = "global"
# GLOBAL = "global" # no longer supported


class KGEntityResult(R2RSerializable):
Expand Down
6 changes: 2 additions & 4 deletions templates/ycombinator_graphrag/web-app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,8 @@ export interface SidebarProps {
searchFilters: string;
setSearchFilters: (filters: string) => void;
selectedModel: string;
kgSearchType: 'local' | 'global';
setKgSearchType: (type: 'local' | 'global') => void;
max_llm_queries_for_global_search: number;
setMax_llm_queries_for_global_search: (value: number) => void;
kgSearchType: 'local';
setKgSearchType: (type: 'local') => void;
top_k: number;
setTop_k: (value: number) => void;
max_tokens_to_sample: number;
Expand Down

0 comments on commit f76a8cb

Please sign in to comment.