diff --git a/gemini/function-calling/function_calling_service/README.md b/gemini/function-calling/function_calling_service/README.md index 12bf7e1a1..bb5248836 100644 --- a/gemini/function-calling/function_calling_service/README.md +++ b/gemini/function-calling/function_calling_service/README.md @@ -5,11 +5,18 @@ This project demonstrates how to use the [Function Calling](https://cloud.google It wraps the address lookup tool in the [Introduction to Function Calling notebook](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/function-calling/intro_function_calling.ipynb) as a Cloud Run service. ## Prerequisites + * Set your Google Cloud Project through an environment variable `GOOGLE_CLOUD_PROJECT`. ## Test + * Install the dependencies with `pip install -r requirements.txt` * Run `python main.py` to locally run the development server to run this Flask application. ## Deployment -Use `gcloud run deploy` to deploy the application to a Cloud Run service. \ No newline at end of file + +Use `gcloud run deploy` to deploy the application to a Cloud Run service. + +## Acknowledgments + +This project includes [highlight.js](https://highlightjs.org/) (Version 11.9.0), licensed under the [BSD License](https://github.com/highlightjs/highlight.js/blob/main/LICENSE). diff --git a/gemini/function-calling/function_calling_service/main.py b/gemini/function-calling/function_calling_service/main.py index 40532905a..a3affb1c3 100644 --- a/gemini/function-calling/function_calling_service/main.py +++ b/gemini/function-calling/function_calling_service/main.py @@ -16,15 +16,13 @@ import logging import os -from flask import Flask, jsonify, render_template, request +from flask import Flask, render_template, request import requests import vertexai from vertexai.generative_models import ( - Content, FunctionDeclaration, GenerationConfig, GenerativeModel, - Part, Tool, ) @@ -64,43 +62,6 @@ vertexai.init(project=PROJECT_ID, location=LOCATION) -@app.route("/static") -def get_static_coordinates(): - """ - Retrieves coordinates for a static address using the Vertex AI Generative AI API and the Nominatim API. - - This function uses a pre-defined prompt to request coordinates for a specific address. - It then extracts the arguments from the function call response and constructs a URL for the Nominatim API. - Finally, it retrieves the coordinates from the Nominatim API and returns them as a JSON object. - """ - - prompt = """ - I want to get the coordinates for the following address: - 1600 Amphitheatre Pkwy, Mountain View, CA 94043, US - """ - - response = model.generate_content( - prompt, - generation_config=GenerationConfig(temperature=0), - tools=[location_tool], - ) - response.candidates[0].content.parts[0] - - x = response.candidates[0].content.parts[0].function_call.args - - url = "https://nominatim.openstreetmap.org/search?" - for i in x: - url += '{}="{}"&'.format(i, x[i]) - url += "format=json" - - headers = { - "User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" - } - x = requests.get(url, headers=headers) - content = x.json() - return content - - @app.route("/", methods=["GET", "POST"]) def get_coordinates(): """ @@ -112,66 +73,31 @@ def get_coordinates(): Finally, it retrieves the coordinates from the Nominatim API and returns them as a JSON object. For GET requests, it simply renders the index.html template. """ - if request.method == "POST": - address = request.form["address"] - prompt = f""" - I want to get the coordinates for the following address: - {address} - """ - response = model.generate_content( - prompt, - generation_config=GenerationConfig(temperature=0), - tools=[location_tool], - ) - - x = response.candidates[0].content.parts[0].function_call.args - if x is None: - content = "" - else: - url = "https://nominatim.openstreetmap.org/search?" - for i in x: - url += '{}="{}"&'.format(i, x[i]) - url += "format=json" - - headers = { - "User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" - } - x = requests.get(url, headers=headers) - raw_content = x.json() - content = json.dumps(raw_content, indent=4) - return render_template("index.html", content=content) - else: + if request.method == "GET": return render_template("index.html") - if request.method == "POST": - address = request.form["address"] - prompt = f""" - I want to get the coordinates for the following address: - {address} - """ - response = model.generate_content( - prompt, - generation_config=GenerationConfig(temperature=0), - tools=[location_tool], - ) - - x = response.candidates[0].content.parts[0].function_call.args - if x is None: - content = "" - else: - url = "https://nominatim.openstreetmap.org/search?" - for i in x: - url += '{}="{}"&'.format(i, x[i]) - url += "format=json" + address = request.form["address"] + prompt = f""" + I want to get the coordinates for the following address: + {address} + """ + response = model.generate_content(prompt) - headers = { - "User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" - } - x = requests.get(url, headers=headers) - raw_content = x.json() - content = json.dumps(raw_content, indent=4) - return render_template("index.html", content=content) + x = response.candidates[0].function_calls[0].args + if x is None: + content = "" else: - return render_template("index.html") + url = "https://nominatim.openstreetmap.org/search?" + for i in x: + url += '{}="{}"&'.format(i, x[i]) + url += "format=json" + + headers = { + "User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" + } + x = requests.get(url, headers=headers) + raw_content = x.json() + content = json.dumps(raw_content, indent=4) + return render_template("index.html", content=content) if __name__ == "__main__": diff --git a/gemini/function-calling/function_calling_service/templates/index.html b/gemini/function-calling/function_calling_service/templates/index.html index d765b2388..48b7bee8a 100644 --- a/gemini/function-calling/function_calling_service/templates/index.html +++ b/gemini/function-calling/function_calling_service/templates/index.html @@ -1,52 +1,73 @@ - - + Get Coordinates - - + + + + + - + - +
-
-
-

Lookup Coordinates

-
+
+
+

Lookup Coordinates

-
-
-
-
-
- edit_location - - -
-
-
-
-
-
-
+
+
+
+
+
+
+ edit_location + + +
+
+
+ +
+
+
-
-
-
-
- Coordinates -
{{ content }}
-
-
+
+
+
+
+
+ Coordinates +
{{ content }}
+
+
- - - \ No newline at end of file + +