Last active
April 23, 2024 02:01
-
-
Save koverholt/a6f549757a75c7a32516093f664ced56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import vertexai | |
from vertexai.generative_models import ( | |
FunctionDeclaration, | |
GenerationConfig, | |
GenerativeModel, | |
Part, | |
Tool, | |
) | |
from datetime import datetime | |
PROJECT_ID = "YOUR_PROJECT_ID" | |
LOCATION = "us-central1" | |
vertexai.init(project=PROJECT_ID, location=LOCATION) | |
get_current_time = FunctionDeclaration( | |
name="get_current_time", | |
description="Get the current time", | |
parameters={ | |
"type": "object", | |
"properties": {}, | |
}, | |
) | |
time_tool = Tool(function_declarations=[get_current_time]) | |
model = GenerativeModel( | |
"gemini-1.0-pro-001", | |
generation_config=GenerationConfig(temperature=0), | |
tools=[time_tool], | |
) | |
chat = model.start_chat() | |
prompt = "What time is it?" | |
print(prompt) | |
response = chat.send_message(prompt) | |
response.candidates[0].content.parts[0] | |
if response.candidates[0].content.parts[0].function_call.name == "get_current_time": | |
now = datetime.now().strftime("%I:%M %p") | |
api_response = {"time": str(now)} | |
response = chat.send_message( | |
Part.from_function_response( | |
name="get_current_time", | |
response={ | |
"content": api_response, | |
}, | |
), | |
) | |
print(response.candidates[0].content.parts[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment