Quickstart

Using AI21 Studio, you can solve challenging text understanding and generation tasks. Its straightforward API and interactive playground allows you to get started in minutes, without any prior knowledge in LLMs.

Below is a quickstart guide to cover all the basics you need in order to start building your solution. After that, you should feel comfortable using our large language models to start and solve your own use-case.

Some basics

A language model takes some text as input and produces a likely continuation. You can think of it as an exceptional student that can follow instructions and imitate examples.

Here are two words you should know:

  • Prompt - the input you provide to the model.

  • Completion - the output text the model returns.

Experiment with your first use-case

Imagine owning an online retail platform. Your job requires you to come up with appealing titles for your products. By using AI21 Studio language models, you can optimize the process.

1. Tell the model what you want to do

The most naive thing to do is simply provide the model a prompt which is a simple instruction. This is what it looks like:

PROMPT Write a product title for a T-shirt to be published on an online retail platform. COMPLETION The coolest T-Shirt on the planet

The title is nice, but it is very generic. Let's add the T-shirt type (sports) to the prompt and see what we get:

PROMPT Write a product title for a sports T-shirt to be published on an online retail platform COMPLETION The Ultimate Sports T-Shirt: Stay cool and dry in style

The title has been refined to reflect the actual product. You can also apply a more realistic scenario: ask the model to incorporate specific keywords in the title.

PROMPT >> Write a product title for a sports T-shirt to be published on an online retail platform.
Include the following keywords: activewear, gym, dryfit

COMPLETION >> Activewear Gym Dryfit T-Shirt - Stay Fit, Stay Cool

This is called Zero-shot prompting, because we are giving the model zero examples in the prompt. If you care about a specific style you want the model to follow, you should consider adding examples to your prompt.

2. Provide examples to the model

By providing the model with examples, just as a human would, it could produce completions that are more aligned with your intentions. It could also adhere to patterns that are more difficult to explain via instructions. In order to help the model distinguish between each example, we use a stop sequence. The stop sequence itself should be immediately recognizable when looking at the text as a whole. In this example, the stop sequence is ##:

PROMPT Write a product title for a T-shirt with a pocket by MSPC to be published on an online retail platform. MSPC Men's Solid Crew Neck Short-Sleeve Pocket T-Shirt ## Write a product title for a graphic T-shirt by PBNJ to be published on an online retail platform. PBNJ Men's Short Sleeve 100% Cotton Nautical Series Graphic Tee ## Write a product title for a sports T-shirt by YSYB to be published on an online retail platform. COMPLETION YSYB Men's Performance Short Sleeve Moisture-Wicking Athletic T-Shirt

This is called Few-shot prompting, because we are inserting a prompt which includes a few examples.

3. Adjust the parameters

Another way to affect the completion is to adjust some of the model parameters. A useful parameter is the temperature. You can increase creativity by tweaking the temperature. With temperature 0, the model will always choose the most probable completion, so it will always be the same. Increasing the temperature will provide variable completions, where the completion may be different for every generation:

TEMPERATURE 0: Try 1 >>YSYB Men's Performance Short Sleeve Athletic T-Shirt Try 2 >>YSYB Men's Performance Short Sleeve Athletic T-Shirt Try 3 >>YSYB Men's Performance Short Sleeve Athletic T-Shirt ...You get the point...
TEMPERATURE 1.0: Try 1 >>YSYB Men's Dry-Fit Performance Short Sleeve Athletic Sport T-Shirt with Reflective Logo Try 2 >>YSYB Men's Quick-Dry Performance Athletic Short Sleeve T-Shirt Try 3 >>YSYB Men's Athletic Performance Short Sleeve T-Shirt

A task that requires accurate results (such as classification) is best performed with low temperature, whereas a task that requires more creativity should be conducted with high temperature (0.7 is a reasonable starting point).

Want to explore the various descriptions the model can generate? Try it yourself!

4. Make it a part of your product

Once you have created your prompt, you can easily integrate it into your product using our Python SDK as shown here:

import os
from ai21 import AI21Client
from ai21.models.chat import ChatMessage
os.environ["AI21_API_KEY"] = "<YOUR_API_KEY>"

client = AI21Client()

def suggest_product_title():
    response = client.chat.completions.create(
        model="jamba-instruct",  # Latest model
        messages=[ChatMessage(   # Single message with a single prompt
            role="user",
            content="Write a product title for a sports T-shirt to be published on an online retail platform. Include the following keywords: activewear, gym, dryfit."
    )],
        temperature=0.8,
        max_tokens=200 # You can also mention a max length in the prompt "limit responses to twenty words"
    )
    print(response.choices[0].message.content)

### RESPONSE

ActiveDryFit Gym T-Shirt: Ultimate Activewear for Men and Women - Perfect for Workout and Fitness
import os
from ai21 import AI21Client
from ai21.models.chat import ChatMessage
os.environ["AI21_API_KEY"] = "<YOUR_API_KEY>"

client = AI21Client()

def suggest_multiple_product_title():
    response = client.chat.completions.create(
        model="jamba-instruct",
        messages=[ChatMessage(
            role="user",
            content="Write a product title for a sports T-shirt to be published on an online retail platform. Include the following keywords: activewear, gym, dryfit."
    )],
        temperature=0.8,
        n=5 # Number of suggestions. Default = 1
    )
    for suggestion in response.choices:
        print(suggestion.message.content)

### RESPONSE

"Premium Activewear Gym Dryfit T-Shirt for Ultimate Performance"
"ActiveGear DryFit Gym Performance T-Shirt for Men and Women"
"Ultra-DryFit Gym Activewear T-Shirt for High-Performance Workouts"
"Activewear Gym Dryfit Performance T-Shirt for Men and Women - Perfect for Workouts and Training"
"ActiveDry: Premium Dryfit Gym T-Shirt for Men - Best Activewear for Workouts"

See the REST API reference for the endpoint called above.

What's next?