Spaces:
Sleeping
Sleeping
File size: 6,194 Bytes
f871fed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
"""
Chat service for API operations.
Provides async interface for chat functionality.
"""
import os
from typing import Any, Dict, List, Optional
import httpx
from loguru import logger
class ChatService:
"""Service for chat-related API operations"""
def __init__(self):
self.base_url = os.getenv("API_BASE_URL", "http://127.0.0.1:5055")
# Add authentication header if password is set
self.headers = {}
password = os.getenv("OPEN_NOTEBOOK_PASSWORD")
if password:
self.headers["Authorization"] = f"Bearer {password}"
async def get_sessions(self, notebook_id: str) -> List[Dict[str, Any]]:
"""Get all chat sessions for a notebook"""
try:
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/api/chat/sessions",
params={"notebook_id": notebook_id},
headers=self.headers
)
response.raise_for_status()
return response.json()
except Exception as e:
logger.error(f"Error fetching chat sessions: {str(e)}")
raise
async def create_session(
self,
notebook_id: str,
title: Optional[str] = None,
model_override: Optional[str] = None,
) -> Dict[str, Any]:
"""Create a new chat session"""
try:
data: Dict[str, Any] = {"notebook_id": notebook_id}
if title is not None:
data["title"] = title
if model_override is not None:
data["model_override"] = model_override
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/api/chat/sessions",
json=data,
headers=self.headers
)
response.raise_for_status()
return response.json()
except Exception as e:
logger.error(f"Error creating chat session: {str(e)}")
raise
async def get_session(self, session_id: str) -> Dict[str, Any]:
"""Get a specific session with messages"""
try:
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/api/chat/sessions/{session_id}",
headers=self.headers
)
response.raise_for_status()
return response.json()
except Exception as e:
logger.error(f"Error fetching session: {str(e)}")
raise
async def update_session(
self,
session_id: str,
title: Optional[str] = None,
model_override: Optional[str] = None,
) -> Dict[str, Any]:
"""Update session properties"""
try:
data: Dict[str, Any] = {}
if title is not None:
data["title"] = title
if model_override is not None:
data["model_override"] = model_override
if not data:
raise ValueError("At least one field must be provided to update a session")
async with httpx.AsyncClient() as client:
response = await client.put(
f"{self.base_url}/api/chat/sessions/{session_id}",
json=data,
headers=self.headers
)
response.raise_for_status()
return response.json()
except Exception as e:
logger.error(f"Error updating session: {str(e)}")
raise
async def delete_session(self, session_id: str) -> Dict[str, Any]:
"""Delete a chat session"""
try:
async with httpx.AsyncClient() as client:
response = await client.delete(
f"{self.base_url}/api/chat/sessions/{session_id}",
headers=self.headers
)
response.raise_for_status()
return response.json()
except Exception as e:
logger.error(f"Error deleting session: {str(e)}")
raise
async def execute_chat(
self,
session_id: str,
message: str,
context: Dict[str, Any],
model_override: Optional[str] = None,
) -> Dict[str, Any]:
"""Execute a chat request"""
try:
data = {
"session_id": session_id,
"message": message,
"context": context
}
if model_override is not None:
data["model_override"] = model_override
# Short connect timeout (10s), long read timeout (10 min) for Ollama/local LLMs
timeout = httpx.Timeout(connect=10.0, read=600.0, write=30.0, pool=10.0)
async with httpx.AsyncClient(timeout=timeout) as client:
response = await client.post(
f"{self.base_url}/api/chat/execute",
json=data,
headers=self.headers
)
response.raise_for_status()
return response.json()
except Exception as e:
logger.error(f"Error executing chat: {str(e)}")
raise
async def build_context(self, notebook_id: str, context_config: Dict[str, Any]) -> Dict[str, Any]:
"""Build context for a notebook"""
try:
data = {
"notebook_id": notebook_id,
"context_config": context_config
}
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/api/chat/context",
json=data,
headers=self.headers
)
response.raise_for_status()
return response.json()
except Exception as e:
logger.error(f"Error building context: {str(e)}")
raise
# Global instance
chat_service = ChatService()
|