Skip to content

Commit

Permalink
Modify return spec (#1205)
Browse files Browse the repository at this point in the history
* Feature/improve r2r telemetry (#1122)

* improve telemetry

* finish telemetry tweaks

* Feature/improve cli infra (#1123)

* improve telemetry

* finish telemetry tweaks

* up

* Feature/add serve fallback to main (#1125)

* improve telemetry

* finish telemetry tweaks

* up

* fallback to main

* Merge fragments (#1127)

* troubleshooting docs (#1128)

* troubleshooting docs (#1129)

* add system diagram (#1130)

* add system diagram

* rm multi

* fix overview

* cleanup and fix

* fix syntax

* change to fast strategy by default (#1133)

* Update parameter passing in js sdk (#1132)

* Docs changes + add entity and relationship types (#1134)

* up

* up

* up

* up

* reduce verbosity

* Feature/dev minor cleanups (#1135)

* cleanups

* bump pkg

* Update Tesseract OCR version in Dockerfile and change chunking strategy to "auto"

* Update chunking strategy to "auto" in r2r.toml

* up

* set response model

* add communities

---------

Co-authored-by: emrgnt-cmplxty <[email protected]>
Co-authored-by: Nolan Tremelling <[email protected]>
Co-authored-by: emrgnt-cmplxty <[email protected]>
  • Loading branch information
4 people authored Sep 19, 2024
1 parent aad4a8b commit 3077327
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 271 deletions.
2 changes: 0 additions & 2 deletions py/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@
"Prompt",
# Search abstractions
"AggregateSearchResult",
"KGLocalSearchResult",
"KGGlobalSearchResult",
"KGSearchResult",
"KGSearchSettings",
"VectorSearchResult",
Expand Down
2 changes: 0 additions & 2 deletions py/core/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
"Prompt",
# Search abstractions
"AggregateSearchResult",
"KGLocalSearchResult",
"KGGlobalSearchResult",
"KGSearchResult",
"KGSearchSettings",
"VectorSearchResult",
Expand Down
4 changes: 0 additions & 4 deletions py/core/base/abstractions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
from .restructure import KGCreationSettings, KGEnrichmentSettings
from .search import (
AggregateSearchResult,
KGGlobalSearchResult,
KGLocalSearchResult,
KGSearchResult,
KGSearchSettings,
VectorSearchResult,
Expand Down Expand Up @@ -82,8 +80,6 @@
"Prompt",
# Search abstractions
"AggregateSearchResult",
"KGLocalSearchResult",
"KGGlobalSearchResult",
"KGSearchResult",
"KGSearchSettings",
"VectorSearchResult",
Expand Down
160 changes: 37 additions & 123 deletions py/core/base/abstractions/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Abstractions for search functionality."""

from enum import Enum
from typing import Any, Optional
from typing import Any, Optional, Union
from uuid import UUID

from pydantic import BaseModel, Field
Expand Down Expand Up @@ -55,139 +55,53 @@ class Config:
}


class KGLocalSearchResult(BaseModel):
"""Result of a local knowledge graph search operation."""
class KGSearchResultType(str, Enum):
ENTITY = "entity"
RELATIONSHIP = "relationship"
COMMUNITY = "community"

query: str
entities: dict[str, Any]
relationships: dict[str, Any]
communities: dict[str, Any]
class KGSearchMethod(str, Enum):
LOCAL = "local"
GLOBAL = "global"

def __str__(self) -> str:
return f"KGLocalSearchResult(query={self.query}, entities={self.entities}, relationships={self.relationships}, communities={self.communities})"

def __repr__(self) -> str:
return self.__str__()

class Config:
json_schema_extra = {
"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": {
"title": "Aristotle and His Contributions",
"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.",
"rating": 9.5,
"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.",
"findings": [
{
"summary": "Aristotle's Foundational Role in Logic",
"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 books ...",
}
],
}
}
},
}
class KGEntityResult(BaseModel):
name: str
description: str
metadata: Optional[dict[str, Any]] = None

class KGRelationshipResult(BaseModel):
name: str
description: str
metadata: Optional[dict[str, Any]] = None

class KGGlobalSearchResult(BaseModel):
"""Result of a global knowledge graph search operation."""

query: str
search_result: list[str]

def __str__(self) -> str:
return f"KGGlobalSearchResult(query={self.query}, search_result={self.search_result})"

def __repr__(self) -> str:
return self.__str__()

def dict(self) -> dict:
return {"query": self.query, "search_result": self.search_result}

class Config:
json_schema_extra = {
"query": "What were Aristotles key contributions to philosophy?",
"search_result": [
"### Aristotle's Key Contributions to Philosophy\n\n"
"Aristotle's extensive body of work laid the foundation for numerous fields within philosophy and beyond, "
"significantly shaping the trajectory of Western thought. His systematic approach to data collection and "
"analysis has had a lasting impact on modern scientific methods. Below, we explore some of his most "
"influential contributions.\n\n"
"#### Foundational Works and Systematic Approach\n\n"
"Aristotle's writings cover a broad spectrum of topics, including logic, biology, ethics, and political science. "
"His key works such as 'Physics,' 'On the Soul,' and 'Nicomachean Ethics' delve into fundamental concepts "
"like substance, memory, and the nature of the city [Data: Reports (1, 2, 3, 4, 5, +more)]. These texts not "
"only provided a comprehensive framework for understanding various aspects of the natural and human world "
"but also established methodologies that continue to influence contemporary scientific inquiry.\n\n"
"#### Ethical and Political Philosophy\n\n"
"In 'Nicomachean Ethics,' Aristotle explores the concept of a virtuous character, emphasizing the importance "
"of moral virtues and the development of good habits. His work 'Politics' further examines the structure and "
"function of the city (polis), addressing issues related to property, trade, and governance. Aristotle's "
"classification of political constitutions and his definition of the city as the natural political community "
"have had a profound and enduring impact on political thought [Data: Reports (11, 12); Triples (21, 22, 23, 24, 25)].\n\n"
"#### Theories on Memory and Perception\n\n"
"Aristotle's theories on memory and perception are articulated in his works 'On the Soul' and 'De Anima iii 3.' "
"He defines memory as the retention of experiences shaped by sensation and discusses the faculty of imagination "
"(phantasia). These theories have significantly influenced subsequent philosophical and psychological studies "
"on cognition and perception [Data: Reports (13, 14); Triples (26, 27, 28, 29, 30)].\n\n"
"#### Epistemology and Scientific Method\n\n"
"Aristotle's epistemology, known as immanent realism, is based on the study of things that exist or happen in "
"the world. This approach emphasizes empirical observation and has been instrumental in shaping the development "
"of scientific methods. His insistence on grounding knowledge in observable phenomena laid the groundwork for "
"future empirical research [Data: Reports (3)].\n\n"
"#### Engagement with Predecessors and Contemporaries\n\n"
"Aristotle was also known for his critical engagement with the ideas of his predecessors and contemporaries. "
"For instance, he refuted Democritus's claim about the Milky Way and criticized Empedocles's materialist theory "
"of 'survival of the fittest.' These critiques highlight Aristotle's active participation in the broader "
"philosophical discourse of his time and his contributions to refining and advancing philosophical thought "
"[Data: Reports (15, 16); Triples (31, 32, 33, 34, 35)].\n\n"
"### Conclusion\n\n"
"Aristotle's contributions to philosophy are vast and multifaceted, encompassing ethics, politics, epistemology, "
"and more. His works continue to be studied and revered for their depth, rigor, and enduring relevance. Through "
"his systematic approach and critical engagement with existing ideas, Aristotle has left an indelible mark on "
"the landscape of Western philosophy."
],
}

class KGCommunityResult(BaseModel):
name: str
description: str
metadata: Optional[dict[str, Any]] = None

class KGGlobalResult(BaseModel):
name: str
description: str
metadata: Optional[dict[str, Any]] = None

class KGSearchResult(BaseModel):
"""Result of a knowledge graph search operation."""

local_result: Optional[KGLocalSearchResult] = None
global_result: Optional[KGGlobalSearchResult] = None

def __str__(self) -> str:
return f"KGSearchResult(local_result={self.local_result}, global_result={self.global_result})"

def __repr__(self) -> str:
return self.__str__()

def dict(self) -> dict:
return {
"local_result": (
self.local_result.dict() if self.local_result else None
),
"global_result": (
self.global_result.dict() if self.global_result else None
),
}
method: KGSearchMethod
content: Union[KGEntityResult, KGRelationshipResult, KGCommunityResult, KGGlobalResult]
result_type: Optional[KGSearchResultType] = None
fragment_ids: Optional[list[UUID]] = None
document_ids: Optional[list[UUID]] = None
metadata: Optional[dict[str, Any]] = None

class Config:
json_schema_extra = {
"local_result": KGLocalSearchResult.Config.json_schema_extra,
"global_result": KGGlobalSearchResult.Config.json_schema_extra,
"method": "local",
"content": "",
"result_type": "entity",
"fragment_ids": [],
"document_ids": [],
"metadata": {},
}


class AggregateSearchResult(BaseModel):
"""Result of an aggregate search operation."""

Expand Down
2 changes: 1 addition & 1 deletion py/core/base/providers/kg.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def structured_query(
param_map = {}

@abstractmethod
def vector_query(
async def vector_query(
self, query, **kwargs: Any
) -> Tuple[list[Entity], list[float]]:
"""Abstract method to query the graph store with a vector store query."""
Expand Down
Loading

0 comments on commit 3077327

Please sign in to comment.