J2 Chat API

📘

Note

If you are a new user, we recommend using Jamba Instruct, our newest foundation model, for chat interactions. Jurassic (J2) models will no longer be supported by November 11, 2024.

Leverage the power the Jurassic-2 chat model to generate dynamic responses for interactive conversations. Here's how you can integrate and make the most of it.

Sending a Request

To solicit a response to a set of messages, dispatch an HTTP request to the chat endpoint. This request should encompass:

  1. A sequence of text messages.
  2. Relevant parameters to modulate the generation of text.

For authentication, it's imperative to include your API key within the request headers.

Post submission of your chat request, anticipate a response encompassing the generated text message.

Chat API Parameters

Utilize the following parameters to modify the Jurassic-2 chat model's behavior, enabling developers to calibrate the model's replies for optimized outcomes:

  • messages: A sequence of messages ingested by the model, which then returns the assistant's response. Each message object should be structured as:
    • text: The message content.
    • role: The sender's role, either user or assistant.
  • system: Offers the model overarching guidance on its response approach, encapsulating context, tone, guardrails, and more.

Parameters such as temperature, maxTokens, numResults, among others, are also compatible with this API. Delve deeper into these parameters here.

Example: Initiating a Chat Message

import requests

url = "https://api.ai21.com/studio/v1/j2-ultra/chat"

payload = {
    "numResults": 1,
    "temperature": 0.7,
    "messages": [
        {
            "text": "I'm crafting a market analysis tool for fintech leaders. How should I initiate the process?",
            "role": "user"
        }
    ],
    "system": "You are an AI assistant for business research. Your responses should be informative and concise."

}
headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "Authorization": "Bearer YOUR_API_KEY"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)

Understanding the Response

Each response contains:

  • id: A distinct identifier for the response.
  • outputs: A list with one object, detailing:
    • text: The generated message corresponding to the request's final message.
    • role: The message sender's role. Note: Messages from the Chat API are invariably from the assistant, implying their role is consistently assistant.

Example Response

{
   "id":"641eb7d2-bbc1-5316-40c4-d8b6e0ff3a43",
   "outputs":[
      {
         "text":"1. Identify the target audience: Clearly define the fintech leaders who will benefit from the analysis tool. Consider factors such as industry, company size, and job function.\n2. Define the purpose of the tool: Define the key objectives of the market analysis tool, such as identifying market trends, assessing competition, or evaluating potential new markets.\n3. Determine the data sources: Identify the data sources that will be used to generate the analysis, such as market research reports, industry publications, and financial data.\n4. Design the user interface: Determine the layout of the user interface, including the types of charts and graphs that should be included, and the placement of key information.\n5. Test and refine the tool: Conduct user testing to ensure that the tool is easy to use and provides valuable insights. Make any necessary adjustments based on feedback.",
         "role":"assistant"
      }
   ]
}

Maintaining Conversation Continuity

To ensure a seamless conversational flow, update the messages list before dispatching a new request. When the API returns a response, append the returned text message to the messages list as a new assistant message, followed by the subsequent user message.

Refer to the following example for clarity:

import requests

url = "https://api.ai21.com/studio/v1/j2-ultra/chat"

payload = {
    "numResults": 1,
    "temperature": 0.7,
    "messages": [
        {
            "text": "I'm crafting a market analysis tool for fintech leaders. How should I initiate the process?",
            "role": "user"
        },
        {
            "text": "1. Identify the target audience: Clearly define the fintech leaders who will benefit from the analysis tool. Consider factors such as industry, company size, and job function.\n2. Define the purpose of the tool: Define the key objectives of the market analysis tool, such as identifying market trends, assessing competition, or evaluating potential new markets.\n3. Determine the data sources: Identify the data sources that will be used to generate the analysis, such as market research reports, industry publications, and financial data.\n4. Design the user interface: Determine the layout of the user interface, including the types of charts and graphs that should be included, and the placement of key information.\n5. Test and refine the tool: Conduct user testing to ensure that the tool is easy to use and provides valuable insights. Make any necessary adjustments based on feedback.",
            "role": "assistant"
        },
        {
            "text": "Can you elaborate on the second item?",
            "role": "user"
        }
    ],
    "system": "You are an AI assistant for business research. Your responses should be informative and concise."

}
headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "Authorization": "Bearer YOUR_API_KEY"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)