Skip to main content

Prompt engineering with OpenAI

In this tutorial, we'll create a Modelbit deployment that performs prompt engineering and response handling while calling the OpenAI GPT API.

Add your API key to Modelbit

Make an OpenAI API Key to load into Modelbit. In Modelbit's Settings, click Integrations and then click the OpenAI tile. Add your API key to this form.

You're now ready to call OpenAI from Modelbit.

Make a Python function that calls OpenAI

In your Python notebook, install OpenAI's Python package, log into Modelbit, and set your OpenAI API key:

pip install openai
import modelbit
mb = modelbit.login()
import os
from openai import OpenAI

os.environ["OPENAI_API_KEY"] = modelbit.get_secret("OPENAI_API_KEY")

Now we'll create a function that generates prompts, sends them to OpenAI, and processes the responses. Our example function will summarize multiple product reviews into one simplified review.

prompt_header = """
Summarize the following product reviews. The reviews are all about the same product.
Be as concise as possible. Your summarized review should include the most important
details about the product's quality.

The reviews are as follows:
""" + "\n"

def summarize_product_reviews(reviews: str):
prompt = prompt_header + "\n\n" + reviews

completion = OpenAI().chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}])

summary = completion.choices[0].message.content
return { "summary": summary }

Let's call our function to test it:

reviews = [
"This vacuum worked for three months, then stopped working.",
"Best vacuum I've ever owned, except it's really loud.",
"My dog is afraid of this vacuum, but otherwise it has worked for 1 year.",
"After 6 months the vacuum started to make a grinding noise, but it still works.",
"I prefer this vacuum over the previous version, but I wish it had a warranty."
"My brother bought this vacuum for me: best present ever!"
]

summarize_product_reviews("\n\n".join(reviews))

Which returns:

{
"summary": "The vacuum works well initially but may have durability and noise issues over time."
}

Deploy to Modelbit to make a REST API

It's time to deploy our review summarizer to Modelbit so we can use it as a REST API in our product:

mb.deploy(summarize_product_reviews)

Click the View in Modelbit button to see your API endpoint, and call it with code like:

curl -s -XPOST "http://<your-workspace>.modelbit.com/v1/summarize_product_reviews/latest" -d '{"data": "This vacuum..." }'

You have successfully deployed an API endpoint that can handle prompt engineering and response formatting when calling the OpenAI API!