From cbebd031bc8d64eeba939dc7631ee46ac8009ceb Mon Sep 17 00:00:00 2001
From: leehuwuj <leehuwuj@gmail.com>
Date: Wed, 12 Feb 2025 09:00:20 +0700
Subject: [PATCH] stg

---
 helpers/python.ts                             |  6 ---
 questions/datasources.ts                      | 10 ++--
 .../components/engines/python/chat/engine.py  | 47 -------------------
 .../python/chat/node_postprocessors.py        | 21 ---------
 .../streaming/fastapi/app/api/routers/chat.py | 39 +++++++--------
 .../streaming/fastapi/app/engine}/engine.py   |  0
 .../fastapi/app/engine}/tools/__init__.py     |  0
 .../fastapi/app/engine}/tools/artifact.py     |  0
 .../app/engine}/tools/document_generator.py   |  0
 .../fastapi/app/engine}/tools/duckduckgo.py   |  0
 .../fastapi/app/engine}/tools/form_filling.py |  0
 .../fastapi/app/engine}/tools/img_gen.py      |  0
 .../fastapi/app/engine}/tools/interpreter.py  |  0
 .../app/engine}/tools/openapi_action.py       |  0
 .../fastapi/app/engine}/tools/query_engine.py |  0
 .../fastapi/app/engine}/tools/weather.py      |  0
 16 files changed, 26 insertions(+), 97 deletions(-)
 delete mode 100644 templates/components/engines/python/chat/engine.py
 delete mode 100644 templates/components/engines/python/chat/node_postprocessors.py
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/engine.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/__init__.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/artifact.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/document_generator.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/duckduckgo.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/form_filling.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/img_gen.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/interpreter.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/openapi_action.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/query_engine.py (100%)
 rename templates/{components/engines/python/agent => types/streaming/fastapi/app/engine}/tools/weather.py (100%)

diff --git a/helpers/python.ts b/helpers/python.ts
index 3569a81d..72183b80 100644
--- a/helpers/python.ts
+++ b/helpers/python.ts
@@ -470,12 +470,6 @@ export const installPythonTemplate = async ({
       }
     }
 
-    // Copy engine code
-    await copy("**", enginePath, {
-      parents: true,
-      cwd: path.join(compPath, "engines", "python", engine),
-    });
-
     // Copy router code
     await copyRouterCode(root, tools ?? []);
   }
