Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPMessangerInterface #410

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chatsky/messengers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
)
from chatsky.messengers.telegram import LongpollingInterface as TelegramInterface
from chatsky.messengers.console import CLIMessengerInterface
from chatsky.messengers.http_interface import HTTPMessengerInterface
54 changes: 54 additions & 0 deletions chatsky/messengers/http_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import time

try:
import uvicorn
from fastapi import FastAPI

web_api_available = True
except ImportError as exc:
web_api_available = False
import_error = exc
from pydantic import BaseModel

from chatsky.core import Message
from chatsky.messengers.common import MessengerInterface


class HealthStatus(BaseModel):
status: str
uptime: float = None


class HTTPMessengerInterface(MessengerInterface):
class ChatskyResponse(BaseModel):
user_id: str
response: Message

def __init__(self, port: int):
if not web_api_available:
raise ImportError(
"Some packages are missing.\nTry to run `pip install chatsky[web-api]`."
) from import_error
super().__init__()
self.port = port
self.start_time = None

def health_check(self):
return {
"status": "ok",
"uptime": time.time() - self.start_time,
}

async def connect(self, pipeline_runner):
app = FastAPI()

@app.post("/chat", response_model=self.ChatskyResponse)
async def respond(user_id: str, user_message: str):
message = Message(text=user_message)
context = await pipeline_runner(message, user_id)
return {"user_id": user_id, "response": context.last_response}

app.get("/health", response_model=HealthStatus)(self.health_check)

self.start_time = time.time()
uvicorn.run(app, host="0.0.0.0", port=self.port)
45 changes: 2 additions & 43 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ opentelemetry-instrumentation = { version = "*", optional = true }
sqlalchemy = { version = "*", extras = ["asyncio"], optional = true }
opentelemetry-exporter-otlp = { version = ">=1.20.0", optional = true } # log body serialization is required
pyyaml = { version = "*", optional = true }
fastapi = { version = "*", optional = true }
uvicorn = { version = "*", optional = true }

[tool.poetry.extras]
json = ["aiofiles"]
Expand All @@ -89,6 +91,7 @@ telegram = ["python-telegram-bot"]
stats = ["opentelemetry-exporter-otlp", "opentelemetry-instrumentation", "requests", "tqdm", "omegaconf"]
benchmark = ["pympler", "humanize", "pandas", "altair", "tqdm"]
yaml = ["pyyaml"]
web-api = ["fastapi", "uvicorn"]


[tool.poetry.group.lint]
Expand Down
Loading