Skip to content

Comments

refactor: extract shared config and utilities for API servers#49

Open
himanshu748 wants to merge 2 commits intokubeflow:mainfrom
himanshu748:refactor/extract-shared-rag-module
Open

refactor: extract shared config and utilities for API servers#49
himanshu748 wants to merge 2 commits intokubeflow:mainfrom
himanshu748:refactor/extract-shared-rag-module

Conversation

@himanshu748
Copy link

Description

This PR extracts massive code duplication (~150 lines) shared between the WebSocket server (server/app.py) and the HTTPS API server (server-https/app.py).

The duplicated configuration constants, system prompt, tool definitions, Milvus search logic, and tool execution helpers have been moved into a new shared/rag_core.py module.

This reduces boilerplate and ensures that both the WebSocket and HTTPS interfaces of the docs-agent remain in sync if we update the RAG system prompt or add new tools.

Testing

  • Verified Python syntax (python3 -m py_compile)
  • DCO signed off

Copilot AI review requested due to automatic review settings February 22, 2026 06:19
@google-oss-prow
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign franciscojavierarceo for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR successfully extracts ~150 lines of duplicated code from the WebSocket server (server/app.py) and HTTPS API server (server-https/app.py) into a new shared module (shared/rag_core.py). The refactoring consolidates configuration constants, the system prompt, tool definitions, Milvus search logic, and tool execution helpers to ensure both server implementations remain synchronized.

Changes:

  • Created shared/rag_core.py module containing all shared RAG-related configuration, prompts, tools, and utility functions
  • Refactored server/app.py to import and use shared code instead of duplicating logic
  • Refactored server-https/app.py to import and use shared code instead of duplicating logic

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 6 comments.

File Description
shared/rag_core.py New shared module containing configuration constants, system prompt, tool definitions, Milvus search function, tool execution logic, and utility functions (deduplicate_citations, build_chat_payload)
shared/init.py Empty init file to make shared a Python package
server/app.py Removed ~150 lines of duplicated code; now imports shared configuration and utilities from shared.rag_core; updated logging from print statements to logger; maintained WebSocket-specific logic
server-https/app.py Removed ~150 lines of duplicated code; now imports shared configuration and utilities from shared.rag_core; updated logging from print statements to logger; maintained FastAPI-specific logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +11 to 27
# Add project root to path so shared module is importable
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from shared.rag_core import ( # noqa: E402
KSERVE_URL,
MODEL,
MILVUS_HOST,
MILVUS_PORT,
MILVUS_COLLECTION,
PORT,
SYSTEM_PROMPT,
TOOLS,
execute_tool,
deduplicate_citations,
build_chat_payload,
)

Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sys.path manipulation to enable shared module imports works for development but will fail in Docker containers because the Dockerfiles (server/Dockerfile and server-https/Dockerfile) don't copy the shared/ directory. The Dockerfiles need to be updated to include COPY shared/ /app/shared/ before the application starts, otherwise the import will fail at runtime with a ModuleNotFoundError.

Suggested change
# Add project root to path so shared module is importable
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from shared.rag_core import ( # noqa: E402
KSERVE_URL,
MODEL,
MILVUS_HOST,
MILVUS_PORT,
MILVUS_COLLECTION,
PORT,
SYSTEM_PROMPT,
TOOLS,
execute_tool,
deduplicate_citations,
build_chat_payload,
)
# Import shared module, supporting both installed-package and repo layouts
try:
from shared.rag_core import ( # noqa: E402
KSERVE_URL,
MODEL,
MILVUS_HOST,
MILVUS_PORT,
MILVUS_COLLECTION,
PORT,
SYSTEM_PROMPT,
TOOLS,
execute_tool,
deduplicate_citations,
build_chat_payload,
)
except ModuleNotFoundError as original_exc:
# Fallback for development: add project root so ../shared is importable
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if project_root not in sys.path:
sys.path.insert(0, project_root)
try:
from shared.rag_core import ( # noqa: E402
KSERVE_URL,
MODEL,
MILVUS_HOST,
MILVUS_PORT,
MILVUS_COLLECTION,
PORT,
SYSTEM_PROMPT,
TOOLS,
execute_tool,
deduplicate_citations,
build_chat_payload,
)
except ModuleNotFoundError:
raise ModuleNotFoundError(
"Could not import 'shared.rag_core'. Ensure the 'shared/' directory "
"is available on PYTHONPATH or copied into the runtime environment "
"(for Docker, include 'COPY shared/ /app/shared/')."
) from original_exc

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +14
# Add project root to path so shared module is importable
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sys.path manipulation to enable shared module imports works for development but will fail in Docker containers because the Dockerfiles (server/Dockerfile and server-https/Dockerfile) don't copy the shared/ directory. The Dockerfiles need to be updated to include COPY shared/ /app/shared/ before the application starts, otherwise the import will fail at runtime with a ModuleNotFoundError.

