-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chat Streaming endpoint bare-minimum PoC
- Loading branch information
1 parent
6fdc215
commit bdfdec0
Showing
8 changed files
with
138 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import logging | ||
|
||
import aiohttp | ||
from django.apps import apps | ||
from django.http import StreamingHttpResponse | ||
from rest_framework.decorators import parser_classes | ||
from rest_framework.parsers import JSONParser | ||
from rest_framework.views import APIView | ||
|
||
from ansible_ai_connect.ai.api.model_pipelines.pipelines import ModelPipelineChatBot | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class StreamingChat(APIView): | ||
|
||
def __init__(self): | ||
self.llm = apps.get_app_config("ai").get_model_pipeline(ModelPipelineChatBot) | ||
|
||
async def call_chatservice(self, request): | ||
async with aiohttp.ClientSession(raise_for_status=True) as session: | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json,text/event-stream", | ||
} | ||
async with session.post( | ||
self.llm.config.inference_url + "/v1/streaming_query", | ||
json=request.data, | ||
headers=headers, | ||
) as r: | ||
async for chunk in r.content: | ||
logger.debug(chunk) | ||
yield chunk | ||
|
||
@parser_classes([JSONParser]) | ||
def post(self, request): | ||
return StreamingHttpResponse( | ||
self.call_chatservice(request), | ||
content_type="text/event-stream", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.