Guide: Chat

We provide models with a chat interface that can be accessed using reka.chat().

This page provides an introduction to using the chat interface. Full documentation of all the parameters for reka.chat() is given in the python client library documentation.

Single turn

A simple single turn request can be made with:

import reka

reka.API_KEY = "your-api-key"

response = reka.chat("What is the capital of the UK?")

Giving a response like:

{
  "type": "model",
  "text": "The capital of the United Kingdom is London.\n\n"
}

Conversation history

The conversation history is modelled as a list of dictionary objects with keys type (either "human" or "model"), and text. These must start with a human turn, and alternate back and forth with the model. Below is an example with a longer conversation history:

response = reka.chat(
    "What famous people share my name?",
    conversation_history=[
        {"type": "human", "text": "My name is Matt."},
        {"type": "model", "text": "Hello Matt! How can I help you today?\n\n"},
    ],
)

Giving a response like:

{
    "type": "model",
    "text": "Matt is a common name and there are many famous people who share it. Here are a few examples:\n\n"
            "- Matt Damon, an American actor and screenwriter\n\n"
            "- Matt LeBlanc, an American actor, [...]"
}

It is also possible to specify how the assistant’s response should start, by ending the conversation_history with a model turn:

import reka

response = reka.chat(
    conversation_history=[
        {
            "type": "human",
            "text": "Which is heavier, [A] a cubic foot of feathers, [B] a cubic foot of water, or [C] neither?",
        },
        {"type": "model", "text": "Final answer: ["},
    ],
    stop_words=["]"],
)
print(response)

This outputs:

{
  "type": "model",
  "text": "B",
  "finish_reason": "stop",
  "metadata": { "input_tokens": 39, "generated_tokens": 2 }
}

Advanced chat usage

There are various parameters that allow controlling the response from reka.chat().

The example below uses a final assistant turn in the conversation history to encourage the model to output a JSON object in our desired format. We also set the temperature to a relatively low value, and use stop_words to stop decoding at the end of the JSON object.

import reka

prompt = """
Below is a paragraph from wikipedia:

The Solar System is the gravitationally bound system of the Sun and the objects that orbit it.
The largest of such objects are the eight planets, in order from the Sun: four terrestrial planets named Mercury,
Venus, Earth and Mars, two gas giants named Jupiter and Saturn, and two ice giants named Uranus and Neptune.
The terrestrial planets have a definite surface and are mostly made of rock and metal. The gas giants are
mostly made of hydrogen and helium, while the ice giants are mostly made of 'volatile' substances such as water,
ammonia, and methane. In some texts, these terrestrial and giant planets are called the inner Solar System and outer
Solar System planets respectively.

Extract information about the planets from this paragraph as a JSON list of objects with keys 'planetName' and
'composition'. The 'composition' key should contain one or two words, and there should be no other keys.
""".strip()

json_prefix = """
[
    {
        "planetName":
""".strip()

response = reka.chat(
    conversation_history=[
        {"type": "human", "text": prompt},
        {
            "type": "model",
            "text": (
                "Sure, here is a JSON object conforming to that format:\n\n"
                f"```json\n{json_prefix}"
            ),
        },
    ],
    request_output_len=512,
    temperature=0.4,
    stop_words=["```\n"],
)

print((json_prefix + response["text"]).rstrip("`\n"))

This outputs:

[
  {
    "planetName": "Mercury",
    "composition": ["rock", "metal"]
  },
  {
    "planetName": "Venus",
    "composition": ["rock", "metal"]
  },
  {
    "planetName": "Earth",
    "composition": ["rock", "metal"]
  },
  {
    "planetName": "Mars",
    "composition": ["rock", "metal"]
  },
  {
    "planetName": "Jupiter",
    "composition": ["hydrogen", "helium"]
  },
  {
    "planetName": "Saturn",
    "composition": ["hydrogen", "helium"]
  },
  {
    "planetName": "Uranus",
    "composition": ["water", "ammonia", "methane"]
  },
  {
    "planetName": "Neptune",
    "composition": ["water", "ammonia", "methane"]
  }
]