Skip to content

Commit

Permalink
Enhance the API documentation. (#54)
Browse files Browse the repository at this point in the history
* feat: add FeedbackResponse schema for api docs

* feat: add sse response for api docs

* chore: add sse DONE example
  • Loading branch information
gbaian10 authored Oct 11, 2024
1 parent 473848a commit 45171fe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
AgentResponse,
ChatMessage,
Feedback,
FeedbackResponse,
StreamInput,
UserInput,
convert_message_content_to_string,
Expand All @@ -13,5 +14,6 @@
"ChatMessage",
"StreamInput",
"Feedback",
"FeedbackResponse",
"convert_message_content_to_string",
]
4 changes: 4 additions & 0 deletions src/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,7 @@ class Feedback(BaseModel):
default={},
examples=[{"comment": "In-line human feedback"}],
)


class FeedbackResponse(BaseModel):
status: Literal["success"] = "success"
31 changes: 26 additions & 5 deletions src/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any
from uuid import uuid4

from fastapi import FastAPI, HTTPException, Request, Response
from fastapi import FastAPI, HTTPException, Request, Response, status
from fastapi.responses import StreamingResponse
from langchain_core._api import LangChainBetaWarning
from langchain_core.runnables import RunnableConfig
Expand All @@ -15,7 +15,14 @@
from langsmith import Client as LangsmithClient

from agent import research_assistant
from schema import ChatMessage, Feedback, StreamInput, UserInput, convert_message_content_to_string
from schema import (
ChatMessage,
Feedback,
FeedbackResponse,
StreamInput,
UserInput,
convert_message_content_to_string,
)

warnings.filterwarnings("ignore", category=LangChainBetaWarning)

Expand Down Expand Up @@ -142,7 +149,21 @@ async def message_generator(user_input: StreamInput) -> AsyncGenerator[str, None
yield "data: [DONE]\n\n"


@app.post("/stream")
def _sse_response_example() -> dict[str, Any]:
return {
status.HTTP_200_OK: {
"description": "Server Sent Event Response",
"content": {
"text/event-stream": {
"example": "data: {'type': 'token', 'content': 'Hello'}\n\ndata: {'type': 'token', 'content': ' World'}\n\ndata: [DONE]\n\n",
"schema": {"type": "string"},
}
},
}
}


@app.post("/stream", response_class=StreamingResponse, responses=_sse_response_example())
async def stream_agent(user_input: StreamInput) -> StreamingResponse:
"""
Stream the agent's response to a user input, including intermediate messages and tokens.
Expand All @@ -154,7 +175,7 @@ async def stream_agent(user_input: StreamInput) -> StreamingResponse:


@app.post("/feedback")
async def feedback(feedback: Feedback) -> dict[str, str]:
async def feedback(feedback: Feedback) -> FeedbackResponse:
"""
Record feedback for a run to LangSmith.
Expand All @@ -170,4 +191,4 @@ async def feedback(feedback: Feedback) -> dict[str, str]:
score=feedback.score,
**kwargs,
)
return {"status": "success"}
return FeedbackResponse()

0 comments on commit 45171fe

Please sign in to comment.