Skip to content

Chat Completion

Getting Started

To get started, you need to have the API_BASE_URL and the API_KEY issued from the NEXTgpt backend.

API_BASE_URL = # the URL for the NEXTgpt API
API_KEY = # add your api key

Chat Completion (without Session)

To start chat_completion for a specific usecase (which requires no session), use the following code sample.

import requests
import json

usecase_name = "GPT3"
debug = False # set to True if you want to see debug messages

payload = {
  "model": usecase_name,
  "messages": [
    {
      "role": "user",
      "content": "What is the capital of France?"
    }
  ]
}

url = f"{API_BASE_URL}/api/v1/chat/completions?debug={debug}"
headers={"Authorization": f"Bearer {API_KEY}"}
response = requests.post(url, headers=headers, data=json.dumps(payload))

if response.status_code == 200:
    print("Success! Response data:")
    print(response.json())
else:
    print(f"Error: {response.status_code}, {response.reason}")

Use the following code sample if your usecase includes image files

import requests
import json

usecase_name = "GPT3"
debug = False # set to True if you want to see debug messages

payload = {
  "model": usecase_name,
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What is the capital of France?"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://example.com/image.jpg",
            "detail": "high"
          }
        }
      ]
    }
  ]
}

url = f"{API_BASE_URL}/api/v1/chat/completions?debug={debug}"
headers={"Authorization": f"Bearer {API_KEY}"}
response = requests.post(url, headers=headers, data=json.dumps(payload))

if response.status_code == 200:
    print("Success! Response data:")
    print(response.json())
else:
    print(f"Error: {response.status_code}, {response.reason}")

Chat Completion Streaming

import requests
import json

usecase_name = "GPT3"
debug = False # set to True if you want to see debug messages

# set stream to True
payload = {
  "model": usecase_name,
  "stream": "True"
  "messages": [
    {
      "role": "user",
      "content": "What is the capital of France?"
    }
  ]
}

url = f"{API_BASE_URL}/api/v1/chat/completions?debug={debug}"
headers={"Authorization": f"Bearer {API_KEY}"}
response = requests.post(url, headers=headers, data=json.dumps(payload))

buffer = ""
for chunk in response.iter_content(decode_unicode=True):
    buffer += chunk

print(buffer)

If your usecase includes image files, update the payload messages as the following sample.

"messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What is the capital of France?"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://example.com/image.jpg",
            "detail": "high"
          }
        }
      ]
    }
  ]
Please refer to Chat Completion Streaming on how to handle the output.

Getting Help

Please contact NEXTgpt development team with your issues/suggestions using this form.