Spaces:
Runtime error
Runtime error
Download services/memory/api.py from greenman123/2323: direct link, hf CLI and curl.
- Browser
- Download file 8.21 kB
-
https://huggingface.co/spaces/greenman123/2323/resolve/main/services/memory/api.py
- Command line
-
hf download hf://spaces/greenman123/2323/services/memory/api.py
-
curl -L -o api.py https://huggingface.co/spaces/greenman123/2323/resolve/main/services/memory/api.py
8.21 kB
| """FastAPI routes for Memory Service.""" | |
| from typing import List, Optional, Dict, Any | |
| from fastapi import APIRouter, HTTPException, Request, Query, Body | |
| from pydantic import BaseModel, Field | |
| router = APIRouter() | |
| # Request/Response Models | |
| class FactCreate(BaseModel): | |
| """Request to create a fact.""" | |
| content: str | |
| source_chat_id: Optional[int] = None | |
| source_message_id: Optional[int] = None | |
| confidence: float = Field(default=1.0, ge=0.0, le=1.0) | |
| tags: Optional[List[str]] = None | |
| metadata: Optional[Dict[str, Any]] = None | |
| class FactResponse(BaseModel): | |
| """Fact response.""" | |
| id: str | |
| content: str | |
| source_chat_id: Optional[int] | |
| source_message_id: Optional[int] | |
| timestamp: str | |
| confidence: float | |
| tags: List[str] | |
| metadata: Dict[str, Any] | |
| similarity: Optional[float] = None | |
| class UserProfileCreate(BaseModel): | |
| """Request to create/update user profile.""" | |
| user_id: int | |
| username: Optional[str] = None | |
| first_name: Optional[str] = None | |
| last_name: Optional[str] = None | |
| facts: Optional[List[str]] = None | |
| metadata: Optional[Dict[str, Any]] = None | |
| class GoalCreate(BaseModel): | |
| """Request to create a goal.""" | |
| chat_id: int | |
| description: str | |
| success_metrics: Optional[Dict[str, Any]] = None | |
| class GoalUpdate(BaseModel): | |
| """Request to update a goal.""" | |
| progress: Optional[float] = Field(default=None, ge=0.0, le=1.0) | |
| status: Optional[str] = None | |
| increment_actions: bool = False | |
| success_metrics: Optional[Dict[str, Any]] = None | |
| # Fact endpoints | |
| async def create_fact(request: Request, fact: FactCreate): | |
| """Store a new fact.""" | |
| fact_store = request.app.state.fact_store | |
| try: | |
| fact_id = await fact_store.store_fact( | |
| content=fact.content, | |
| source_chat_id=fact.source_chat_id, | |
| source_message_id=fact.source_message_id, | |
| confidence=fact.confidence, | |
| tags=fact.tags, | |
| metadata=fact.metadata | |
| ) | |
| return {"id": fact_id} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def search_facts( | |
| request: Request, | |
| query: str = Query(..., min_length=1), | |
| limit: int = Query(default=5, ge=1, le=50), | |
| threshold: float = Query(default=0.7, ge=0.0, le=1.0), | |
| chat_id: Optional[int] = None | |
| ): | |
| """Search for facts using semantic search.""" | |
| fact_store = request.app.state.fact_store | |
| try: | |
| results = await fact_store.search_facts( | |
| query=query, | |
| limit=limit, | |
| threshold=threshold, | |
| chat_id=chat_id | |
| ) | |
| return results | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_fact(request: Request, fact_id: str): | |
| """Get a specific fact.""" | |
| fact_store = request.app.state.fact_store | |
| try: | |
| fact = await fact_store.get_fact(fact_id) | |
| if not fact: | |
| raise HTTPException(status_code=404, detail="Fact not found") | |
| return fact | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def delete_fact(request: Request, fact_id: str): | |
| """Delete a fact.""" | |
| fact_store = request.app.state.fact_store | |
| try: | |
| deleted = await fact_store.delete_fact(fact_id) | |
| if not deleted: | |
| raise HTTPException(status_code=404, detail="Fact not found") | |
| return {"status": "deleted"} | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # User profile endpoints | |
| async def create_or_update_user(request: Request, profile: UserProfileCreate): | |
| """Create or update user profile.""" | |
| user_manager = request.app.state.user_manager | |
| try: | |
| result = await user_manager.create_or_update_profile( | |
| user_id=profile.user_id, | |
| username=profile.username, | |
| first_name=profile.first_name, | |
| last_name=profile.last_name, | |
| facts=profile.facts, | |
| metadata=profile.metadata | |
| ) | |
| return result | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_user_profile(request: Request, user_id: int): | |
| """Get user profile.""" | |
| user_manager = request.app.state.user_manager | |
| try: | |
| profile = await user_manager.get_profile(user_id) | |
| if not profile: | |
| raise HTTPException(status_code=404, detail="User not found") | |
| return profile | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def search_users( | |
| request: Request, | |
| query: Optional[str] = None, | |
| limit: int = Query(default=50, ge=1, le=200) | |
| ): | |
| """Search user profiles.""" | |
| user_manager = request.app.state.user_manager | |
| try: | |
| profiles = await user_manager.search_profiles(query=query, limit=limit) | |
| return profiles | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Goal endpoints | |
| async def create_goal(request: Request, goal: GoalCreate): | |
| """Create a new conversation goal.""" | |
| goal_tracker = request.app.state.goal_tracker | |
| try: | |
| goal_id = await goal_tracker.create_goal( | |
| chat_id=goal.chat_id, | |
| description=goal.description, | |
| success_metrics=goal.success_metrics | |
| ) | |
| return {"id": goal_id} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_goals( | |
| request: Request, | |
| chat_id: Optional[int] = None, | |
| status: Optional[str] = None | |
| ): | |
| """Get goals, optionally filtered.""" | |
| goal_tracker = request.app.state.goal_tracker | |
| try: | |
| goals = await goal_tracker.get_goals(chat_id=chat_id, status=status) | |
| return goals | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def update_goal(request: Request, goal_id: str, update: GoalUpdate): | |
| """Update goal progress.""" | |
| goal_tracker = request.app.state.goal_tracker | |
| try: | |
| result = await goal_tracker.update_goal( | |
| goal_id=goal_id, | |
| progress=update.progress, | |
| status=update.status, | |
| increment_actions=update.increment_actions, | |
| success_metrics=update.success_metrics | |
| ) | |
| return result | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_goal_progress(request: Request, goal_id: str): | |
| """Get goal progress details.""" | |
| goal_tracker = request.app.state.goal_tracker | |
| try: | |
| progress = await goal_tracker.get_goal_progress(goal_id) | |
| if not progress: | |
| raise HTTPException(status_code=404, detail="Goal not found") | |
| return progress | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Configuration endpoints | |
| async def get_config(request: Request): | |
| """Get system configuration.""" | |
| # TODO: Implement config retrieval from database | |
| return {"status": "not_implemented"} | |
| async def update_config(request: Request, config: Dict[str, Any] = Body(...)): | |
| """Update system configuration.""" | |
| # TODO: Implement config update | |
| return {"status": "not_implemented"} | |
| async def get_status(request: Request): | |
| """Get service status.""" | |
| return { | |
| "status": "running", | |
| "vector_engine_loaded": request.app.state.vector_engine.model is not None | |
| } | |