diff --git a/comps/cores/proto/docarray.py b/comps/cores/proto/docarray.py index 490e7a9a8..8c71086f5 100644 --- a/comps/cores/proto/docarray.py +++ b/comps/cores/proto/docarray.py @@ -17,7 +17,7 @@ class TopologyInfo: class TextDoc(BaseDoc, TopologyInfo): - text: str = None + text: Union[str, List[str]] = None class Audio2text(BaseDoc, TopologyInfo): @@ -93,15 +93,15 @@ class DocPath(BaseDoc): class EmbedDoc(BaseDoc): - text: str - embedding: conlist(float, min_length=0) + text: Union[str, List[str]] + embedding: Union[conlist(float, min_length=0), List[conlist(float, min_length=0)]] search_type: str = "similarity" k: int = 4 distance_threshold: Optional[float] = None fetch_k: int = 20 lambda_mult: float = 0.5 score_threshold: float = 0.2 - constraints: Optional[Union[Dict[str, Any], None]] = None + constraints: Optional[Union[Dict[str, Any], List[Dict[str, Any]], None]] = None class EmbedMultimodalDoc(EmbedDoc): diff --git a/comps/embeddings/mosec/langchain/README.md b/comps/embeddings/mosec/langchain/README.md index 88c888a97..2ea3f32bc 100644 --- a/comps/embeddings/mosec/langchain/README.md +++ b/comps/embeddings/mosec/langchain/README.md @@ -25,9 +25,34 @@ docker run -d --name="embedding-langchain-mosec-server" -e http_proxy=$http_prox ## run client test -``` -curl localhost:6000/v1/embeddings \ - -X POST \ - -d '{"text":"Hello, world!"}' \ - -H 'Content-Type: application/json' +Use our basic API. + +```bash +## query with single text +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"text":"Hello, world!"}' \ + -H 'Content-Type: application/json' + +## query with multiple texts +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"text":["Hello, world!","How are you?"]}' \ + -H 'Content-Type: application/json' +``` + +We are also compatible with [OpenAI API](https://platform.openai.com/docs/api-reference/embeddings). + +```bash +## Input single text +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":"Hello, world!"}' \ + -H 'Content-Type: application/json' + +## Input multiple texts with parameters +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":["Hello, world!","How are you?"], "dimensions":100}' \ + -H 'Content-Type: application/json' ``` diff --git a/comps/embeddings/mosec/langchain/embedding_mosec.py b/comps/embeddings/mosec/langchain/embedding_mosec.py index fde9e17af..38e92b5a7 100644 --- a/comps/embeddings/mosec/langchain/embedding_mosec.py +++ b/comps/embeddings/mosec/langchain/embedding_mosec.py @@ -4,7 +4,7 @@ import asyncio import os import time -from typing import List, Optional +from typing import List, Optional, Union from langchain_community.embeddings import OpenAIEmbeddings @@ -18,6 +18,12 @@ register_statistics, statistics_dict, ) +from comps.cores.proto.api_protocol import ( + ChatCompletionRequest, + EmbeddingRequest, + EmbeddingResponse, + EmbeddingResponseData, +) logger = CustomLogger("embedding_mosec") logflag = os.getenv("LOGFLAG", False) @@ -62,18 +68,43 @@ async def get_embedding(e: Optional[List[float]]) -> List[float]: output_datatype=EmbedDoc, ) @register_statistics(names=["opea_service@embedding_mosec"]) -async def embedding(input: TextDoc) -> EmbedDoc: +async def embedding( + input: Union[TextDoc, EmbeddingRequest, ChatCompletionRequest] +) -> Union[EmbedDoc, EmbeddingResponse, ChatCompletionRequest]: if logflag: logger.info(input) start = time.time() - embed_vector = await embeddings.aembed_query(input.text) - res = EmbedDoc(text=input.text, embedding=embed_vector) + if isinstance(input, TextDoc): + embed_vector = await get_embeddings(input.text) + embedding_res = embed_vector[0] if isinstance(input.text, str) else embed_vector + res = EmbedDoc(text=input.text, embedding=embedding_res) + else: + embed_vector = await get_embeddings(input.input) + if input.dimensions is not None: + embed_vector = [embed_vector[i][: input.dimensions] for i in range(len(embed_vector))] + + # for standard openai embedding format + res = EmbeddingResponse( + data=[EmbeddingResponseData(index=i, embedding=embed_vector[i]) for i in range(len(embed_vector))] + ) + + if isinstance(input, ChatCompletionRequest): + input.embedding = res + # keep + res = input + statistics_dict["opea_service@embedding_mosec"].append_latency(time.time() - start, None) if logflag: logger.info(res) return res +async def get_embeddings(text: Union[str, List[str]]) -> List[List[float]]: + texts = [text] if isinstance(text, str) else text + embed_vector = await embeddings.aembed_documents(texts) + return embed_vector + + if __name__ == "__main__": MOSEC_EMBEDDING_ENDPOINT = os.environ.get("MOSEC_EMBEDDING_ENDPOINT", "http://127.0.0.1:8080") os.environ["OPENAI_API_BASE"] = MOSEC_EMBEDDING_ENDPOINT diff --git a/comps/embeddings/multimodal_clip/README.md b/comps/embeddings/multimodal_clip/README.md index c7c74e33d..eb3651495 100644 --- a/comps/embeddings/multimodal_clip/README.md +++ b/comps/embeddings/multimodal_clip/README.md @@ -44,9 +44,34 @@ curl http://localhost:6000/v1/health_check\ ### 2.2 Consume Embedding Service +Use our basic API. + +```bash +## query with single text +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"text":"Hello, world!"}' \ + -H 'Content-Type: application/json' + +## query with multiple texts +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"text":["Hello, world!","How are you?"]}' \ + -H 'Content-Type: application/json' +``` + +We are also compatible with [OpenAI API](https://platform.openai.com/docs/api-reference/embeddings). + ```bash -curl http://localhost:6000/v1/embeddings \ - -X POST -d '{"text":"Sample text"}' \ - -H 'Content-Type: application/json' +## Input single text +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":"Hello, world!"}' \ + -H 'Content-Type: application/json' +## Input multiple texts with parameters +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":["Hello, world!","How are you?"], "dimensions":100}' \ + -H 'Content-Type: application/json' ``` diff --git a/comps/embeddings/multimodal_clip/embedding_multimodal.py b/comps/embeddings/multimodal_clip/embedding_multimodal.py index 334b2c029..888278e7a 100644 --- a/comps/embeddings/multimodal_clip/embedding_multimodal.py +++ b/comps/embeddings/multimodal_clip/embedding_multimodal.py @@ -2,12 +2,15 @@ # SPDX-License-Identifier: Apache-2.0 import datetime +import os import time +from typing import List, Optional, Union from dateparser.search import search_dates from embeddings_clip import vCLIP from comps import ( + CustomLogger, EmbedDoc, ServiceType, TextDoc, @@ -16,6 +19,15 @@ register_statistics, statistics_dict, ) +from comps.cores.proto.api_protocol import ( + ChatCompletionRequest, + EmbeddingRequest, + EmbeddingResponse, + EmbeddingResponseData, +) + +logger = CustomLogger("embedding_multimodal") +logflag = os.getenv("LOGFLAG", False) def filtler_dates(prompt): @@ -64,21 +76,49 @@ def filtler_dates(prompt): output_datatype=EmbedDoc, ) @register_statistics(names=["opea_service@embedding_multimodal"]) -def embedding(input: TextDoc) -> EmbedDoc: +async def embedding( + input: Union[TextDoc, EmbeddingRequest, ChatCompletionRequest] +) -> Union[EmbedDoc, EmbeddingResponse, ChatCompletionRequest]: + if logflag: + logger.info(input) start = time.time() if isinstance(input, TextDoc): - # Handle text input - embed_vector = embeddings.embed_query(input.text).tolist()[0] - res = EmbedDoc(text=input.text, embedding=embed_vector, constraints=filtler_dates(input.text)) - + embed_vector = await get_embeddings(input.text) + if isinstance(input.text, str): + embedding_res = embed_vector[0] + constraints_res = filtler_dates(input.text) + else: + embedding_res = embed_vector + constraints_res = [filtler_dates(input.text[i]) for i in range(len(input.text))] + res = EmbedDoc(text=input.text, embedding=embedding_res, constraints=constraints_res) else: - raise ValueError("Invalid input type") + embed_vector = await get_embeddings(input.input) + if input.dimensions is not None: + embed_vector = [embed_vector[i][: input.dimensions] for i in range(len(embed_vector))] + + # for standard openai embedding format + res = EmbeddingResponse( + data=[EmbeddingResponseData(index=i, embedding=embed_vector[i]) for i in range(len(embed_vector))] + ) + + if isinstance(input, ChatCompletionRequest): + input.embedding = res + # keep + res = input statistics_dict["opea_service@embedding_multimodal"].append_latency(time.time() - start, None) + if logflag: + logger.info(res) return res +async def get_embeddings(text: Union[str, List[str]]) -> List[List[float]]: + texts = [text] if isinstance(text, str) else text + embed_vector = embeddings.embed_query(texts).tolist() + return embed_vector + + if __name__ == "__main__": embeddings = vCLIP({"model_name": "openai/clip-vit-base-patch32", "num_frm": 4}) opea_microservices["opea_service@embedding_multimodal"].start() diff --git a/comps/embeddings/predictionguard/README.md b/comps/embeddings/predictionguard/README.md index 4dccd3354..bec54350c 100644 --- a/comps/embeddings/predictionguard/README.md +++ b/comps/embeddings/predictionguard/README.md @@ -31,9 +31,34 @@ docker run -d --name="embedding-predictionguard" -p 6000:6000 -e PREDICTIONGUARD ## 🚀 Consume Embeddings Service +Use our basic API. + ```bash -curl localhost:6000/v1/embeddings \ - -X POST \ - -d '{"text":"Hello, world!"}' \ - -H 'Content-Type: application/json' +## query with single text +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"text":"Hello, world!"}' \ + -H 'Content-Type: application/json' + +## query with multiple texts +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"text":["Hello, world!","How are you?"]}' \ + -H 'Content-Type: application/json' +``` + +We are also compatible with [OpenAI API](https://platform.openai.com/docs/api-reference/embeddings). + +```bash +## Input single text +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":"Hello, world!"}' \ + -H 'Content-Type: application/json' + +## Input multiple texts with parameters +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":["Hello, world!","How are you?"], "dimensions":100}' \ + -H 'Content-Type: application/json' ``` diff --git a/comps/embeddings/predictionguard/embedding_predictionguard.py b/comps/embeddings/predictionguard/embedding_predictionguard.py index 4ea557f70..f5274e3b5 100644 --- a/comps/embeddings/predictionguard/embedding_predictionguard.py +++ b/comps/embeddings/predictionguard/embedding_predictionguard.py @@ -4,10 +4,12 @@ import os import time +from typing import List, Optional, Union from predictionguard import PredictionGuard from comps import ( + CustomLogger, EmbedDoc, ServiceType, TextDoc, @@ -16,6 +18,15 @@ register_statistics, statistics_dict, ) +from comps.cores.proto.api_protocol import ( + ChatCompletionRequest, + EmbeddingRequest, + EmbeddingResponse, + EmbeddingResponseData, +) + +logger = CustomLogger("embedding_predictionguard") +logflag = os.getenv("LOGFLAG", False) # Initialize Prediction Guard client client = PredictionGuard() @@ -31,16 +42,46 @@ output_datatype=EmbedDoc, ) @register_statistics(names=["opea_service@embedding_predictionguard"]) -def embedding(input: TextDoc) -> EmbedDoc: +async def embedding( + input: Union[TextDoc, EmbeddingRequest, ChatCompletionRequest] +) -> Union[EmbedDoc, EmbeddingResponse, ChatCompletionRequest]: + if logflag: + logger.info(input) start = time.time() - response = client.embeddings.create(model=pg_embedding_model_name, input=[{"text": input.text}]) - embed_vector = response["data"][0]["embedding"] - embed_vector = embed_vector[:512] # Keep only the first 512 elements - res = EmbedDoc(text=input.text, embedding=embed_vector) + + if isinstance(input, TextDoc): + embed_vector = await get_embeddings(input.text) + embedding_res = embed_vector[0] if isinstance(input.text, str) else embed_vector + res = EmbedDoc(text=input.text, embedding=embedding_res) + else: + embed_vector = await get_embeddings(input.input) + input.dimensions = input.dimensions if input.dimensions is not None else 512 + embed_vector = [embed_vector[i][: input.dimensions] for i in range(len(embed_vector))] + + # for standard openai embedding format + res = EmbeddingResponse( + data=[EmbeddingResponseData(index=i, embedding=embed_vector[i]) for i in range(len(embed_vector))] + ) + + if isinstance(input, ChatCompletionRequest): + input.embedding = res + # keep + res = input + statistics_dict["opea_service@embedding_predictionguard"].append_latency(time.time() - start, None) + if logflag: + logger.info(res) return res +async def get_embeddings(text: Union[str, List[str]]) -> List[List[float]]: + texts = [text] if isinstance(text, str) else text + texts = [{"text": texts[i]} for i in range(len(texts))] + response = client.embeddings.create(model=pg_embedding_model_name, input=texts)["data"] + embed_vector = [response[i]["embedding"] for i in range(len(response))] + return embed_vector + + if __name__ == "__main__": pg_embedding_model_name = os.getenv("PG_EMBEDDING_MODEL_NAME", "bridgetower-large-itm-mlm-itc") print("Prediction Guard Embedding initialized.") diff --git a/comps/embeddings/tei/langchain/README.md b/comps/embeddings/tei/langchain/README.md index 05705c4bb..96163c915 100644 --- a/comps/embeddings/tei/langchain/README.md +++ b/comps/embeddings/tei/langchain/README.md @@ -113,9 +113,34 @@ curl http://localhost:6000/v1/health_check\ ### 3.2 Consume Embedding Service +Use our basic API. + ```bash +## query with single text curl http://localhost:6000/v1/embeddings\ -X POST \ -d '{"text":"Hello, world!"}' \ -H 'Content-Type: application/json' + +## query with multiple texts +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"text":["Hello, world!","How are you?"]}' \ + -H 'Content-Type: application/json' +``` + +We are also compatible with [OpenAI API](https://platform.openai.com/docs/api-reference/embeddings). + +```bash +## Input single text +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":"Hello, world!"}' \ + -H 'Content-Type: application/json' + +## Input multiple texts with parameters +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":["Hello, world!","How are you?"], "dimensions":100}' \ + -H 'Content-Type: application/json' ``` diff --git a/comps/embeddings/tei/langchain/embedding_tei.py b/comps/embeddings/tei/langchain/embedding_tei.py index 02596f467..20e61196d 100644 --- a/comps/embeddings/tei/langchain/embedding_tei.py +++ b/comps/embeddings/tei/langchain/embedding_tei.py @@ -57,19 +57,22 @@ async def embedding( logger.info(input) if isinstance(input, TextDoc): embed_vector = await aembed_query(input.text, async_client) - res = EmbedDoc(text=input.text, embedding=embed_vector) + embedding_res = embed_vector[0] if isinstance(input.text, str) else embed_vector + res = EmbedDoc(text=input.text, embedding=embedding_res) else: embed_vector = await aembed_query(input.input, async_client) if input.dimensions is not None: - embed_vector = embed_vector[: input.dimensions] + embed_vector = [embed_vector[i][: input.dimensions] for i in range(len(embed_vector))] + + # for standard openai embedding format + res = EmbeddingResponse( + data=[EmbeddingResponseData(index=i, embedding=embed_vector[i]) for i in range(len(embed_vector))] + ) if isinstance(input, ChatCompletionRequest): - input.embedding = embed_vector + input.embedding = res # keep res = input - if isinstance(input, EmbeddingRequest): - # for standard openai embedding format - res = EmbeddingResponse(data=[EmbeddingResponseData(index=0, embedding=embed_vector)]) statistics_dict["opea_service@embedding_tei_langchain"].append_latency(time.time() - start, None) if logflag: @@ -77,8 +80,11 @@ async def embedding( return res -async def aembed_query(text: str, async_client: AsyncInferenceClient, model_kwargs=None, task=None) -> List[float]: - response = (await aembed_documents([text], async_client, model_kwargs=model_kwargs, task=task))[0] +async def aembed_query( + text: Union[str, List[str]], async_client: AsyncInferenceClient, model_kwargs=None, task=None +) -> List[List[float]]: + texts = [text] if isinstance(text, str) else text + response = await aembed_documents(texts, async_client, model_kwargs=model_kwargs, task=task) return response diff --git a/comps/embeddings/tei/llama_index/README.md b/comps/embeddings/tei/llama_index/README.md index e466c47ea..dd1f5006c 100644 --- a/comps/embeddings/tei/llama_index/README.md +++ b/comps/embeddings/tei/llama_index/README.md @@ -113,9 +113,34 @@ curl http://localhost:6000/v1/health_check\ ### 3.2 Consume Embedding Service +Use our basic API. + ```bash +## query with single text curl http://localhost:6000/v1/embeddings\ -X POST \ -d '{"text":"Hello, world!"}' \ -H 'Content-Type: application/json' + +## query with multiple texts +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"text":["Hello, world!","How are you?"]}' \ + -H 'Content-Type: application/json' +``` + +We are also compatible with [OpenAI API](https://platform.openai.com/docs/api-reference/embeddings). + +```bash +## Input single text +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":"Hello, world!"}' \ + -H 'Content-Type: application/json' + +## Input multiple texts with parameters +curl http://localhost:6000/v1/embeddings\ + -X POST \ + -d '{"input":["Hello, world!","How are you?"], "dimensions":100}' \ + -H 'Content-Type: application/json' ``` diff --git a/comps/embeddings/tei/llama_index/embedding_tei.py b/comps/embeddings/tei/llama_index/embedding_tei.py index e96b75e75..a3ff25a70 100644 --- a/comps/embeddings/tei/llama_index/embedding_tei.py +++ b/comps/embeddings/tei/llama_index/embedding_tei.py @@ -2,10 +2,17 @@ # SPDX-License-Identifier: Apache-2.0 import os +from typing import List, Union from llama_index.embeddings.text_embeddings_inference import TextEmbeddingsInference from comps import CustomLogger, EmbedDoc, ServiceType, TextDoc, opea_microservices, register_microservice +from comps.cores.proto.api_protocol import ( + ChatCompletionRequest, + EmbeddingRequest, + EmbeddingResponse, + EmbeddingResponseData, +) logger = CustomLogger("embedding_tei_llamaindex") logflag = os.getenv("LOGFLAG", False) @@ -20,16 +27,41 @@ input_datatype=TextDoc, output_datatype=EmbedDoc, ) -async def embedding(input: TextDoc) -> EmbedDoc: +async def embedding( + input: Union[TextDoc, EmbeddingRequest, ChatCompletionRequest] +) -> Union[EmbedDoc, EmbeddingResponse, ChatCompletionRequest]: if logflag: logger.info(input) - embed_vector = await embeddings.aget_query_embedding(input.text) - res = EmbedDoc(text=input.text, embedding=embed_vector) + if isinstance(input, TextDoc): + embed_vector = await get_embeddings(input.text) + embedding_res = embed_vector[0] if isinstance(input.text, str) else embed_vector + res = EmbedDoc(text=input.text, embedding=embedding_res) + else: + embed_vector = await get_embeddings(input.input) + if input.dimensions is not None: + embed_vector = [embed_vector[i][: input.dimensions] for i in range(len(embed_vector))] + + # for standard openai embedding format + res = EmbeddingResponse( + data=[EmbeddingResponseData(index=i, embedding=embed_vector[i]) for i in range(len(embed_vector))] + ) + + if isinstance(input, ChatCompletionRequest): + input.embedding = res + # keep + res = input + if logflag: logger.info(res) return res +async def get_embeddings(text: Union[str, List[str]]) -> List[List[float]]: + texts = [text] if isinstance(text, str) else text + embed_vector = await embeddings._aget_text_embeddings(texts) + return embed_vector + + if __name__ == "__main__": tei_embedding_model_name = os.getenv("TEI_EMBEDDING_MODEL_NAME", "BAAI/bge-base-en-v1.5") tei_embedding_endpoint = os.getenv("TEI_EMBEDDING_ENDPOINT", "http://localhost:8090") diff --git a/tests/embeddings/test_embeddings_mosec_langchain.sh b/tests/embeddings/test_embeddings_mosec_langchain.sh index 4140e7011..0c7b1bc3c 100644 --- a/tests/embeddings/test_embeddings_mosec_langchain.sh +++ b/tests/embeddings/test_embeddings_mosec_langchain.sh @@ -38,15 +38,16 @@ function start_service() { docker run -d --name="test-comps-embedding-langchain-mosec-endpoint" -p $mosec_endpoint:8000 opea/embedding-langchain-mosec-endpoint:comps export MOSEC_EMBEDDING_ENDPOINT="http://${ip_address}:${mosec_endpoint}" mosec_service_port=5002 - docker run -d --name="test-comps-embedding-langchain-mosec-server" -e http_proxy=$http_proxy -e https_proxy=$https_proxy -p ${mosec_service_port}:6000 --ipc=host -e MOSEC_EMBEDDING_ENDPOINT=$MOSEC_EMBEDDING_ENDPOINT opea/embedding-langchain-mosec:comps + docker run -d --name="test-comps-embedding-langchain-mosec-server" -e LOGFLAG=True -e http_proxy=$http_proxy -e https_proxy=$https_proxy -p ${mosec_service_port}:6000 --ipc=host -e MOSEC_EMBEDDING_ENDPOINT=$MOSEC_EMBEDDING_ENDPOINT opea/embedding-langchain-mosec:comps sleep 3m } -function validate_microservice() { +function validate_service() { + local INPUT_DATA="$1" mosec_service_port=5002 http_proxy="" curl http://${ip_address}:$mosec_service_port/v1/embeddings \ -X POST \ - -d '{"text":"What is Deep Learning?"}' \ + -d "$INPUT_DATA" \ -H 'Content-Type: application/json' if [ $? -eq 0 ]; then echo "curl command executed successfully" @@ -58,6 +59,24 @@ function validate_microservice() { fi } +function validate_microservice() { + ## query with single text + validate_service \ + '{"text":"What is Deep Learning?"}' + + ## query with multiple texts + validate_service \ + '{"text":["What is Deep Learning?","How are you?"]}' + + ## Test OpenAI API, input single text + validate_service \ + '{"input":"What is Deep Learning?"}' + + ## Test OpenAI API, input multiple texts with parameters + validate_service \ + '{"input":["What is Deep Learning?","How are you?"], "dimensions":100}' +} + function stop_docker() { cid=$(docker ps -aq --filter "name=test-comps-embedding-langchain-mosec-*") if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi diff --git a/tests/embeddings/test_embeddings_multimodal_clip.sh b/tests/embeddings/test_embeddings_multimodal_clip.sh index c3d0bccde..770f2dc3d 100644 --- a/tests/embeddings/test_embeddings_multimodal_clip.sh +++ b/tests/embeddings/test_embeddings_multimodal_clip.sh @@ -20,15 +20,16 @@ function build_docker_images() { } function start_service() { - docker run -d --name="test-embedding-multimodal-server" -e http_proxy=$http_proxy -e https_proxy=$https_proxy -p 5038:6000 --ipc=host opea/embedding-multimodal:comps + docker run -d --name="test-embedding-multimodal-server" -e LOGFLAG=True -e http_proxy=$http_proxy -e https_proxy=$https_proxy -p 5038:6000 --ipc=host opea/embedding-multimodal:comps sleep 3m } -function validate_microservice() { +function validate_service() { + local INPUT_DATA="$1" service_port=5038 result=$(http_proxy="" curl http://${ip_address}:$service_port/v1/embeddings \ -X POST \ - -d '{"text":"how many cars are in this image?"}' \ + -d "$INPUT_DATA" \ -H 'Content-Type: application/json') if [[ $result == *"embedding"* ]]; then echo "Result correct." @@ -39,6 +40,24 @@ function validate_microservice() { fi } +function validate_microservice() { + ## query with single text + validate_service \ + '{"text":"What is Deep Learning?"}' + + ## query with multiple texts + validate_service \ + '{"text":["What is Deep Learning?","How are you?"]}' + + ## Test OpenAI API, input single text + validate_service \ + '{"input":"What is Deep Learning?"}' + + ## Test OpenAI API, input multiple texts with parameters + validate_service \ + '{"input":["What is Deep Learning?","How are you?"], "dimensions":100}' +} + function stop_docker() { cid=$(docker ps -aq --filter "name=test-embedding-multimodal-server-*") if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi diff --git a/tests/embeddings/test_embeddings_predictionguard.sh b/tests/embeddings/test_embeddings_predictionguard.sh index 567b4fc0b..a6727b6bf 100644 --- a/tests/embeddings/test_embeddings_predictionguard.sh +++ b/tests/embeddings/test_embeddings_predictionguard.sh @@ -26,17 +26,18 @@ function start_service() { tei_service_port=6000 unset http_proxy docker run -d --name=test-comps-embedding-pg-server \ - -e http_proxy= -e https_proxy= \ + -e LOGFLAG=True -e http_proxy= -e https_proxy= \ -e PREDICTIONGUARD_API_KEY=${PREDICTIONGUARD_API_KEY} \ -p 6000:6000 --ipc=host opea/embedding-pg:comps sleep 60 # Sleep for 1 minute to allow the service to start } -function validate_microservice() { +function validate_service() { + local INPUT_DATA="$1" tei_service_port=6000 result=$(http_proxy="" curl http://${ip_address}:${tei_service_port}/v1/embeddings \ -X POST \ - -d '{"text":"What is Deep Learning?"}' \ + -d "$INPUT_DATA" \ -H 'Content-Type: application/json') # Check for a proper response format @@ -53,6 +54,24 @@ function validate_microservice() { fi } +function validate_microservice() { + ## query with single text + validate_service \ + '{"text":"What is Deep Learning?"}' + + ## query with multiple texts + validate_service \ + '{"text":["What is Deep Learning?","How are you?"]}' + + ## Test OpenAI API, input single text + validate_service \ + '{"input":"What is Deep Learning?"}' + + ## Test OpenAI API, input multiple texts with parameters + validate_service \ + '{"input":["What is Deep Learning?","How are you?"], "dimensions":100}' +} + function stop_docker() { cid=$(docker ps -aq --filter "name=test-comps-embedding-pg-*") if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi diff --git a/tests/embeddings/test_embeddings_tei_langchain.sh b/tests/embeddings/test_embeddings_tei_langchain.sh index 8c369a116..df2642cf1 100644 --- a/tests/embeddings/test_embeddings_tei_langchain.sh +++ b/tests/embeddings/test_embeddings_tei_langchain.sh @@ -26,15 +26,16 @@ function start_service() { docker run -d --name="test-comps-embedding-tei-endpoint" -p $tei_endpoint:80 -v ./data:/data --pull always ghcr.io/huggingface/text-embeddings-inference:cpu-1.5 --model-id $model export TEI_EMBEDDING_ENDPOINT="http://${ip_address}:${tei_endpoint}" tei_service_port=5002 - docker run -d --name="test-comps-embedding-tei-server" -e http_proxy=$http_proxy -e https_proxy=$https_proxy -p ${tei_service_port}:6000 --ipc=host -e TEI_EMBEDDING_ENDPOINT=$TEI_EMBEDDING_ENDPOINT opea/embedding-tei:comps + docker run -d --name="test-comps-embedding-tei-server" -e LOGFLAG=True -e http_proxy=$http_proxy -e https_proxy=$https_proxy -p ${tei_service_port}:6000 --ipc=host -e TEI_EMBEDDING_ENDPOINT=$TEI_EMBEDDING_ENDPOINT opea/embedding-tei:comps sleep 3m } -function validate_microservice() { +function validate_service() { + local INPUT_DATA="$1" tei_service_port=5002 result=$(http_proxy="" curl http://${ip_address}:$tei_service_port/v1/embeddings \ -X POST \ - -d '{"text":"What is Deep Learning?"}' \ + -d "$INPUT_DATA" \ -H 'Content-Type: application/json') if [[ $result == *"embedding"* ]]; then echo "Result correct." @@ -46,6 +47,24 @@ function validate_microservice() { fi } +function validate_microservice() { + ## query with single text + validate_service \ + '{"text":"What is Deep Learning?"}' + + ## query with multiple texts + validate_service \ + '{"text":["What is Deep Learning?","How are you?"]}' + + ## Test OpenAI API, input single text + validate_service \ + '{"input":"What is Deep Learning?"}' + + ## Test OpenAI API, input multiple texts with parameters + validate_service \ + '{"input":["What is Deep Learning?","How are you?"], "dimensions":100}' +} + function validate_microservice_with_openai() { tei_service_port=5002 python3 ${WORKPATH}/tests/utils/validate_svc_with_openai.py $ip_address $tei_service_port "embedding" diff --git a/tests/embeddings/test_embeddings_tei_llama_index.sh b/tests/embeddings/test_embeddings_tei_llama_index.sh index 46bbd150c..e1d04ab5e 100644 --- a/tests/embeddings/test_embeddings_tei_llama_index.sh +++ b/tests/embeddings/test_embeddings_tei_llama_index.sh @@ -26,18 +26,20 @@ function start_service() { docker run -d --name="test-comps-embedding-tei-llama-index-endpoint" -p $tei_endpoint:80 -v ./data:/data -e http_proxy=$http_proxy -e https_proxy=$https_proxy --pull always ghcr.io/huggingface/text-embeddings-inference:cpu-1.5 --model-id $model export TEI_EMBEDDING_ENDPOINT="http://${ip_address}:${tei_endpoint}" tei_service_port=5034 - docker run -d --name="test-comps-embedding-tei-llama-index-server" -e http_proxy=$http_proxy -e https_proxy=$https_proxy -p ${tei_service_port}:6000 --ipc=host -e TEI_EMBEDDING_ENDPOINT=$TEI_EMBEDDING_ENDPOINT opea/embedding-tei-llama-index:comps + docker run -d --name="test-comps-embedding-tei-llama-index-server" -e LOGFLAG=True -e http_proxy=$http_proxy -e https_proxy=$https_proxy -p ${tei_service_port}:6000 --ipc=host -e TEI_EMBEDDING_ENDPOINT=$TEI_EMBEDDING_ENDPOINT opea/embedding-tei-llama-index:comps sleep 3m } -function validate_microservice() { +function validate_service() { + local INPUT_DATA="$1" + tei_service_port=5034 URL="http://${ip_address}:$tei_service_port/v1/embeddings" docker logs test-comps-embedding-tei-llama-index-server >> ${LOG_PATH}/embedding.log - HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X POST -d '{"text":"What is Deep Learning?"}' -H 'Content-Type: application/json' "$URL") + HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X POST -d "$INPUT_DATA" -H 'Content-Type: application/json' "$URL") if [ "$HTTP_STATUS" -eq 200 ]; then echo "[ embedding - llama_index ] HTTP status is 200. Checking content..." - local CONTENT=$(curl -s -X POST -d '{"text":"What is Deep Learning?"}' -H 'Content-Type: application/json' "$URL" | tee ${LOG_PATH}/embedding.log) + local CONTENT=$(curl -s -X POST -d "$INPUT_DATA" -H 'Content-Type: application/json' "$URL" | tee ${LOG_PATH}/embedding.log) if echo '"text":"What is Deep Learning?","embedding":\[' | grep -q "$EXPECTED_RESULT"; then echo "[ embedding - llama_index ] Content is as expected." @@ -53,6 +55,24 @@ function validate_microservice() { fi } +function validate_microservice() { + ## query with single text + validate_service \ + '{"text":"What is Deep Learning?"}' + + ## query with multiple texts + validate_service \ + '{"text":["What is Deep Learning?","How are you?"]}' + + ## Test OpenAI API, input single text + validate_service \ + '{"input":"What is Deep Learning?"}' + + ## Test OpenAI API, input multiple texts with parameters + validate_service \ + '{"input":["What is Deep Learning?","How are you?"], "dimensions":100}' +} + function stop_docker() { cid=$(docker ps -aq --filter "name=test-comps-embedding-*") if [[ ! -z "$cid" ]]; then docker stop $cid && docker rm $cid && sleep 1s; fi