Skip to content

Commit

Permalink
Retrying if ToolSelector fails to select a tool (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbraza authored Sep 11, 2024
1 parent 6256550 commit 8905157
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ repos:
additional_dependencies:
- aiohttp
- fhaviary[llm]>=0.6 # Match pyproject.toml
- ldp>=0.4 # Match pyproject.toml
- ldp>=0.6 # Match pyproject.toml
- html2text
- httpx
- numpy
Expand Down
17 changes: 15 additions & 2 deletions paperqa/agents/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pydoc import locate
from typing import TYPE_CHECKING, Any, cast

from aviary.message import Message
from aviary.message import MalformedMessageError, Message
from aviary.tools import (
Tool,
ToolCall,
Expand All @@ -14,6 +14,12 @@
ToolSelector,
)
from pydantic import BaseModel, Field, TypeAdapter
from tenacity import (
Retrying,
before_sleep_log,
retry_if_exception_type,
stop_after_attempt,
)

try:
from ldp.agent import (
Expand Down Expand Up @@ -317,7 +323,14 @@ async def run_aviary_agent(

while not done:
agent_state.messages += obs
action = await agent(agent_state.messages, tools)
for attempt in Retrying(
stop=stop_after_attempt(5),
retry=retry_if_exception_type(MalformedMessageError),
before_sleep=before_sleep_log(logger, logging.WARNING),
reraise=True,
):
with attempt: # Retrying if ToolSelector fails to select a tool
action = await agent(agent_state.messages, tools)
agent_state.messages = [*agent_state.messages, action]
if on_agent_action_callback:
await on_agent_action_callback(action, agent_state)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ requires-python = ">=3.11"

[project.optional-dependencies]
ldp = [
"ldp>=0.4",
"ldp>=0.6", # For LiteLLM 1.42.1 pinning's UnsupportedParamsError fix
]
typing = [
"types-PyYAML",
Expand Down
10 changes: 5 additions & 5 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8905157

Please sign in to comment.