Skip to content

Commit e9182e5

Browse files
douglas-reidGWeale
authored andcommitted
feat: Add Gemma3Ollama model integration and a sample
This change introduces `Gemma3Ollama`, a new LLM model class for running Gemma 3 models locally via Ollama, leveraging LiteLLM. The function calling logic previously in the `Gemma` class has been refactored into a `GemmaFunctionCallingMixin` and is now used by both `Gemma` and `Gemma3Ollama`. A new sample application, `hello_world_gemma3_ollama`, is added to demonstrate using `Gemma3Ollama` with an agent. Unit tests for `Gemma3Ollama` are also included. Merge: #3120 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 839996879
1 parent b0c3cc6 commit e9182e5

File tree

6 files changed

+357
-61
lines changed

6 files changed

+357
-61
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from . import agent
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
import random
17+
18+
from google.adk.agents.llm_agent import Agent
19+
from google.adk.models import Gemma3Ollama
20+
21+
litellm_logger = logging.getLogger("LiteLLM")
22+
litellm_logger.setLevel(logging.WARNING)
23+
24+
25+
def roll_die(sides: int) -> int:
26+
"""Roll a die and return the rolled result.
27+
28+
Args:
29+
sides: The integer number of sides the die has.
30+
31+
Returns:
32+
An integer of the result of rolling the die.
33+
"""
34+
return random.randint(1, sides)
35+
36+
37+
async def check_prime(nums: list[int]) -> str:
38+
"""Check if a given list of numbers are prime.
39+
40+
Args:
41+
nums: The list of numbers to check.
42+
43+
Returns:
44+
A str indicating which number is prime.
45+
"""
46+
primes = set()
47+
for number in nums:
48+
number = int(number)
49+
if number <= 1:
50+
continue
51+
is_prime = True
52+
for i in range(2, int(number**0.5) + 1):
53+
if number % i == 0:
54+
is_prime = False
55+
break
56+
if is_prime:
57+
primes.add(number)
58+
return (
59+
"No prime numbers found."
60+
if not primes
61+
else f"{', '.join(str(num) for num in primes)} are prime numbers."
62+
)
63+
64+
65+
root_agent = Agent(
66+
model=Gemma3Ollama(),
67+
name="data_processing_agent",
68+
description=(
69+
"hello world agent that can roll a dice of 8 sides and check prime"
70+
" numbers."
71+
),
72+
instruction="""
73+
You roll dice and answer questions about the outcome of the dice rolls.
74+
You can roll dice of different sizes.
75+
You can use multiple tools in parallel by calling functions in parallel (in one request and in one round).
76+
It is ok to discuss previous dice rolls, and comment on the dice rolls.
77+
When you are asked to roll a die, you must call the roll_die tool with the number of sides. Be sure to pass in an integer. Do not pass in a string.
78+
You should never roll a die on your own.
79+
When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string.
80+
You should not check prime numbers before calling the tool.
81+
When you are asked to roll a die and check prime numbers, you should always make the following two function calls:
82+
1. You should first call the roll_die tool to get a roll. Wait for the function response before calling the check_prime tool.
83+
2. After you get the function response from roll_die tool, you should call the check_prime tool with the roll_die result.
84+
2.1 If user asks you to check primes based on previous rolls, make sure you include the previous rolls in the list.
85+
3. When you respond, you must include the roll_die result from step 1.
86+
You should always perform the previous 3 steps when asking for a roll and checking prime numbers.
87+
You should not rely on the previous history on prime results.
88+
""",
89+
tools=[
90+
roll_die,
91+
check_prime,
92+
],
93+
)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import asyncio
17+
import time
18+
19+
import agent
20+
from dotenv import load_dotenv
21+
from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService
22+
from google.adk.cli.utils import logs
23+
from google.adk.runners import Runner
24+
from google.adk.sessions.in_memory_session_service import InMemorySessionService
25+
from google.adk.sessions.session import Session
26+
from google.genai import types
27+
28+
load_dotenv(override=True)
29+
logs.log_to_tmp_folder()
30+
31+
32+
async def main():
33+
34+
app_name = 'my_app'
35+
user_id_1 = 'user1'
36+
session_service = InMemorySessionService()
37+
artifact_service = InMemoryArtifactService()
38+
runner = Runner(
39+
app_name=app_name,
40+
agent=agent.root_agent,
41+
artifact_service=artifact_service,
42+
session_service=session_service,
43+
)
44+
session_1 = await session_service.create_session(
45+
app_name=app_name, user_id=user_id_1
46+
)
47+
48+
async def run_prompt(session: Session, new_message: str):
49+
content = types.Content(
50+
role='user', parts=[types.Part.from_text(text=new_message)]
51+
)
52+
print('** User says:', content.model_dump(exclude_none=True))
53+
async for event in runner.run_async(
54+
user_id=user_id_1,
55+
session_id=session.id,
56+
new_message=content,
57+
):
58+
if event.content.parts and event.content.parts[0].text:
59+
print(f'** {event.author}: {event.content.parts[0].text}')
60+
61+
start_time = time.time()
62+
print('Start time:', start_time)
63+
print('------------------------------------')
64+
await run_prompt(session_1, 'Hi, introduce yourself.')
65+
await run_prompt(
66+
session_1, 'Roll a die with 100 sides and check if it is prime'
67+
)
68+
await run_prompt(session_1, 'Roll it again.')
69+
await run_prompt(session_1, 'What numbers did I get?')
70+
end_time = time.time()
71+
print('------------------------------------')
72+
print('End time:', end_time)
73+
print('Total time:', end_time - start_time)
74+
75+
76+
if __name__ == '__main__':
77+
asyncio.run(main())

src/google/adk/models/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@
5353
except Exception:
5454
# LiteLLM support requires: pip install google-adk[extensions]
5555
pass
56+
57+
# Optionally register Gemma3Ollama if litellm package is installed
58+
try:
59+
from .gemma_llm import Gemma3Ollama
60+
61+
LLMRegistry.register(Gemma3Ollama)
62+
__all__.append('Gemma3Ollama')
63+
except Exception:
64+
# Gemma3Ollama requires LiteLLM: pip install google-adk[extensions]
65+
pass

0 commit comments

Comments
 (0)