Skip to content

Commit

Permalink
fix(agents-api,integrations): Misc fixes and tenacity retry in integr…
Browse files Browse the repository at this point in the history
…ations" (#710)

- **fix(integrations): General upkeep + tenacity retry**
- **feat(agents-api): Misc fixes in agents-api**

<!-- ELLIPSIS_HIDDEN -->


----

> [!IMPORTANT]
> Add `tenacity` retry logic to integration functions, remove unused
integrations, and improve agents-api and integrations services.
> 
>   - **Integrations**:
> - Add `tenacity` retry logic to `search()` in `brave.py`, `load()` in
`browserbase.py`, `send()` in `email.py`, `crawl()` in `spider.py`,
`get()` in `weather.py`, and `search()` in `wikipedia.py`.
> - Remove unused integrations: `dalle_image_generator.py`,
`duckduckgo_search.py`, `gmail/send_mail.py`, `hacker_news.py`, and
`request.py`.
>     - Remove `hacker_news` provider from `providers.py`.
>   - **Agents API**:
> - Update `log_step()` in `log_step.py` to include
`include_remote=True` in `context.model_dump()`.
>     - Add `NEWLINE` constant to `utils.py` and `template.py`.
> - Add `ApplicationError` to non-retryable exceptions in
`interceptors.py`.
> - Increase sleep time in `test_doc()` in `fixtures.py` from 0.1 to 0.5
seconds.
>   - **Misc**:
> - Remove unused imports in `base_models.py`, `email.py`,
`wikipedia.py`, `get_integration.py`, and `execute_integration.py`.
>     - Add `tenacity` to `pyproject.toml` dependencies.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=julep-ai%2Fjulep&utm_source=github&utm_medium=referral)<sup>
for 55fe24a. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->

---------

Signed-off-by: Diwank Singh Tomer <[email protected]>
  • Loading branch information
creatorrr authored Oct 19, 2024
1 parent 69532b5 commit 12f8698
Show file tree
Hide file tree
Showing 27 changed files with 92 additions and 132 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/translate-readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ jobs:
git add README-*.md
git commit -m "chore(readme): translate README.md"
- name: Push changes
uses: ad-m/[email protected]
- name: Create Pull Request
uses: peter-evans/create-pull-request@v7
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "chore(readme): translate README.md"
title: "Translate README.md"
body: "This PR updates the translated versions of README.md"
branch: "translate-readme"
base: dev
add-paths: |
README.md
README-*.md
4 changes: 3 additions & 1 deletion agents-api/agents_api/activities/task_steps/log_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ async def log_step(context: StepContext) -> StepOutcome:

template: str = context.current_step.log
output = await render_template(
template, context.model_dump(), skip_vars=["developer_id"]
template,
context.model_dump(include_remote=True),
skip_vars=["developer_id"],
)

result = StepOutcome(output=output)
Expand Down
7 changes: 6 additions & 1 deletion agents-api/agents_api/activities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,18 @@ class stdlib_statistics:
"statistics": stdlib_statistics,
}

constants = {
"NEWLINE": "\n",
}


@beartype
def get_evaluator(
names: dict[str, Any], extra_functions: dict[str, Callable] | None = None
) -> SimpleEval:
evaluator = EvalWithCompoundTypes(
names=names | stdlib, functions=ALLOWED_FUNCTIONS | (extra_functions or {})
names=names | stdlib | constants,
functions=ALLOWED_FUNCTIONS | (extra_functions or {}),
)

return evaluator
Expand Down
2 changes: 2 additions & 0 deletions agents-api/agents_api/common/interceptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async def execute_activity(self, input: ExecuteActivityInput):
CompleteAsyncError,
TemporalError,
FailureError,
ApplicationError,
):
raise
except BaseException as e:
Expand Down Expand Up @@ -78,6 +79,7 @@ async def execute_workflow(self, input: ExecuteWorkflowInput):
CompleteAsyncError,
TemporalError,
FailureError,
ApplicationError,
):
raise
except BaseException as e:
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/common/utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
jinja_env.globals["true"] = True
jinja_env.globals["false"] = False
jinja_env.globals["null"] = None

jinja_env.globals["NEWLINE"] = "\n"

simple_jinja_regex = re.compile(r"{{|{%.+}}|%}", re.DOTALL)

Expand Down
2 changes: 1 addition & 1 deletion agents-api/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_doc(
client=client,
)

time.sleep(0.1)
time.sleep(0.5)

yield doc

Expand Down
4 changes: 2 additions & 2 deletions integrations-service/integrations/models/base_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Annotated, Any, Optional
from typing import Annotated, Optional

from pydantic import BaseModel, Field, RootModel
from pydantic import BaseModel, Field
from pydantic_core import Url

IdentifierName = Annotated[str, Field(max_length=40, pattern="^[^\\W0-9]\\w*$")]
Expand Down
4 changes: 3 additions & 1 deletion integrations-service/integrations/models/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class EmailSetup(BaseSetup):

class EmailArguments(BaseArguments):
to: EmailStr = Field(..., description="The email address to send the email to")
from_: EmailStr = Field(..., alias="from", description="The email address to send the email from")
from_: EmailStr = Field(
..., alias="from", description="The email address to send the email from"
)
subject: str = Field(..., description="The subject of the email")
body: str = Field(..., description="The body of the email")

Expand Down
2 changes: 0 additions & 2 deletions integrations-service/integrations/models/wikipedia.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Literal

from langchain_core.documents import Document
from pydantic import Field

Expand Down
22 changes: 0 additions & 22 deletions integrations-service/integrations/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
EmailArguments,
EmailOutput,
EmailSetup,
HackerNewsFetchArguments,
HackerNewsFetchOutput,
ProviderInfo,
SpiderFetchArguments,
SpiderFetchOutput,
Expand Down Expand Up @@ -61,25 +59,6 @@
),
)

