# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from google.adk.agents import Agent from google.adk.runners import Runner from google.adk.sessions import InMemorySessionService from google.genai import types import yfinance as yf APP_NAME = "stock_app" USER_ID = "1234" SESSION_ID = "session1234" def get_stock_price(symbol: str): """ Retrieves the current stock price for a given symbol. Args: symbol (str): The stock symbol (e.g., "AAPL", "GOOG"). Returns: float: The current stock price, or None if an error occurs. """ try: stock = yf.Ticker(symbol) historical_data = stock.history(period="1d") if not historical_data.empty: current_price = historical_data['Close'].iloc[-1] return current_price else: return None except Exception as e: print(f"Error retrieving stock price for {symbol}: {e}") return None stock_price_agent = Agent( model='gemini-2.0-flash', name='stock_agent', instruction= 'You are an agent who retrieves stock prices. If a ticker symbol is provided, fetch the current price. If only a company name is given, first perform a Google search to find the correct ticker symbol before retrieving the stock price. If the provided ticker symbol is invalid or data cannot be retrieved, inform the user that the stock price could not be found.', description='This agent specializes in retrieving real-time stock prices. Given a stock ticker symbol (e.g., AAPL, GOOG, MSFT) or the stock name, use the tools and reliable data sources to provide the most up-to-date price.', tools=[get_stock_price], # You can add Python functions directly to the tools list; they will be automatically wrapped as FunctionTools. ) # Session and Runner async def setup_session_and_runner(): session_service = InMemorySessionService() session = await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID) runner = Runner(agent=stock_price_agent, app_name=APP_NAME, session_service=session_service) return session, runner # Agent Interaction async def call_agent_async(query): content = types.Content(role='user', parts=[types.Part(text=query)]) session, runner = await setup_session_and_runner() events = runner.run_async(user_id=USER_ID, session_id=SESSION_ID, new_message=content) async for event in events: if event.is_final_response(): final_response = event.content.parts[0].text print("Agent Response: ", final_response) # Note: In Colab, you can directly use 'await' at the top level. # If running this code as a standalone Python script, you'll need to use asyncio.run() or manage the event loop. await call_agent_async("stock price of GOOG")