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

Fix Doc-Sum stream output format #1219

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
27 changes: 22 additions & 5 deletions comps/llms/src/doc-summarization/integrations/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,32 @@ async def generate(self, input: DocSumChatCompletionRequest, client):

if input.stream:

async def stream_generator():
from langserve.serialization import WellKnownLCSerializer
import json

from langserve.serialization import WellKnownLCSerializer

_serializer = WellKnownLCSerializer()

def extract_llm_tokens(stream_output):
op_data = _serializer.dumps({"ops": stream_output.ops}).decode("utf-8")
parsed_data = json.loads(op_data)

tokens = []
for op in parsed_data.get("ops", []):
if op["op"] == "add" and "/streamed_output_str" in op["path"]:
tokens.append(op["value"])

_serializer = WellKnownLCSerializer()
return "".join(tokens)

async def stream_generator():
async for chunk in llm_chain.astream_log(docs):
data = _serializer.dumps({"ops": chunk.ops}).decode("utf-8")
data = extract_llm_tokens(chunk)

if logflag:
logger.info(data)
yield f"data: {data}\n\n"
if data != "":
yield f"data: {data}\n\n"

yield "data: [DONE]\n\n"

return StreamingResponse(stream_generator(), media_type="text/event-stream")
Expand Down
Loading