From cd454dd780f19a232649458697b5fc1894699c89 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Mon, 16 Sep 2024 11:37:24 -0700 Subject: [PATCH] update clarity --- backend/danswer/chat/process_message.py | 6 --- backend/danswer/llm/utils.py | 53 ++-------------------- web/src/app/chat/AIMessageSequenceUtils.ts | 10 +++- 3 files changed, 12 insertions(+), 57 deletions(-) diff --git a/backend/danswer/chat/process_message.py b/backend/danswer/chat/process_message.py index 0474f84c4..0032a770b 100644 --- a/backend/danswer/chat/process_message.py +++ b/backend/danswer/chat/process_message.py @@ -634,12 +634,6 @@ def stream_chat_message_objects( ) # LLM prompt building, response capturing, etc. - logger.info(f"Is connected function: {is_connected}") - if is_connected: - logger.debug(f"Is connected function type: {type(is_connected)}") - logger.debug(f"Is connected function callable: {callable(is_connected)}") - else: - logger.warning("Is connected function is None or falsy") answer = Answer( is_connected=is_connected, question=final_msg.message, diff --git a/backend/danswer/llm/utils.py b/backend/danswer/llm/utils.py index 7383e5d72..05ac7f76c 100644 --- a/backend/danswer/llm/utils.py +++ b/backend/danswer/llm/utils.py @@ -99,29 +99,6 @@ def litellm_exception_to_error_msg(e: Exception, llm: LLM) -> str: return error_msg -# def translate_danswer_msg_to_langchain( -# msg: Union[ChatMessage, "PreviousMessage"], -# ) -> BaseMessage: -# files: list[InMemoryChatFile] = [] - -# # If the message is a `ChatMessage`, it doesn't have the downloaded files -# # attached. Just ignore them for now. Also, OpenAI doesn't allow files to -# # be attached to AI messages, so we must remove them -# if not isinstance(msg, ChatMessage) and msg.message_type != MessageType.ASSISTANT: -# files = msg.files -# content = build_content_with_imgs(msg.message, files) - -# if msg.message_type == MessageType.SYSTEM: -# raise ValueError("System messages are not currently part of history") -# if msg.message_type == MessageType.ASSISTANT: -# return AIMessage(content=content) -# if msg.message_type == MessageType.USER: -# return HumanMessage(content=content) - -# raise ValueError(f"New message type {msg.message_type} not handled") - - -# TODO This is quite janky def translate_danswer_msg_to_langchain( msg: Union[ChatMessage, "PreviousMessage"], ) -> BaseMessage: @@ -136,37 +113,13 @@ def translate_danswer_msg_to_langchain( if msg.message_type == MessageType.SYSTEM: return SystemMessage(content=content) - wrapped_content = "" if msg.message_type == MessageType.ASSISTANT: - try: - parsed_content = ( - json.loads(content) if isinstance(content, str) else content - ) - if ( - "name" in parsed_content - and parsed_content["name"] == "run_image_generation" - ): - wrapped_content += f"I, the AI, am now generating an \ - image based on the prompt: '{parsed_content['args']['prompt']}'\n" - wrapped_content += "[/AI IMAGE GENERATION REQUEST]" - elif ( - "id" in parsed_content - and parsed_content["id"] == "image_generation_response" - ): - wrapped_content += "I, the AI, have generated the following image(s) based on the previous request:\n" - for img in parsed_content["response"]: - wrapped_content += f"- Description: {img['revised_prompt']}\n" - wrapped_content += f" Image URL: {img['url']}\n\n" - wrapped_content += "[/AI IMAGE GENERATION RESPONSE]" - else: - wrapped_content = str(content) - except json.JSONDecodeError: - wrapped_content = str(content) - return AIMessage(content=wrapped_content) - + return AIMessage(content=content) if msg.message_type == MessageType.USER: return HumanMessage(content=content) + raise ValueError(f"New message type {msg.message_type} not handled") + def translate_history_to_basemessages( history: list[ChatMessage] | list["PreviousMessage"], diff --git a/web/src/app/chat/AIMessageSequenceUtils.ts b/web/src/app/chat/AIMessageSequenceUtils.ts index 6e7544616..021891573 100644 --- a/web/src/app/chat/AIMessageSequenceUtils.ts +++ b/web/src/app/chat/AIMessageSequenceUtils.ts @@ -1,8 +1,12 @@ -// For handling AI message `sequences` (ie. ai messages which are streamed in sequence as separate messags but are in reality one message) +// This module handles AI message sequences - consecutive AI messages that are streamed +// separately but represent a single logical message. These utilities are used for +// processing and displaying such sequences in the chat interface. import { Message } from "@/app/chat/interfaces"; import { DanswerDocument } from "@/lib/search/interfaces"; +// Retrieves the consecutive AI messages at the end of the message history. +// This is useful for combining or processing the latest AI response sequence. export function getConsecutiveAIMessagesAtEnd( messageHistory: Message[] ): Message[] { @@ -16,6 +20,10 @@ export function getConsecutiveAIMessagesAtEnd( } return aiMessages; } + +// Extracts unique documents from a sequence of AI messages. +// This is used to compile a comprehensive list of referenced documents +// across multiple parts of an AI response. export function getUniqueDocumentsFromAIMessages( messages: Message[] ): DanswerDocument[] {