Using Google’s Gemini Function Calling to Fetch Real-Time Company News and Insights from External APIs

Ishana Shinde
Google Cloud - Community
5 min readMay 9, 2024

--

In today’s fast-paced financial markets, professional and seasoned investors are constantly on the lookout for efficient ways to stay ahead. The sheer volume of financial data available can be overwhelming, but powerful generative models such as the Gemini model in Vertex AI along with tool frameworks like Function Calling in Gemini offer a promising solution. These tool frameworks not only automate the process of gathering financial insights but also revolutionizes the way investors can use generative models to interact with market data.

Overview

Function Calling is a native framework in Gemini that enables the generative model to call specific functions and perform tasks based on user prompts. It allows you to get output from the Gemini model in a structured way, trigger function calls that process data, retrieve information from external sources, or execute operations via API calls. This approach allows you to build AI agents that can perform complex workflows and opens up new possibilities for automation.

When using Function Calling in Gemini, you can still communicate with Gemini through natural language prompts, which Gemini then transforms into structured data outputs. This interaction provides a flexible way to convert natural language into API calls that access real-time information from external APIs and quickly generate insights. Let’s dive into a use case that illustrates how this technology can streamline news and financial data analysis of various companies.

The Investor’s Challenge

Meet Jane! She’s a busy investor who’s always on the lookout for the latest market trends and financial news. She needs information quickly and accurately, but sifting through endless articles and reports is time-consuming.

Jane’s daily routine involves analyzing a flood of financial reports and market data to identify investment opportunities. By relying solely on Gemini, she faces significant challenges like frozen training data, making it difficult to access the latest market trends. Inconsistent responses further hinder her analysis, as the model struggles to provide accurate and relevant information consistently. Additionally, the limited functionality of generative models without function and tool framework prevents Jane from seamlessly integrating real-time data and executing complex financial calculations, leaving her with a fragmented and inefficient workflow.

Discovering the Solution: Gemini Function Calling

The breakthrough came when Jane discovered Function Calling in Gemini. This framework uses native functionality in the Gemini model to translate natural language into structured data that we can use to help the generative model integrate with external data sources and fetch tailored financial and company insights. It’s not just about speed; it’s about the relevance and precision of the information that we’re sending to the Gemini model to augment the knowledge it was trained on with real-time information from external systems.

How Jane Uses Gemini Function Calling

Here’s a look at how Jane uses Function Calling in Gemini to provide functions as tools to enhance her data analysis:

Function Declaration

get_company_overview_func = FunctionDeclaration(
name="get_company_overview",
description="This function returns the company information, financial ratios, and other key metrics for the equity specified. Data is generally refreshed on the same day a company reports its latest earnings and financials.",
parameters={
"type": "object",
"properties": {"ticker": {"type": "string", "description": "The symbol of the ticker of your choice. For example: symbol=IBM"}},
},
)

In this code snippet, Jane defines a function called get_company_overview. This function is designed to fetch essential company information, financial ratios, and key metrics for a specific stock ticker. The description provides a clear explanation of the function's purpose, while the parameters specify the required input (the stock ticker symbol).

Prompt

Give me the company overview of Google?

Jane poses a simple question to Gemini, requesting an overview of Google’s company information.

API Call

def get_company_overview_from_api(content):
url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={content["ticker"]}&apikey=INSERT_YOUR_API_KEY'
api_response = requests.get(url) # Get the JSON response from the external news sentiment API
return api_response.text

Behind the scenes, Gemini recognizes the intent of Jane’s query and triggers the get_company_overview function. This function, in turn, constructs a URL to query the Alpha Vantage API, replacing the placeholder {content["ticker"]} with "GOOG" (the ticker symbol for Google). The API call fetches the relevant company data in JSON format.

Gemini Response

## Google's Company Overview:
* **Alphabet Inc Class C (GOOG)** is a multinational technology conglomerate headquartered in Mountain View, California.
* It's the parent company of Google and several former Google subsidiaries.
* As of March 31, 2024, it boasts a market capitalization of over $2 trillion.
* Key financial ratios:
* P/E Ratio: 25.39
* PEG Ratio: 1.659
* Price/Book Ratio: 6.99
* Recent performance:
* Quarterly earnings growth year-over-year: 0.609
* Quarterly revenue growth year-over-year: 0.154

Gemini processes the JSON data received from the API and crafts a concise summary in natural language. This summary highlights key aspects of Google’s company overview, including its full name, market capitalization, financial ratios, and recent performance metrics.

As demonstrated in this example, Gemini Function Calling streamlines the process of accessing company information. By simply asking a question, Jane can instantly receive a comprehensive overview of Google’s financial standing, saving her valuable time and effort.

Impact and Advancements

Integrating Gemini Function Calling has significantly reduced the time Jane spends on company research, allowing her to focus more on strategic analysis and less on operational tasks. Since Jane was also able to build a web app and share it with her team, the tool’s efficiency in delivering real-time, actionable insights has notably improved her team’s responsiveness to market changes.

Extend the Functionality

You can take the above example and extend it to serve your own needs, including integrating different data sources or defining additional functions that the Gemini model can use as tools at runtime. These improvements could further refine data accuracy and expand its utility across different industries.

Explore the Code

If you’re interested in exploring Gemini Function Calling further, visit the GoogleCloudPlatform/generative-ai code repository on GitHub to view and run the sample notebook.

Conclusion

Function Calling in Gemini offers a powerful way for generative models to interact with external systems and APIs to automate complex tasks. By integrating with external APIs, you can quickly retrieve the latest financial news and stock information, enabling faster and more informed decision-making.

Beyond mere automation, Function Calling ensures consistent and structured responses - eliminating the ambiguity and inconsistencies often associated with generative models. Developers gain full control in tailoring functions to specific financial tasks, such as real-time portfolio analysis or risk assessment. This powerful feature also augments generative models with real-time information from diverse sources, including databases, document repositories, and any other system accessible via an API.

Whether you’re a financial analyst, data scientist, or technology enthusiast, this approach can significantly streamline your workflow, improve accuracy, and unlock new possibilities in the way you access and process financial data.

--

--