Saturday, March 29, 2025

LLM Function Calling, ReAct, & Few-Shot Prompting in Gemma 2

Agentic AI for Dynamic Storytelling: Implementing LLM Function Calling, ReAct, and Few-Shot Prompting with Gemma 2

Gemma with Agentic AI

The Gemma family of open-source, lightweight, generative artificial intelligence (AI) models is based on the same technology and research as the Gemini models. It demonstrated a text-based adventure game made with Gemma in a blog post from the previous year. This blog post will teach you how to use Gemma with Agentic AI, a type of AI that provides an alternative method of using Large Language Models (LLMs).

Today’s most prevalent AIs are reactive. They react to certain requests, such as when a smart speaker is asked to play music. They are helpful, but they are limited to following instructions.

Agentic AI, on the other hand, is proactive and self-governing. It chooses how to accomplish its objectives on its own. Using outside resources, such as search engines, specialised software, and other applications, to obtain information beyond their own knowledge base is a crucial component. This enables Agentic AI to operate and resolve issues in a very autonomous and efficient manner.

We’ll cover important technical topics like “function calling,” “ReAct,” and “Few-shot prompting” in this hands-on approach to building an agentic AI system based on Gemma 2. This artificial intelligence system will work as a dynamic lore generator for a made-up game, actively adding to its past and giving players a unique, constantly changing storyline.

Overcoming the Divide

Let’s examine Gemma’s agentic AI capabilities before getting into the coding. You can use Google AI Studio to directly explore with it. Numerous Gemma 2 models are available in Google AI Studio. For optimal performance, the 27B model is advised; but, as you can see below, the smaller model, such as the 2B, can still be utilised. In this example, we will ask Gemma to give us the time in Paris and Tokyo after informing her that there is a get_current_time() function.

Function Calling
LLM function calling Image credit to Google

This outcome demonstrates that using the get_current_time() function is not advised by Gemma 2. “Function Calling” is a crucial element that allows AI to communicate with external systems and APIs in order to retrieve data.

Gemma can’t behave as an agent because of its restricted built-in function calling capabilities. Its powerful ability to obey instructions, however, can be leveraged to make up for this lack of functionality. Let’s see how we can use these features to increase Gemma’s usefulness.

The ReAct (Reasoning and Acting) prompting technique will be the basis for the prompt that it use. ReAct specifies a certain framework for interaction as well as the tools that are available. Gemma can participate in cycles of Thought (reasoning), Action (using tools), and Observation (evaluating the results) because to this framework.

Gemma is trying to utilise the get_current_time() function for both Tokyo and Paris, as you can see. A Gemma model can’t just run by itself. The produced code must be run by you or as part of your system in order for this to function. You can still continue without it and see Gemma’s response, which is comparable to the one given below.

function calling
LLM function calling Image credit to Google

You have now seen the manifestation of Gemma’s function calling. It can carry out activities independently in the background without requiring direct user intervention with its function calling capability.

Building a History AI Agent is the real demo, so let’s get our hands dirty!

Demo Configuration

The “Agentic AI with Gemma 2” notebook in Gemma’s Cookbook contains all of the following prompts. One distinction between using Gemma in Google AI Studio and Python directly on Colab is that Gemma requires instructions in a specific syntax, such as . The official documents have further information regarding this.

Consider a fictitious gaming environment where AI agents create dynamic content.

These agents can produce in-game content, such as novels, poetry, and songs, in reaction to player choices or important events in the game’s plot. They are made with specific goals in mind.

These AI agents’ capacity to deconstruct complicated objectives into manageable chunks is one of their primary characteristics. They are able to examine several strategies, assess possible results, and modify their plans in light of fresh data.

The fact that agentic AI is not merely passively throwing out information is where it really excels. To accomplish their preprogrammed goals, they can interact with digital (and possibly physical) environments, carry out tasks, and make decisions on their own.

How does it work?

This is an example of a ReAct-style prompt for an AI agent that creates in-game content and has the ability to obtain historical data via function calls.

<start_of_turn>user
You are an AI Historian in a game. Your goal is to create books, poems, and songs found in the game world so that the player's choices meaningfully impact the unfolding of events.

You have access to the following tools:

* `get_historical_events(year, location=None, keyword=None)`: Retrieves a list of historical events within a specific year.
* `get_person_info(name)`: Retrieves information about a historical figure.
* `get_location_info(location_name)`: Retrieves information about a location.

Use the following multi-step conversation:

Thought: I need to do something...
Action: I should use the tool `tool_name` with input `tool_input`

Wait user to get the result of the tool is `tool_output`

And finally answer the Content of books, poems, or songs.

Lets try to writing a book. View the following sample outputs:

Zero-shot prompting

Zero-shot prompting
Zero-shot prompting

Due to a lack of function calling training, Gemma may suffer.

It can use “One-shot prompting,” a type of in-context learning in which demonstrations are incorporated into the prompt, to get around this restriction. By using this example as a guide, Gemma will be able to comprehend the work at hand and enhance its performance through contextual learning.

One-Shot Prompting

(Note: the actual prompt appears after the green area, which is an example presented.)

One-Shot Prompting
One-Shot Prompting

Notably, because Action has the right input, the model works better.

Few-shot prompting

“Few-shot prompting” is used for more difficult jobs. It enables the model to understand the underlying pattern by giving a limited number of instances (often two to five, but occasionally more) that illustrate the intended input-output relationship.

With the parameter values “name: Anya, the Rebel Leader” and the function name get_person_info, we now have to connect to an API and call the method in the game. For this API interaction, a synthetic response payload will be used.

Few-shot prompting
Few-shot prompting

It should be noted that the spy wrote a book about the Rebel Leader of Eldoria using the material supplied.

The Future Is Agentic

Although the development of agentic AI is still in its infancy, it is moving quickly. It may anticipate that these systems will become more and more important in our lives as they advance in sophistication.

The following are some possible uses, mostly related to gaming:

  • Lifelike NPCs: NPCs will start to show distinct personalities and adjust to player interactions, making them more realistic.
  • Dynamic Stories: To ensure enduring replayability, games will feature dynamically created quests and stories.
  • Effective Development: AI can speed up development cycles and improve quality by streamlining game testing.

However, with ramifications that go beyond:

  • GUI Automation: From within a web browser, models can be utilised to interact with graphical user interfaces.
  • Mathematical Tool Integration: To get around restrictions on carrying out intricate computations, AI can make use of instruments such as calculators.
  • Contextual Knowledge Retrieval: AI is capable of determining when to consult outside knowledge sources, such as RAG systems.
Drakshi
Drakshi
Since June 2023, Drakshi has been writing articles of Artificial Intelligence for govindhtech. She was a postgraduate in business administration. She was an enthusiast of Artificial Intelligence.
RELATED ARTICLES

Recent Posts

Popular Post