Skip to content

Commit 9e775ff

Browse files
committed
feat(genai): add 2.0 chat function calling examples
1 parent be4cb8e commit 9e775ff

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

generative_ai/chat_completions/chat_completions_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import chat_completions_streaming_image
2323
import chat_completions_streaming_text
2424
import chat_completions_streaming_text_self_deployed
25+
import chat_function_calling_basic
26+
import chat_function_calling_config
2527

2628

2729
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
@@ -30,6 +32,14 @@
3032
ENDPOINT_ID = "6714120476014149632"
3133

3234

35+
def test_chat_function_calling_basic() -> None:
36+
assert chat_function_calling_basic.generate_text()
37+
38+
39+
def test_chat_function_calling_config() -> None:
40+
assert chat_function_calling_config.generate_text()
41+
42+
3343
def test_authentication() -> None:
3444
response = chat_completions_authentication.generate_text(PROJECT_ID, LOCATION)
3545
assert response
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright 2024 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+
# https://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 os
16+
17+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
18+
19+
20+
def generate_text() -> object:
21+
# [START generativeaionvertexai_gemini_chat_completions_function_calling_basic]
22+
import vertexai
23+
import openai
24+
25+
from google.auth import default, transport
26+
27+
# TODO(developer): Update & uncomment below line
28+
# PROJECT_ID = "your-project-id"
29+
location = "us-central1"
30+
31+
vertexai.init(project=PROJECT_ID, location=location)
32+
33+
# Programmatically get an access token
34+
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
35+
auth_request = transport.requests.Request()
36+
credentials.refresh(auth_request)
37+
38+
# # OpenAI Client
39+
client = openai.OpenAI(
40+
base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
41+
api_key=credentials.token,
42+
)
43+
44+
tools = [
45+
{
46+
"type": "function",
47+
"function": {
48+
"name": "get_current_weather",
49+
"description": "Get the current weather in a given location",
50+
"parameters": {
51+
"type": "object",
52+
"properties": {
53+
"location": {
54+
"type": "string",
55+
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
56+
},
57+
},
58+
"required": ["location"],
59+
},
60+
},
61+
}
62+
]
63+
64+
messages = []
65+
messages.append(
66+
{
67+
"role": "system",
68+
"content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.",
69+
}
70+
)
71+
messages.append({"role": "user", "content": "What is the weather in Boston?"})
72+
73+
response = client.chat.completions.create(
74+
model="google/gemini-2.0-flash-001",
75+
messages=messages,
76+
tools=tools,
77+
)
78+
79+
print("Function:", response.choices[0].message.tool_calls[0].id)
80+
print("Arguments:", response.choices[0].message.tool_calls[0].function.arguments)
81+
# Example response:
82+
# Function: get_current_weather
83+
# Arguments: {"location":"Boston"}
84+
85+
# [END generativeaionvertexai_gemini_chat_completions_function_calling_basic]
86+
return response
87+
88+
89+
if __name__ == "__main__":
90+
generate_text()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright 2024 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+
# https://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 os
16+
17+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
18+
19+
20+
def generate_text() -> object:
21+
# [START generativeaionvertexai_gemini_chat_completions_function_calling_config]
22+
import vertexai
23+
import openai
24+
25+
from google.auth import default, transport
26+
27+
# TODO(developer): Update & uncomment below line
28+
# PROJECT_ID = "your-project-id"
29+
location = "us-central1"
30+
31+
vertexai.init(project=PROJECT_ID, location=location)
32+
33+
# Programmatically get an access token
34+
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
35+
auth_request = transport.requests.Request()
36+
credentials.refresh(auth_request)
37+
38+
# OpenAI Client
39+
client = openai.OpenAI(
40+
base_url=f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{location}/endpoints/openapi",
41+
api_key=credentials.token,
42+
)
43+
44+
tools = [
45+
{
46+
"type": "function",
47+
"function": {
48+
"name": "get_current_weather",
49+
"description": "Get the current weather in a given location",
50+
"parameters": {
51+
"type": "object",
52+
"properties": {
53+
"location": {
54+
"type": "string",
55+
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
56+
},
57+
},
58+
"required": ["location"],
59+
},
60+
},
61+
}
62+
]
63+
64+
messages = []
65+
messages.append(
66+
{
67+
"role": "system",
68+
"content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.",
69+
}
70+
)
71+
messages.append({"role": "user", "content": "What is the weather in Boston, MA?"})
72+
73+
response = client.chat.completions.create(
74+
model="google/gemini-2.0-flash-001",
75+
messages=messages,
76+
tools=tools,
77+
tool_choice="auto",
78+
)
79+
80+
print("Function:", response.choices[0].message.tool_calls[0].id)
81+
print("Arguments:", response.choices[0].message.tool_calls[0].function.arguments)
82+
# Example response:
83+
# Function: get_current_weather
84+
# Arguments: {"location":"Boston"}
85+
# [END generativeaionvertexai_gemini_chat_completions_function_calling_config]
86+
87+
return response
88+
89+
90+
if __name__ == "__main__":
91+
generate_text()

0 commit comments

Comments
 (0)