-
Notifications
You must be signed in to change notification settings - Fork 21
added recipe for use case generate-acceptance-tests
#103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HitiIbm
wants to merge
4
commits into
main
Choose a base branch
from
recipe/419-generate-acceptance-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bbb156d
added recipe for use case `generate-acceptance-tests`
HitiIbm a2e2701
Merge branch 'main' of https://github.com/ibm-granite-community/grani…
HitiIbm 2ecc78d
added `recipes/Generate-Acceptance-Tests/Generate_Test_Api_Spec.ipynb`
HitiIbm 82a9fac
Merge branch 'main' into recipe/419-generate-acceptance-tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
275 changes: 275 additions & 0 deletions
275
recipes/Generate-Acceptance-Tests/Generate_Test_Api_Spec.ipynb
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,275 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "3e98b2a4-cf47-4523-bd07-827683728d31", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h2> Generate Acceptance Test Python code covering open api specification behaviour using ibm-granite-code hosted on Replicate</h2>" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "61bd9f68", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h3>Notebook goals:</h3>\n", | ||
| "\n", | ||
| "The learning goals of this notebook are:\n", | ||
| "\n", | ||
| "1. Connect to `ibm-granite/granite-8b-code-instruct-128k` hosted on Replicate, and use it to Generate code for Acceptance Test Python Code\n", | ||
| "2. Connect to `ibm-granite/granite-20b-code-instruct-8k` hosted on Replicate, and use it to Generate code for Acceptance Test Python Code\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "dd2578b0", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h3>Prerequisites: </h3>\n", | ||
| "\n", | ||
| "1. Create an account on Replicate.\n", | ||
| "2. Copy the Replicate API Token to an environment file (.env file) and place it in the same directory as this notebook. Environment variable can be named as `REPLICATE_API_TOKEN`.\n", | ||
| "3. Install `langchain` and `ibm-granite-community/utils` python packages using below pip command." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "f95d7b88", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%pip install langchain\n", | ||
| "%pip install git+https://github.com/ibm-granite-community/utils" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "00a45854-f221-466b-a0b4-78effeae9f41", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Introduction\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "f1ad0a1a", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h3>Acceptance tests: OpenAPI specification</h3>\n", | ||
| "\n", | ||
| "1. Designed for user, who inputs (chat UI) or selects (IDE plugin) an OpenAPI specification to request generation of acceptance tests for it.\n", | ||
| "2. Creates a response from the system that returns the acceptance test Python code that covers the behavior of the API.\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "fe10934b-14de-4b36-a251-1178a3ada873", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Using a local model" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "4352513c", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h3>Get the variables from .env file</h3>\n", | ||
| "\n", | ||
| "This guide will demonstrate a basic inference call using the replicate package.\n", | ||
| "\n", | ||
| "To establish an authenticated session, the code below imports necessary packages, and loads the environment variable `REPLICATE_API_TOKEN` from the .env file." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "cfc017ba-fe00-43a4-9299-d4610946076d", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import os\n", | ||
| "from dotenv import load_dotenv\n", | ||
| "from ibm_granite_community.langchain_utils import find_langchain_model\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:', replicate_api_token)\n", | ||
| "else:\n", | ||
| " print('Failed to load API token. Please check your .env file.')" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "2a3c0dce-3630-4408-bd51-23db676874cf", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Using a remotely-hosted model\n", | ||
| "\n", | ||
| "The Granite Code models are available on [Replicate](https://replicate.com/).\n", | ||
| "\n", | ||
| "At the moment, they are only available to members of the Granite Code team.\n", | ||
| "Request an invite to get access.\n", | ||
| "\n", | ||
| "This guide will demonstrate a basic inference call using the `replicate` package as well\n", | ||
| "as via LangChain.\n", | ||
| "In both cases, you will provide a [Replicate API Token](https://replicate.com/account/api-tokens).\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "5465c776-857f-48ec-ad32-40f5d6577c3a", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h3>Deployment ID and Model ID Specifications</h3>\n", | ||
| "\n", | ||
| "Mention the Deployment ID as granite and Specify the Model ID to be used for building the use-case." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 18, | ||
| "id": "2eeb4c2e-ec15-4b73-8229-35dae503115c", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "deployment_id = \"ibm/granite-dev\"\n", | ||
| "\n", | ||
| "model_id = \"ibm/granite-8b-code-instruct:50da94a0b1b5d28e3161d1312077d856eb673b87e633438362e4820fed563444\"" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "b4497b8b-786f-4b40-b0c9-0c12ad180219", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h3>Prompt Definition</h3>\n", | ||
| "\n", | ||
| "We further need to specify the prompt as an input to the model with apt configuration by including within it the specification along with the correct requirements needed. \n", | ||
| "\n", | ||
| "For an instance, in this use case, we implement the prompt to contain an instruction for generating acceptance test in the form of python code to cover the behavior of application of open api onto testing orders." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 19, | ||
| "id": "02f99de7-ff2e-4ae4-b1f2-1ac4453b4c19", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "prompt = \"Please generate acceptance test Python code that covers the behavior of the open api specification as per the specification \\\\n\\\\nopenapi: 3.0.0\\\\ninfo:\\\\n title: Test Generation System\\\\n description: API for creating test scenarios for orders\\\\n version: 1.0.0\\\\npaths:\\\\n /orders/count:\\\\n get:\\\\n summary: Test the fetch query for the count of orders in the system\\\\n responses:\\\\n 200:\\\\n description: OK\\\\n content:\\\\n application/json:\\\\n schema:\\\\n\\\"\"" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "cd20e206-40b8-4f45-a7a3-6e22d1c76c90", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "### Replicate package" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "79ba85a7", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "Python package replicate needs to be installed for being imported in the next step." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "4dcdf174-6f25-432c-b2d6-6370f4eff98b", | ||
| "metadata": { | ||
| "collapsed": true, | ||
| "jupyter": { | ||
| "outputs_hidden": true | ||
| }, | ||
| "scrolled": true | ||
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%pip install replicate" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "4b67dbe7", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h3>Import all the necessary libraries and packages</h3>\n", | ||
| "\n", | ||
| "Replicate package needs to be imported in order to be used for refering to the deployment for which the prompt is being directed to achieving the test/s generated." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 21, | ||
| "id": "65423430-1e61-4ed8-8396-3f96860463fe", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import replicate" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "4819e892", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "<h3>Fetching Acceptance Test as response to the prompt</h3>\n", | ||
| "\n", | ||
| "Next, using replicate, prediction is created with the prompt designed as per the configuration in the previous step. Furthermore, the exception handling is curated onto the response of the prompt to generate clean output for the user in terms of the test generated inclusing error handling." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "736d4153-3900-473c-a46b-d5e326dfb929", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "deployment = replicate.deployments.get(deployment_id)\n", | ||
| "print(\"prompt\", prompt)\n", | ||
| "prediction = deployment.predictions.create(\n", | ||
| " input={\"prompt\": prompt}\n", | ||
| ")\n", | ||
| "\n", | ||
| "prediction.wait()\n", | ||
| "\n", | ||
| "if (prediction.output[0].__contains__('mock')): \n", | ||
| " print('The provided input specification seems corrupted. Kindly check again and reattampt.')\n", | ||
| "else:\n", | ||
| " print(prediction.output[0].split('```python')[1])\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.5" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want to use
get_env_varfrom the utils repo.