Skip to main content

Prompt engineering with OpenAI

You can call OpenAI from Modelbit deployments, making it easy to add business logic and data processing around the OpenAI API call.

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.

Example function calling OpenAI

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.

import os, modelbit
from openai import OpenAI

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

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 }

Call the function to test that it works:

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

Finally, use mb.deploy or Git to deploy summarize_product_reviews to Modelbit.