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

fix: tool parameters #168

Merged
merged 7 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 23 additions & 8 deletions dynamiq/nodes/tools/exa_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,37 @@ class ExaInputSchema(BaseModel):

query: str = Field(description="The search query string.")
include_full_content: bool | None = Field(
default=None, description="If true, retrieve full content, highlights, and summaries for search results."
default=None,
description="If true, retrieve full content, highlights, and summaries for search results.",
is_accessible_to_agent=False,
)
use_autoprompt: bool | None = Field(
default=None, description="If true, query will be converted to a Exa query.", is_accessible_to_agent=False
)
use_autoprompt: bool | None = Field(default=None, description="If true, query will be converted to a Exa query.")
query_type: QueryType | None = Field(
default=None,
description="Type of query to be used. Options are 'keyword', 'neural', or 'auto'.",
is_accessible_to_agent=False,
)
category: str | None = Field(
default=None, description="A data category to focus on (e.g., company, research paper, news article)."
default=None,
description="A data category to focus on (e.g., company, research paper, news article).",
is_accessible_to_agent=False,
)
limit: int | None = Field(
default=None, ge=1, le=100, description="Number of search results to return.", is_accessible_to_agent=False
)
include_domains: list[str] | None = Field(
default=None, description="List of domains to include in the search.", is_accessible_to_agent=False
)
exclude_domains: list[str] | None = Field(
default=None, description="List of domains to exclude from the search.", is_accessible_to_agent=False
)
include_text: list[str] | None = Field(
default=None, description="Strings that must be present in webpage text.", is_accessible_to_agent=False
)
limit: int | None = Field(default=None, ge=1, le=100, description="Number of search results to return.")
include_domains: list[str] | None = Field(default=None, description="List of domains to include in the search.")
exclude_domains: list[str] | None = Field(default=None, description="List of domains to exclude from the search.")
include_text: list[str] | None = Field(default=None, description="Strings that must be present in webpage text.")
exclude_text: list[str] | None = Field(
default=None, description="Strings that must not be present in webpage text."
default=None, description="Strings that must not be present in webpage text.", is_accessible_to_agent=False
)


Expand Down
31 changes: 21 additions & 10 deletions dynamiq/nodes/tools/tavily.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@

class TavilyInputSchema(BaseModel):
query: str = Field(..., description="Parameter to provide a search query.")
search_depth: str | None = Field(default=None, description="The search depth to use.")
topic: str | None = Field(default=None, description="The topic to search for.")
search_depth: str | None = Field(default=None, description="The search depth to use.", is_accessible_to_agent=False)
topic: str | None = Field(default=None, description="The topic to search for.", is_accessible_to_agent=False)
max_results: int | None = Field(
default=None,
description="The maximum number of search results to return.",
default=None, description="The maximum number of search results to return.", is_accessible_to_agent=False
)
include_images: bool | None = Field(
default=None, description="Include images in search results.", is_accessible_to_agent=False
)
include_answer: bool | None = Field(
default=None, description="Include answer in search results.", is_accessible_to_agent=False
)
include_raw_content: bool | None = Field(
default=None, description="Include raw content in search results.", is_accessible_to_agent=False
)
include_domains: list[str] | None = Field(
default=None, description="The domains to include in search results.", is_accessible_to_agent=False
)
exclude_domains: list[str] | None = Field(
default=None, description="The domains to exclude from search results.", is_accessible_to_agent=False
)
use_cache: bool | None = Field(
default=None, description="Use cache for search results.", is_accessible_to_agent=False
)
include_images: bool | None = Field(default=None, description="Include images in search results.")
include_answer: bool | None = Field(default=None, description="Include answer in search results.")
include_raw_content: bool | None = Field(default=None, description="Include raw content in search results.")
include_domains: list[str] | None = Field(default=None, description="The domains to include in search results.")
exclude_domains: list[str] | None = Field(default=None, description="The domains to exclude from search results.")
use_cache: bool | None = Field(default=None, description="Use cache for search results.")


class TavilyTool(ConnectionNode):
Expand Down
100 changes: 26 additions & 74 deletions dynamiq/prompts/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ class VisionMessage(BaseModel):
content: list[VisionMessageTextContent | VisionMessageImageContent]
role: MessageRole = MessageRole.USER

