Skip to content

Commit 1249c4f

Browse files
Prediction Guard LVM component (#676)
* added files for PG lvm component Signed-off-by: sharanshirodkar7 <[email protected]> * Fix pre-commit issues: end-of-file, requirements.txt, trailing whitespace, imports, and formatting Signed-off-by: sharanshirodkar7 <[email protected]> * added package Signed-off-by: sharanshirodkar7 <[email protected]> --------- Signed-off-by: sharanshirodkar7 <[email protected]>
1 parent 8adbcce commit 1249c4f

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

.github/workflows/docker/compose/lvms-compose-cd.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ services:
1717
build:
1818
dockerfile: comps/lvms/llava/dependency/Dockerfile.intel_hpu
1919
image: ${REGISTRY:-opea}/llava-hpu:${TAG:-latest}
20+
lvm-predictionguard:
21+
build:
22+
dockerfile: comps/lvms/predictionguard/Dockerfile
23+
image: ${REGISTRY:-opea}/lvm-predictionguard:${TAG:-latest}

comps/lvms/predictionguard/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (C) 2024 Prediction Guard, Inc.
2+
# SPDX-License-Identified: Apache-2.0
3+
4+
FROM python:3.11-slim
5+
6+
# Set environment variables
7+
ENV LANG=en_US.UTF-8
8+
9+
COPY comps /home/comps
10+
11+
RUN pip install --no-cache-dir --upgrade pip && \
12+
pip install --no-cache-dir -r /home/comps/lvms/predictionguard/requirements.txt
13+
14+
ENV PYTHONPATH=$PYTHONPATH:/home
15+
16+
WORKDIR /home/comps/lvms/predictionguard
17+
18+
ENTRYPOINT ["python", "lvm.py"]

comps/lvms/predictionguard/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# LVM Prediction Guard Microservice
2+
3+
[Prediction Guard](https://docs.predictionguard.com) allows you to utilize hosted open access LLMs, LVMs, and embedding functionality with seamlessly integrated safeguards. In addition to providing a scalable access to open models, Prediction Guard allows you to configure factual consistency checks, toxicity filters, PII filters, and prompt injection blocking. Join the [Prediction Guard Discord channel](https://discord.gg/TFHgnhAFKd) and request an API key to get started.
4+
5+
Visual Question and Answering is one of the multimodal tasks empowered by LVMs (Large Visual Models). This microservice supports visual Q&A by using a LLaVA model available via the Prediction Guard API. It accepts two inputs: a prompt and an image. It outputs the answer to the prompt about the image.
6+
7+
# 🚀1. Start Microservice with Python
8+
9+
## 1.1 Install Requirements
10+
11+
```bash
12+
pip install -r requirements.txt
13+
```
14+
15+
## 1.2 Start LVM Service
16+
17+
```bash
18+
python lvm.py
19+
```
20+
21+
# 🚀2. Start Microservice with Docker (Option 2)
22+
23+
## 2.1 Setup Environment Variables
24+
25+
Setup the following environment variables first
26+
27+
```bash
28+
export PREDICTIONGUARD_API_KEY=${your_predictionguard_api_key}
29+
```
30+
31+
## 2.1 Build Docker Images
32+
33+
```bash
34+
cd ../../..
35+
docker build -t opea/lvm-predictionguard:latest -f comps/lvms/predictionguard/Dockerfile .
36+
```
37+
38+
## 2.2 Start Service
39+
40+
```bash
41+
docker run -d --name="lvm-predictionguard" -p 9399:9399 -e PREDICTIONGUARD_API_KEY=$PREDICTIONGUARD_API_KEY opea/lvm-predictionguard:latest
42+
```
43+
44+
# 🚀3. Consume LVM Service
45+
46+
```bash
47+
curl -X POST http://localhost:9399/v1/lvm \
48+
-H 'Content-Type: application/json' \
49+
-d '{
50+
"image": "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mP8/5+hnoEIwDiqkL4KAcT9GO0U4BxoAAAAAElFTkSuQmCC",
51+
"prompt": "What is this?",
52+
"max_new_tokens": 30
53+
}'
54+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (C) 2024 Prediction Guard, Inc.
2+
# SPDX-License-Identified: Apache-2.0

comps/lvms/predictionguard/lvm.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (C) 2024 Prediction Guard, Inc.
2+
# SPDX-License-Identified: Apache-2.0
3+
4+
import time
5+
6+
from docarray import BaseDoc
7+
from predictionguard import PredictionGuard
8+
9+
from comps import ServiceType, TextDoc, opea_microservices, register_microservice, register_statistics, statistics_dict
10+
11+
12+
class LVMDoc(BaseDoc):
13+
image: str
14+
prompt: str
15+
max_new_tokens: int = 100
16+
top_k: int = 50
17+
top_p: float = 0.99
18+
temperature: float = 1.0
19+
20+
21+
@register_microservice(
22+
name="opea_service@lvm_predictionguard",
23+
service_type=ServiceType.LVM,
24+
endpoint="/v1/lvm",
25+
host="0.0.0.0",
26+
port=9399,
27+
input_datatype=LVMDoc,
28+
output_datatype=TextDoc,
29+
)
30+
@register_statistics(names=["opea_service@lvm_predictionguard"])
31+
async def lvm(request: LVMDoc) -> TextDoc:
32+
start = time.time()
33+
34+
# make a request to the Prediction Guard API using the LlaVa model
35+
messages = [
36+
{
37+
"role": "user",
38+
"content": [
39+
{"type": "text", "text": request.prompt},
40+
{"type": "image_url", "image_url": {"url": request.image}},
41+
],
42+
},
43+
]
44+
result = client.chat.completions.create(
45+
model="llava-1.5-7b-hf",
46+
messages=messages,
47+
max_tokens=request.max_new_tokens,
48+
top_k=request.top_k,
49+
top_p=request.top_p,
50+
temperature=request.temperature,
51+
)
52+
53+
statistics_dict["opea_service@lvm_predictionguard"].append_latency(time.time() - start, None)
54+
55+
return TextDoc(text=result["choices"][0]["message"]["content"])
56+
57+
58+
if __name__ == "__main__":
59+
client = PredictionGuard()
60+
opea_microservices["opea_service@lvm_predictionguard"].start()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
aiohttp
2+
docarray
3+
fastapi
4+
httpx
5+
opentelemetry-api
6+
opentelemetry-exporter-otlp
7+
opentelemetry-sdk
8+
Pillow
9+
predictionguard
10+
prometheus-fastapi-instrumentator
11+
pyyaml
12+
requests
13+
shortuuid
14+
uvicorn
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
# Copyright (C) 2024 Prediction Guard, Inc.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
set -x # Print commands and their arguments as they are executed
6+
7+
WORKPATH=$(dirname "$PWD")
8+
ip_address=$(hostname -I | awk '{print $1}') # Adjust to a more reliable command
9+
if [ -z "$ip_address" ]; then
10+
ip_address="localhost" # Default to localhost if IP address is empty
11+
fi
12+
13+
function build_docker_images() {
14+
cd $WORKPATH
15+
echo $(pwd)
16+
docker build --no-cache -t opea/lvm-pg:comps -f comps/lvms/predictionguard/Dockerfile .
17+
if [ $? -ne 0 ]; then
18+
echo "opea/lvm-pg build failed"
19+
exit 1
20+
else
21+
echo "opea/lvm-pg built successfully"
22+
fi
23+
}
24+
25+
function start_service() {
26+
lvm_service_port=9399
27+
unset http_proxy
28+
docker run -d --name=test-comps-lvm-pg-server \
29+
-e http_proxy= -e https_proxy= \
30+
-e PREDICTIONGUARD_API_KEY=${PREDICTIONGUARD_API_KEY} \
31+
-p 9399:9399 --ipc=host opea/lvm-pg:comps
32+
sleep 60 # Sleep for 1 minute to allow the service to start
33+
}
34+
35+
function validate_microservice() {
36+
lvm_service_port=9399
37+
result=$(http_proxy="" curl http://${ip_address}:${lvm_service_port}/v1/lvm \
38+
-X POST \
39+
-d '{"image": "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mP8/5+hnoEIwDiqkL4KAcT9GO0U4BxoAAAAAElFTkSuQmCC", "prompt": "Describe the image.", "max_new_tokens": 100}' \
40+
-H 'Content-Type: application/json')
41+
42+
if [[ $result == *"text"* ]]; then
43+
echo "Service response is correct."
44+
else
45+
echo "Result wrong. Received was $result"
46+
docker logs test-comps-lvm-pg-server
47+
exit 1
48+
fi
49+
}
50+
51+
function stop_docker() {
52+
cid=$(docker ps -aq --filter "name=test-comps-lvm-pg-*")
53+
if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi
54+
}
55+
56+
function main() {
57+
stop_docker
58+
59+
build_docker_images
60+
start_service
61+
62+
validate_microservice
63+
64+
stop_docker
65+
echo y | docker system prune
66+
}
67+
68+
main

0 commit comments

Comments
 (0)