Skip to content

Commit

Permalink
Escape markdown tags from print_response
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Feb 5, 2025
1 parent a02d2af commit 00925a1
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 12 deletions.
1 change: 1 addition & 0 deletions cookbook/models/groq/reasoning/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmp
Empty file.
7 changes: 7 additions & 0 deletions cookbook/models/groq/reasoning/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from agno.agent import Agent
from agno.models.groq import Groq

agent = Agent(model=Groq(id="deepseek-r1-distill-llama-70b-specdec"), markdown=True)

# Print the response on the terminal
agent.print_response("Share a 2 sentence horror story")
7 changes: 7 additions & 0 deletions cookbook/models/groq/reasoning/basic_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from agno.agent import Agent
from agno.models.groq import Groq

agent = Agent(model=Groq(id="deepseek-r1-distill-llama-70b-specdec"), markdown=True)

# Print the response on the terminal
agent.print_response("Share a 2 sentence horror story", stream=True)
30 changes: 30 additions & 0 deletions cookbook/models/groq/reasoning/finance_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from agno.agent import Agent
from agno.models.groq import Groq
from agno.tools.yfinance import YFinanceTools

# Create an Agent with Groq and YFinanceTools
finance_agent = Agent(
model=Groq(id="deepseek-r1-distill-llama-70b-specdec"),
tools=[
YFinanceTools(
stock_price=True,
analyst_recommendations=True,
stock_fundamentals=True,
company_info=True,
)
],
description="You are an investment analyst with deep expertise in market analysis",
instructions=[
"Use tables to display data where possible.",
"Always call the tool before you answer.",
],
add_datetime_to_instructions=True,
show_tool_calls=True, # Uncomment to see tool calls in the response
markdown=True,
)

# Example usage
finance_agent.print_response(
"Write a report on NVDA with stock price, analyst recommendations, and stock fundamentals.",
stream=True,
)
15 changes: 15 additions & 0 deletions cookbook/models/groq/reasoning/web_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from agno.agent import Agent
from agno.models.groq import Groq
from agno.tools.duckduckgo import DuckDuckGoTools

# Initialize the agent with the Groq model and tools for DuckDuckGo
agent = Agent(
model=Groq(id="deepseek-r1-distill-llama-70b-specdec"),
description="You are an enthusiastic news reporter with a flair for storytelling!",
tools=[DuckDuckGoTools()], # Add DuckDuckGo tool to search the web
show_tool_calls=True, # Shows tool calls in the response, set to False to hide
markdown=True, # Format responses in markdown
)

# Prompt the agent to fetch a breaking news story from New York
agent.print_response("Tell me about a breaking news story from New York.", stream=True)
52 changes: 40 additions & 12 deletions libs/agno/agno/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Literal,
Optional,
Sequence,
Set,
Type,
Union,
cast,
Expand Down Expand Up @@ -3211,6 +3212,16 @@ def create_panel(self, content, title, border_style="blue"):
content, title=title, title_align="left", border_style=border_style, box=HEAVY, expand=True, padding=(1, 1)
)

def escape_markdown_tags(self, content: str, tags: Set[str]) -> str:
"""Escape special tags in markdown content."""
escaped_content = content
for tag in tags:
# Escape opening tag
escaped_content = escaped_content.replace(f"<{tag}>", f"&lt;{tag}&gt;")
# Escape closing tag
escaped_content = escaped_content.replace(f"</{tag}>", f"&lt;/{tag}&gt;")
return escaped_content

def print_response(
self,
message: Optional[Union[List, Dict, str, Message]] = None,
Expand All @@ -3225,6 +3236,8 @@ def print_response(
show_reasoning: bool = True,
show_full_reasoning: bool = False,
console: Optional[Any] = None,
# Add tags to include in markdown content
tags_to_include_in_markdown: Set[str] = {"think", "thinking"},
**kwargs: Any,
) -> None:
import json
Expand Down Expand Up @@ -3279,7 +3292,11 @@ def print_response(
if resp.extra_data is not None and resp.extra_data.reasoning_steps is not None:
reasoning_steps = resp.extra_data.reasoning_steps

response_content_stream = Markdown(_response_content) if self.markdown else _response_content
response_content_stream: Union[str, Markdown] = _response_content
# Escape special tags before markdown conversion
if self.markdown:
escaped_content = self.escape_markdown_tags(_response_content, tags_to_include_in_markdown)
response_content_stream = Markdown(escaped_content)

panels = [status]

Expand Down Expand Up @@ -3418,11 +3435,13 @@ def print_response(
response_content_batch: Union[str, JSON, Markdown] = ""
if isinstance(run_response, RunResponse):
if isinstance(run_response.content, str):
response_content_batch = (
Markdown(run_response.content)
if self.markdown
else run_response.get_content_as_string(indent=4)
)
if self.markdown:
escaped_content = self.escape_markdown_tags(
run_response.content, tags_to_include_in_markdown
)
response_content_batch = Markdown(escaped_content)
else:
response_content_batch = run_response.get_content_as_string(indent=4)
elif self.response_model is not None and isinstance(run_response.content, BaseModel):
try:
response_content_batch = JSON(
Expand Down Expand Up @@ -3462,6 +3481,8 @@ async def aprint_response(
show_reasoning: bool = True,
show_full_reasoning: bool = False,
console: Optional[Any] = None,
# Add tags to include in markdown content
tags_to_include_in_markdown: Set[str] = {"think", "thinking"},
**kwargs: Any,
) -> None:
import json
Expand Down Expand Up @@ -3516,7 +3537,12 @@ async def aprint_response(
_response_content += resp.content
if resp.extra_data is not None and resp.extra_data.reasoning_steps is not None:
reasoning_steps = resp.extra_data.reasoning_steps
response_content_stream = Markdown(_response_content) if self.markdown else _response_content

response_content_stream: Union[str, Markdown] = _response_content
# Escape special tags before markdown conversion
if self.markdown:
escaped_content = self.escape_markdown_tags(_response_content, tags_to_include_in_markdown)
response_content_stream = Markdown(escaped_content)

panels = [status]

Expand Down Expand Up @@ -3655,11 +3681,13 @@ async def aprint_response(
response_content_batch: Union[str, JSON, Markdown] = ""
if isinstance(run_response, RunResponse):
if isinstance(run_response.content, str):
response_content_batch = (
Markdown(run_response.content)
if self.markdown
else run_response.get_content_as_string(indent=4)
)
if self.markdown:
escaped_content = self.escape_markdown_tags(
run_response.content, tags_to_include_in_markdown
)
response_content_batch = Markdown(escaped_content)
else:
response_content_batch = run_response.get_content_as_string(indent=4)
elif self.response_model is not None and isinstance(run_response.content, BaseModel):
try:
response_content_batch = JSON(
Expand Down

0 comments on commit 00925a1

Please sign in to comment.