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