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 3 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
52 changes: 52 additions & 0 deletions chatsky/messengers/http_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
import time
from typing import Optional

import dotenv
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

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

dotenv.load_dotenv()

HTTP_INTERFACE_PORT = int(os.getenv("HTTP_INTERFACE_PORT", 8020))
Ramimashkouk marked this conversation as resolved.
Show resolved Hide resolved


class HealthStatus(BaseModel):
status: str
uptime: Optional[float]


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

class Output(BaseModel):
Ramimashkouk marked this conversation as resolved.
Show resolved Hide resolved
user_id: str
response: Message

@app.post("/chat", response_model=Output)
async def respond(
user_id: str,
user_message: Message,
):
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)
async def health_check():
return {
"status": "ok",
"uptime": str(time.time() - self.start_time),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to define these (Output, respond, health_check) outside of the connect method and add to the app via

app.post("/chat", response_model=Output)(respond)
app.post("/health", response_model=HealthStatus)(respond)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this's possible, particularly for the respond method as it needs the pipeline_runner unserializable variable. I.e. it's not possible to pass pipeline_runner as a dependency to the endpoint


self.start_time = time.time()
uvicorn.run(
app,
host="0.0.0.0",
port=HTTP_INTERFACE_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