Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,337 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "3e98b2a4-cf47-4523-bd07-827683728d31",
"metadata": {},
"source": [
"# Generate OpenAPI Specification with partial endpoint description using ibm-granite model"
]
},
{
"cell_type": "markdown",
"id": "00a45854-f221-466b-a0b4-78effeae9f41",
"metadata": {},
"source": [
"## Introduction - Text to OpenAPI Specification (2a)\n",
"In this notebook, you will learn how to convert natural language text that has partial endpoint descrption to Open API Spec using LLM. \n",
"Note: This notebook was tested in Windows OS\n"
]
},
{
"cell_type": "markdown",
"id": "fe10934b-14de-4b36-a251-1178a3ada873",
"metadata": {},
"source": [
"## Pre-requisites\n",
"Python 3.11 or later. \n",
"Install langchain and replicate Python packages using the following command in your terminal or command prompt:\n",
"!pip install langchain replicate\n",
"!pip install git+https://github.com/ibm-granite-community/utils"
]
},
{
"cell_type": "markdown",
"id": "1f63f1ed",
"metadata": {},
"source": [
"## Get the variables from .env file\n",
"The code below imports necessary packages, and loads the environment variable REPLICATE_API_TOKEN from the .env file."
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "cfc017ba-fe00-43a4-9299-d4610946076d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: python-dotenv in c:\\users\\00324h744\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (1.0.1)\n",
"Note: you may need to restart the kernel to use updated packages.\n",
"API token loaded successfully:\n"
]
}
],
"source": [
"import os\n",
"%pip install python-dotenv\n",
"from dotenv import load_dotenv\n",
"\n",
"# Load environment variables from the .env file\n",
"load_dotenv()\n",
"\n",
"# Retrieve the API token directly from environment variables\n",
"replicate_api_token = os.getenv('REPLICATE_API_TOKEN')\n",
"\n",
"if replicate_api_token:\n",
" print('API token loaded successfully:')\n",
"else:\n",
" print('Failed to load API token. Please check your .env file.')\n"
]
},
{
"cell_type": "markdown",
"id": "2a3c0dce-3630-4408-bd51-23db676874cf",
"metadata": {},
"source": [
"## Model Selection\n",
"In this section, we specify the model ID used to invoke specific models from IBM Granite hosted on Replicate platform.\n",
"\n",
"Model : granite-8b-code-instruct-128k"
]
},
{
"cell_type": "markdown",
"id": "5465c776-857f-48ec-ad32-40f5d6577c3a",
"metadata": {},
"source": [
"We will keep the model constant through the guide as well.\n",
"Replicate distinguishes between a \"deployment\" of a model from a \"model\".\n",
"In this case, we want to specify the Granite Code development deployment."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "2eeb4c2e-ec15-4b73-8229-35dae503115c",
"metadata": {},
"outputs": [],
"source": [
"from ibm_granite_community.langchain_utils import find_langchain_model\n",
"\n",
"model_id = \"ibm-granite/granite-8b-code-instruct-128k\""
]
},
{
"cell_type": "markdown",
"id": "df4c2501",
"metadata": {},
"source": [
"## Model Retrieval\n",
"Here, we retrieve the model using the find_langchain_model function. This function is designed to locate and initialize models hosted on Replicate."
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "651a6349",
"metadata": {},
"outputs": [],
"source": [
"granite_via_replicate = find_langchain_model(platform=\"Replicate\", model_id=model_id,\n",
" model_kwargs={\"top_k\": 50,\"temperature\":0.0,\"top_p\": 1,\"max_tokens\":8192})"
]
},
{
"cell_type": "markdown",
"id": "b4497b8b-786f-4b40-b0c9-0c12ad180219",
"metadata": {},
"source": [
"# Prompt for min/max values\n",
"A prompt is constructed to instruct the language model on what is expected. This prompt includes partial endpoint descrption and asks for a openAPI specification to be generated."
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "02f99de7-ff2e-4ae4-b1f2-1ac4453b4c19",
"metadata": {},
"outputs": [],
"source": [
"prompt = \"Generate OpenAPI specification for an API to create a new customer. The age of the customer should range between a min value of 18 and a max value of 100.\""
]
},
{
"cell_type": "markdown",
"id": "cd20e206-40b8-4f45-a7a3-6e22d1c76c90",
"metadata": {},
"source": [
"OpenAPI spec is generated."
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "736d4153-3900-473c-a46b-d5e326dfb929",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here is an example OpenAPI specification for an API to create a new customer:\n",
"```\n",
"openapi: 3.0.0\n",
"info:\n",
" title: Customer API\n",
" description: API to create and manage customers\n",
" version: 1.0.0\n",
"paths:\n",
" /customers:\n",
" post:\n",
" summary: Create a new customer\n",
" requestBody:\n",
" content:\n",
" application/json:\n",
" schema:\n",
" type: object\n",
" properties:\n",
" name:\n",
" type: string\n",
" description: The name of the customer\n",
" age:\n",
" type: integer\n",
" description: The age of the customer\n",
" minimum: 18\n",
" maximum: 100\n",
" responses:\n",
" '201':\n",
" description: Customer created successfully\n",
" content:\n",
" application/json:\n",
" schema:\n",
" type: object\n",
" properties:\n",
" id:\n",
" type: integer\n",
" description: The ID of the created customer\n",
" name:\n",
" type: string\n",
" description: The name of the customer\n",
" age:\n",
" type: integer\n",
" description: The age of the customer\n",
"```\n",
"In this specification, the `/customers` path is defined with a `POST` method to create a new customer. The request body is defined with a JSON schema that includes a `name` field of type `string` and an `age` field of type `integer` with a minimum value of 18 and a maximum value of 100. The response body is also defined with a JSON schema that includes an `id` field of type `integer` and `name` and `age` fields of type `string` and `integer`, respectively.\n",
"\n"
]
}
],
"source": [
"answer = granite_via_replicate.invoke(prompt)\n",
"print(answer)\n"
]
},
{
"cell_type": "markdown",
"id": "3eb49efa",
"metadata": {},
"source": [
"# Prompt for enumerations\n",
"A prompt is constructed to instruct the language model on what is expected. This prompt includes partial endpoint descrption and asks for a openAPI specification to be generated."
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "7f48c222",
"metadata": {},
"outputs": [],
"source": [
"prompt = \"Generate OpenAPI specification for an API to fetch list of products. The product status should be inventory vailable.\""
]
},
{
"cell_type": "markdown",
"id": "4d92db6c",
"metadata": {},
"source": [
"OpenAPI spec is generated."
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "086115b8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sure, here's an example OpenAPI specification for an API to fetch a list of products with an inventory available status:\n",
"```yaml\n",
"openapi: 3.0.0\n",
"info:\n",
" title: Product API\n",
" description: API to fetch a list of products with an inventory available status\n",
" version: 1.0.0\n",
"servers:\n",
" - url: https://api.example.com/products\n",
"paths:\n",
" /products:\n",
" get:\n",
" summary: Fetch a list of products with an inventory available status\n",
" responses:\n",
" '200':\n",
" description: Success\n",
" content:\n",
" application/json:\n",
" schema:\n",
" type: array\n",
" items:\n",
" $ref: '#/components/schemas/Product'\n",
" '400':\n",
" description: Bad Request\n",
" '500':\n",
" description: Internal Server Error\n",
"components:\n",
" schemas:\n",
" Product:\n",
" type: object\n",
" properties:\n",
" id:\n",
" type: integer\n",
" format: int64\n",
" name:\n",
" type: string\n",
" description:\n",
" type: string\n",
" price:\n",
" type: number\n",
" format: float\n",
" quantity:\n",
" type: integer\n",
" format: int32\n",
" status:\n",
" type: string\n",
" enum:\n",
" - available\n",
" - out-of-stock\n",
"```\n",
"This specification defines an API with a single endpoint `/products` that returns a list of products with an `available` status. The response includes a list of product objects with properties such as `id`, `name`, `description`, `price`, and `quantity`. The `status` property is an enum with two possible values: `available` and `out-of-stock`.\n",
"\n"
]
}
],
"source": [
"answer = granite_via_replicate.invoke(prompt)\n",
"print(answer)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}