Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with summarization Error for images #910

Merged
merged 5 commits into from
Mar 20, 2025
Merged
Changes from all commits
Commits
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
35 changes: 28 additions & 7 deletions src/codegen/extensions/langchain/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
self.config = config
self.max_messages = config.get("max_messages", 100) if config else 100
self.keep_first_messages = config.get("keep_first_messages", 1) if config else 1
self.store = InMemoryBaseStore()

Check failure on line 97 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Need type annotation for "store" [var-annotated]

# =================================== NODES ====================================

Expand Down Expand Up @@ -155,34 +155,55 @@

# Format messages with appropriate headers
formatted_messages = []
for msg in to_summarize: # No need for slice when iterating full list
image_urls = [] # Track image URLs for the summary prompt

for msg in to_summarize:
if isinstance(msg, HumanMessage):
formatted_messages.append(format_header("human") + msg.content)
# Now we know content is always a list
for item in msg.content:
if item.get("type") == "text":

Check failure on line 164 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "str" of "str | dict[Any, Any]" has no attribute "get" [union-attr]
text_content = item.get("text", "")

Check failure on line 165 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "str" of "str | dict[Any, Any]" has no attribute "get" [union-attr]
if text_content:
formatted_messages.append(format_header("human") + text_content)
elif item.get("type") == "image_url":

Check failure on line 168 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "str" of "str | dict[Any, Any]" has no attribute "get" [union-attr]
image_url = item.get("image_url", {}).get("url")

Check failure on line 169 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "str" of "str | dict[Any, Any]" has no attribute "get" [union-attr]
if image_url:
# We are not including any string data in the summary for image. The image will be present itself!
image_urls.append({"type": "image_url", "image_url": {"url": image_url}})
elif isinstance(msg, AIMessage):
# Check for summary message using additional_kwargs
if msg.additional_kwargs.get("is_summary"):
formatted_messages.append(format_header("summary") + msg.content)

Check failure on line 176 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Unsupported operand types for + ("str" and "list[str | dict[Any, Any]]") [operator]
elif isinstance(msg.content, list) and len(msg.content) > 0 and isinstance(msg.content[0], dict):
for item in msg.content: # No need for slice when iterating full list
for item in msg.content:
if item.get("type") == "text":

Check failure on line 179 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "str" of "str | dict[Any, Any]" has no attribute "get" [union-attr]
formatted_messages.append(format_header("ai") + item["text"])

Check failure on line 180 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Invalid index type "str" for "str"; expected type "SupportsIndex | slice[Any, Any, Any]" [index]
elif item.get("type") == "tool_use":

Check failure on line 181 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "str" of "str | dict[Any, Any]" has no attribute "get" [union-attr]
formatted_messages.append(format_header("tool_call") + f"Tool: {item['name']}\nInput: {item['input']}")

Check failure on line 182 in src/codegen/extensions/langchain/graph.py

View workflow job for this annotation

GitHub Actions / mypy

error: Invalid index type "str" for "str"; expected type "SupportsIndex | slice[Any, Any, Any]" [index]
else:
formatted_messages.append(format_header("ai") + msg.content)
elif isinstance(msg, ToolMessage):
formatted_messages.append(format_header("tool_response") + msg.content)

conversation = "\n".join(formatted_messages) # No need for slice when joining full list
conversation = "\n".join(formatted_messages)

summary_llm = LLM(
model_provider="anthropic",
model_name="claude-3-5-sonnet-latest",
temperature=0.3,
)

chain = ChatPromptTemplate.from_template(SUMMARIZE_CONVERSATION_PROMPT) | summary_llm
new_summary = chain.invoke({"conversation": conversation}).content
# Choose template based on whether we have images
summarizer_content = [{"type": "text", "text": SUMMARIZE_CONVERSATION_PROMPT}]
for image_url in image_urls:
summarizer_content.append(image_url)
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to summarize the images?


chain = ChatPromptTemplate([("human", summarizer_content)]) | summary_llm
new_summary = chain.invoke(
{
"conversation": conversation,
}
).content

return {"messages": {"type": "summarize", "summary": new_summary, "tail": tail, "head": head}}

Expand All @@ -199,7 +220,7 @@
return "summarize_conversation"

# Summarize if the last message exceeds the max input tokens of the model - 10000 tokens
elif isinstance(last_message, AIMessage) and not just_summarized and curr_input_tokens > (max_input_tokens - 10000):
elif isinstance(last_message, AIMessage) and not just_summarized and curr_input_tokens > (max_input_tokens - 30000):
return "summarize_conversation"

elif hasattr(last_message, "tool_calls") and last_message.tool_calls:
Expand Down