Skip to content

Commit

Permalink
update llm microservice output to binary (#123)
Browse files Browse the repository at this point in the history
Signed-off-by: letonghan <letong.han@intel.com>
  • Loading branch information
letonghan authored May 31, 2024
1 parent 4c06671 commit 3448b6f
Showing 1 changed file with 3 additions and 20 deletions.
23 changes: 3 additions & 20 deletions comps/llms/text-generation/tgi/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@
from comps import GeneratedDoc, LLMParamsDoc, ServiceType, opea_microservices, register_microservice


@traceable(run_type="tool")
def post_process_text(text: str):
if text == " ":
return "data: @#$\n\n"
if text == "\n":
return "data: <br/>\n\n"
if text.isspace():
return None
new_text = text.replace("Answer: ", "").replace("Human: ", "").replace(" ", "@#$")
return f"data: {new_text}\n\n"


@register_microservice(
name="opea_service@llm_tgi",
service_type=ServiceType.LLM,
Expand Down Expand Up @@ -61,14 +49,9 @@ async def stream_generator():
chat_response = ""
async for text in llm.astream(input.query):
chat_response += text
processed_text = post_process_text(text)
if text and processed_text:
if "</s>" in text:
res = text.split("</s>")[0]
if res != "":
yield res
break
yield processed_text
chunk_repr = repr(text.encode("utf-8"))
print(f"[llm - chat_stream] chunk:{chunk_repr}")
yield f"data: {chunk_repr}\n\n"
print(f"[llm - chat_stream] stream response: {chat_response}")
yield "data: [DONE]\n\n"

Expand Down

0 comments on commit 3448b6f

Please sign in to comment.