hacker_news = BaseProvider(
provider="hacker_news",
setup=None,
methods=[
BaseProviderMethod(
method="fetch",
description="Get the top stories from Hacker News",
arguments=HackerNewsFetchArguments,
output=HackerNewsFetchOutput,
),
],
info=ProviderInfo(
url="https://news.ycombinator.com/",
docs="https://news.ycombinator.com/newsguidelines.html",
icon="https://news.ycombinator.com/favicon.ico",
friendly_name="Hacker News",
),
)

spider = BaseProvider(
provider="spider",
setup=SpiderSetup,
Expand Down Expand Up @@ -156,7 +135,6 @@
providers = {
"wikipedia": wikipedia,
"weather": weather,
"hacker_news": hacker_news,
"spider": spider,
"brave": brave,
"browserbase": browserbase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ async def execute(


@router.post("/execute/{provider}/{method}", tags=["execution"])
def execute(
async def execute(
provider: IdentifierName,
method: IdentifierName,
data: ExecutionRequest,
) -> ExecutionResponse:
try:
return execute_integration(
return await execute_integration(
provider=provider, arguments=data.arguments, setup=data.setup, method=method
)
except ValueError as e:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List

from ...providers import providers
from .router import router

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def execute_integration(
setup = setup_class(**setup.model_dump())

arguments_class = next(m for m in provider.methods if m.method == method).arguments

if not isinstance(arguments, arguments_class):
parsed_arguments = arguments_class(**arguments.model_dump())
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .brave import search
from .browserbase import load
from .email import send
from .hacker_news import fetch
from .spider import crawl
from .weather import get
from .wikipedia import search
6 changes: 6 additions & 0 deletions integrations-service/integrations/utils/integrations/brave.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from langchain_community.tools import BraveSearch
from tenacity import retry, stop_after_attempt, wait_exponential

from ...models import BraveSearchArguments, BraveSearchOutput, BraveSearchSetup


@retry(
wait=wait_exponential(multiplier=1, min=4, max=10),
reraise=True,
stop=stop_after_attempt(4),
)
async def search(
setup: BraveSearchSetup, arguments: BraveSearchArguments
) -> BraveSearchOutput:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from langchain_community.document_loaders import BrowserbaseLoader
from tenacity import retry, stop_after_attempt, wait_exponential

from ...models import BrowserBaseLoadArguments, BrowserBaseLoadOutput, BrowserBaseSetup


@retry(
wait=wait_exponential(multiplier=1, min=4, max=10),
reraise=True,
stop=stop_after_attempt(3),
)
async def load(
setup: BrowserBaseSetup, arguments: BrowserBaseLoadArguments
) -> BrowserBaseLoadOutput:
Expand Down

This file was deleted.

This file was deleted.

12 changes: 7 additions & 5 deletions integrations-service/integrations/utils/integrations/email.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from email.message import EmailMessage
from smtplib import SMTP

from beartype import beartype
from tenacity import retry, stop_after_attempt, wait_exponential

from ...models import EmailArguments, EmailOutput, EmailSetup


# @beartype
async def send(
setup: EmailSetup, arguments: EmailArguments
) -> EmailOutput:
@retry(
wait=wait_exponential(multiplier=1, min=4, max=10),
reraise=True,
stop=stop_after_attempt(4),
)
async def send(setup: EmailSetup, arguments: EmailArguments) -> EmailOutput:
"""
Sends an email with the provided details.
"""
Expand Down

This file was deleted.

This file was deleted.

Empty file.
10 changes: 9 additions & 1 deletion integrations-service/integrations/utils/integrations/spider.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from langchain_community.document_loaders import SpiderLoader
from tenacity import retry, stop_after_attempt, wait_exponential

from ...models import SpiderFetchArguments, SpiderFetchOutput, SpiderSetup


async def crawl(setup: SpiderSetup, arguments: SpiderFetchArguments) -> SpiderFetchOutput:
@retry(
wait=wait_exponential(multiplier=1, min=4, max=10),
reraise=True,
stop=stop_after_attempt(4),
)
async def crawl(
setup: SpiderSetup, arguments: SpiderFetchArguments
) -> SpiderFetchOutput:
"""
Fetches data from a specified URL.
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from langchain_community.utilities import OpenWeatherMapAPIWrapper
from tenacity import retry, stop_after_attempt, wait_exponential

from ...models import WeatherGetArguments, WeatherGetOutput, WeatherSetup


@retry(
wait=wait_exponential(multiplier=1, min=4, max=10),
reraise=True,
stop=stop_after_attempt(4),
)
async def get(setup: WeatherSetup, arguments: WeatherGetArguments) -> WeatherGetOutput:
"""
Fetches weather data for a specified location using OpenWeatherMap API.
Expand All @@ -20,4 +26,3 @@ async def get(setup: WeatherSetup, arguments: WeatherGetArguments) -> WeatherGet
weather = OpenWeatherMapAPIWrapper(openweathermap_api_key=openweathermap_api_key)
result = weather.run(location)
return WeatherGetOutput(result=result)

Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from langchain_community.document_loaders import WikipediaLoader
from tenacity import retry, stop_after_attempt, wait_exponential

from ...models import WikipediaSearchArguments, WikipediaSearchOutput


@retry(
wait=wait_exponential(multiplier=1, min=4, max=10),
reraise=True,
stop=stop_after_attempt(4),
)
def search(arguments: WikipediaSearchArguments) -> WikipediaSearchOutput:
"""
Searches Wikipedia for a given query and returns formatted results.
Expand Down
Loading

0 comments on commit 12f8698

Please sign in to comment.