diff --git a/questions/datasources.ts b/questions/datasources.ts
index 1961e4c8..b750184c 100644
--- a/questions/datasources.ts
+++ b/questions/datasources.ts
@@ -19,10 +19,12 @@ export const getDataSourceChoices = (
     });
   }
   if (selectedDataSource === undefined || selectedDataSource.length === 0) {
-    choices.push({
-      title: "No datasource",
-      value: "none",
-    });
+    if (framework !== "fastapi") {
+      choices.push({
+        title: "No datasource",
+        value: "none",
+      });
+    }
     choices.push({
       title:
         process.platform !== "linux"
diff --git a/templates/components/engines/python/chat/engine.py b/templates/components/engines/python/chat/engine.py
deleted file mode 100644
index f3795afd..00000000
--- a/templates/components/engines/python/chat/engine.py
+++ /dev/null
@@ -1,47 +0,0 @@
-import os
-
-from app.engine.index import IndexConfig, get_index
-from app.engine.node_postprocessors import NodeCitationProcessor
-from fastapi import HTTPException
-from llama_index.core.callbacks import CallbackManager
-from llama_index.core.chat_engine import CondensePlusContextChatEngine
-from llama_index.core.memory import ChatMemoryBuffer
-from llama_index.core.settings import Settings
-
-
-def get_chat_engine(params=None, event_handlers=None, **kwargs):
-    system_prompt = os.getenv("SYSTEM_PROMPT")
-    citation_prompt = os.getenv("SYSTEM_CITATION_PROMPT", None)
-    top_k = int(os.getenv("TOP_K", 0))
-    llm = Settings.llm
-    memory = ChatMemoryBuffer.from_defaults(
-        token_limit=llm.metadata.context_window - 256
-    )
-    callback_manager = CallbackManager(handlers=event_handlers or [])
-
-    node_postprocessors = []
-    if citation_prompt:
-        node_postprocessors = [NodeCitationProcessor()]
-        system_prompt = f"{system_prompt}\n{citation_prompt}"
-
-    index_config = IndexConfig(callback_manager=callback_manager, **(params or {}))
-    index = get_index(index_config)
-    if index is None:
-        raise HTTPException(
-            status_code=500,
-            detail=str(
-                "StorageContext is empty - call 'poetry run generate' to generate the storage first"
-            ),
-        )
-    if top_k != 0 and kwargs.get("similarity_top_k") is None:
-        kwargs["similarity_top_k"] = top_k
-    retriever = index.as_retriever(**kwargs)
-
-    return CondensePlusContextChatEngine(
-        llm=llm,
-        memory=memory,
-        system_prompt=system_prompt,
-        retriever=retriever,
-        node_postprocessors=node_postprocessors,  # type: ignore
-        callback_manager=callback_manager,
-    )
diff --git a/templates/components/engines/python/chat/node_postprocessors.py b/templates/components/engines/python/chat/node_postprocessors.py
deleted file mode 100644
index 336cd0ed..00000000
--- a/templates/components/engines/python/chat/node_postprocessors.py
+++ /dev/null
@@ -1,21 +0,0 @@
-from typing import List, Optional
-
-from llama_index.core import QueryBundle
-from llama_index.core.postprocessor.types import BaseNodePostprocessor
-from llama_index.core.schema import NodeWithScore
-
-
-class NodeCitationProcessor(BaseNodePostprocessor):
-    """
-    Append node_id into metadata for citation purpose.
-    Config SYSTEM_CITATION_PROMPT in your runtime environment variable to enable this feature.
-    """
-
-    def _postprocess_nodes(
-        self,
-        nodes: List[NodeWithScore],
-        query_bundle: Optional[QueryBundle] = None,
-    ) -> List[NodeWithScore]:
-        for node_score in nodes:
-            node_score.node.metadata["node_id"] = node_score.node.node_id
-        return nodes
diff --git a/templates/types/streaming/fastapi/app/api/routers/chat.py b/templates/types/streaming/fastapi/app/api/routers/chat.py
index 0377bb6a..8c2c7f38 100644
--- a/templates/types/streaming/fastapi/app/api/routers/chat.py
+++ b/templates/types/streaming/fastapi/app/api/routers/chat.py
@@ -59,25 +59,26 @@ async def chat(
 
 
 # TODO: Update non-streaming endpoint
-# non-streaming endpoint - delete if not needed
-@r.post("/request")
-async def chat_request(
-    data: ChatData,
-) -> Result:
-    last_message_content = data.get_last_message_content()
-    messages = data.get_history_messages()
+# Would be better if we use same chat.py endpoint for both agent and multiagent templates
+# # non-streaming endpoint - delete if not needed
+# @r.post("/request")
+# async def chat_request(
+#     data: ChatData,
+# ) -> Result:
+#     last_message_content = data.get_last_message_content()
+#     messages = data.get_history_messages()
 
-    doc_ids = data.get_chat_document_ids()
-    filters = generate_filters(doc_ids)
-    params = data.data or {}
-    logger.info(
-        f"Creating chat engine with filters: {str(filters)}",
-    )
+#     doc_ids = data.get_chat_document_ids()
+#     filters = generate_filters(doc_ids)
+#     params = data.data or {}
+#     logger.info(
+#         f"Creating chat engine with filters: {str(filters)}",
+#     )
 
-    chat_engine = get_chat_engine(filters=filters, params=params)
+#     chat_engine = get_chat_engine(filters=filters, params=params)
 
-    response = await chat_engine.achat(last_message_content, messages)
-    return Result(
-        result=Message(role=MessageRole.ASSISTANT, content=response.response),
-        nodes=SourceNodes.from_source_nodes(response.source_nodes),
-    )
+#     response = await chat_engine.achat(last_message_content, messages)
+#     return Result(
+#         result=Message(role=MessageRole.ASSISTANT, content=response.response),
+#         nodes=SourceNodes.from_source_nodes(response.source_nodes),
+#     )
diff --git a/templates/components/engines/python/agent/engine.py b/templates/types/streaming/fastapi/app/engine/engine.py
similarity index 100%
rename from templates/components/engines/python/agent/engine.py
rename to templates/types/streaming/fastapi/app/engine/engine.py
diff --git a/templates/components/engines/python/agent/tools/__init__.py b/templates/types/streaming/fastapi/app/engine/tools/__init__.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/__init__.py
rename to templates/types/streaming/fastapi/app/engine/tools/__init__.py
diff --git a/templates/components/engines/python/agent/tools/artifact.py b/templates/types/streaming/fastapi/app/engine/tools/artifact.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/artifact.py
rename to templates/types/streaming/fastapi/app/engine/tools/artifact.py
diff --git a/templates/components/engines/python/agent/tools/document_generator.py b/templates/types/streaming/fastapi/app/engine/tools/document_generator.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/document_generator.py
rename to templates/types/streaming/fastapi/app/engine/tools/document_generator.py
diff --git a/templates/components/engines/python/agent/tools/duckduckgo.py b/templates/types/streaming/fastapi/app/engine/tools/duckduckgo.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/duckduckgo.py
rename to templates/types/streaming/fastapi/app/engine/tools/duckduckgo.py
diff --git a/templates/components/engines/python/agent/tools/form_filling.py b/templates/types/streaming/fastapi/app/engine/tools/form_filling.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/form_filling.py
rename to templates/types/streaming/fastapi/app/engine/tools/form_filling.py
diff --git a/templates/components/engines/python/agent/tools/img_gen.py b/templates/types/streaming/fastapi/app/engine/tools/img_gen.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/img_gen.py
rename to templates/types/streaming/fastapi/app/engine/tools/img_gen.py
diff --git a/templates/components/engines/python/agent/tools/interpreter.py b/templates/types/streaming/fastapi/app/engine/tools/interpreter.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/interpreter.py
rename to templates/types/streaming/fastapi/app/engine/tools/interpreter.py
diff --git a/templates/components/engines/python/agent/tools/openapi_action.py b/templates/types/streaming/fastapi/app/engine/tools/openapi_action.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/openapi_action.py
rename to templates/types/streaming/fastapi/app/engine/tools/openapi_action.py
diff --git a/templates/components/engines/python/agent/tools/query_engine.py b/templates/types/streaming/fastapi/app/engine/tools/query_engine.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/query_engine.py
rename to templates/types/streaming/fastapi/app/engine/tools/query_engine.py
diff --git a/templates/components/engines/python/agent/tools/weather.py b/templates/types/streaming/fastapi/app/engine/tools/weather.py
similarity index 100%
rename from templates/components/engines/python/agent/tools/weather.py
rename to templates/types/streaming/fastapi/app/engine/tools/weather.py
-- 
GitLab