-
Notifications
You must be signed in to change notification settings - Fork 183
Sql tool #233
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
Merged
Merged
Sql tool #233
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8bc0967
add tool to run sql queries over postgres
srilaasya 5be3f58
fixed var names, assertion err
srilaasya 2d18ae3
fixed workflow and added tests for compile_llms_txt.py, converted con…
srilaasya da14a29
Err with accessing current working dir, fix updated
srilaasya 7f39402
Merge branch 'refs/heads/main' into sql_tool
bboynton97 3b799b7
global connection and docs
bboynton97 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 hidden or 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,86 @@ | ||
| import os | ||
| import psycopg2 | ||
| from typing import Dict, Any | ||
|
|
||
| connection = None | ||
|
|
||
| def _get_connection(): | ||
| """Get PostgreSQL database connection""" | ||
|
|
||
| global connection | ||
| if connection is None: | ||
| connection = psycopg2.connect( | ||
| dbname=os.getenv('POSTGRES_DB'), | ||
| user=os.getenv('POSTGRES_USER'), | ||
| password=os.getenv('POSTGRES_PASSWORD'), | ||
| host=os.getenv('POSTGRES_HOST', 'localhost'), | ||
| port=os.getenv('POSTGRES_PORT', '5432') | ||
| ) | ||
|
|
||
| return connection | ||
|
|
||
| def get_schema() -> Dict[str, Any]: | ||
| """ | ||
| Initialize connection and get database schema. | ||
| Returns a dictionary containing the database schema. | ||
| """ | ||
| try: | ||
| conn = _get_connection() | ||
| cursor = conn.cursor() | ||
|
|
||
| # Query to get all tables in the current schema | ||
| schema_query = """ | ||
| SELECT table_name | ||
| FROM information_schema.tables | ||
| WHERE table_schema = 'public' | ||
| AND table_type = 'BASE TABLE'; | ||
| """ | ||
|
|
||
| cursor.execute(schema_query) | ||
| tables = cursor.fetchall() | ||
|
|
||
| # Create schema dictionary | ||
| schema = {} | ||
| for (table_name,) in tables: | ||
| # Get column information for each table | ||
| column_query = """ | ||
| SELECT column_name | ||
| FROM information_schema.columns | ||
| WHERE table_schema = 'public' | ||
| AND table_name = %s; | ||
| """ | ||
| cursor.execute(column_query, (table_name,)) | ||
| columns = [col[0] for col in cursor.fetchall()] | ||
| schema[table_name] = columns | ||
|
|
||
| cursor.close() | ||
| # conn.close() | ||
| return schema | ||
|
|
||
| except Exception as e: | ||
| print(f"Error getting database schema: {str(e)}") | ||
| return {} | ||
|
|
||
| def execute_query(query: str) -> list: | ||
| """ | ||
| Execute a SQL query on the database. | ||
| Args: | ||
| query: SQL query to execute | ||
| Returns: | ||
| List of query results | ||
| """ | ||
| try: | ||
| conn = _get_connection() | ||
| cursor = conn.cursor() | ||
|
|
||
| # Execute the query | ||
| cursor.execute(query) | ||
| results = cursor.fetchall() | ||
|
|
||
| cursor.close() | ||
| # conn.close() | ||
| return results | ||
|
|
||
| except Exception as e: | ||
| print(f"Error executing query: {str(e)}") | ||
| return [] | ||
This file contains hidden or 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,20 @@ | ||
| { | ||
| "name": "sql", | ||
| "url": "https://pypi.org/project/psycopg2/", | ||
| "category": "database", | ||
| "env": { | ||
| "POSTGRES_DB": null, | ||
| "POSTGRES_USER": null, | ||
| "POSTGRES_PASSWORD": null, | ||
| "POSTGRES_HOST": null, | ||
| "POSTGRES_PORT": null | ||
| }, | ||
| "dependencies": [ | ||
| "psycopg2-binary>=2.9.9" | ||
| ], | ||
| "tools": [ | ||
| "get_schema", | ||
| "execute_query" | ||
| ], | ||
| "cta": "Set up your PostgreSQL connection variables in the environment file." | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| --- | ||
| title: 'Perplexity' | ||
| description: 'Agentic Search' | ||
| --- | ||
|
|
||
| ## Description | ||
| Enable your agent to perform queries directly on a database | ||
|
|
||
| <Note> | ||
| There is no built-in sandboxing using this tool. Agents may perform destructive queries that may be irreversible. | ||
| </Note> | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| agentstack tools add sql | ||
| ``` | ||
|
|
||
| Set the API keys | ||
| ```env | ||
| POSTGRES_DB=... | ||
| POSTGRES_USER=... | ||
| POSTGRES_PASSWORD=... | ||
| POSTGRES_HOST=... | ||
| POSTGRES_PORT=... | ||
| ``` |
This file contains hidden or 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,92 @@ | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| import sys | ||
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
| from docs.compile_llms_txt import compile_llms_txt | ||
|
|
||
| class TestCompileLLMsTxt(unittest.TestCase): | ||
| def setUp(self): | ||
| self.original_cwd = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
|
||
| # Create a temporary directory for test files | ||
| self.test_dir = tempfile.mkdtemp() | ||
| self.docs_dir = Path(self.test_dir) | ||
|
|
||
| # Change to the temporary directory | ||
| os.chdir(self.docs_dir) | ||
|
|
||
| def tearDown(self): | ||
| os.chdir(self.original_cwd) | ||
| shutil.rmtree(self.test_dir) | ||
|
|
||
| def create_test_mdx_file(self, path: str, content: str): | ||
| """Helper to create test MDX files""" | ||
| file_path = self.docs_dir / path | ||
| file_path.parent.mkdir(parents=True, exist_ok=True) | ||
| file_path.write_text(content) | ||
|
|
||
| def test_basic_compilation(self): | ||
| """Test basic MDX file compilation""" | ||
| # Create test MDX files | ||
| self.create_test_mdx_file("test1.mdx", "Test content 1") | ||
| self.create_test_mdx_file("test2.mdx", "Test content 2") | ||
|
|
||
| # Run compilation | ||
| compile_llms_txt() | ||
|
|
||
| # Check output file exists and contains expected content | ||
| output_path = self.docs_dir / "llms.txt" | ||
| self.assertTrue(output_path.exists()) | ||
|
|
||
| content = output_path.read_text() | ||
| self.assertIn("## test1.mdx", content) | ||
| self.assertIn("Test content 1", content) | ||
| self.assertIn("## test2.mdx", content) | ||
| self.assertIn("Test content 2", content) | ||
|
|
||
| def test_excluded_directories(self): | ||
| """Test that files in excluded directories are skipped""" | ||
| # Create files in both regular and excluded directories | ||
| self.create_test_mdx_file("regular/file.mdx", "Regular content") | ||
| self.create_test_mdx_file("tool/file.mdx", "Tool content") | ||
|
|
||
| compile_llms_txt() | ||
|
|
||
| content = (self.docs_dir / "llms.txt").read_text() | ||
| self.assertIn("Regular content", content) | ||
| self.assertNotIn("Tool content", content) | ||
|
|
||
| def test_excluded_files(self): | ||
| """Test that excluded files are skipped""" | ||
| self.create_test_mdx_file("regular.mdx", "Regular content") | ||
| self.create_test_mdx_file("tool.mdx", "Tool content") | ||
|
|
||
| compile_llms_txt() | ||
|
|
||
| content = (self.docs_dir / "llms.txt").read_text() | ||
| self.assertIn("Regular content", content) | ||
| self.assertNotIn("Tool content", content) | ||
|
|
||
| def test_nested_directories(self): | ||
| """Test compilation from nested directory structure""" | ||
| self.create_test_mdx_file("dir1/test1.mdx", "Content 1") | ||
| self.create_test_mdx_file("dir1/dir2/test2.mdx", "Content 2") | ||
|
|
||
| compile_llms_txt() | ||
|
|
||
| content = (self.docs_dir / "llms.txt").read_text() | ||
| self.assertIn("## dir1/test1.mdx", content) | ||
| self.assertIn("## dir1/dir2/test2.mdx", content) | ||
| self.assertIn("Content 1", content) | ||
| self.assertIn("Content 2", content) | ||
|
|
||
| def test_empty_directory(self): | ||
| """Test compilation with no MDX files""" | ||
| compile_llms_txt() | ||
|
|
||
| content = (self.docs_dir / "llms.txt").read_text() | ||
| self.assertEqual(content, "") |
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.
Uh oh!
There was an error while loading. Please reload this page.