Skip to content

feat: context grounding create index and ingest data #239

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

Merged
merged 1 commit into from
Apr 8, 2025
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "uipath"
version = "2.0.0.dev2"
version = "2.0.0.dev3"
description = "Python SDK and CLI for UiPath Platform, enabling programmatic interaction with automation services, process management, and deployment tools."
readme = { file = "README.md", content-type = "text/markdown" }
requires-python = ">=3.9"
Expand Down
2 changes: 2 additions & 0 deletions src/uipath/_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .assets import UserAsset
from .connections import Connection, ConnectionToken
from .context_grounding import ContextGroundingQueryResponse
from .exceptions import IngestionInProgressException
from .interrupt_models import CreateAction, InvokeProcess, WaitAction, WaitJob
from .job import Job
from .processes import Process
Expand Down Expand Up @@ -32,4 +33,5 @@
"WaitJob",
"WaitAction",
"CreateAction",
"IngestionInProgressException",
]
60 changes: 60 additions & 0 deletions src/uipath/_models/context_grounding_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from datetime import datetime
from typing import Any, List, Optional

from pydantic import BaseModel, ConfigDict, Field


class ContextGroundingField(BaseModel):
id: Optional[str] = Field(default=None, alias="id")
name: Optional[str] = Field(default=None, alias="name")
description: Optional[str] = Field(default=None, alias="description")
type: Optional[str] = Field(default=None, alias="type")
is_filterable: Optional[bool] = Field(default=None, alias="isFilterable")
searchable_type: Optional[str] = Field(default=None, alias="searchableType")
is_user_defined: Optional[bool] = Field(default=None, alias="isUserDefined")


class ContextGroundingDataSource(BaseModel):
model_config = ConfigDict(
validate_by_name=True,
validate_by_alias=True,
use_enum_values=True,
arbitrary_types_allowed=True,
extra="allow",
json_encoders={datetime: lambda v: v.isoformat() if v else None},
)
id: Optional[str] = Field(default=None, alias="id")
folder: Optional[str] = Field(default=None, alias="folder")


class ContextGroundingIndex(BaseModel):
model_config = ConfigDict(
validate_by_name=True,
validate_by_alias=True,
use_enum_values=True,
arbitrary_types_allowed=True,
extra="allow",
json_encoders={datetime: lambda v: v.isoformat() if v else None},
)
id: Optional[str] = Field(default=None, alias="id")
name: Optional[str] = Field(default=None, alias="name")
description: Optional[str] = Field(default=None, alias="description")
memory_usage: Optional[int] = Field(default=None, alias="memoryUsage")
disk_usage: Optional[int] = Field(default=None, alias="diskUsage")
data_source: Optional[ContextGroundingDataSource] = Field(
default=None, alias="dataSource"
)
pre_processing: Any = Field(default=None, alias="preProcessing")
fields: Optional[List[ContextGroundingField]] = Field(default=None, alias="fields")
last_ingestion_status: Optional[str] = Field(
default=None, alias="lastIngestionStatus"
)
last_ingested: Optional[datetime] = Field(default=None, alias="lastIngested")
last_queried: Optional[datetime] = Field(default=None, alias="lastQueried")
folder_key: Optional[str] = Field(default=None, alias="folderKey")

def in_progress_ingestion(self):
return (
self.last_ingestion_status == "Queued"
or self.last_ingestion_status == "In Progress"
)
6 changes: 6 additions & 0 deletions src/uipath/_models/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class IngestionInProgressException(Exception):
"""An exception that is triggered when a search is attempted on an index that is currently undergoing ingestion."""

def __init__(self, index_name):
self.message = f"index {index_name} cannot be searched during ingestion"
super().__init__(self.message)
2 changes: 2 additions & 0 deletions src/uipath/_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .buckets_service import BucketsService
from .connections_service import ConnectionsService
from .context_grounding_service import ContextGroundingService
from .folder_service import FolderService
from .jobs_service import JobsService
from .llm_gateway_service import UiPathLlmChatService, UiPathOpenAIService
from .processes_service import ProcessesService
Expand All @@ -21,4 +22,5 @@
"JobsService",
"UiPathOpenAIService",
"UiPathLlmChatService",
"FolderService",
]
73 changes: 68 additions & 5 deletions src/uipath/_services/buckets_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict
from typing import Any, Dict, Optional, Union

from httpx import request

Expand Down Expand Up @@ -59,7 +59,9 @@ def download(

def upload(
self,
bucket_key: str,
*,
bucket_key: Optional[str] = None,
bucket_name: Optional[str] = None,
blob_file_path: str,
content_type: str,
source_path: str,
Expand All @@ -68,11 +70,18 @@ def upload(

Args:
bucket_key: The key of the bucket
bucket_name: The name of the bucket
blob_file_path: The path where the file will be stored in the bucket
content_type: The MIME type of the file
source_path: The local path of the file to upload
"""
bucket = self.retrieve_by_key(bucket_key)
if bucket_key:
bucket = self.retrieve_by_key(bucket_key)
elif bucket_name:
bucket = self.retrieve(bucket_name)
else:
raise ValueError("Must specify a bucket name or bucket key")

bucket_id = bucket["Id"]

endpoint = Endpoint(
Expand All @@ -99,6 +108,60 @@ def upload(
else:
request("PUT", write_uri, headers=headers, files={"file": file})

def upload_from_memory(
self,
*,
bucket_key: Optional[str] = None,
bucket_name: Optional[str] = None,
blob_file_path: str,
content_type: str,
content: Union[str, bytes],
) -> None:
"""Upload content from memory to a bucket.

Args:
bucket_key: The key of the bucket
bucket_name: The name of the bucket
blob_file_path: The path where the content will be stored in the bucket
content_type: The MIME type of the content
content: The content to upload (string or bytes)
"""
if bucket_key:
bucket = self.retrieve_by_key(bucket_key)
elif bucket_name:
bucket = self.retrieve(bucket_name)
else:
raise ValueError("Must specify a bucket name or bucket key")

bucket_id = bucket["Id"]

endpoint = Endpoint(
f"/orchestrator_/odata/Buckets({bucket_id})/UiPath.Server.Configuration.OData.GetWriteUri"
)

result = self.request(
"GET",
endpoint,
params={"path": blob_file_path, "contentType": content_type},
).json()
write_uri = result["Uri"]

headers = {
key: value
for key, value in zip(
result["Headers"]["Keys"], result["Headers"]["Values"]
)
}

# Convert string to bytes if needed
if isinstance(content, str):
content = content.encode("utf-8")

if result["RequiresAuth"]:
self.request("PUT", write_uri, headers=headers, content=content)
else:
request("PUT", write_uri, headers=headers, content=content)

@infer_bindings()
def retrieve(self, name: str) -> Any:
"""Retrieve bucket information by its name.
Expand Down Expand Up @@ -192,14 +255,14 @@ def custom_headers(self) -> Dict[str, str]:
def _retrieve_spec(self, name: str) -> RequestSpec:
return RequestSpec(
method="GET",
endpoint=Endpoint("/odata/Buckets"),
endpoint=Endpoint("/orchestrator_/odata/Buckets"),
params={"$filter": f"Name eq '{name}'", "$top": 1},
)

def _retrieve_by_key_spec(self, key: str) -> RequestSpec:
return RequestSpec(
method="GET",
endpoint=Endpoint(
f"/odata/Buckets/UiPath.Server.Configuration.OData.GetByKey(identifier={key})"
f"/orchestrator_/odata/Buckets/UiPath.Server.Configuration.OData.GetByKey(identifier={key})"
),
)
Loading
Loading