diff --git a/README.md b/README.md index 4077be1..9020f61 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/serve_mcphost_openai_compatible.py b/serve_mcphost.py similarity index 69% rename from serve_mcphost_openai_compatible.py rename to serve_mcphost.py index 601f77e..bd5e7f5 100644 --- a/serve_mcphost_openai_compatible.py +++ b/serve_mcphost.py @@ -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) diff --git a/test.sh b/test.sh index 4bb6b7f..c4a9197 100755 --- a/test.sh +++ b/test.sh @@ -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 }'