add self restart endpoint , to clean chathistory

This commit is contained in:
TCUDIKEL
2025-05-11 19:35:54 +03:00
parent d2dfb36d6f
commit 2e1e95a2c0
3 changed files with 50 additions and 5 deletions

View File

@@ -33,7 +33,7 @@ Configure the application by setting the following environment variables or upda
Start the OpenAI-compatible API server:
```bash
python serve_mcphost_openai_compatible.py
python serve_mcphost.py
```
Or using uvicorn directly:

View File

@@ -1,3 +1,6 @@
import asyncio
import socket
import subprocess
import time
from contextlib import asynccontextmanager
from typing import List
@@ -109,18 +112,60 @@ async def chat_completions(request: ChatCompletionRequest):
)
# Optional: Add a root endpoint that redirects to documentation
@app.get("/")
async def root():
return {"message": "MCPHost OpenAI-compatible API server. Visit /docs for documentation."}
# Optional: Add a health check endpoint
@app.get("/health")
async def health_check():
return {"status": "healthy", "mcphost_alive": mcp_manager._is_alive()}
@app.post("/restart-service")
async def restart_service():
"""Restart the llm-api-mcphost service using supervisorctl"""
hostname = socket.gethostname()
if hostname == "afsar":
async def delayed_restart():
await asyncio.sleep(1) # Wait 1 second
try:
result = await asyncio.create_subprocess_exec(
"sudo", "supervisorctl", "restart", "llm-api-mcphost",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await result.communicate()
if result.returncode != 0:
logger.error(f"Failed to restart service: {stderr.decode()}")
else:
logger.info("Successfully restarted llm-api-mcphost service")
except Exception as e:
logger.error(f"Error restarting service: {str(e)}")
# Schedule the restart without waiting for it
asyncio.create_task(delayed_restart())
logger.info("Service restart scheduled")
return {
"status": "scheduled",
"message": "Service restart has been scheduled",
"hostname": hostname,
"note": "Service will restart in 1 second"
}
else:
logger.info(f"Restart request ignored - hostname is '{hostname}', not 'afsar'")
return {
"status": "ignored",
"message": "Service restart not performed - incorrect hostname",
"hostname": hostname,
"required_hostname": "afsar"
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host=settings.host, port=settings.port)

View File

@@ -16,10 +16,10 @@ curl -X 'POST' \
"messages": [
{
"role": "user",
"content": "sum all first 10 digits of pi? /no_think"
"content": "fetch hackernews headlines and provide me a summary of the top 10"
}
],
"temperature": 0.7,
"stream": false,
"max_tokens": 2058
"max_tokens": 4096
}'