Skip to content

Commit

Permalink
[#145] Create health endpoint in assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Jan 2, 2025
1 parent 208eb5d commit e0d78fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions assistant/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import chromadb
import nltk
import threading

from time import sleep
from openai import OpenAI
Expand All @@ -15,6 +16,7 @@
from typing import Optional
from db import connect_to_sqlite, get_stable_prompt
from nltk.corpus import words, stopwords
from health_check_handler import run


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -418,4 +420,9 @@ async def consumer():
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()

# Start the HTTP server in a separate thread
server_thread = threading.Thread(target=run, kwargs={"port": 9001})
server_thread.start()

asyncio.run(consumer())
24 changes: 24 additions & 0 deletions assistant/health_check_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging
from http.server import BaseHTTPRequestHandler, HTTPServer


logger = logging.getLogger(__name__)


class HealthCheckHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/health":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(b'{"status": "ok"}')
else:
self.send_response(404)
self.end_headers()


def run(server_class=HTTPServer, handler_class=HealthCheckHandler, port=9001):
server_address = ("", port)
httpd = server_class(server_address, handler_class)
logger.info(f"Starting httpd server on port {port}")
httpd.serve_forever()

0 comments on commit e0d78fc

Please sign in to comment.