-
Notifications
You must be signed in to change notification settings - Fork 2
Add Custom Agent #97
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
Add Custom Agent #97
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e8a939e
Add custom agent. Ignore config and local db. Include ui formatting.
jbouder e0b9e3e
Update chart for austin permits database.
jbouder 40b950a
Ignore backend yamls.
jbouder 17e028a
Make custom agent optional.
jbouder 4b71f0d
Set version and appVersion to 0.0.20-dev
viniciusdc ef3f252
Bump temp versions.
jbouder 3827c89
Bump versions.
jbouder 5b6c0fd
Remove unnecessary configs.
jbouder f6b3aa9
Bump verions.
jbouder e74417d
Merge branch 'main' into custom-agent
pmeier 87d4802
Merge branch 'main' into custom-agent
jbouder 161ed62
Refactor for database object and pydantic-ai agent.
jbouder 13b8655
Enhance database configs.
jbouder 2a40b78
Add psycopg dependency.
jbouder 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
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,148 @@ | ||
| import uuid | ||
| from typing import Any | ||
|
|
||
| import psycopg | ||
| from ag_ui.core import ActivitySnapshotEvent | ||
| from pydantic_ai import Agent, ToolReturn | ||
|
|
||
| from ravnar.agents import PydanticAiAgentWrapper | ||
|
|
||
|
|
||
| class PostgresDatabase: | ||
| def __init__( | ||
| self, | ||
| *, | ||
| host: str, | ||
| port: int = 5432, | ||
| name: str, | ||
| user: str, | ||
| password: str, | ||
| ) -> None: | ||
| self._host = host | ||
| self._port = port | ||
| self._name = name | ||
| self._user = user | ||
| self._password = password | ||
|
|
||
| async def _connect(self) -> psycopg.AsyncConnection[Any]: | ||
| return await psycopg.AsyncConnection.connect( | ||
| dbname=self._name, | ||
| user=self._user, | ||
| password=self._password, | ||
| host=self._host, | ||
| port=self._port, | ||
| ) | ||
|
|
||
| async def execute(self, query: str) -> list[tuple]: | ||
| async with await self._connect() as conn, conn.cursor() as cur: | ||
| await cur.execute(query) | ||
| return list(await cur.fetchall()) | ||
|
|
||
|
|
||
| def create_agent( | ||
| agent: Agent, | ||
| database: PostgresDatabase, | ||
| ) -> PydanticAiAgentWrapper: | ||
| @agent.system_prompt | ||
| def _system_prompt() -> str: | ||
| return """ | ||
| You are an agent with the sole task of answering the user's | ||
| questions related to the Austin Permits Database. You have a suite | ||
| of tools available to fetch the db schema, make queries, and create | ||
| visualizations from the data. The database is mounted as readonly, | ||
| so don't generate any SQL that would modify the database. Your | ||
| query will fail if it attempts to modify the db. Plan your actions | ||
| accordingly by ensuring that you understand the db schema before | ||
| generating SQL queries. | ||
| """ | ||
|
|
||
| @agent.tool_plain | ||
| async def get_db_schema() -> list[tuple]: | ||
| """Get the schema for the Austin Permits database. | ||
|
|
||
| Use this tool to understand the db structure before issuing | ||
| any queries to the db. | ||
|
|
||
| """ | ||
| query = """ | ||
| SELECT | ||
| t.table_name, | ||
| c.column_name, | ||
| c.data_type, | ||
| c.is_nullable, | ||
| c.ordinal_position | ||
| FROM information_schema.tables t | ||
| JOIN information_schema.columns c ON t.table_name = c.table_name | ||
| WHERE t.table_schema = 'public' AND t.table_type = 'BASE TABLE' | ||
| ORDER BY t.table_name, c.ordinal_position; | ||
| """ | ||
| return await database.execute(query) | ||
|
|
||
| @agent.tool_plain | ||
| async def execute_query(query: str) -> list[tuple]: | ||
| """Execute a query against the Austin Permits database. | ||
|
|
||
| The query should not attempt to modify the db in any way. | ||
|
|
||
| The db is mounted as readonly, so queries that attempt to modify it will fail. | ||
|
|
||
| This tool takes a single argument, which is the SQL to execute | ||
| against the db. The db is Postgres, so use Postgres-compatible | ||
| SQL syntax. | ||
|
|
||
| """ | ||
| return await database.execute(query) | ||
|
|
||
| @agent.tool_plain | ||
| async def create_chart(option: dict) -> ToolReturn: | ||
| """Create a chart from the data retrieved by a query to the db. | ||
|
|
||
| This tool takes a single argument, which is a Python dictionary | ||
| that follows the format of an Apache ECharts configuration object. | ||
|
|
||
| The object must be serializable to JSON, so it cannot include | ||
| JS function callbacks. Only use the features of the Apache ECharts | ||
| config that can be serialized as plain JSON data. | ||
|
|
||
| """ | ||
| return ToolReturn( | ||
| return_value="Chart Created", | ||
| metadata=[ | ||
| ActivitySnapshotEvent( | ||
| message_id=str(uuid.uuid4()), activity_type="application/json+echart", content=option | ||
| ) | ||
| ], | ||
| ) | ||
|
|
||
| @agent.tool_plain | ||
| async def create_map(data: dict) -> ToolReturn: | ||
| """Create a map with markers from the data retrieved by a query to the db. | ||
|
|
||
| This tool takes a single argument, which is a Python dictionary of | ||
| the following form: | ||
| { 'center': [number, number], 'features': GeoJSONFeatureCollection } | ||
|
|
||
| Use the 'center' key to define the center of the map in | ||
| [latitude, longitude] floating point numbers. | ||
|
|
||
| Use the 'features' key to define an additional GeoJSON feature | ||
| collection, such as markers with popup metadata, to add to the map. | ||
| This dictionary must be a valid GeoJSON feature collection. | ||
|
|
||
| To add a popup, create a 'popup' key in the properties of the feature. | ||
| Use an HTML string to define the contents of the popup. Any other key | ||
| in the properties of a feature besides 'popup' will be ignored. | ||
|
|
||
| If a link to the permit is available, include it in any popup. | ||
|
|
||
| """ | ||
| return ToolReturn( | ||
| return_value="Map Created", | ||
| metadata=[ | ||
| ActivitySnapshotEvent( | ||
| message_id=str(uuid.uuid4()), activity_type="application/json+leaflet", content=data | ||
| ) | ||
| ], | ||
| ) | ||
|
|
||
| return PydanticAiAgentWrapper(agent) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
Note, i'm going to leave this here until after this weeks demos have completed