Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci

Signed-off-by: XuhuiRen <xuhui.ren@intel.com>
  • Loading branch information
pre-commit-ci[bot] authored and XuhuiRen committed May 31, 2024
1 parent 6f20171 commit 62912a8
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 76 deletions.
7 changes: 4 additions & 3 deletions comps/cores/proto/docarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion comps/ragas/tgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
# limitations under the License.
2 changes: 1 addition & 1 deletion comps/ragas/tgi/build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
docker build -t ghcr.io/huggingface/tgi-gaudi:1.2.1 . --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy
86 changes: 43 additions & 43 deletions comps/ragas/tgi/docker_compose_llm.yaml
Original file line number Diff line number Diff line change
@@ -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
# 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
2 changes: 1 addition & 1 deletion comps/ragas/tgi/launch_tgi_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ else
fi

# Execute the Docker run command
eval $docker_cmd
eval $docker_cmd
61 changes: 36 additions & 25 deletions comps/ragas/tgi/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == " ":
Expand All @@ -38,14 +44,14 @@ 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,
endpoint="/v1/chat/completions",
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")
Expand All @@ -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()
opea_microservices["opea_service@llm_tgi"].start()
4 changes: 2 additions & 2 deletions comps/ragas/tgi/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
datasets
docarray[full]
fastapi
huggingface_hub
Expand All @@ -7,7 +8,6 @@ langsmith
opentelemetry-api
opentelemetry-exporter-otlp
opentelemetry-sdk
ragas
shortuuid
transformers
ragas
datasets

0 comments on commit 62912a8

Please sign in to comment.