-
Notifications
You must be signed in to change notification settings - Fork 44.5k
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
feat(blocks): Add Exa API Blocks #8835
Open
aarushik93
wants to merge
10
commits into
dev
Choose a base branch
from
aarushikansal/exa-api
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+294
β0
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5150ab1
add exa api block
aarushik93 f3b4b58
update frontend
aarushik93 efc9805
update optional fields
aarushik93 f000ac6
fix linting
aarushik93 d1612bd
Merge branch 'dev' into aarushikansal/exa-api
aarushik93 48bb66f
fix imports
aarushik93 024df85
add calendar input
aarushik93 4e97e92
Merge branch 'dev' into aarushikansal/exa-api
aarushik93 49612a8
revert uneeded changes
aarushik93 26fb5ea
Merge branch 'dev' into aarushikansal/exa-api
aarushik93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Literal | ||
|
||
from pydantic import SecretStr | ||
|
||
from backend.data.model import APIKeyCredentials, CredentialsField, CredentialsMetaInput | ||
|
||
ExaCredentials = APIKeyCredentials | ||
ExaCredentialsInput = CredentialsMetaInput[ | ||
Literal["exa"], | ||
Literal["api_key"], | ||
] | ||
|
||
TEST_CREDENTIALS = APIKeyCredentials( | ||
id="01234567-89ab-cdef-0123-456789abcdef", | ||
provider="exa", | ||
api_key=SecretStr("mock-exa-api-key"), | ||
title="Mock Exa API key", | ||
expires_at=None, | ||
) | ||
|
||
TEST_CREDENTIALS_INPUT = { | ||
"provider": TEST_CREDENTIALS.provider, | ||
"id": TEST_CREDENTIALS.id, | ||
"type": TEST_CREDENTIALS.type, | ||
"title": TEST_CREDENTIALS.title, | ||
} | ||
|
||
|
||
def ExaCredentialsField() -> ExaCredentialsInput: | ||
"""Creates an Exa credentials input on a block.""" | ||
return CredentialsField( | ||
provider="exa", | ||
supported_credential_types={"api_key"}, | ||
description="The Exa integration requires an API Key.", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
from datetime import datetime | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
|
||
from backend.blocks.exa._auth import ( | ||
ExaCredentials, | ||
ExaCredentialsField, | ||
ExaCredentialsInput, | ||
) | ||
from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema | ||
from backend.data.model import SchemaField | ||
from backend.util.request import requests | ||
|
||
|
||
class ContentSettings(BaseModel): | ||
text: dict = SchemaField( | ||
description="Text content settings", | ||
default={"maxCharacters": 1000, "includeHtmlTags": False}, | ||
) | ||
highlights: dict = SchemaField( | ||
description="Highlight settings", | ||
default={"numSentences": 3, "highlightsPerUrl": 3}, | ||
) | ||
summary: dict = SchemaField( | ||
description="Summary settings", | ||
default={"query": ""}, | ||
) | ||
|
||
|
||
class ExaSearchBlock(Block): | ||
class Input(BlockSchema): | ||
credentials: ExaCredentialsInput = ExaCredentialsField() | ||
query: str = SchemaField(description="The search query") | ||
useAutoprompt: bool = SchemaField( | ||
description="Whether to use autoprompt", | ||
default=True, | ||
) | ||
type: str = SchemaField( | ||
description="Type of search", | ||
default="", | ||
) | ||
category: str = SchemaField( | ||
description="Category to search within", | ||
default="", | ||
) | ||
numResults: int = SchemaField( | ||
description="Number of results to return", | ||
default=10, | ||
) | ||
includeDomains: List[str] = SchemaField( | ||
description="Domains to include in search", | ||
default=[], | ||
) | ||
excludeDomains: List[str] = SchemaField( | ||
description="Domains to exclude from search", | ||
default=[], | ||
) | ||
startCrawlDate: datetime = SchemaField( | ||
description="Start date for crawled content", | ||
) | ||
endCrawlDate: datetime = SchemaField( | ||
description="End date for crawled content", | ||
) | ||
startPublishedDate: datetime = SchemaField( | ||
description="Start date for published content", | ||
) | ||
endPublishedDate: datetime = SchemaField( | ||
description="End date for published content", | ||
) | ||
includeText: List[str] = SchemaField( | ||
description="Text patterns to include", | ||
default=[], | ||
) | ||
excludeText: List[str] = SchemaField( | ||
description="Text patterns to exclude", | ||
default=[], | ||
) | ||
contents: ContentSettings = SchemaField( | ||
description="Content retrieval settings", | ||
default=ContentSettings(), | ||
) | ||
|
||
class Output(BlockSchema): | ||
results: list = SchemaField( | ||
description="List of search results", | ||
default=[], | ||
) | ||
|
||
def __init__(self): | ||
super().__init__( | ||
id="996cec64-ac40-4dde-982f-b0dc60a5824d", | ||
description="Searches the web using Exa's advanced search API", | ||
categories={BlockCategory.SEARCH}, | ||
input_schema=ExaSearchBlock.Input, | ||
output_schema=ExaSearchBlock.Output, | ||
) | ||
|
||
def run( | ||
self, input_data: Input, *, credentials: ExaCredentials, **kwargs | ||
) -> BlockOutput: | ||
url = "https://api.exa.ai/search" | ||
headers = { | ||
"Content-Type": "application/json", | ||
"x-api-key": credentials.api_key.get_secret_value(), | ||
} | ||
|
||
payload = { | ||
"query": input_data.query, | ||
"useAutoprompt": input_data.useAutoprompt, | ||
"numResults": input_data.numResults, | ||
"contents": { | ||
"text": {"maxCharacters": 1000, "includeHtmlTags": False}, | ||
"highlights": { | ||
"numSentences": 3, | ||
"highlightsPerUrl": 3, | ||
}, | ||
"summary": {"query": ""}, | ||
}, | ||
} | ||
|
||
# Add dates if they exist | ||
date_fields = [ | ||
"startCrawlDate", | ||
"endCrawlDate", | ||
"startPublishedDate", | ||
"endPublishedDate", | ||
] | ||
for field in date_fields: | ||
value = getattr(input_data, field, None) | ||
if value: | ||
payload[field] = value.strftime("%Y-%m-%dT%H:%M:%S.000Z") | ||
|
||
# Add other fields | ||
optional_fields = [ | ||
"type", | ||
"category", | ||
"includeDomains", | ||
"excludeDomains", | ||
"includeText", | ||
"excludeText", | ||
] | ||
|
||
for field in optional_fields: | ||
value = getattr(input_data, field) | ||
if value: # Only add non-empty values | ||
payload[field] = value | ||
|
||
try: | ||
response = requests.post(url, headers=headers, json=payload) | ||
response.raise_for_status() | ||
data = response.json() | ||
# Extract just the results array from the response | ||
yield "results", data.get("results", []) | ||
except Exception as e: | ||
yield "error", str(e) | ||
yield "results", [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jsonschema converts data time to type string with format date-time