Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
142ed75
((feat:fs_abstact) base
ManishMadan2882 Apr 15, 2025
89b2937
((feat:fs_abstact) local
ManishMadan2882 Apr 15, 2025
e567d88
((feat:fs_abstact) s3
ManishMadan2882 Apr 15, 2025
377e33c
(feat:file_abstract) process files method
ManishMadan2882 Apr 15, 2025
0a0e165
(feat:fs_abstract) attachment uploads
ManishMadan2882 Apr 16, 2025
9454150
(fix:s3) processor func
ManishMadan2882 Apr 16, 2025
68e4cf4
(feat:fsabstract) add factory class
ManishMadan2882 Apr 16, 2025
0d3e615
(feat:attachmentUpload) parse content before upload
ManishMadan2882 Apr 17, 2025
c35d1ce
(feat:file_abstract) return storage metadata after upload
ManishMadan2882 Apr 17, 2025
335c21c
(fix:attachment) dont calculate MIME again
ManishMadan2882 Apr 17, 2025
5aa51f5
(feat:file_abstract) openai attachments comply
ManishMadan2882 Apr 17, 2025
c8efef8
(fix:openai) image uplads, use lambda in process_files
ManishMadan2882 Apr 18, 2025
c50ff6f
(feat:fs abstract) googleLLM class
ManishMadan2882 Apr 18, 2025
e9a6044
Merge branch 'main' into main
ManishMadan2882 Apr 20, 2025
38476cf
(gfeat:storage) get storage instance based on settings
ManishMadan2882 Apr 21, 2025
0a31dda
(feat:storage) use get storage
ManishMadan2882 Apr 21, 2025
64c42f0
(feat:storage) file, indexes uploads
ManishMadan2882 Apr 21, 2025
5ad34e2
(fix:indexes) look for the right path
ManishMadan2882 Apr 22, 2025
24c8b24
Revert "(fix:indexes) look for the right path"
ManishMadan2882 Apr 22, 2025
637d3a2
Revert "(feat:storage) file, indexes uploads"
ManishMadan2882 Apr 22, 2025
e60f78a
(feat:storage) file uploads
ManishMadan2882 Apr 22, 2025
0ce27f2
(feat:storage) file indexes/faiss
ManishMadan2882 Apr 22, 2025
3cd9a72
add storage type to the settings cofig
dartpain Apr 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 23 additions & 32 deletions application/agents/llm_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,95 +15,86 @@
@abstractmethod
def handle_response(self, agent, resp, tools_dict, messages, attachments=None, **kwargs):
pass

def prepare_messages_with_attachments(self, agent, messages, attachments=None):
"""
Prepare messages with attachment content if available.

Args:
agent: The current agent instance.
messages (list): List of message dictionaries.
attachments (list): List of attachment dictionaries with content.

Returns:
list: Messages with attachment context added to the system prompt.
"""
if not attachments:
return messages

logger.info(f"Preparing messages with {len(attachments)} attachments")

supported_types = agent.llm.get_supported_attachment_types()

supported_attachments = []
unsupported_attachments = []

for attachment in attachments:
mime_type = attachment.get('mime_type')
if not mime_type:
import mimetypes
file_path = attachment.get('path')
if file_path:
mime_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream'
else:
unsupported_attachments.append(attachment)
continue

if mime_type in supported_types:
supported_attachments.append(attachment)
else:
unsupported_attachments.append(attachment)

# Process supported attachments with the LLM's custom method
prepared_messages = messages
if supported_attachments:
logger.info(f"Processing {len(supported_attachments)} supported attachments with {agent.llm.__class__.__name__}'s method")
prepared_messages = agent.llm.prepare_messages_with_attachments(messages, supported_attachments)

# Process unsupported attachments with the default method
if unsupported_attachments:
logger.info(f"Processing {len(unsupported_attachments)} unsupported attachments with default method")
prepared_messages = self._append_attachment_content_to_system(prepared_messages, unsupported_attachments)

return prepared_messages

def _append_attachment_content_to_system(self, messages, attachments):
"""
Default method to append attachment content to the system prompt.

Args:
messages (list): List of message dictionaries.
attachments (list): List of attachment dictionaries with content.

Returns:
list: Messages with attachment context added to the system prompt.
"""
prepared_messages = messages.copy()

attachment_texts = []
for attachment in attachments:
logger.info(f"Adding attachment {attachment.get('id')} to context")
if 'content' in attachment:
attachment_texts.append(f"Attached file content:\n\n{attachment['content']}")

if attachment_texts:
combined_attachment_text = "\n\n".join(attachment_texts)

system_found = False
for i in range(len(prepared_messages)):
if prepared_messages[i].get("role") == "system":
prepared_messages[i]["content"] += f"\n\n{combined_attachment_text}"
system_found = True
break

if not system_found:
prepared_messages.insert(0, {"role": "system", "content": combined_attachment_text})

return prepared_messages

class OpenAILLMHandler(LLMHandler):
def handle_response(self, agent, resp, tools_dict, messages, attachments=None, stream: bool = True):

messages = self.prepare_messages_with_attachments(agent, messages, attachments)
logger.info(f"Messages with attachments: {messages}")
if not stream:
Expand Down Expand Up @@ -167,7 +158,7 @@
if isinstance(chunk, str) and len(chunk) > 0:
yield chunk
continue
elif hasattr(chunk, "delta"):
elif hasattr(chunk, "delta"):

Check warning on line 161 in application/agents/llm_handler.py

View check run for this annotation

Codecov / codecov/patch

application/agents/llm_handler.py#L161

Added line #L161 was not covered by tests
chunk_delta = chunk.delta

if (
Expand Down Expand Up @@ -258,7 +249,7 @@
return resp
elif isinstance(chunk, str) and len(chunk) == 0:
continue

logger.info(f"Regenerating with messages: {messages}")
resp = agent.llm.gen_stream(
model=agent.gpt_model, messages=messages, tools=agent.tools
Expand All @@ -269,9 +260,9 @@
class GoogleLLMHandler(LLMHandler):
def handle_response(self, agent, resp, tools_dict, messages, attachments=None, stream: bool = True):
from google.genai import types

messages = self.prepare_messages_with_attachments(agent, messages, attachments)

while True:
if not stream:
response = agent.llm.gen(
Expand Down
23 changes: 13 additions & 10 deletions application/api/internal/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from flask import Blueprint, request, send_from_directory
from werkzeug.utils import secure_filename
from bson.objectid import ObjectId

import logging
from application.core.mongo_db import MongoDB
from application.core.settings import settings
from application.storage.storage_creator import StorageCreator


logger = logging.getLogger(__name__)
mongo = MongoDB.get_client()
db = mongo["docsgpt"]
conversations_collection = db["conversations"]
Expand Down Expand Up @@ -45,26 +48,26 @@
remote_data = request.form["remote_data"] if "remote_data" in request.form else None
sync_frequency = secure_filename(request.form["sync_frequency"]) if "sync_frequency" in request.form else None

save_dir = os.path.join(current_dir, "indexes", str(id))
storage = StorageCreator.get_storage()
index_base_path = f"indexes/{id}"

Check warning on line 52 in application/api/internal/routes.py

View check run for this annotation

Codecov / codecov/patch

application/api/internal/routes.py#L51-L52

Added lines #L51 - L52 were not covered by tests

if settings.VECTOR_STORE == "faiss":
if "file_faiss" not in request.files:
print("No file part")
logger.error("No file_faiss part")

Check warning on line 56 in application/api/internal/routes.py

View check run for this annotation

Codecov / codecov/patch

application/api/internal/routes.py#L56

Added line #L56 was not covered by tests
return {"status": "no file"}
file_faiss = request.files["file_faiss"]
if file_faiss.filename == "":
return {"status": "no file name"}
if "file_pkl" not in request.files:
print("No file part")
logger.error("No file_pkl part")

Check warning on line 62 in application/api/internal/routes.py

View check run for this annotation

Codecov / codecov/patch

application/api/internal/routes.py#L62

Added line #L62 was not covered by tests
return {"status": "no file"}
file_pkl = request.files["file_pkl"]
if file_pkl.filename == "":
return {"status": "no file name"}
# saves index files

if not os.path.exists(save_dir):
os.makedirs(save_dir)
file_faiss.save(os.path.join(save_dir, "index.faiss"))
file_pkl.save(os.path.join(save_dir, "index.pkl"))

# Save index files to storage
storage.save_file(file_faiss, f"{index_base_path}/index.faiss")
storage.save_file(file_pkl, f"{index_base_path}/index.pkl")

Check warning on line 70 in application/api/internal/routes.py

View check run for this annotation

Codecov / codecov/patch

application/api/internal/routes.py#L69-L70

Added lines #L69 - L70 were not covered by tests

existing_entry = sources_collection.find_one({"_id": ObjectId(id)})
if existing_entry:
Expand Down
Loading