diff --git a/comps/cores/proto/docarray.py b/comps/cores/proto/docarray.py index 9865b9c79..a009f0d5c 100644 --- a/comps/cores/proto/docarray.py +++ b/comps/cores/proto/docarray.py @@ -90,16 +90,17 @@ class LLMParams(BaseDoc): temperature: float = 0.01 repetition_penalty: float = 1.03 streaming: bool = True - + + class RAGASParams(BaseDoc): questions: DocList[TextDoc] answers: DocList[TextDoc] docs: DocList[TextDoc] ground_truths: DocList[TextDoc] - + + class RAGASScores(BaseDoc): answer_relevancy: float faithfulness: float context_recallL: float context_precision: float - diff --git a/comps/ragas/tgi/__init__.py b/comps/ragas/tgi/__init__.py index fa9642f66..b9a479460 100644 --- a/comps/ragas/tgi/__init__.py +++ b/comps/ragas/tgi/__init__.py @@ -10,4 +10,4 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and -# limitations under the License. \ No newline at end of file +# limitations under the License. diff --git a/comps/ragas/tgi/build_docker.sh b/comps/ragas/tgi/build_docker.sh index 3ef93cb2b..0dd7283d4 100644 --- a/comps/ragas/tgi/build_docker.sh +++ b/comps/ragas/tgi/build_docker.sh @@ -16,4 +16,4 @@ git clone https://github.com/huggingface/tgi-gaudi.git cd ./tgi-gaudi/ -docker build -t ghcr.io/huggingface/tgi-gaudi:1.2.1 . --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy \ No newline at end of file +docker build -t ghcr.io/huggingface/tgi-gaudi:1.2.1 . --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy diff --git a/comps/ragas/tgi/docker_compose_llm.yaml b/comps/ragas/tgi/docker_compose_llm.yaml index 1c59b89c8..adffb8973 100644 --- a/comps/ragas/tgi/docker_compose_llm.yaml +++ b/comps/ragas/tgi/docker_compose_llm.yaml @@ -1,43 +1,43 @@ -# Copyright (c) 2024 Intel Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -version: "3.8" - -services: - tgi_service: - image: ghcr.io/huggingface/text-generation-inference:1.4 - container_name: tgi-service - ports: - - "8008:80" - volumes: - - "./data:/data" - shm_size: 1g - command: --model-id ${LLM_MODEL_ID} - llm: - image: opea/gen-ai-comps:llm-tgi-server - container_name: llm-tgi-server - ports: - - "9000:9000" - ipc: host - environment: - http_proxy: ${http_proxy} - https_proxy: ${https_proxy} - TGI_LLM_ENDPOINT: ${TGI_LLM_ENDPOINT} - HUGGINGFACEHUB_API_TOKEN: ${HUGGINGFACEHUB_API_TOKEN} - LANGCHAIN_API_KEY: ${LANGCHAIN_API_KEY} - restart: unless-stopped - -networks: - default: - driver: bridge \ No newline at end of file +# Copyright (c) 2024 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +version: "3.8" + +services: + tgi_service: + image: ghcr.io/huggingface/text-generation-inference:1.4 + container_name: tgi-service + ports: + - "8008:80" + volumes: + - "./data:/data" + shm_size: 1g + command: --model-id ${LLM_MODEL_ID} + llm: + image: opea/gen-ai-comps:llm-tgi-server + container_name: llm-tgi-server + ports: + - "9000:9000" + ipc: host + environment: + http_proxy: ${http_proxy} + https_proxy: ${https_proxy} + TGI_LLM_ENDPOINT: ${TGI_LLM_ENDPOINT} + HUGGINGFACEHUB_API_TOKEN: ${HUGGINGFACEHUB_API_TOKEN} + LANGCHAIN_API_KEY: ${LANGCHAIN_API_KEY} + restart: unless-stopped + +networks: + default: + driver: bridge diff --git a/comps/ragas/tgi/launch_tgi_service.sh b/comps/ragas/tgi/launch_tgi_service.sh index bb11495d0..d77b29bf9 100644 --- a/comps/ragas/tgi/launch_tgi_service.sh +++ b/comps/ragas/tgi/launch_tgi_service.sh @@ -47,4 +47,4 @@ else fi # Execute the Docker run command -eval $docker_cmd \ No newline at end of file +eval $docker_cmd diff --git a/comps/ragas/tgi/llm.py b/comps/ragas/tgi/llm.py index 9806e3c6b..a12e42be5 100644 --- a/comps/ragas/tgi/llm.py +++ b/comps/ragas/tgi/llm.py @@ -14,19 +14,25 @@ import os -from langchain_community.llms import HuggingFaceEndpoint -from langsmith import traceable from config import EMBED_MODEL from datasets import Dataset +from langchain_community.embeddings import ( + HuggingFaceBgeEmbeddings, + HuggingFaceEmbeddings, + HuggingFaceHubEmbeddings, + HuggingFaceInstructEmbeddings, +) +from langchain_community.llms import HuggingFaceEndpoint +from langsmith import traceable from ragas import evaluate -from ragas.embeddings import LangchainEmbeddingsWrapper # pylint: disable=E0611 -from langchain_community.embeddings import HuggingFaceBgeEmbeddings,HuggingFaceEmbeddings, \ - HuggingFaceInstructEmbeddings, HuggingFaceHubEmbeddings -from comps import GeneratedDoc, RAGASParams, ServiceType, opea_microservices, register_microservice, RAGASScores +from ragas.embeddings import LangchainEmbeddingsWrapper # pylint: disable=E0611 + +from comps import GeneratedDoc, RAGASParams, RAGASScores, ServiceType, opea_microservices, register_microservice tei_embedding_endpoint = os.getenv("TEI_ENDPOINT") EMBED_MODEL = os.getenv("EMBED_MODEL", "BAAI/bge-base-en-v1.5") + @traceable(run_type="tool") def post_process_text(text: str): if text == " ": @@ -38,6 +44,7 @@ def post_process_text(text: str): new_text = text.replace("Answer: ", "").replace("Human: ", "").replace(" ", "@#$") return f"data: {new_text}\n\n" + @register_microservice( name="opea_service@llm_tgi", service_type=ServiceType.LLM, @@ -45,7 +52,6 @@ def post_process_text(text: str): host="0.0.0.0", port=9000, ) - @traceable(run_type="llm") def llm_generate(input: RAGASParams): llm_endpoint = os.getenv("TGI_LLM_ENDPOINT", "http://localhost:8080") @@ -69,29 +75,34 @@ def llm_generate(input: RAGASParams): streaming=input.streaming, timeout=600, ) - + data_collections = { - 'question': input.questions, - 'answer': input.answers, - 'docs' : input.docs, - 'ground_truth': input.groundtruths + "question": input.questions, + "answer": input.answers, + "docs": input.docs, + "ground_truth": input.groundtruths, } dataset = Dataset.from_dict(data_collections) - score = evaluate(dataset, metrics=[answer_relevancy, faithfulness, context_recall, context_precision], \ - llm=llm, embeddings= embedder) + score = evaluate( + dataset, + metrics=[answer_relevancy, faithfulness, context_recall, context_precision], + llm=llm, + embeddings=embedder, + ) df = score.to_pandas() - answer_relevancy_average = df['answer_relevancy'][:].mean() - faithfulness_average = df['faithfulness'][:].mean() - context_recall_average = df['context_recall'][:].mean() - context_precision_average = df['context_precision'][:].mean() - - - return RAGASScores(answer_relevancy=answer_relevancy_average, - faithfulness=faithfulness_average, - context_recallL=context_recall_average, - context_precision=context_precision_average) + answer_relevancy_average = df["answer_relevancy"][:].mean() + faithfulness_average = df["faithfulness"][:].mean() + context_recall_average = df["context_recall"][:].mean() + context_precision_average = df["context_precision"][:].mean() + + return RAGASScores( + answer_relevancy=answer_relevancy_average, + faithfulness=faithfulness_average, + context_recallL=context_recall_average, + context_precision=context_precision_average, + ) if __name__ == "__main__": - opea_microservices["opea_service@llm_tgi"].start() \ No newline at end of file + opea_microservices["opea_service@llm_tgi"].start() diff --git a/comps/ragas/tgi/requirements.txt b/comps/ragas/tgi/requirements.txt index b49b09227..d0a85eb44 100644 --- a/comps/ragas/tgi/requirements.txt +++ b/comps/ragas/tgi/requirements.txt @@ -1,3 +1,4 @@ +datasets docarray[full] fastapi huggingface_hub @@ -7,7 +8,6 @@ langsmith opentelemetry-api opentelemetry-exporter-otlp opentelemetry-sdk +ragas shortuuid transformers -ragas -datasets \ No newline at end of file