Skip to content

Commit

Permalink
address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kweinmeister committed Jun 18, 2024
1 parent d68f89a commit 30f4dcc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 138 deletions.
9 changes: 8 additions & 1 deletion gemini/function-calling/function_calling_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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).
120 changes: 23 additions & 97 deletions gemini/function-calling/function_calling_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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():
"""
Expand All @@ -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__":
Expand Down
101 changes: 61 additions & 40 deletions gemini/function-calling/function_calling_service/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,52 +1,73 @@
<!DOCTYPE html>
<html>

<head>
<head>
<title>Get Coordinates</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"
/>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/icon?family=Material+Icons"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css"
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>

Check warning

Code scanning / CodeQL

Inclusion of functionality from an untrusted source Medium

Script loaded from content delivery network with no integrity check.
<script>
hljs.highlightAll();
</script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
</head>

<body>
<body>
<div class="container">
<div class="row">
<div class="col s12 m10 offset-m1">
<h1 class="header center-align">Lookup Coordinates</h1>
</div>
<div class="row">
<div class="col s12 m10 offset-m1">
<h1 class="header center-align">Lookup Coordinates</h1>
</div>
<div class="row">
<div class="col s12 m10 offset-m1">
<form method="POST">
<div class="row">
<div class="input-field col s10">
<span class="material-icons prefix">edit_location</span>
<input placeholder="1600 Amphitheatre Pkwy, Mountain View, CA 94043, US" name="address"
type="text" class="validate">
<label class="active" for="address">Address</label>
</div>
<div class="col s2">
<div class="input-field"> <button class="btn waves-effect waves-light" type="submit"
name="action">Submit
<i class="material-icons right">send</i>
</button>
</div>
</div>
</div>
</form>
</div>
<div class="row">
<div class="col s12 m10 offset-m1">
<form method="POST">
<div class="row">
<div class="input-field col s10">
<span class="material-icons prefix">edit_location</span>
<input
placeholder="1600 Amphitheatre Pkwy, Mountain View, CA 94043, US"
name="address"
type="text"
class="validate"
/>
<label class="active" for="address">Address</label>
</div>
<div class="col s2">
<div class="input-field">
<button
class="btn waves-effect waves-light"
type="submit"
name="action"
>
Submit
<i class="material-icons right">send</i>
</button>
</div>
</div>
</div>
</form>
</div>
<div class="row">
<div class="col s12 m10 offset-m1">
<div class="card">
<div class="card-content">
<span class="card-title">Coordinates</span>
<pre>{{ content }}</pre>
</div>
</div>
</div>
<div class="row">
<div class="col s12 m10 offset-m1">
<div class="card">
<div class="card-content">
<span class="card-title">Coordinates</span>
<pre><code>{{ content }}</code></pre>
</div>
</div>
</div>
</div>
</div>
</body>

</html>
</body>
</html>

0 comments on commit 30f4dcc

Please sign in to comment.