Skip to main content
POST
/
generate
Generate
curl --request POST \
  --url https://api.contextual.ai/v1/generate \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "<string>",
  "messages": [
    {
      "content": "<string>"
    }
  ],
  "knowledge": [
    "<string>"
  ],
  "system_prompt": "<string>",
  "avoid_commentary": false,
  "temperature": 0,
  "top_p": 0.9,
  "max_new_tokens": 1024
}
'
import requests

url = "https://api.contextual.ai/v1/generate"

payload = {
"model": "<string>",
"messages": [{ "content": "<string>" }],
"knowledge": ["<string>"],
"system_prompt": "<string>",
"avoid_commentary": False,
"temperature": 0,
"top_p": 0.9,
"max_new_tokens": 1024
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}

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

print(response.text)
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: '<string>',
messages: [{content: '<string>'}],
knowledge: ['<string>'],
system_prompt: '<string>',
avoid_commentary: false,
temperature: 0,
top_p: 0.9,
max_new_tokens: 1024
})
};

fetch('https://api.contextual.ai/v1/generate', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
<?php

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://api.contextual.ai/v1/generate",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => '<string>',
'messages' => [
[
'content' => '<string>'
]
],
'knowledge' => [
'<string>'
],
'system_prompt' => '<string>',
'avoid_commentary' => false,
'temperature' => 0,
'top_p' => 0.9,
'max_new_tokens' => 1024
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"strings"
"net/http"
"io"
)

func main() {

url := "https://api.contextual.ai/v1/generate"

payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"knowledge\": [\n \"<string>\"\n ],\n \"system_prompt\": \"<string>\",\n \"avoid_commentary\": false,\n \"temperature\": 0,\n \"top_p\": 0.9,\n \"max_new_tokens\": 1024\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://api.contextual.ai/v1/generate")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"knowledge\": [\n \"<string>\"\n ],\n \"system_prompt\": \"<string>\",\n \"avoid_commentary\": false,\n \"temperature\": 0,\n \"top_p\": 0.9,\n \"max_new_tokens\": 1024\n}")
.asString();
require 'uri'
require 'net/http'

url = URI("https://api.contextual.ai/v1/generate")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"messages\": [\n {\n \"content\": \"<string>\"\n }\n ],\n \"knowledge\": [\n \"<string>\"\n ],\n \"system_prompt\": \"<string>\",\n \"avoid_commentary\": false,\n \"temperature\": 0,\n \"top_p\": 0.9,\n \"max_new_tokens\": 1024\n}"

response = http.request(request)
puts response.read_body
{
  "response": "<string>"
}
{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>"
}
]
}

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Body

application/json

/generate input request object.

model
string
required

The version of the Contextual's GLM to use. Currently, we have v1 and v2.

messages
MessageAndRoleGenerateAPI · object[]
required

List of messages in the conversation so far. The last message must be from the user.

Minimum array length: 1
knowledge
string[]
required

The knowledge sources the model can use when generating a response.

system_prompt
string

Instructions that the model follows when generating responses. Note that we do not guarantee that the model follows these instructions exactly.

avoid_commentary
boolean
default:false

Flag to indicate whether the model should avoid providing additional commentary in responses. Commentary is conversational in nature and does not contain verifiable claims; therefore, commentary is not strictly grounded in available context. However, commentary may provide useful context which improves the helpfulness of responses.

temperature
number
default:0

The sampling temperature, which affects the randomness in the response. Note that higher temperature values can reduce groundedness.

Required range: 0 <= x <= 1
top_p
number
default:0.9

A parameter for nucleus sampling, an alternative to temperature which also affects the randomness of the response. Note that higher top_p values can reduce groundedness.

Required range: x <= 1
max_new_tokens
integer
default:1024

The maximum number of tokens that the model can generate in the response.

Required range: 1 <= x <= 2048

Response

Successful Response

/generate result object.

response
string
required

The model's response to the last user message.