Skip to content

Commit 1b654de

Browse files
Add Gateway for Translation (#169)
* add translation gateway Signed-off-by: zehao-intel <[email protected]> * fix import Signed-off-by: zehao-intel <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: zehao-intel <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5c07730 commit 1b654de

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

comps/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
from comps.cores.mega.orchestrator import ServiceOrchestrator
2626
from comps.cores.mega.orchestrator_with_yaml import ServiceOrchestratorWithYaml
2727
from comps.cores.mega.micro_service import MicroService, register_microservice, opea_microservices
28-
from comps.cores.mega.gateway import Gateway, ChatQnAGateway, CodeGenGateway, CodeTransGateway, DocSumGateway
28+
from comps.cores.mega.gateway import (
29+
Gateway,
30+
ChatQnAGateway,
31+
CodeGenGateway,
32+
CodeTransGateway,
33+
DocSumGateway,
34+
TranslationGateway,
35+
)
2936

3037
# Telemetry
3138
from comps.cores.telemetry.opea_telemetry import opea_telemetry

comps/cores/mega/gateway.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,51 @@ async def handle_request(self, request: Request):
230230
return ChatCompletionResponse(model="codetrans", choices=choices, usage=usage)
231231

232232

233+
class TranslationGateway(Gateway):
234+
def __init__(self, megaservice, host="0.0.0.0", port=8888):
235+
super().__init__(
236+
megaservice, host, port, str(MegaServiceEndpoint.TRANSLATION), ChatCompletionRequest, ChatCompletionResponse
237+
)
238+
239+
async def handle_request(self, request: Request):
240+
data = await request.json()
241+
language_from = data["language_from"]
242+
language_to = data["language_to"]
243+
source_language = data["source_language"]
244+
prompt_template = """
245+
Translate this from {language_from} to {language_to}:
246+
247+
{language_from}:
248+
{source_language}
249+
250+
{language_to}:
251+
"""
252+
prompt = prompt_template.format(
253+
language_from=language_from, language_to=language_to, source_language=source_language
254+
)
255+
result_dict = await self.megaservice.schedule(initial_inputs={"query": prompt})
256+
for node, response in result_dict.items():
257+
# Here it suppose the last microservice in the megaservice is LLM.
258+
if (
259+
isinstance(response, StreamingResponse)
260+
and node == list(self.megaservice.services.keys())[-1]
261+
and self.megaservice.services[node].service_type == ServiceType.LLM
262+
):
263+
return response
264+
last_node = self.megaservice.all_leaves()[-1]
265+
response = result_dict[last_node]["text"]
266+
choices = []
267+
usage = UsageInfo()
268+
choices.append(
269+
ChatCompletionResponseChoice(
270+
index=0,
271+
message=ChatMessage(role="assistant", content=response),
272+
finish_reason="stop",
273+
)
274+
)
275+
return ChatCompletionResponse(model="translation", choices=choices, usage=usage)
276+
277+
233278
class DocSumGateway(Gateway):
234279
def __init__(self, megaservice, host="0.0.0.0", port=8888):
235280
super().__init__(

0 commit comments

Comments
 (0)