-
Notifications
You must be signed in to change notification settings - Fork 44.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blocks(exa): Add more Exa blocks (#9097)
Revamp the Exa search block and add two more for Content and Similarity search. ### Changes 🏗️ - Updated the exa search block input names to be snakecase not camel case - Added Advanced to non required fields - Pulled Content settings into helpers for reuse across blocks - Updated customnode.css to handle long inputs, especially in the case of the date input ### Checklist 📋 #### For code changes: - [ ] I have clearly listed my changes in the PR description - [ ] I have made a test plan - [ ] I have tested my changes according to the test plan: <!-- Put your test plan here: --> - [ ] ...
- Loading branch information
1 parent
a8339d0
commit 54f8d3b
Showing
5 changed files
with
381 additions
and
58 deletions.
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,87 @@ | ||
from typing import List, Optional | ||
|
||
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 ContentRetrievalSettings(BaseModel): | ||
text: Optional[dict] = SchemaField( | ||
description="Text content settings", | ||
default={"maxCharacters": 1000, "includeHtmlTags": False}, | ||
advanced=True, | ||
) | ||
highlights: Optional[dict] = SchemaField( | ||
description="Highlight settings", | ||
default={ | ||
"numSentences": 3, | ||
"highlightsPerUrl": 3, | ||
"query": "", | ||
}, | ||
advanced=True, | ||
) | ||
summary: Optional[dict] = SchemaField( | ||
description="Summary settings", | ||
default={"query": ""}, | ||
advanced=True, | ||
) | ||
|
||
|
||
class ExaContentsBlock(Block): | ||
class Input(BlockSchema): | ||
credentials: ExaCredentialsInput = ExaCredentialsField() | ||
ids: List[str] = SchemaField( | ||
description="Array of document IDs obtained from searches", | ||
) | ||
contents: ContentRetrievalSettings = SchemaField( | ||
description="Content retrieval settings", | ||
default=ContentRetrievalSettings(), | ||
advanced=True, | ||
) | ||
|
||
class Output(BlockSchema): | ||
results: list = SchemaField( | ||
description="List of document contents", | ||
default=[], | ||
) | ||
|
||
def __init__(self): | ||
super().__init__( | ||
id="c52be83f-f8cd-4180-b243-af35f986b461", | ||
description="Retrieves document contents using Exa's contents API", | ||
categories={BlockCategory.SEARCH}, | ||
input_schema=ExaContentsBlock.Input, | ||
output_schema=ExaContentsBlock.Output, | ||
) | ||
|
||
def run( | ||
self, input_data: Input, *, credentials: ExaCredentials, **kwargs | ||
) -> BlockOutput: | ||
url = "https://api.exa.ai/contents" | ||
headers = { | ||
"Content-Type": "application/json", | ||
"x-api-key": credentials.api_key.get_secret_value(), | ||
} | ||
|
||
payload = { | ||
"ids": input_data.ids, | ||
"text": input_data.contents.text, | ||
"highlights": input_data.contents.highlights, | ||
"summary": input_data.contents.summary, | ||
} | ||
|
||
try: | ||
response = requests.post(url, headers=headers, json=payload) | ||
response.raise_for_status() | ||
data = response.json() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from backend.data.model import SchemaField | ||
|
||
|
||
class TextSettings(BaseModel): | ||
max_characters: int = SchemaField( | ||
default=1000, | ||
description="Maximum number of characters to return", | ||
placeholder="1000", | ||
) | ||
include_html_tags: bool = SchemaField( | ||
default=False, | ||
description="Whether to include HTML tags in the text", | ||
placeholder="False", | ||
) | ||
|
||
|
||
class HighlightSettings(BaseModel): | ||
num_sentences: int = SchemaField( | ||
default=3, | ||
description="Number of sentences per highlight", | ||
placeholder="3", | ||
) | ||
highlights_per_url: int = SchemaField( | ||
default=3, | ||
description="Number of highlights per URL", | ||
placeholder="3", | ||
) | ||
|
||
|
||
class SummarySettings(BaseModel): | ||
query: Optional[str] = SchemaField( | ||
default="", | ||
description="Query string for summarization", | ||
placeholder="Enter query", | ||
) | ||
|
||
|
||
class ContentSettings(BaseModel): | ||
text: TextSettings = SchemaField( | ||
default=TextSettings(), | ||
description="Text content settings", | ||
) | ||
highlights: HighlightSettings = SchemaField( | ||
default=HighlightSettings(), | ||
description="Highlight settings", | ||
) | ||
summary: SummarySettings = SchemaField( | ||
default=SummarySettings(), | ||
description="Summary settings", | ||
) |
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.