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

Retrying if ToolSelector fails to select a tool #373

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
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
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.