Skip to content

Commit f33e090

Browse files
genquan9copybara-github
authored andcommitted
feat: Add ADK examples for litellm with add_function_to_prompt
Add examples for for #1273 PiperOrigin-RevId: 775352677
1 parent abc89d2 commit f33e090

File tree

5 files changed

+183
-3
lines changed

5 files changed

+183
-3
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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 random
17+
18+
from google.adk import Agent
19+
from google.adk.models.lite_llm import LiteLlm
20+
from langchain_core.utils.function_calling import convert_to_openai_function
21+
22+
23+
def roll_die(sides: int) -> int:
24+
"""Roll a die and return the rolled result.
25+
26+
Args:
27+
sides: The integer number of sides the die has.
28+
29+
Returns:
30+
An integer of the result of rolling the die.
31+
"""
32+
return random.randint(1, sides)
33+
34+
35+
def check_prime(number: int) -> str:
36+
"""Check if a given number is prime.
37+
38+
Args:
39+
number: The input number to check.
40+
41+
Returns:
42+
A str indicating the number is prime or not.
43+
"""
44+
if number <= 1:
45+
return f"{number} is not prime."
46+
is_prime = True
47+
for i in range(2, int(number**0.5) + 1):
48+
if number % i == 0:
49+
is_prime = False
50+
break
51+
if is_prime:
52+
return f"{number} is prime."
53+
else:
54+
return f"{number} is not prime."
55+
56+
57+
root_agent = Agent(
58+
model=LiteLlm(
59+
model="vertex_ai/meta/llama-4-maverick-17b-128e-instruct-maas",
60+
# If the model is not trained with functions and you would like to
61+
# enable function calling, you can add functions to the models, and the
62+
# functions will be added to the prompts during inferences.
63+
functions=[
64+
convert_to_openai_function(roll_die),
65+
convert_to_openai_function(check_prime),
66+
],
67+
),
68+
name="data_processing_agent",
69+
description="""You are a helpful assistant.""",
70+
instruction="""
71+
You are a helpful assistant, and call tools optionally.
72+
If call tools, the tool format should be in json, and the tool arguments should be parsed from users inputs.
73+
""",
74+
tools=[
75+
roll_die,
76+
check_prime,
77+
],
78+
)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 import Runner
22+
from google.adk.artifacts import InMemoryArtifactService
23+
from google.adk.cli.utils import logs
24+
from google.adk.sessions import InMemorySessionService
25+
from google.adk.sessions 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+
app_name = 'my_app'
34+
user_id_1 = 'user1'
35+
session_service = InMemorySessionService()
36+
artifact_service = InMemoryArtifactService()
37+
runner = Runner(
38+
app_name=app_name,
39+
agent=agent.root_agent,
40+
artifact_service=artifact_service,
41+
session_service=session_service,
42+
)
43+
session_11 = await session_service.create_session(
44+
app_name=app_name, user_id=user_id_1
45+
)
46+
47+
async def run_prompt(session: Session, new_message: str):
48+
content = types.Content(
49+
role='user', parts=[types.Part.from_text(text=new_message)]
50+
)
51+
print('** User says:', content.model_dump(exclude_none=True))
52+
async for event in runner.run_async(
53+
user_id=user_id_1,
54+
session_id=session.id,
55+
new_message=content,
56+
):
57+
if event.content.parts:
58+
part = event.content.parts[0]
59+
if part.text:
60+
print(f'** {event.author}: {part.text}')
61+
if part.function_call:
62+
print(f'** {event.author} calls tool: {part.function_call}')
63+
if part.function_response:
64+
print(
65+
f'** {event.author} gets tool response: {part.function_response}'
66+
)
67+
68+
start_time = time.time()
69+
print('Start time:', start_time)
70+
print('------------------------------------')
71+
await run_prompt(session_11, 'Hi, introduce yourself.')
72+
await run_prompt(session_11, 'Roll a die with 100 sides.')
73+
await run_prompt(session_11, 'Check if it is prime.')
74+
end_time = time.time()
75+
print('------------------------------------')
76+
print('End time:', end_time)
77+
print('Total time:', end_time - start_time)
78+
79+
80+
if __name__ == '__main__':
81+
asyncio.run(main())

src/google/adk/models/lite_llm.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from typing import Union
3030

3131
from google.genai import types
32+
import litellm
3233
from litellm import acompletion
3334
from litellm import ChatCompletionAssistantMessage
3435
from litellm import ChatCompletionAssistantToolCall
@@ -53,6 +54,9 @@
5354
from .llm_request import LlmRequest
5455
from .llm_response import LlmResponse
5556

57+
# This will add functions to prompts if functions are provided.
58+
litellm.add_function_to_prompt = True
59+
5660
logger = logging.getLogger("google_adk." + __name__)
5761

5862
_NEW_LINE = "\n"
@@ -662,6 +666,10 @@ async def generate_content_async(
662666

663667
messages, tools, response_format = _get_completion_inputs(llm_request)
664668

669+
if "functions" in self._additional_args:
670+
# LiteLLM does not support both tools and functions together.
671+
tools = None
672+
665673
completion_args = {
666674
"model": self.model,
667675
"messages": messages,

tests/integration/models/test_litellm_with_function.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
from google.genai import types
1818
from google.genai.types import Content
1919
from google.genai.types import Part
20-
import litellm
2120
import pytest
2221

23-
litellm.add_function_to_prompt = True
24-
2522
_TEST_MODEL_NAME = "vertex_ai/meta/llama-3.1-405b-instruct-maas"
2623

2724
_SYSTEM_PROMPT = """

0 commit comments

Comments
 (0)