def parse_bytes_to_base64(self, file_bytes: bytes) -> str:
"""
Parses file bytes in base64 format.

Args:
file_bytes (bytes): File bytes.

Returns:
str: Base64 encoded file.
"""
extension = filetype.guess_extension(file_bytes)
if not extension:
extension = "txt"

encoded_str = base64.b64encode(file_bytes).decode("utf-8")

mime_type, _ = mimetypes.guess_type(f"file.{extension}")

if mime_type is None:
mime_type = "text/plain"

return f"data:{mime_type};base64,{encoded_str}"

def parse_image_url_parameters(self, url_template: str, kwargs: dict) -> None:
"""
Converts image URL parameters in kwargs to Base64-encoded Data URLs if they contain image data.
Expand All @@ -152,18 +175,10 @@ def parse_image_url_parameters(self, url_template: str, kwargs: dict) -> None:

if isinstance(value, io.BytesIO):
image_bytes = value.getvalue()
extension = filetype.guess_extension(image_bytes)
if not extension:
raise ValueError(f"Cannot determine file type for parameter '{param}'.")
encoded_str = base64.b64encode(image_bytes).decode("utf-8")
processed_value = f"data:image/{extension};base64,{encoded_str}"
processed_value = self.parse_bytes_to_base64(image_bytes)

elif isinstance(value, bytes):
extension = filetype.guess_extension(value)
if not extension:
raise ValueError(f"Cannot determine file type for parameter '{param}'.")
encoded_str = base64.b64encode(value).decode("utf-8")
processed_value = f"data:image/{extension};base64,{encoded_str}"
processed_value = self.parse_bytes_to_base64(value)

elif isinstance(value, str):
pass # No action needed; assuming it's a regular URL or already a Data URL
Expand Down Expand Up @@ -198,7 +213,7 @@ def format_message(self, **kwargs):
raise ValueError(f"Invalid content type: {content.type}")

if len(out_msg_content) == 1 and out_msg_content[0].type == VisionMessageType.TEXT:
return Message(self.role, content=out_msg_content[0].text)
return Message(role=self.role, content=out_msg_content[0].text)

return VisionMessage(role=self.role, content=out_msg_content)

Expand Down Expand Up @@ -320,69 +335,6 @@ def get_required_parameters(self) -> set[str]:

return parameters

def parse_bytes_to_base64(self, file_bytes: bytes) -> str:
"""
Parses file bytes in base64 format.

Args:
file_bytes (bytes): File bytes.

Returns:
str: Base64 encoded file.
"""
extension = filetype.guess_extension(file_bytes)
if not extension:
extension = "txt"

encoded_str = base64.b64encode(file_bytes).decode("utf-8")

mime_type, _ = mimetypes.guess_type(f"file.{extension}")

if mime_type is None:
mime_type = "text/plain"

return f"data:{mime_type};base64,{encoded_str}"

def parse_image_url_parameters(self, url_template: str, kwargs: dict) -> None:
"""
Converts image URL parameters in kwargs to Base64-encoded Data URLs if they contain image data.

Args:
url_template (str): Jinja template for the image URL.
kwargs (dict): Dictionary of parameters to be used with the template.

Raises:
KeyError: If a required parameter is missing in kwargs.
ValueError: If the file type cannot be determined or unsupported data type is provided.
"""
template_params = self.get_parameters_for_template(url_template)

for param in template_params:
if param not in kwargs:
raise KeyError(f"Missing required parameter: '{param}'")

value = kwargs[param]

# Initialize as unchanged; will be modified if image data is detected
processed_value = value

if isinstance(value, io.BytesIO):
image_bytes = value.getvalue()
processed_value = self.parse_bytes_to_base64(image_bytes)

elif isinstance(value, bytes):
processed_value = self.parse_bytes_to_base64(value)

elif isinstance(value, str):
pass # No action needed; assuming it's a regular URL or already a Data URL

else:
# Unsupported data type for image parameter
raise ValueError(f"Unsupported data type for parameter '{param}': {type(value)}")

# Update the parameter with the processed value
kwargs[param] = processed_value

def format_messages(self, **kwargs) -> list[dict]:
"""
Formats the messages in the prompt, rendering any templates.
Expand Down
Loading