Skip to content

Commit

Permalink
fix(integrations): General upkeep + tenacity retry
Browse files Browse the repository at this point in the history
Signed-off-by: Diwank Singh Tomer <[email protected]>
  • Loading branch information
creatorrr committed Oct 19, 2024
1 parent e341fb1 commit ffd0f76
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 124 deletions.
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
38 changes: 19 additions & 19 deletions integrations-service/poetry.lock

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

1 change: 1 addition & 0 deletions integrations-service/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spider-client = "^0.0.70"
browserbase = "^0.3.0"
setuptools = "^75.1.0"
beartype = "^0.19.0"
tenacity = "^9.0.0"

[tool.poe.tasks]
format = "ruff format"
Expand Down

0 comments on commit ffd0f76

Please sign in to comment.