"title"=>"Using Duet AI to generate a Starter App from an OpenAPI Specification",
"summary"=>nil,
"content"=>"
OpenAPI specification is well known with API Developers and one of the areas that I wanted to explore was to see how well Duet AI for Developers is able to generate an OpenAPI Specification and then use that to generate a starter application.
Pre-requisites
We will assume that you have the following in place:
- A Google Cloud Project with billing enabled
- Duet AI enabled for your Cloud Project
In case you are looking for setting up Duet AI for your Google Cloud project, check out the steps over here. The steps help you enable Duet AI in your Google Cloud Project and also setup the Cloud Shell IDE for working with Duet AI.
My setup
I am using Google Cloud Shell IDE and sending across my prompts to Duet AI via the Chat Interface available in the IDE.
Generate the OpenAPI Specification
We will keep things simple here. We will work with a fictitious application that manages a list of books. Each book will have a list of attributes that we will specify and we will keep things focused on a few retrieval operations across books only.
We askDuet AI to generate an OpenAPI Specification via the prompt given below:
I am designing an API for searching a library of books. Each book has the following attributes: id, title, a list of categories, a list of authors, year published and a brief summary. Please generate an OpenAPI Specification that allows one to search for the following operations:
1. All books
2. A single book by id
3. Given a book id, find its categories
4. Given a book id, find its authors
Duet AI came back with the following OpenAPI Specification
swagger: "2.0"
info:
title: "Book Search API"
description: "This API allows you to search for books in a library."
version: "1.0.0"
host: "bookshelf.googleapis.com"
schemes:
- "https"
paths:
/books:
get:
summary: "Get all books"
operationId: "getAllBooks"
responses:
"200":
description: "A list of all books"
schema:
type: "array"
items:
type: "object"
properties:
id:
type: "integer"
format: "int64"
title:
type: "string"
categories:
type: "array"
items:
type: "string"
authors:
type: "array"
items:
type: "string"
yearPublished:
type: "integer"
format: "int32"
summary:
type: "string"
/books/{id}:
get:
summary: "Get a single book by id"
operationId: "getBookById"
parameters:
- name: "id"
in: "path"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: "The book with the given id"
schema:
type: "object"
properties:
id:
type: "integer"
format: "int64"
title:
type: "string"
categories:
type: "array"
items:
type: "string"
authors:
type: "array"
items:
type: "string"
yearPublished:
type: "integer"
format: "int32"
summary:
type: "string"
/books/{id}/categories:
get:
summary: "Get the categories for a book by id"
operationId: "getCategoriesForBookById"
parameters:
- name: "id"
in: "path"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: "The categories for the book with the given id"
schema:
type: "array"
items:
type: "string"
/books/{id}/authors:
get:
summary: "Get the authors for a book by id"
operationId: "getAuthorsForBookById"
parameters:
- name: "id"
in: "path"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: "The authors for the book with the given id"
schema:
type: "array"
items:
type: "string"
It could have done better by defining the book schema. In fact, that is what I ask next via the following prompt:
Please update the above specification to use a book schema.
It comes back with the following:
swagger: "2.0"
info:
title: "Book Search API"
description: "This API allows you to search for books in a library."
version: "1.0.0"
host: "bookshelf.googleapis.com"
schemes:
- "https"
paths:
/books:
get:
summary: "Get all books"
operationId: "getAllBooks"
responses:
"200":
description: "A list of all books"
schema:
type: "array"
items:
$ref: "#/definitions/Book"
/books/{id}:
get:
summary: "Get a single book by id"
operationId: "getBookById"
parameters:
- name: "id"
in: "path"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: "The book with the given id"
schema:
$ref: "#/definitions/Book"
/books/{id}/categories:
get:
summary: "Get the categories for a book by id"
operationId: "getCategoriesForBookById"
parameters:
- name: "id"
in: "path"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: "The categories for the book with the given id"
schema:
type: "array"
items:
type: "string"
/books/{id}/authors:
get:
summary: "Get the authors for a book by id"
operationId: "getAuthorsForBookById"
parameters:
- name: "id"
in: "path"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: "The authors for the book with the given id"
schema:
type: "array"
items:
type: "string"
definitions:
Book:
type: "object"
properties:
id:
type: "integer"
format: "int64"
title:
type: "string"
categories:
type: "array"
items:
type: "string"
authors:
type: "array"
items:
type: "string"
yearPublished:
type: "integer"
format: "int32"
summary:
type: "string"
Generate the starter app
Now that the OpenAPI Specification has been generated, it would be good to get a starter application template for the API implementation, based on the specification and in a language/framework of your choice. Let’s ask Duet AI for help.
I save the specification YAML in a file and then give the following prompt:
Based on the above OpenAPI spec, can you generate a Python Flask App that uses only the Flask framework and will work with a local in memory list of books.
Duet AI responded with the following:
from flask import Flask, jsonify, request
app = Flask(__name__)
books = [
{
"id": 1,
"title": "The Catcher in the Rye",
"categories": ["Fiction", "Coming of age"],
"authors": ["J.D. Salinger"],
"yearPublished": 1951,
"summary": "The Catcher in the Rye is a 1951 novel by J. D. Salinger. Originally published for adults, it has since become popular with adolescent readers for its themes of teenage angst and alienation. The novel has been translated widely. Around one million copies are sold each year with total sales of more than 65 million books."
},
{
"id": 2,
"title": "To Kill a Mockingbird",
"categories": ["Fiction", "Southern Gothic"],
"authors": ["Harper Lee"],
"yearPublished": 1960,
"summary": "To Kill a Mockingbird is a novel by American author Harper Lee. It was published in 1960 and was instantly successful. The plot and characters are loosely based on Lee's observations of her family and neighbors, as well as on an event that occurred near her hometown in 1936, when she was 10 years old."
},
{
"id": 3,
"title": "The Great Gatsby",
"categories": ["Fiction", "American literature"],
"authors": ["F. Scott Fitzgerald"],
"yearPublished": 1925,
"summary": "The Great Gatsby is a 1925 novel written by American author F. Scott Fitzgerald that follows a cast of characters living in the fictional town of West Egg on prosperous Long Island in the summer of 1922."
}
]
@app.route("/")
def index():
return jsonify(books)
@app.route("/books/<int:id>", methods=["GET"])
def get_book(id):
book = [book for book in books if book["id"] == id]
if book:
return jsonify(book[0])
else:
return jsonify({"message": "Book not found"}), 404
@app.route("/books/<int:id>/categories", methods=["GET"])
def get_categories_for_book(id):
book = [book for book in books if book["id"] == id]
if book:
return jsonify(book[0]["categories"])
else:
return jsonify({"message": "Book not found"}), 404
@app.route("/books/<int:id>/authors", methods=["GET"])
def get_authors_for_book(id):
book = [book for book in books if book["id"] == id]
if book:
return jsonify(book[0]["authors"])
else:
return jsonify({"message": "Book not found"}), 404
if __name__ == "__main__":
app.run()
The response from Duet AI also included some information on the endpoints as shown below:
and instructions on how to run the application.
This is good stuff and helps me to get up and running with minutes and I have a basic Python Flask implementation for my API.
Using Duet AI to generate a Starter App from an OpenAPI Specification was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.
","author"=>"Romin Irani",
"link"=>"https://medium.com/google-cloud/using-duet-ai-to-generate-a-starter-app-from-an-openapi-specification-15d92e408bf9?source=rss-802a4d428d95------2",
"published_date"=>Fri, 01 Mar 2024 01:54:02.000000000 UTC +00:00,
"image_url"=>nil,
"feed_url"=>"https://medium.com/google-cloud/using-duet-ai-to-generate-a-starter-app-from-an-openapi-specification-15d92e408bf9?source=rss-802a4d428d95------2",
"language"=>nil,
"active"=>true,
"ricc_source"=>"feedjira::v1",
"created_at"=>Sun, 31 Mar 2024 21:41:19.845392000 UTC +00:00,
"updated_at"=>Mon, 13 May 2024 18:38:24.418003000 UTC +00:00,
"newspaper"=>"Romin Irani - Medium",
"macro_region"=>"Blogs"}