Skip to content

Commit

Permalink
route decorator cleanup (#92)
Browse files Browse the repository at this point in the history
* Consolidate the /invoke and /stream endpoints

* Consolidate ainvoke
  • Loading branch information
JoshuaC215 authored Nov 17, 2024
1 parent 667d29f commit 66c1575
Showing 1 changed file with 15 additions and 38 deletions.
53 changes: 15 additions & 38 deletions src/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ def _parse_input(user_input: UserInput) -> tuple[dict[str, Any], str]:
return kwargs, run_id


async def ainvoke(user_input: UserInput, agent_id: str = DEFAULT_AGENT) -> ChatMessage:
@router.post("/{agent_id}/invoke")
@router.post("/invoke")
async def invoke(user_input: UserInput, agent_id: str = DEFAULT_AGENT) -> ChatMessage:
"""
Invoke an agent with user input to retrieve a final response.
If agent_id is not provided, the default agent will be used.
Use thread_id to persist and continue a multi-turn conversation. run_id kwarg
is also attached to messages for recording feedback.
"""
agent: CompiledStateGraph = agents[agent_id]
kwargs, run_id = _parse_input(user_input)
try:
Expand All @@ -90,28 +99,6 @@ async def ainvoke(user_input: UserInput, agent_id: str = DEFAULT_AGENT) -> ChatM
raise HTTPException(status_code=500, detail="Unexpected error")


@router.post("/invoke")
async def invoke(user_input: UserInput) -> ChatMessage:
"""
Invoke the default agent with user input to retrieve a final response.
Use thread_id to persist and continue a multi-turn conversation. run_id kwarg
is also attached to messages for recording feedback.
"""
return await ainvoke(user_input=user_input)


@router.post("/{agent_id}/invoke")
async def agent_invoke(user_input: UserInput, agent_id: str) -> ChatMessage:
"""
Invoke an agent with user input to retrieve a final response.
Use thread_id to persist and continue a multi-turn conversation. run_id kwarg
is also attached to messages for recording feedback.
"""
return await ainvoke(user_input=user_input, agent_id=agent_id)


async def message_generator(
user_input: StreamInput, agent_id: str = DEFAULT_AGENT
) -> AsyncGenerator[str, None]:
Expand Down Expand Up @@ -187,33 +174,23 @@ def _sse_response_example() -> dict[int, Any]:
}


@router.post("/stream", response_class=StreamingResponse, responses=_sse_response_example())
async def stream(user_input: StreamInput) -> StreamingResponse:
"""
Stream the default agent's response to a user input, including intermediate messages and tokens.
Use thread_id to persist and continue a multi-turn conversation. run_id kwarg
is also attached to all messages for recording feedback.
Set `stream_tokens=false` to return intermediate messages but not token-by-token.
"""
return StreamingResponse(message_generator(user_input), media_type="text/event-stream")


@router.post(
"/{agent_id}/stream", response_class=StreamingResponse, responses=_sse_response_example()
)
async def agent_stream(user_input: StreamInput, agent_id: str) -> StreamingResponse:
@router.post("/stream", response_class=StreamingResponse, responses=_sse_response_example())
async def stream(user_input: StreamInput, agent_id: str = DEFAULT_AGENT) -> StreamingResponse:
"""
Stream an agent's response to a user input, including intermediate messages and tokens.
If agent_id is not provided, the default agent will be used.
Use thread_id to persist and continue a multi-turn conversation. run_id kwarg
is also attached to all messages for recording feedback.
Set `stream_tokens=false` to return intermediate messages but not token-by-token.
"""
return StreamingResponse(
message_generator(user_input, agent_id=agent_id), media_type="text/event-stream"
message_generator(user_input, agent_id),
media_type="text/event-stream",
)


Expand Down

0 comments on commit 66c1575

Please sign in to comment.