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

Small bugfixes on prompts, return types #965

Merged
merged 1 commit into from
Aug 23, 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
Binary file added js/sdk/r2r-js-0.3.0.tgz
Binary file not shown.
58 changes: 10 additions & 48 deletions js/sdk/src/r2rClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ export class r2rClient {
rag_generation_config?: GenerationConfig | Record<string, any>,
task_prompt_override?: string,
include_title_if_available?: boolean,
): Promise<any> {
): Promise<any | AsyncGenerator<string, void, unknown>> {
this._ensureAuthenticated();

const json_data: Record<string, any> = {
Expand All @@ -1180,38 +1180,19 @@ export class r2rClient {
}

// TODO: can we remove this and pull this into rag?
@featureGenerator("streamingRag")
private async *streamRag(
@feature("streamingRag")
private async streamRag(
rag_data: Record<string, any>,
): AsyncGenerator<string, void, unknown> {
): Promise<ReadableStream<Uint8Array>> {
this._ensureAuthenticated();

const response = await this._makeRequest<Response>("POST", "rag", {
return this._makeRequest<ReadableStream<Uint8Array>>("POST", "rag", {
data: rag_data,
headers: {
"Content-Type": "application/json",
},
responseType: "stream",
});

if (!response.body) {
throw new Error("Response body is null");
}

const reader = response.body.getReader();
const decoder = new TextDecoder();

try {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
yield decoder.decode(value);
}
} finally {
reader.releaseLock();
}
}

/**
Expand All @@ -1232,7 +1213,7 @@ export class r2rClient {
rag_generation_config?: GenerationConfig | Record<string, any>,
task_prompt_override?: string,
include_title_if_available?: boolean,
): Promise<any> {
): Promise<any | AsyncGenerator<string, void, unknown>> {
this._ensureAuthenticated();

const json_data: Record<string, any> = {
Expand All @@ -1256,38 +1237,19 @@ export class r2rClient {
}

// TODO: can we remove this and pull this into agent?
@featureGenerator("streamingAgent")
private async *streamAgent(
@feature("streamingAgent")
private async streamAgent(
agent_data: Record<string, any>,
): AsyncGenerator<string, void, unknown> {
): Promise<ReadableStream<Uint8Array>> {
this._ensureAuthenticated();

const response = await this._makeRequest<Response>("POST", "agent", {
return this._makeRequest<ReadableStream<Uint8Array>>("POST", "agent", {
data: agent_data,
headers: {
"Content-Type": "application/json",
},
responseType: "stream",
});

if (!response.body) {
throw new Error("Response body is null");
}

const reader = response.body.getReader();
const decoder = new TextDecoder();

try {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
yield decoder.decode(value);
}
} finally {
reader.releaseLock();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion py/cli/commands/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def serve(
).replace(":", "")

if docker:

run_docker_serve(
client,
host,
Expand Down
2 changes: 1 addition & 1 deletion py/core/base/abstractions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
from .restructure import KGEnrichmentSettings
from .search import (
AggregateSearchResult,
KGLocalSearchResult,
KGGlobalSearchResult,
KGLocalSearchResult,
KGSearchResult,
KGSearchSettings,
VectorSearchResult,
Expand Down
22 changes: 14 additions & 8 deletions py/core/base/abstractions/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ class Config:
},
}


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

query: str
entities: dict[str, Any]
relationships: dict[str, Any]
Expand All @@ -70,6 +72,7 @@ def __repr__(self) -> str:

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

query: str
search_result: list[str]

Expand All @@ -80,26 +83,29 @@ def __repr__(self) -> str:
return self.__str__()

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


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
"local_result": (
self.local_result.dict() if self.local_result else None
),
"global_result": (
self.global_result.dict() if self.global_result else None
),
}


Expand Down
8 changes: 0 additions & 8 deletions py/core/main/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ def __init__(
async def aingest_documents(self, *args, **kwargs):
return await self.ingestion_service.ingest_documents(*args, **kwargs)

@syncable
async def aupdate_documents(self, *args, **kwargs):
return await self.ingestion_service.update_documents(*args, **kwargs)

@syncable
async def aingest_files(self, *args, **kwargs):
return await self.ingestion_service.ingest_files(*args, **kwargs)
Expand All @@ -109,10 +105,6 @@ async def arag(self, *args, **kwargs):
async def arag_agent(self, *args, **kwargs):
return await self.retrieval_service.agent(*args, **kwargs)

@syncable
async def aevaluate(self, *args, **kwargs):
return await self.retrieval_service.evaluate(*args, **kwargs)

@syncable
async def aupdate_prompt(self, *args, **kwargs):
return await self.management_service.update_prompt(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion py/core/main/services/ingestion_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ async def ingest_documents(
logger.error("All provided documents already exist.")
raise R2RException(
status_code=409,
message="All provided documents already exist. Use the `update_documents` endpoint instead to update these documents.",
message="All provided documents already exist. Use the `update_files` endpoint instead to update these documents.",
)

# Insert pending document info
Expand Down
12 changes: 12 additions & 0 deletions py/core/main/services/management_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ async def document_chunks(
document_id, offset=offset, limit=limit
)

@telemetry_event("UpdatePrompt")
async def update_prompt(
self,
name: str,
template: Optional[str] = None,
input_types: Optional[dict[str, str]] = None,
):
if input_types is None:
input_types = {}
self.providers.prompt.update_prompt(name, template, input_types)
return {"message": f"Prompt '{name}' updated successfully."}

@telemetry_event("UsersOverview")
async def users_overview(
self,
Expand Down
18 changes: 13 additions & 5 deletions py/core/pipes/retrieval/kg_search_search_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
KGSearchSettings,
PipeType,
PromptProvider,
R2RException,
RunLoggingSingleton,
R2RException
)
from core.base.abstractions.search import (
KGGlobalSearchResult,
Expand All @@ -25,6 +25,7 @@

logger = logging.getLogger(__name__)


class KGSearchSearchPipe(GeneratorPipe):
"""
Embeds and stores documents using a specified embedding model and database.
Expand Down Expand Up @@ -132,11 +133,18 @@ async def local_search(
)
all_search_results.append(search_result)


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

yield KGLocalSearchResult(query=message, entities=all_search_results[0], relationships=all_search_results[1], communities=all_search_results[2])
yield KGLocalSearchResult(
query=message,
entities=all_search_results[0],
relationships=all_search_results[1],
communities=all_search_results[2],
)

async def global_search(
self,
Expand Down
7 changes: 2 additions & 5 deletions py/core/providers/database/vecs/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,8 @@ def parse_condition(key, value):
else:
# Handle JSON-based filters
json_col = self.table.c.metadata
if not key.startswith("metadata."):
raise FilterError(
"metadata key must start with 'metadata.'"
)
key = key.split("metadata.")[1]
if key.startswith("metadata."):
key.split("metadata.")[1]
if isinstance(value, dict):
if len(value) > 1:
raise FilterError("only one operator permitted")
Expand Down
2 changes: 1 addition & 1 deletion py/core/providers/prompts/defaults/rag_agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rag_agent:
When asked a question, perform a search to find relevant information and provide a response.
The response should contain line-item attributions to relevent search results, and be as informative if possible.
The response should contain line-item attributions to relevant search results, and be as informative if possible.
If no relevant results are found, then state that no results were found.
Expand Down
30 changes: 17 additions & 13 deletions py/sdk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class Config:
},
}


class KGLocalSearchResult(BaseModel):
query: str
entities: list[dict[str, Any]]
Expand All @@ -195,15 +196,16 @@ class KGLocalSearchResult(BaseModel):

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

def dict(self) -> dict:
return {
"query": self.query,
"entities": self.entities,
"relationships": self.relationships,
"communities": self.communities
"communities": self.communities,
}


class KGGlobalSearchResult(BaseModel):
query: str
search_result: list[str]
Expand All @@ -213,12 +215,9 @@ def __str__(self) -> str:

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

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


class KGSearchResult(BaseModel):
Expand All @@ -230,11 +229,15 @@ def __str__(self) -> str:

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
"local_result": (
self.local_result.dict() if self.local_result else None
),
"global_result": (
self.global_result.dict() if self.global_result else None
),
}

class Config:
Expand All @@ -245,7 +248,7 @@ class Config:
"entities": {
"Paris": {
"name": "Paris",
"description": "Paris is the capital of France."
"description": "Paris is the capital of France.",
}
},
"relationships": {},
Expand All @@ -255,11 +258,12 @@ class Config:
"query": "What is the capital of France?",
"search_result": [
"Paris is the capital and most populous city of France."
]
}
],
},
}
}


class R2RException(Exception):
def __init__(
self, message: str, status_code: int, detail: Optional[Any] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
}
},
"reingest_sample_file": {
"results": "All provided documents already exist. Use the `update_documents` endpoint instead to update these documents."
"results": "All provided documents already exist. Use the `update_files` endpoint instead to update these documents."
},
"documents_overview": {
"results": [
Expand Down
Loading