Skip to content

Commit

Permalink
feat: 🎸 sources (#2092)
Browse files Browse the repository at this point in the history
added metadata object a bit bigger

# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
  • Loading branch information
StanGirard authored Jan 26, 2024
1 parent 7c9fdd2 commit 6c5496f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
32 changes: 21 additions & 11 deletions backend/llm/knowledge_brain_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from logger import get_logger
from models import BrainSettings
from modules.brain.service.brain_service import BrainService
from modules.chat.dto.chats import ChatQuestion
from modules.chat.dto.chats import ChatQuestion, Sources
from modules.chat.dto.inputs import CreateChatHistory
from modules.chat.dto.outputs import GetChatHistoryOutput
from modules.chat.service.chat_service import ChatService
Expand Down Expand Up @@ -296,21 +296,31 @@ async def wrap_done(fn: Awaitable, event: asyncio.Event):
logger.error("Error during streaming tokens: %s", e)
try:
result = await run
source_documents = result.get("source_documents", [])
# Deduplicate source documents
source_documents = list(
{doc.metadata["file_name"]: doc for doc in source_documents}.values()
)

sources_list: List[Sources] = []
source_documents = result.get("source_documents", [])
if source_documents:
# Formatting the source documents using Markdown without new lines for each source
sources_list = [
f"[{doc.metadata['file_name']}])" for doc in source_documents
]
serialized_sources_list = []
for doc in source_documents:
sources_list.append(
Sources(
**{
"name": doc.metadata["url"]
if "url" in doc.metadata
else doc.metadata["file_name"],
"type": "url" if "url" in doc.metadata else "file",
"source_url": doc.metadata["url"]
if "url" in doc.metadata
else "",
}
)
)
# Create metadata if it doesn't exist
if not streamed_chat_history.metadata:
streamed_chat_history.metadata = {}
streamed_chat_history.metadata["sources"] = sources_list
# Serialize the sources list
serialized_sources_list = [source.dict() for source in sources_list]
streamed_chat_history.metadata["sources"] = serialized_sources_list
yield f"data: {json.dumps(streamed_chat_history.dict())}"
else:
logger.info(
Expand Down
12 changes: 12 additions & 0 deletions backend/modules/chat/dto/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ class ChatQuestion(BaseModel):
prompt_id: Optional[UUID]


class Sources(BaseModel):
name: str
source_url: str
type: str

class Config:
json_encoders = {
**BaseModel.Config.json_encoders,
UUID: lambda v: str(v),
}


class ChatItemType(Enum):
MESSAGE = "MESSAGE"
NOTIFICATION = "NOTIFICATION"
Expand Down
10 changes: 7 additions & 3 deletions backend/modules/notification/repository/notifications.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from datetime import datetime, timedelta

from fastapi import HTTPException
from logger import get_logger
from modules.notification.dto.outputs import DeleteNotificationResponse
from modules.notification.entity.notification import Notification
from modules.notification.repository.notifications_interface import (
NotificationInterface,
)

logger = get_logger(__name__)


class Notifications(NotificationInterface):
def __init__(self, supabase_client):
Expand Down Expand Up @@ -35,7 +37,8 @@ def update_notification_by_id(
).data

if response == []:
raise HTTPException(404, "Notification not found")
logger.info(f"Notification with id {notification_id} not found")
return None

return Notification(**response[0])

Expand All @@ -57,7 +60,8 @@ def remove_notification_by_id(self, notification_id):
)

if response == []:
raise HTTPException(404, "Notification not found")
logger.info(f"Notification with id {notification_id} not found")
return None

return DeleteNotificationResponse(
status="deleted", notification_id=notification_id
Expand Down
14 changes: 1 addition & 13 deletions backend/routes/crawl_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from models import UserUsage
from modules.knowledge.dto.inputs import CreateKnowledgeProperties
from modules.knowledge.service.knowledge_service import KnowledgeService
from modules.notification.dto.inputs import CreateNotificationProperties
from modules.notification.entity.notification import NotificationsStatusEnum
from modules.notification.service.notification_service import NotificationService
from modules.user.entity.user_identity import UserIdentity
from packages.files.crawl.crawler import CrawlWebsite
Expand Down Expand Up @@ -56,16 +54,6 @@ async def crawl_endpoint(
"type": "error",
}
else:
crawl_notification_id = None
if chat_id:
crawl_notification_id = notification_service.add_notification(
CreateNotificationProperties(
action="CRAWL",
chat_id=chat_id,
status=NotificationsStatusEnum.Pending,
).id
)

knowledge_to_add = CreateKnowledgeProperties(
brain_id=brain_id,
url=crawl_website.url,
Expand All @@ -78,7 +66,7 @@ async def crawl_endpoint(
process_crawl_and_notify.delay(
crawl_website_url=crawl_website.url,
brain_id=brain_id,
notification_id=crawl_notification_id,
notification_id=None,
)

return {"message": "Crawl processing has started."}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { CopyButton } from "./components/CopyButton";
import { MessageContent } from "./components/MessageContent";
import { QuestionBrain } from "./components/QuestionBrain";
import { QuestionPrompt } from "./components/QuestionPrompt";
import { SourcesButton } from "./components/SourcesButton";
import { useMessageRow } from "./hooks/useMessageRow";

type MessageRowProps = {
Expand Down Expand Up @@ -60,7 +59,6 @@ export const MessageRow = React.forwardRef(
<div className="flex items-center gap-2">
{!isUserSpeaker && (
<>
{hasSources && <SourcesButton sources={sourcesContent} />}
<CopyButton handleCopy={handleCopy} isCopied={isCopied} />
</>
)}
Expand Down

0 comments on commit 6c5496f

Please sign in to comment.