Copilot uses AI. Check for mistakes.
server/app.py Outdated
if citations_collector is None:
citations_collector = []
try:
import httpx
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The httpx import is moved inside the function rather than at the module level. While this works, it's inconsistent with the pattern used in server-https/app.py where httpx is imported at the top. For consistency and conventional Python practices, httpx should be imported at the module level alongside other imports.

Copilot uses AI. Check for mistakes.

def deduplicate_citations(citations: List[str]) -> List[str]:
"""Remove duplicate citations while preserving order."""
seen: set[str] = set()
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modern Python 3.9+ syntax set[str] is used here, but this is incompatible with Python 3.8 and earlier. Since the Dockerfiles specify Python 3.11, this is fine. However, for better compatibility with the typing imports already used in the file (e.g., List[str] from typing module), consider using Set[str] from the typing module for consistency with the rest of the type hints in this file.

Copilot uses AI. Check for mistakes.
Comment on lines +133 to +134
encoder = SentenceTransformer(EMBEDDING_MODEL)
query_vec = encoder.encode(query).tolist()
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SentenceTransformer model is instantiated on every search call. This is inefficient as it will reload the model from disk or cache each time. Consider moving the model initialization to module-level or using a singleton pattern to reuse the same model instance across multiple searches. This will significantly improve performance and reduce memory overhead.

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +22
SYSTEM_PROMPT,
TOOLS,
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imported SYSTEM_PROMPT and TOOLS constants are not directly used in this file - they are only used indirectly through build_chat_payload() from the shared module. Consider removing these from the import list to keep the imports clean and avoid confusion about what is actually used in this module.

Suggested change
SYSTEM_PROMPT,
TOOLS,

Copilot uses AI. Check for mistakes.
@himanshu748
Copy link
Author

Feedback addressed: Included the 'shared/' module in Docker builds (server and server-https), optimized imports in rag_core.py and app.py, and added beautifulsoup4 to requirements.txt.

@SanthoshToorpu
Copy link
Contributor

Hello @himanshu748 the duplication was done inorder to ensure appropriate deployment and ease of switching in between sockets and http.

However the notion behind this PR is good but the merge might take a while. Also this code would be deprecated in future

@himanshu748 himanshu748 force-pushed the refactor/extract-shared-rag-module branch from a98cc9b to b581b57 Compare February 24, 2026 05:32
Copy link
Author

@himanshu748 himanshu748 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DCO failing check fixed by signing off on the commits. The branch has also been rebased against main. I understand the merge might take a while and the code might be deprecated later. Thanks for the context!

@himanshu748 himanshu748 force-pushed the refactor/extract-shared-rag-module branch 4 times, most recently from 19b160f to 56d04dc Compare February 24, 2026 05:51
Extracts duplicated configuration, system prompts, Milvus search logic,
and tool execution code from `server/app.py` and `server-https/app.py`
into a new `shared/rag_core.py` module.

Helps reduce code duplication and eases maintainability for future
tool-calling expansions in the docs agent.

Signed-off-by: himanshu748 <jhahimanshu653@gmail.com>
Signed-off-by: himanshu748 <jhahimanshu653@gmail.com>
@himanshu748 himanshu748 force-pushed the refactor/extract-shared-rag-module branch from 56d04dc to 003bca2 Compare February 24, 2026 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants