-
Notifications
You must be signed in to change notification settings - Fork 11
Feat/mcp interface #260
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
Feat/mcp interface #260
Changes from all commits
605b9a1
a5865f6
09e5df7
acef338
8bc6091
7f36d20
50a47fd
dbae4ba
fdb2f66
e7e309f
7bc291c
56756c1
53f8ff5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,114 @@ | ||||||
| """FastAPI application for AutoIntent pipeline inference.""" | ||||||
|
|
||||||
| import logging | ||||||
| from collections.abc import AsyncGenerator | ||||||
| from contextlib import asynccontextmanager | ||||||
| from functools import lru_cache | ||||||
| from pathlib import Path | ||||||
|
|
||||||
| from fastapi import FastAPI, HTTPException | ||||||
| from pydantic import BaseModel, Field | ||||||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||||||
|
|
||||||
| from autointent import Pipeline | ||||||
| from autointent.custom_types import ListOfLabelsWithOOS | ||||||
|
|
||||||
|
|
||||||
| class Settings(BaseSettings): | ||||||
| """Application settings loaded from environment variables.""" | ||||||
|
|
||||||
| model_config = SettingsConfigDict(env_file=".env", env_prefix="AUTOINTENT_") | ||||||
| path: str = Field(..., description="Path to the optimized pipeline assets") | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| host: str = "127.0.0.1" | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ruff ругается на это |
||||||
| port: int = 8013 | ||||||
|
|
||||||
|
|
||||||
| class PredictRequest(BaseModel): | ||||||
| """Request model for the predict endpoint.""" | ||||||
|
|
||||||
| utterances: list[str] = Field(..., description="List of text utterances to classify") | ||||||
|
|
||||||
|
|
||||||
| class PredictResponse(BaseModel): | ||||||
| """Response model for the predict endpoint.""" | ||||||
|
|
||||||
| predictions: ListOfLabelsWithOOS = Field(..., description="List of predicted class labels") | ||||||
|
|
||||||
|
|
||||||
| settings = Settings() | ||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
|
|
||||||
| @lru_cache(maxsize=1) | ||||||
| def load_pipeline() -> Pipeline: | ||||||
| """Load the optimized pipeline from disk.""" | ||||||
| pipeline_path = Path(settings.path) | ||||||
| if not pipeline_path.exists(): | ||||||
| msg = f"Pipeline path does not exist: {pipeline_path}" | ||||||
| logger.error(msg) | ||||||
| raise HTTPException(status_code=404, detail=msg) | ||||||
|
|
||||||
| try: | ||||||
| msg = f"Loading pipeline from: {pipeline_path}" | ||||||
| logger.info(msg) | ||||||
| pipeline = Pipeline.load(pipeline_path) | ||||||
| logger.info("Pipeline loaded successfully") | ||||||
|
|
||||||
| except Exception: | ||||||
| logger.exception("Failed to load pipeline") | ||||||
| raise | ||||||
| else: | ||||||
| return pipeline | ||||||
|
|
||||||
|
|
||||||
| @asynccontextmanager | ||||||
| async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]: | ||||||
| """Load pipe.""" | ||||||
| load_pipeline() | ||||||
| yield | ||||||
|
|
||||||
|
|
||||||
| app = FastAPI( | ||||||
| title="AutoIntent Pipeline API", | ||||||
| description="API for serving AutoIntent predictions", | ||||||
| version="0.0.1", | ||||||
| lifespan=lifespan, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| @app.get("/health") | ||||||
| async def health_check() -> dict[str, str]: | ||||||
| """Health check endpoint.""" | ||||||
| return {"status": "healthy"} | ||||||
|
|
||||||
|
|
||||||
| @app.post("/predict") | ||||||
| async def predict(request: PredictRequest) -> PredictResponse: | ||||||
| """Predict class labels for the given utterances. | ||||||
|
|
||||||
| Args: | ||||||
| request: Request containing list of utterances to classify | ||||||
|
|
||||||
| Returns: | ||||||
| Response containing predicted class labels | ||||||
| """ | ||||||
| current_pipeline = load_pipeline() | ||||||
|
|
||||||
| if not request.utterances: | ||||||
| return PredictResponse(predictions=[]) | ||||||
|
|
||||||
| predictions = current_pipeline.predict(request.utterances) | ||||||
|
|
||||||
| return PredictResponse(predictions=predictions) | ||||||
|
|
||||||
|
|
||||||
| def main() -> None: | ||||||
| """Main entry point for the HTTP server.""" | ||||||
| import uvicorn | ||||||
|
|
||||||
| uvicorn.run( | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно потом добавить пример как запускать с
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а зачем это может быть полезно? я никогда так не запускал, не шарю
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Альтернатива такому запуску https://fastapi.tiangolo.com/deployment/manually/. Тут можно поменять на |
||||||
| "autointent.server.http:app", | ||||||
| host=settings.host, | ||||||
| port=settings.port, | ||||||
| reload=False, | ||||||
| ) | ||||||
Uh oh!
There was an error while loading. Please reload this page.