-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
159 additions
and
124 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Web scraper tool.""" | ||
|
||
import json | ||
import logging | ||
|
||
import trafilatura | ||
from homeassistant.core import HomeAssistant | ||
|
||
from ..llm_tools import llm_tool | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def setup(hass: HomeAssistant): | ||
"""Register the tool on integration startup.""" | ||
|
||
@llm_tool(hass) | ||
def web_scrape(url: str): | ||
"""Get latest content of a web page.""" | ||
downloaded = trafilatura.fetch_url("linux.org.ru") | ||
|
||
parsed = trafilatura.extract( | ||
downloaded, | ||
output_format="json", | ||
include_links=True, | ||
deduplicate=True, | ||
favor_precision=True, | ||
) | ||
|
||
result = json.loads(parsed) | ||
|
||
if "comments" in result and not result["comments"]: | ||
del result["comments"] | ||
|
||
return result |
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 |
---|---|---|
|
@@ -21,3 +21,4 @@ ha-ffmpeg | |
pymicro-vad | ||
duckduckgo-search | ||
RestrictedPython | ||
trafilatura |
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 was deleted.
Oops, something went wrong.
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,47 @@ | ||
"""Test python script tool.""" | ||
|
||
|
||
def test_test(hass): | ||
"""Workaround for https://github.com/MatthewFlamm/pytest-homeassistant-custom-component/discussions/160.""" | ||
|
||
|
||
async def test_python_script_tool(async_call_tool) -> None: | ||
"""Test python script tool.""" | ||
|
||
source = """ | ||
output["test"] = "passed" | ||
output["test2"] = "passed2" | ||
""" | ||
|
||
response = await async_call_tool("python_code_execute", source=source) | ||
|
||
assert response == {"output": {"test": "passed", "test2": "passed2"}} | ||
|
||
|
||
async def test_python_script_tool_import(async_call_tool) -> None: | ||
"""Test python script tool with import.""" | ||
|
||
source = """ | ||
import math | ||
output["test"] = math.cos(0) | ||
""" | ||
|
||
response = await async_call_tool("python_code_execute", source=source) | ||
|
||
assert response == {"output": {"test": 1.0}} | ||
|
||
|
||
async def test_python_script_tool_print(async_call_tool) -> None: | ||
"""Test print in python script tool.""" | ||
|
||
source = """ | ||
print("test1") | ||
def test2(): | ||
print("test2") | ||
test2() | ||
""" | ||
|
||
response = await async_call_tool("python_code_execute", source=source) | ||
|
||
assert response == {"printed": "test1\ntest2\n"} |
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,30 @@ | ||
"""Test web scrape tool.""" | ||
|
||
from unittest.mock import patch | ||
|
||
|
||
def test_test(hass): | ||
"""Workaround for https://github.com/MatthewFlamm/pytest-homeassistant-custom-component/discussions/160.""" | ||
|
||
|
||
async def test_web_scrape_tool(async_call_tool) -> None: | ||
"""Test web scrape tool.""" | ||
|
||
helloworld = """<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Hello World</title> | ||
</head> | ||
<body> | ||
<h1>Hello, World!</h1> | ||
<p>This is a simple HTML page with a greeting message.</p> | ||
</body> | ||
</html> | ||
""" | ||
|
||
with patch("trafilatura.fetch_url", return_value=helloworld): | ||
response = await async_call_tool("web_scrape", url="example.com") | ||
|
||
assert response == {"text": "This is a simple HTML page with a greeting message."} |