|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | + |
| 4 | +from google.adk.agents import LlmAgent |
| 5 | +from google.adk.models.lite_llm import LiteLlm |
| 6 | +from google.adk.runners import Runner |
| 7 | +from google.adk.sessions import InMemorySessionService |
| 8 | +from google.genai import types |
| 9 | + |
| 10 | +from thirdweb_ai import Insight |
| 11 | +from thirdweb_ai.adapters.google_adk.google_adk import get_google_adk_tools |
| 12 | + |
| 13 | +# Example app configuration |
| 14 | +APP_NAME = "thirdweb_insight_app" |
| 15 | +USER_ID = "test_user" |
| 16 | +SESSION_ID = "test_session" |
| 17 | + |
| 18 | + |
| 19 | +async def setup_agent() -> Runner: |
| 20 | + """Set up an agent with Thirdweb Insight tools. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + Runner: Google ADK runner for the agent |
| 24 | + """ |
| 25 | + # Initialize Insight with secret key |
| 26 | + secret_key = os.getenv("THIRDWEB_SECRET_KEY") |
| 27 | + if not secret_key: |
| 28 | + raise ValueError("THIRDWEB_SECRET_KEY environment variable is required") |
| 29 | + |
| 30 | + # Get Insight tools |
| 31 | + insight = Insight(secret_key=secret_key, chain_id=1) |
| 32 | + insight_tools = insight.get_tools() |
| 33 | + |
| 34 | + # Convert to Google ADK tools |
| 35 | + adk_tools = get_google_adk_tools(insight_tools) |
| 36 | + |
| 37 | + # Print all available tools for debugging |
| 38 | + print(f"Available tools ({len(adk_tools)}):") |
| 39 | + for tool_count, tool in enumerate(adk_tools, start=1): |
| 40 | + print(f"- Tool #{tool_count} {tool.name}") |
| 41 | + |
| 42 | + # Create the agent with the tools |
| 43 | + agent = LlmAgent( |
| 44 | + model=LiteLlm(model="gpt-4o-mini"), |
| 45 | + name="thirdweb_insight_agent", |
| 46 | + # Convert BaseTool to the expected type for LlmAgent |
| 47 | + tools=adk_tools, # type: ignore |
| 48 | + ) |
| 49 | + |
| 50 | + # Set up session |
| 51 | + session_service = InMemorySessionService() |
| 52 | + # We need to create the session but don't need to store it |
| 53 | + await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID) |
| 54 | + |
| 55 | + # Return runner |
| 56 | + return Runner(agent=agent, app_name=APP_NAME, session_service=session_service) |
| 57 | + |
| 58 | + |
| 59 | +async def call_agent(query: str) -> None: |
| 60 | + """Run a query through the agent. |
| 61 | +
|
| 62 | + Args: |
| 63 | + query: The query to send to the agent |
| 64 | + """ |
| 65 | + runner = await setup_agent() |
| 66 | + content = types.Content(role="user", parts=[types.Part(text=query)]) |
| 67 | + events = runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=content) |
| 68 | + |
| 69 | + for event in events: |
| 70 | + if ( |
| 71 | + hasattr(event, "is_final_response") |
| 72 | + and event.is_final_response() |
| 73 | + and (event.content and hasattr(event.content, "parts") and event.content.parts) |
| 74 | + ): |
| 75 | + final_response = event.content.parts[0].text |
| 76 | + print("Agent Response: ", final_response) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + test_query = "Find information on transaction: 0x45027cce9d2b990349b4a1e015ec29ca7c7ef15d82487d898f24866a09e8b84c." |
| 81 | + asyncio.run(call_agent(test_query)) |
0 commit comments