forked from opea-project/GenAIComps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor llm predictionguard (opea-project#1143)
* refactor llm predictionguard Signed-off-by: Xinyao Wang <[email protected]> * refine predictionguard ut Signed-off-by: Xinyao Wang <[email protected]> * remove duplicated dockerfile path Signed-off-by: Xinyao Wang <[email protected]> * fix bug Signed-off-by: Xinyao Wang <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix bug Signed-off-by: Xinyao Wang <[email protected]> --------- Signed-off-by: Xinyao Wang <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: ZePan110 <[email protected]>
- Loading branch information
1 parent
89dd628
commit 4c21738
Showing
12 changed files
with
119 additions
and
164 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
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
101 changes: 101 additions & 0 deletions
101
comps/llms/src/text-generation/integrations/predictionguard.py
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,101 @@ | ||
# Copyright (C) 2024 Prediction Guard, Inc. | ||
# SPDX-License-Identified: Apache-2.0 | ||
|
||
import os | ||
import time | ||
|
||
from fastapi import HTTPException | ||
from fastapi.responses import StreamingResponse | ||
from predictionguard import PredictionGuard | ||
|
||
from comps import CustomLogger, OpeaComponent, OpeaComponentRegistry, ServiceType | ||
from comps.cores.proto.api_protocol import ChatCompletionRequest | ||
|
||
logger = CustomLogger("opea_textgen_predictionguard") | ||
logflag = os.getenv("LOGFLAG", False) | ||
|
||
|
||
@OpeaComponentRegistry.register("OPEATextGen_Predictionguard") | ||
class OPEATextGen_Predictionguard(OpeaComponent): | ||
"""A specialized OPEA TextGen component derived from OpeaComponent for interacting with Predictionguard services. | ||
Attributes: | ||
client (Predictionguard): An instance of the Predictionguard client for text generation. | ||
""" | ||
|
||
def __init__(self, name: str, description: str, config: dict = None): | ||
super().__init__(name, ServiceType.LLM.name.lower(), description, config) | ||
self.client = PredictionGuard() | ||
health_status = self.check_health() | ||
if not health_status: | ||
logger.error("OPEATextGen_Predictionguard health check failed.") | ||
else: | ||
logger.info("OPEATextGen_Predictionguard health check success.") | ||
|
||
def check_health(self) -> bool: | ||
"""Checks the health of the Predictionguard LLM service. | ||
Returns: | ||
bool: True if the service is reachable and healthy, False otherwise. | ||
""" | ||
|
||
try: | ||
response = self.client.models.list() | ||
return response is not None | ||
except Exception as e: | ||
logger.error(e) | ||
logger.error("Health check failed") | ||
return False | ||
|
||
async def invoke(self, input: ChatCompletionRequest): | ||
"""Invokes the Predictionguard LLM service to generate output for the provided input. | ||
Args: | ||
input (ChatCompletionRequest): The input text(s). | ||
""" | ||
if isinstance(input.messages, str): | ||
messages = [ | ||
{ | ||
"role": "system", | ||
"content": "You are a helpful assistant. Your goal is to provide accurate, detailed, and safe responses to the user's queries.", | ||
}, | ||
{"role": "user", "content": input.messages}, | ||
] | ||
else: | ||
messages = input.messages | ||
|
||
if input.stream: | ||
|
||
async def stream_generator(): | ||
chat_response = "" | ||
for res in self.client.chat.completions.create( | ||
model=input.model, | ||
messages=messages, | ||
max_tokens=input.max_tokens, | ||
temperature=input.temperature, | ||
top_p=input.top_p, | ||
top_k=input.top_k, | ||
stream=True, | ||
): | ||
if "choices" in res["data"] and "delta" in res["data"]["choices"][0]: | ||
delta_content = res["data"]["choices"][0]["delta"]["content"] | ||
chat_response += delta_content | ||
yield f"data: {delta_content}\n\n" | ||
else: | ||
yield "data: [DONE]\n\n" | ||
|
||
return StreamingResponse(stream_generator(), media_type="text/event-stream") | ||
else: | ||
try: | ||
response = self.client.chat.completions.create( | ||
model=input.model, | ||
messages=messages, | ||
max_tokens=input.max_tokens, | ||
temperature=input.temperature, | ||
top_p=input.top_p, | ||
top_k=input.top_k, | ||
) | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e)) | ||
|
||
return response |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
comps/llms/text-generation/predictionguard/docker_compose_llm.yaml
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
85 changes: 0 additions & 85 deletions
85
comps/llms/text-generation/predictionguard/llm_predictionguard.py
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
comps/llms/text-generation/predictionguard/requirements.txt
This file was deleted.
Oops, something went wrong.
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