From 8eec0c8a1a0e6bb13b7ed942691de8b672631ae7 Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Tue, 12 Nov 2024 16:16:12 +0800 Subject: [PATCH 01/18] Embedding TEI Langchain compatible with OpenAI API Signed-off-by: Xinyao Wang --- comps/embeddings/tei/langchain/README.md | 16 ++++++++++++++++ comps/embeddings/tei/langchain/embedding_tei.py | 17 +++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/comps/embeddings/tei/langchain/README.md b/comps/embeddings/tei/langchain/README.md index 05705c4bb..61219af83 100644 --- a/comps/embeddings/tei/langchain/README.md +++ b/comps/embeddings/tei/langchain/README.md @@ -113,9 +113,25 @@ curl http://localhost:6000/v1/health_check\ ### 3.2 Consume Embedding Service +Basic API, query with single text. ```bash curl http://localhost:6000/v1/embeddings\ -X POST \ -d '{"text":"Hello, world!"}' \ -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..3cab975f5 100644 --- a/comps/embeddings/tei/langchain/embedding_tei.py +++ b/comps/embeddings/tei/langchain/embedding_tei.py @@ -57,19 +57,19 @@ 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) + res = EmbedDoc(text=input.text, embedding=embed_vector[0]) 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 +77,9 @@ 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 From cddd0bdbc21b36d8e863ee5496d971090a15369d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Nov 2024 08:56:58 +0000 Subject: [PATCH 02/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comps/embeddings/tei/langchain/README.md | 2 ++ comps/embeddings/tei/langchain/embedding_tei.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/comps/embeddings/tei/langchain/README.md b/comps/embeddings/tei/langchain/README.md index 61219af83..95b78511b 100644 --- a/comps/embeddings/tei/langchain/README.md +++ b/comps/embeddings/tei/langchain/README.md @@ -114,6 +114,7 @@ curl http://localhost:6000/v1/health_check\ ### 3.2 Consume Embedding Service Basic API, query with single text. + ```bash curl http://localhost:6000/v1/embeddings\ -X POST \ @@ -122,6 +123,7 @@ curl http://localhost:6000/v1/embeddings\ ``` 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\ diff --git a/comps/embeddings/tei/langchain/embedding_tei.py b/comps/embeddings/tei/langchain/embedding_tei.py index 3cab975f5..f4f98b8b8 100644 --- a/comps/embeddings/tei/langchain/embedding_tei.py +++ b/comps/embeddings/tei/langchain/embedding_tei.py @@ -64,7 +64,9 @@ async def embedding( 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))]) + res = EmbeddingResponse( + data=[EmbeddingResponseData(index=i, embedding=embed_vector[i]) for i in range(len(embed_vector))] + ) if isinstance(input, ChatCompletionRequest): input.embedding = res @@ -77,9 +79,11 @@ async def embedding( return res -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)) +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 From 763e927f9d555d7edda3c98a10798b5a0e7e2a30 Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 09:15:33 +0800 Subject: [PATCH 03/18] TextDoc support list Signed-off-by: Xinyao Wang --- comps/cores/proto/docarray.py | 6 +++--- comps/embeddings/tei/langchain/README.md | 9 ++++++++- comps/embeddings/tei/langchain/embedding_tei.py | 3 ++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/comps/cores/proto/docarray.py b/comps/cores/proto/docarray.py index 71b6f15ec..12465dd38 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 FactualityDoc(BaseDoc): @@ -83,8 +83,8 @@ 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 diff --git a/comps/embeddings/tei/langchain/README.md b/comps/embeddings/tei/langchain/README.md index 95b78511b..96163c915 100644 --- a/comps/embeddings/tei/langchain/README.md +++ b/comps/embeddings/tei/langchain/README.md @@ -113,13 +113,20 @@ curl http://localhost:6000/v1/health_check\ ### 3.2 Consume Embedding Service -Basic API, query with single text. +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). diff --git a/comps/embeddings/tei/langchain/embedding_tei.py b/comps/embeddings/tei/langchain/embedding_tei.py index f4f98b8b8..20e61196d 100644 --- a/comps/embeddings/tei/langchain/embedding_tei.py +++ b/comps/embeddings/tei/langchain/embedding_tei.py @@ -57,7 +57,8 @@ 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[0]) + 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: From 0a1edb0ccbfe4293ffd1f340916c2c094465f152 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 01:55:24 +0000 Subject: [PATCH 04/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comps/cores/proto/docarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comps/cores/proto/docarray.py b/comps/cores/proto/docarray.py index 12465dd38..10031e478 100644 --- a/comps/cores/proto/docarray.py +++ b/comps/cores/proto/docarray.py @@ -84,7 +84,7 @@ class DocPath(BaseDoc): class EmbedDoc(BaseDoc): text: Union[str, List[str]] - embedding: Union[conlist(float, min_length=0),List[conlist(float, min_length=0)]] + 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 From d5bd70ec3fd2edcdfe3e092cf283be670e6540be Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 09:55:43 +0800 Subject: [PATCH 05/18] support tei llama index openai compatible API Signed-off-by: Xinyao Wang --- comps/embeddings/tei/llama_index/README.md | 25 +++++++++++++ .../tei/llama_index/embedding_tei.py | 37 +++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/comps/embeddings/tei/llama_index/README.md b/comps/embeddings/tei/llama_index/README.md index e466c47ea..019b55ad3 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' +``` \ No newline at end of file diff --git a/comps/embeddings/tei/llama_index/embedding_tei.py b/comps/embeddings/tei/llama_index/embedding_tei.py index e96b75e75..75238c3a0 100644 --- a/comps/embeddings/tei/llama_index/embedding_tei.py +++ b/comps/embeddings/tei/llama_index/embedding_tei.py @@ -2,10 +2,16 @@ # 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,15 +26,38 @@ 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") From a9173367b659650142f95f62ffdd9e70ebf4bc4d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 02:35:51 +0000 Subject: [PATCH 06/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comps/embeddings/tei/llama_index/README.md | 2 +- comps/embeddings/tei/llama_index/embedding_tei.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/comps/embeddings/tei/llama_index/README.md b/comps/embeddings/tei/llama_index/README.md index 019b55ad3..dd1f5006c 100644 --- a/comps/embeddings/tei/llama_index/README.md +++ b/comps/embeddings/tei/llama_index/README.md @@ -143,4 +143,4 @@ curl http://localhost:6000/v1/embeddings\ -X POST \ -d '{"input":["Hello, world!","How are you?"], "dimensions":100}' \ -H 'Content-Type: application/json' -``` \ No newline at end of file +``` diff --git a/comps/embeddings/tei/llama_index/embedding_tei.py b/comps/embeddings/tei/llama_index/embedding_tei.py index 75238c3a0..a3ff25a70 100644 --- a/comps/embeddings/tei/llama_index/embedding_tei.py +++ b/comps/embeddings/tei/llama_index/embedding_tei.py @@ -3,6 +3,7 @@ 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 @@ -39,7 +40,7 @@ async def embedding( 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))] @@ -49,16 +50,18 @@ async def embedding( 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") From 2fb3d54c19c99d7e8aef6ebf3dcccc85b283c9c3 Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 13:00:16 +0800 Subject: [PATCH 07/18] support mosec langchain openai compatible API Signed-off-by: Xinyao Wang --- comps/embeddings/mosec/langchain/README.md | 35 ++++++++++++++--- .../mosec/langchain/embedding_mosec.py | 38 ++++++++++++++++--- 2 files changed, 63 insertions(+), 10 deletions(-) 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..2f6800277 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,7 +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,17 +67,40 @@ 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") From b62b75e16a86162ea3c8fcf5b933ea7ccb833ea0 Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 13:35:41 +0800 Subject: [PATCH 08/18] update UT for embedding tests Signed-off-by: Xinyao Wang --- .../test_embeddings_mosec_langchain.sh | 22 ++++++++++++++-- .../test_embeddings_tei_langchain.sh | 23 ++++++++++++++-- .../test_embeddings_tei_llama_index.sh | 26 ++++++++++++++++--- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/tests/embeddings/test_embeddings_mosec_langchain.sh b/tests/embeddings/test_embeddings_mosec_langchain.sh index 4140e7011..d99d656b0 100644 --- a/tests/embeddings/test_embeddings_mosec_langchain.sh +++ b/tests/embeddings/test_embeddings_mosec_langchain.sh @@ -42,11 +42,12 @@ function start_service() { 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" @@ -56,6 +57,23 @@ function validate_microservice() { docker logs test-comps-embedding-langchain-mosec-server exit 1 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() { diff --git a/tests/embeddings/test_embeddings_tei_langchain.sh b/tests/embeddings/test_embeddings_tei_langchain.sh index 8c369a116..f8db9824c 100644 --- a/tests/embeddings/test_embeddings_tei_langchain.sh +++ b/tests/embeddings/test_embeddings_tei_langchain.sh @@ -30,11 +30,12 @@ function start_service() { 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..15d7d22cc 100644 --- a/tests/embeddings/test_embeddings_tei_llama_index.sh +++ b/tests/embeddings/test_embeddings_tei_llama_index.sh @@ -30,14 +30,16 @@ function start_service() { 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 From 66c18681bfe2ef2da58a9ed741a0e887e2aa30d7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 05:40:37 +0000 Subject: [PATCH 09/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comps/embeddings/mosec/langchain/embedding_mosec.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/comps/embeddings/mosec/langchain/embedding_mosec.py b/comps/embeddings/mosec/langchain/embedding_mosec.py index 2f6800277..38e92b5a7 100644 --- a/comps/embeddings/mosec/langchain/embedding_mosec.py +++ b/comps/embeddings/mosec/langchain/embedding_mosec.py @@ -24,6 +24,7 @@ EmbeddingResponse, EmbeddingResponseData, ) + logger = CustomLogger("embedding_mosec") logflag = os.getenv("LOGFLAG", False) @@ -81,7 +82,7 @@ async def embedding( 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))] @@ -97,11 +98,13 @@ async def embedding( 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 From 7e46ac9baa708d0e0600807512d5b5bbdead9ac8 Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 13:42:20 +0800 Subject: [PATCH 10/18] fix ut bug Signed-off-by: Xinyao Wang --- tests/embeddings/test_embeddings_mosec_langchain.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/embeddings/test_embeddings_mosec_langchain.sh b/tests/embeddings/test_embeddings_mosec_langchain.sh index d99d656b0..e2fe1348e 100644 --- a/tests/embeddings/test_embeddings_mosec_langchain.sh +++ b/tests/embeddings/test_embeddings_mosec_langchain.sh @@ -57,6 +57,7 @@ function validate_service() { docker logs test-comps-embedding-langchain-mosec-server exit 1 fi +} function validate_microservice() { ## query with single text From d6f1a1da181c324f5fd6a5e0d52350d93a805b5d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 06:16:55 +0000 Subject: [PATCH 11/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/embeddings/test_embeddings_mosec_langchain.sh | 6 +++--- tests/embeddings/test_embeddings_tei_langchain.sh | 6 +++--- tests/embeddings/test_embeddings_tei_llama_index.sh | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/embeddings/test_embeddings_mosec_langchain.sh b/tests/embeddings/test_embeddings_mosec_langchain.sh index d99d656b0..a2c969de7 100644 --- a/tests/embeddings/test_embeddings_mosec_langchain.sh +++ b/tests/embeddings/test_embeddings_mosec_langchain.sh @@ -62,15 +62,15 @@ 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}' diff --git a/tests/embeddings/test_embeddings_tei_langchain.sh b/tests/embeddings/test_embeddings_tei_langchain.sh index f8db9824c..0918b025f 100644 --- a/tests/embeddings/test_embeddings_tei_langchain.sh +++ b/tests/embeddings/test_embeddings_tei_langchain.sh @@ -51,15 +51,15 @@ 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}' diff --git a/tests/embeddings/test_embeddings_tei_llama_index.sh b/tests/embeddings/test_embeddings_tei_llama_index.sh index 15d7d22cc..cc4f769bc 100644 --- a/tests/embeddings/test_embeddings_tei_llama_index.sh +++ b/tests/embeddings/test_embeddings_tei_llama_index.sh @@ -59,15 +59,15 @@ 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}' From f569d346b7094dfb31b8bff98cb69b683fb2622b Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 14:18:58 +0800 Subject: [PATCH 12/18] support embedding predictionguard openai compatible API Signed-off-by: Xinyao Wang --- comps/embeddings/predictionguard/README.md | 33 +++++++++++-- .../embedding_predictionguard.py | 49 ++++++++++++++++--- .../test_embeddings_predictionguard.sh | 23 ++++++++- 3 files changed, 93 insertions(+), 12 deletions(-) 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..e77749fbe 100644 --- a/comps/embeddings/predictionguard/embedding_predictionguard.py +++ b/comps/embeddings/predictionguard/embedding_predictionguard.py @@ -4,10 +4,11 @@ import os import time - +from typing import List, Optional, Union from predictionguard import PredictionGuard from comps import ( + CustomLogger, EmbedDoc, ServiceType, TextDoc, @@ -16,6 +17,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,15 +41,42 @@ 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 + 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") diff --git a/tests/embeddings/test_embeddings_predictionguard.sh b/tests/embeddings/test_embeddings_predictionguard.sh index 567b4fc0b..83272908e 100644 --- a/tests/embeddings/test_embeddings_predictionguard.sh +++ b/tests/embeddings/test_embeddings_predictionguard.sh @@ -32,11 +32,12 @@ function start_service() { 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 From 4a34e44bda7eaaf42c306f04bc1206c4031bb5b7 Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 14:43:58 +0800 Subject: [PATCH 13/18] support embedding multimodal clip OpenAI compatible API Signed-off-by: Xinyao Wang --- comps/cores/proto/docarray.py | 2 +- comps/embeddings/multimodal_clip/README.md | 31 ++++++++++-- .../multimodal_clip/embedding_multimodal.py | 50 ++++++++++++++++--- .../test_embeddings_multimodal_clip.sh | 23 ++++++++- 4 files changed, 92 insertions(+), 14 deletions(-) diff --git a/comps/cores/proto/docarray.py b/comps/cores/proto/docarray.py index 88c67b7a4..50871f7bf 100644 --- a/comps/cores/proto/docarray.py +++ b/comps/cores/proto/docarray.py @@ -101,7 +101,7 @@ class EmbedDoc(BaseDoc): 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/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..a9fa88e5c 100644 --- a/comps/embeddings/multimodal_clip/embedding_multimodal.py +++ b/comps/embeddings/multimodal_clip/embedding_multimodal.py @@ -3,11 +3,12 @@ import datetime 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,7 +17,14 @@ 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,20 +72,46 @@ 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}) diff --git a/tests/embeddings/test_embeddings_multimodal_clip.sh b/tests/embeddings/test_embeddings_multimodal_clip.sh index c3d0bccde..802d5350a 100644 --- a/tests/embeddings/test_embeddings_multimodal_clip.sh +++ b/tests/embeddings/test_embeddings_multimodal_clip.sh @@ -24,11 +24,12 @@ function start_service() { 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 From 792e419e6c06406521b32865e6339de6daea6ff2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 06:59:18 +0000 Subject: [PATCH 14/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comps/embeddings/predictionguard/embedding_predictionguard.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/comps/embeddings/predictionguard/embedding_predictionguard.py b/comps/embeddings/predictionguard/embedding_predictionguard.py index e77749fbe..793f35d2d 100644 --- a/comps/embeddings/predictionguard/embedding_predictionguard.py +++ b/comps/embeddings/predictionguard/embedding_predictionguard.py @@ -5,6 +5,7 @@ import os import time from typing import List, Optional, Union + from predictionguard import PredictionGuard from comps import ( @@ -72,12 +73,14 @@ async def embedding( 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 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.") From 3e4be8032a9a04dd6441a891710e930a17d18261 Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 14:52:36 +0800 Subject: [PATCH 15/18] fix bug Signed-off-by: Xinyao Wang --- comps/embeddings/multimodal_clip/embedding_multimodal.py | 2 +- comps/embeddings/predictionguard/embedding_predictionguard.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/comps/embeddings/multimodal_clip/embedding_multimodal.py b/comps/embeddings/multimodal_clip/embedding_multimodal.py index a9fa88e5c..2401326a0 100644 --- a/comps/embeddings/multimodal_clip/embedding_multimodal.py +++ b/comps/embeddings/multimodal_clip/embedding_multimodal.py @@ -2,7 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 import datetime -import time +import time,os from typing import List, Optional, Union from dateparser.search import search_dates from embeddings_clip import vCLIP diff --git a/comps/embeddings/predictionguard/embedding_predictionguard.py b/comps/embeddings/predictionguard/embedding_predictionguard.py index 793f35d2d..f5274e3b5 100644 --- a/comps/embeddings/predictionguard/embedding_predictionguard.py +++ b/comps/embeddings/predictionguard/embedding_predictionguard.py @@ -76,6 +76,7 @@ async def embedding( 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 From e9a6159322713f950584da673b7eb93633e5d64e Mon Sep 17 00:00:00 2001 From: Xinyao Wang Date: Wed, 13 Nov 2024 14:55:26 +0800 Subject: [PATCH 16/18] enable debug mode for embedding UT Signed-off-by: Xinyao Wang --- tests/embeddings/test_embeddings_mosec_langchain.sh | 2 +- tests/embeddings/test_embeddings_multimodal_clip.sh | 2 +- tests/embeddings/test_embeddings_predictionguard.sh | 2 +- tests/embeddings/test_embeddings_tei_langchain.sh | 2 +- tests/embeddings/test_embeddings_tei_llama_index.sh | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/embeddings/test_embeddings_mosec_langchain.sh b/tests/embeddings/test_embeddings_mosec_langchain.sh index 22ece5395..0c7b1bc3c 100644 --- a/tests/embeddings/test_embeddings_mosec_langchain.sh +++ b/tests/embeddings/test_embeddings_mosec_langchain.sh @@ -38,7 +38,7 @@ 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 } diff --git a/tests/embeddings/test_embeddings_multimodal_clip.sh b/tests/embeddings/test_embeddings_multimodal_clip.sh index 802d5350a..770f2dc3d 100644 --- a/tests/embeddings/test_embeddings_multimodal_clip.sh +++ b/tests/embeddings/test_embeddings_multimodal_clip.sh @@ -20,7 +20,7 @@ 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 } diff --git a/tests/embeddings/test_embeddings_predictionguard.sh b/tests/embeddings/test_embeddings_predictionguard.sh index 83272908e..a6727b6bf 100644 --- a/tests/embeddings/test_embeddings_predictionguard.sh +++ b/tests/embeddings/test_embeddings_predictionguard.sh @@ -26,7 +26,7 @@ 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 diff --git a/tests/embeddings/test_embeddings_tei_langchain.sh b/tests/embeddings/test_embeddings_tei_langchain.sh index 0918b025f..df2642cf1 100644 --- a/tests/embeddings/test_embeddings_tei_langchain.sh +++ b/tests/embeddings/test_embeddings_tei_langchain.sh @@ -26,7 +26,7 @@ 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 } diff --git a/tests/embeddings/test_embeddings_tei_llama_index.sh b/tests/embeddings/test_embeddings_tei_llama_index.sh index cc4f769bc..e1d04ab5e 100644 --- a/tests/embeddings/test_embeddings_tei_llama_index.sh +++ b/tests/embeddings/test_embeddings_tei_llama_index.sh @@ -26,7 +26,7 @@ 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 } From d2e5df1e4d0d1ed08d43b3b1d897698a626fa291 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 07:28:09 +0000 Subject: [PATCH 17/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comps/cores/proto/docarray.py | 2 +- comps/embeddings/multimodal_clip/embedding_multimodal.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/comps/cores/proto/docarray.py b/comps/cores/proto/docarray.py index 50871f7bf..79f961ca2 100644 --- a/comps/cores/proto/docarray.py +++ b/comps/cores/proto/docarray.py @@ -101,7 +101,7 @@ class EmbedDoc(BaseDoc): fetch_k: int = 20 lambda_mult: float = 0.5 score_threshold: float = 0.2 - constraints: Optional[Union[Dict[str, Any], List[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/multimodal_clip/embedding_multimodal.py b/comps/embeddings/multimodal_clip/embedding_multimodal.py index a9fa88e5c..c791f1471 100644 --- a/comps/embeddings/multimodal_clip/embedding_multimodal.py +++ b/comps/embeddings/multimodal_clip/embedding_multimodal.py @@ -4,6 +4,7 @@ import datetime import time from typing import List, Optional, Union + from dateparser.search import search_dates from embeddings_clip import vCLIP @@ -23,9 +24,11 @@ EmbeddingResponse, EmbeddingResponseData, ) + logger = CustomLogger("embedding_multimodal") logflag = os.getenv("LOGFLAG", False) + def filtler_dates(prompt): base_date = datetime.datetime.today() @@ -108,11 +111,13 @@ async def embedding( 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() From e429ca01d8da2bdb9871a350137ac276cb1de439 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 07:35:47 +0000 Subject: [PATCH 18/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- comps/embeddings/multimodal_clip/embedding_multimodal.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/comps/embeddings/multimodal_clip/embedding_multimodal.py b/comps/embeddings/multimodal_clip/embedding_multimodal.py index aafee3986..888278e7a 100644 --- a/comps/embeddings/multimodal_clip/embedding_multimodal.py +++ b/comps/embeddings/multimodal_clip/embedding_multimodal.py @@ -2,7 +2,8 @@ # SPDX-License-Identifier: Apache-2.0 import datetime -import time,os +import os +import time from typing import List, Optional, Union from dateparser.search import search_dates