Skip to content
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
**Official Python SDK for CZero Engine**
*Personal AI Interface: full local AI suite with document processing, semantic search, and RAG system with AI personas*

[Installation](#-installation) • [Quick Start](#-quick-start) • [Examples](#-examples) • [API Docs](#-api-reference) • [Contributing](CONTRIBUTING.md)
[Installation](#-installation) • [Quick Start](#-quick-start) • [Examples](#-examples) • [API Docs](https://docs.czero.cc) • [Contributing](CONTRIBUTING.md)

</div>

Expand Down
63 changes: 37 additions & 26 deletions examples/02_rag_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ async def rag_example():
print("\n🚀 RAG System Example")
print("=" * 50)

workspace_id = None

# Step 1: Create knowledge base from documents
print("\n1. Creating Knowledge Base")
print("-" * 30)
Expand Down Expand Up @@ -70,49 +72,55 @@ async def rag_example():
chunk_overlap=50
)

workspace_id = result['workspace']['id']
print(f"✅ Created workspace: {result['workspace']['name']}")
print(f" ID: {result['workspace']['id']}")
print(f" ID: {workspace_id}")
print(f" Processed {result['files_processed']} files")
print(f" Created {result['chunks_created']} chunks")

# Important: Give the system time to index the documents
print("\n⏳ Waiting for indexing to complete...")
await asyncio.sleep(5) # Wait 5 seconds for indexing

# Step 2: Demonstrate hierarchical search
print("\n2. Hierarchical Semantic Search")
# Step 2: Verify documents are indexed by searching
print("\n2. Verifying Document Indexing")
print("-" * 30)
async with CZeroEngineClient() as client:
# Search with hierarchy support
results = await client.semantic_search(
query="How does AI and machine learning work?",
# Search for content we just indexed, using workspace filter
test_results = await client.semantic_search(
query="artificial intelligence",
limit=3,
include_hierarchy=True,
hierarchy_level=None # Search all levels
similarity_threshold=0.3, # Low threshold to ensure we find something
workspace_filter=workspace_id # Search only in our workspace
)

print(f"Found {len(results.results)} results with hierarchy:")
for i, res in enumerate(results.results, 1):
print(f"\n {i}. Score: {res.similarity:.3f}")
print(f" {res.content[:100]}...")
if res.parent_chunk:
print(f" ↳ Has parent context")
if test_results.results:
print(f"✅ Found {len(test_results.results)} indexed documents")
for i, res in enumerate(test_results.results, 1):
print(f" {i}. Score: {res.similarity:.3f}")
print(f" {res.content[:100]}...")
else:
print("⚠️ Documents may still be indexing. Continuing with example...")

# Step 3: Use RAG for Q&A
print("\n3. RAG-Enhanced Q&A")
print("-" * 30)
print("Note: RAG searches across all workspaces, not just the one we created.")
async with RAGWorkflow() as rag_workflow:

# Ask questions with RAG
# Ask questions that match our indexed documents
questions = [
"What is CZero Engine and what are its main features?",
"What is artificial intelligence and machine learning?",
"How does semantic search work?",
"What's the difference between AI, machine learning, and deep learning?",
"Does CZero Engine support GPU acceleration?"
"What features does CZero Engine provide?"
]

for i, question in enumerate(questions, 1):
print(f"\n📝 Q{i}: {question}")
response = await rag_workflow.ask(
question=question,
chunk_limit=3,
similarity_threshold=0.5
similarity_threshold=0.3 # Lower threshold to be more inclusive
)
print(f"💡 A{i}: {response.response[:250]}...")

Expand All @@ -124,7 +132,7 @@ async def rag_example():
# Step 4: Compare with and without RAG
print("\n4. RAG vs Non-RAG Comparison")
print("-" * 30)
comparison_q = "What document processing features does CZero Engine provide?"
comparison_q = "What is machine learning and how does it relate to AI?"

async with RAGWorkflow() as rag_workflow:
comparison = await rag_workflow.compare_with_without_rag(
Expand All @@ -133,28 +141,31 @@ async def rag_example():

print(f"\n🤔 Question: {comparison_q}")
print("\n❌ Without RAG (generic response):")
print(f" {comparison['without_rag'][:200]}...")
print(f" {comparison['without_rag'].response[:200]}...")
print("\n✅ With RAG (context-aware):")
print(f" {comparison['with_rag'][:200]}...")
print(f" {comparison['with_rag'].response[:200]}...")
print(f"\n📊 Statistics:")
print(f" Context chunks used: {comparison['chunks_used']}")
chunks_used = len(comparison['with_rag'].context_used) if comparison['with_rag'].context_used else 0
print(f" Context chunks used: {chunks_used}")
print(f" Improvement: More specific and accurate with RAG")

# Step 5: Find similar content
print("\n5. Similarity Search")
print("-" * 30)
async with CZeroEngineClient() as client:
# Get all chunks first
# Search in our workspace for semantic search content
search_res = await client.semantic_search(
query="semantic search",
limit=1
limit=1,
workspace_filter=workspace_id
)

if search_res.results:
chunk_id = search_res.results[0].chunk_id
similar = await client.similarity_search(
chunk_id=chunk_id,
limit=3
limit=3,
similarity_threshold=0.3 # Lower threshold
)

print(f"Content similar to chunk '{chunk_id[:20]}...':\n")
Expand Down
23 changes: 20 additions & 3 deletions src/czero_engine/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
SimilaritySearchRequest, RecommendationsRequest,
DocumentsResponse, DocumentMetadata,
EmbeddingRequest, EmbeddingResponse,
WorkspaceCreateRequest, WorkspaceResponse,
WorkspaceCreateRequest, WorkspaceResponse, WorkspaceListResponse, WorkspaceInfo,
ProcessFilesRequest, ProcessFilesResponse, ProcessingConfig,
PersonaListResponse, PersonaChatRequest, PersonaChatResponse,
HealthResponse,
Expand Down Expand Up @@ -99,7 +99,8 @@ async def chat(
temperature: float = 0.7,
similarity_threshold: float = 0.7,
chunk_limit: int = 5,
use_web_search: bool = False
use_web_search: bool = False,
workspace_filter: Optional[str] = None
) -> ChatResponse:
"""
Send a chat message to CZero Engine LLM with optional RAG.
Expand All @@ -117,6 +118,7 @@ async def chat(
similarity_threshold: Minimum similarity for RAG chunks
chunk_limit: Maximum number of context chunks to retrieve
use_web_search: Whether to enable web search (if available)
workspace_filter: Optional workspace ID to filter RAG context

Returns:
ChatResponse with generated text and optional context chunks
Expand All @@ -132,7 +134,8 @@ async def chat(
use_web_search=use_web_search,
system_prompt=system_prompt,
max_tokens=max_tokens,
temperature=temperature
temperature=temperature,
workspace_filter=workspace_filter
)

self._log(f"Sending chat request (RAG: {use_rag})...")
Expand Down Expand Up @@ -341,6 +344,20 @@ async def create_workspace(
response.raise_for_status()
return WorkspaceResponse(**response.json())

async def list_workspaces(self) -> WorkspaceListResponse:
"""
List all available workspaces.

Returns a list of workspaces with their IDs, names, paths, and status.

Returns:
WorkspaceListResponse containing list of WorkspaceInfo objects
"""
self._log("Listing workspaces...")
response = await self.client.get(f"{self.base_url}/api/workspaces")
response.raise_for_status()
return WorkspaceListResponse(**response.json())

async def process_files(
self,
workspace_id: str,
Expand Down
17 changes: 17 additions & 0 deletions src/czero_engine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ChatRequest(BaseModel):
system_prompt: Optional[str] = None
max_tokens: Optional[int] = 1024
temperature: Optional[float] = 0.7
workspace_filter: Optional[str] = None


class ContextChunk(BaseModel):
Expand Down Expand Up @@ -131,6 +132,22 @@ class WorkspaceResponse(BaseModel):
created_at: str


class WorkspaceInfo(BaseModel):
"""Information about a workspace."""
id: str
name: str
path: str
description: Optional[str] = None
status: str
created_at: str
updated_at: str


class WorkspaceListResponse(BaseModel):
"""Response model for /api/workspaces endpoint."""
workspaces: List[WorkspaceInfo]


class ProcessingConfig(BaseModel):
"""Configuration for file processing."""
chunk_size: Optional[int] = 1000
Expand Down
17 changes: 1 addition & 16 deletions src/czero_engine/workflows/persona_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class PersonaWorkflow:
Workflow for interacting with AI personas in CZero Engine.

Personas provide specialized interaction styles and expertise:
- Gestalt: Adaptive general assistant
- Sage: Research and analysis expert
- Pioneer: Innovation and creative solutions
- Gestalt: General AI assistant (default persona)

Each persona maintains conversation context for coherent dialogue.
"""
Expand Down Expand Up @@ -409,19 +407,6 @@ async def example_personas():
"Can you help me understand semantic search?"
)

# Switch to Sage persona
await workflow.select_persona("sage")
response = await workflow.chat(
"What are the philosophical implications of AI?"
)

# Multi-persona discussion
discussion = await workflow.multi_persona_discussion(
topic="The future of human-AI collaboration",
persona_ids=["gestalt-default", "sage", "pioneer"],
rounds=2
)

# Compare persona responses
comparison = await workflow.persona_comparison(
"How should we approach learning new technologies?"
Expand Down
Loading