diff --git a/README.md b/README.md index 1b77cbae..0684f50a 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,6 @@ git clone git@github.com:aigc-apps/PAI-RAG.git ``` -注:如果需要调用Open AI,需要使用新加坡开发机器,不能连通弹内Gitlab环境,需要手动将代码上传到新加坡机器 - ### Step2: 配置环境 本项目使用poetry进行管理,建议在安装环境之前先创建一个空环境。为了确保环境一致性并避免因Python版本差异造成的问题,我们指定Python版本为3.10。 @@ -36,7 +34,7 @@ poetry install pai_rag run [--host HOST] [--port PORT] [--config CONFIG_FILE] ``` -现在你可以使用命令行向服务侧发送API请求,或者直接打开http://HOST:PORT。 +现在你可以使用命令行向服务侧发送API请求,或者直接打开http://localhost:8000 1. diff --git a/src/pai_rag/app/api/models.py b/src/pai_rag/app/api/models.py index 4ad55816..af0d421d 100644 --- a/src/pai_rag/app/api/models.py +++ b/src/pai_rag/app/api/models.py @@ -35,9 +35,13 @@ class RagResponse(BaseModel): class LlmResponse(BaseModel): answer: str +class ContextDoc(BaseModel): + text: str + score: float + metadata: Dict class RetrievalResponse(BaseModel): - answer: str + docs: List[ContextDoc] class KnowledgeInput(BaseModel): diff --git a/src/pai_rag/app/web/rag_client.py b/src/pai_rag/app/web/rag_client.py index af4cc467..1be5b5d3 100644 --- a/src/pai_rag/app/web/rag_client.py +++ b/src/pai_rag/app/web/rag_client.py @@ -76,7 +76,8 @@ def query_vector(self, text: str): session_id = r.headers["x-session-id"] response = dotdict(json.loads(r.text)) response.session_id = session_id - + formatted_text = "\n\n".join([f"""[Doc {i+1}] [score: {doc["score"]}]\n{doc["text"]}""" for i,doc in enumerate(response["docs"])]) + response["answer"] = formatted_text return response def add_knowledge(self, file_dir: str, enable_qa_extraction: bool): diff --git a/src/pai_rag/app/web/ui.py b/src/pai_rag/app/web/ui.py index caf0b299..1cc97753 100644 --- a/src/pai_rag/app/web/ui.py +++ b/src/pai_rag/app/web/ui.py @@ -25,7 +25,7 @@ #### \N{fire} Platform: [PAI](https://help.aliyun.com/zh/pai) / [PAI-EAS](https://www.aliyun.com/product/bigdata/learn/eas) / [PAI-DSW](https://pai.console.aliyun.com/notebook)   \N{rocket} Supported VectorStores: [Hologres](https://www.aliyun.com/product/bigdata/hologram) / [ElasticSearch](https://www.aliyun.com/product/bigdata/elasticsearch) / [AnalyticDB](https://www.aliyun.com/product/apsaradb/gpdb) / [FAISS](https://python.langchain.com/docs/integrations/vectorstores/faiss) - #### \N{fire} API Docs   \N{rocket} View Tracing   \N{fire} 欢迎加入【PAI】RAG答疑群 27370042974 + #### \N{fire} API Docs   \N{rocket} \N{fire} 欢迎加入【PAI】RAG答疑群 27370042974 """ css_style = """ @@ -145,11 +145,7 @@ def respond(input_elements: List[Any]): def create_ui(): with gr.Blocks(css=css_style) as homepage: - gr.Markdown( - value=welcome_message_markdown.format( - EAS_TRACE_ENDPOINT=environ.get("EAS_ARIZE_PHOENIX_URL", "") - ) - ) + gr.Markdown(value=welcome_message_markdown) with gr.Tab("\N{rocket} Settings"): with gr.Row(): diff --git a/src/pai_rag/core/rag_application.py b/src/pai_rag/core/rag_application.py index e100d5ba..93456852 100644 --- a/src/pai_rag/core/rag_application.py +++ b/src/pai_rag/core/rag_application.py @@ -9,6 +9,7 @@ RetrievalQuery, RagResponse, LlmResponse, + ContextDoc, RetrievalResponse, ) @@ -64,15 +65,11 @@ async def aquery_vectordb(self, query: RetrievalQuery) -> RetrievalResponse: session_id = correlation_id.get() or DEFAULT_SESSION_ID self.logger.info(f"Get session ID: {session_id}.") node_results = await self.retriever.aretrieve(query.question) - text_results = [ - score_node.node.get_content().replace("\n", " ") + + docs = [ContextDoc(text = score_node.node.get_content(), metadata=score_node.node.metadata, score=score_node.score) for score_node in node_results ] - formatted_results = [ - f"**Doc [{i}]**: {text} \n" for i, text in enumerate(text_results) - ] - result = "\n".join(formatted_results) - return RetrievalResponse(answer=result) + return RetrievalResponse(docs=docs) async def aquery(self, query: RagQuery) -> RagResponse: """Query answer from RAG App asynchronously. diff --git a/src/pai_rag/integrations/llms/paieas/README.md b/src/pai_rag/integrations/llms/paieas/README.md deleted file mode 100644 index 798a44d4..00000000 --- a/src/pai_rag/integrations/llms/paieas/README.md +++ /dev/null @@ -1 +0,0 @@ -### PAI EAS llm integration diff --git a/src/pai_rag/integrations/llms/paieas/__init__.py b/src/pai_rag/integrations/llms/paieas/__init__.py deleted file mode 100644 index e4b3dd89..00000000 --- a/src/pai_rag/integrations/llms/paieas/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from llama_index.llms.paieas.base import PaiEAS - -__all__ = ["PaiEAS"] diff --git a/src/pai_rag/integrations/llms/paieas/llama_index/llms/paieas/base.py b/src/pai_rag/integrations/llms/paieas/base.py similarity index 99% rename from src/pai_rag/integrations/llms/paieas/llama_index/llms/paieas/base.py rename to src/pai_rag/integrations/llms/paieas/base.py index eeb79310..40b7edf5 100644 --- a/src/pai_rag/integrations/llms/paieas/llama_index/llms/paieas/base.py +++ b/src/pai_rag/integrations/llms/paieas/base.py @@ -79,7 +79,7 @@ def __init__( @classmethod def class_name(cls) -> str: - return "PaiEasLLM" + return "PaiEAS" @property def metadata(self) -> LLMMetadata: diff --git a/src/pai_rag/integrations/llms/paieas/poetry.lock b/src/pai_rag/integrations/llms/paieas/poetry.lock deleted file mode 100644 index 4e5b56dc..00000000 --- a/src/pai_rag/integrations/llms/paieas/poetry.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. -package = [] - -[metadata] -lock-version = "2.0" -python-versions = "^3.10" -content-hash = "53f2eabc9c26446fbcc00d348c47878e118afc2054778c3c803a0a8028af27d9" diff --git a/src/pai_rag/integrations/llms/paieas/pyproject.toml b/src/pai_rag/integrations/llms/paieas/pyproject.toml deleted file mode 100644 index afa20346..00000000 --- a/src/pai_rag/integrations/llms/paieas/pyproject.toml +++ /dev/null @@ -1,21 +0,0 @@ -[build-system] -requires = ["poetry-core"] -build-backend = "poetry.core.masonry.api" - -[tool.llamahub] -contains_example = false -import_path = "llama_index.llms.paieas" - -[tool.llamahub.class_authors] -PaiEas = "llama-index" - -[tool.poetry] -name = "llama-index-llms-paieas" -version = "0.1.0" -description = "" -authors = ["陆逊 "] -readme = "README.md" -packages = [{include = "llama_index/"}] - -[tool.poetry.dependencies] -python = "^3.10" diff --git a/src/pai_rag/integrations/llms/paieas/tests/test_pai_eas_llm.py b/src/pai_rag/integrations/llms/paieas/tests/test_pai_eas_llm.py deleted file mode 100644 index ef72ee26..00000000 --- a/src/pai_rag/integrations/llms/paieas/tests/test_pai_eas_llm.py +++ /dev/null @@ -1,55 +0,0 @@ -from llama_index.llms.paieas.base import PaiEAS -from llama_index.core.base.llms.types import ChatMessage -import os - - -def _get_eas_llm() -> PaiEAS: - eas_url = os.environ.get("TEST_EAS_URL", None) - eas_token = os.environ.get("TEST_EAS_TOKEN", None) - - if not eas_url or not eas_token: - return None - - eas_llm = PaiEAS(endpoint=eas_url, token=eas_token, model_name="EasCustomModel") - return eas_llm - - -def test_pai_eas_llm_complete(): - eas_llm = _get_eas_llm() - if eas_llm: - response = eas_llm.complete("你好呀,最近怎么样?") - assert len(response.text) > 0 - - -def test_pai_eas_llm_stream_complete(): - eas_llm = _get_eas_llm() - - if eas_llm: - response = eas_llm.stream_complete("你好呀,最近怎么样?") - text = None - for r in response: - text = r.text - - assert len(text) > 0 - - -def test_pai_eas_llm_chat(): - eas_llm = _get_eas_llm() - if eas_llm: - chat_messages = [ChatMessage(role="user", content="你好呀,最近怎么样?")] - response = eas_llm.chat(chat_messages) - print(response.message.content) - assert len(response.message.content) > 0 - - -def test_pai_eas_llm_stream_chat(): - eas_llm = _get_eas_llm() - if eas_llm: - chat_messages = [ChatMessage(role="user", content="你好呀,最近怎么样?")] - response = eas_llm.stream_chat(chat_messages) - - text = None - for r in response: - text = r.message.content - print(text) - assert len(text) > 0 diff --git a/src/pai_rag/modules/llm/llm_module.py b/src/pai_rag/modules/llm/llm_module.py index 3da49a8e..d5417622 100644 --- a/src/pai_rag/modules/llm/llm_module.py +++ b/src/pai_rag/modules/llm/llm_module.py @@ -4,7 +4,7 @@ from llama_index.llms.openai import OpenAI from llama_index.llms.azure_openai import AzureOpenAI from llama_index.llms.dashscope import DashScope -from pai_rag.integrations.llms.paieas import PaiEAS +from pai_rag.integrations.llms.paieas.base import PaiEAS from pai_rag.modules.base.configurable_module import ConfigurableModule from pai_rag.modules.base.module_constants import MODULE_PARAM_CONFIG