From 9fa86f7d39d1fa5a5c93bd115da72aa41dd3cc11 Mon Sep 17 00:00:00 2001 From: arahangua Date: Mon, 11 Aug 2025 13:09:34 +0900 Subject: [PATCH 1/2] fixed: readme subsection title links --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d7596a..274b59c 100644 --- a/README.md +++ b/README.md @@ -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) From c4e5f2e16221881b1edf25a1c7bbccc55b605456 Mon Sep 17 00:00:00 2001 From: arahangua Date: Mon, 11 Aug 2025 18:34:42 +0900 Subject: [PATCH 2/2] fixed:remarks on non-existent personas and handling of chatresponse --- examples/02_rag_system.py | 63 +++++++++++-------- src/czero_engine/client.py | 23 ++++++- src/czero_engine/models.py | 17 +++++ .../workflows/persona_workflow.py | 17 +---- 4 files changed, 75 insertions(+), 45 deletions(-) diff --git a/examples/02_rag_system.py b/examples/02_rag_system.py index b359bc4..761aeff 100644 --- a/examples/02_rag_system.py +++ b/examples/02_rag_system.py @@ -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) @@ -70,41 +72,47 @@ 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): @@ -112,7 +120,7 @@ async def rag_example(): 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]}...") @@ -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( @@ -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") diff --git a/src/czero_engine/client.py b/src/czero_engine/client.py index 6017dcd..6599f62 100644 --- a/src/czero_engine/client.py +++ b/src/czero_engine/client.py @@ -14,7 +14,7 @@ SimilaritySearchRequest, RecommendationsRequest, DocumentsResponse, DocumentMetadata, EmbeddingRequest, EmbeddingResponse, - WorkspaceCreateRequest, WorkspaceResponse, + WorkspaceCreateRequest, WorkspaceResponse, WorkspaceListResponse, WorkspaceInfo, ProcessFilesRequest, ProcessFilesResponse, ProcessingConfig, PersonaListResponse, PersonaChatRequest, PersonaChatResponse, HealthResponse, @@ -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. @@ -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 @@ -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})...") @@ -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, diff --git a/src/czero_engine/models.py b/src/czero_engine/models.py index aba4025..9bd2167 100644 --- a/src/czero_engine/models.py +++ b/src/czero_engine/models.py @@ -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): @@ -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 diff --git a/src/czero_engine/workflows/persona_workflow.py b/src/czero_engine/workflows/persona_workflow.py index c257ce5..4518b17 100644 --- a/src/czero_engine/workflows/persona_workflow.py +++ b/src/czero_engine/workflows/persona_workflow.py @@ -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. """ @@ -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?"