Skip to content

Chat Completion with Session

Session

Some usecases require a session to provide a response. The session should be created before making the chat completion request. Currently, NEXTgpt supports two types of parameters for creating sessions:

  1. File uploads: Upload .txt, .doc, .docm, .docx, .pptx, .pdf, .md files.
  2. Parameter choice: Select options based on a predefined set of choices.

1. Session Creation

The parameters for creating a session are unique to each usecase. In order to determine the required parameters;

Make a GET request to /usecases/{usecase}/session/schema endpoint, which returns an OpenAPI schema JSON.

To view the parameters required for creating a session, use Swagger tools such as Swagger Editor. These tools will make it easier to understand the parameters needed for creating a session.

Using Python Code to Get Session ID

Alternatively, you can use the Python code provided below to configure a session and obtain a session ID. This script will:

  1. Prompt you for file paths (if needed) based on the returned schema.
  2. Present you with choices to select from (if needed) based on the returned schema.
  3. Send a POST request to the server to create a session.
  4. Return the session ID, which can be used for making API calls to the usecase.

Copy the code below and save it as configure_session.py:

from pathlib import Path
import requests

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

def get_session_id(usecase):
    # Obtain the session schema for the usecase
    headers={"Authorization": f"Bearer {API_KEY}"}
    url = f"{API_BASE_URL}/api/v1/usecases/{usecase}/session/schema"
    response = requests.get(url, headers=headers)
    if response.status_code == 422:
        print(f"Usecase {usecase} does not require a session id.")
        return ""
    response.raise_for_status()
    # Get user inputs for the session
    files = {}
    data = {}
    for name, schema in response.json()["components"]["schemas"].items():
        if name in ["HTTPValidationError", "ValidationError"]:
            continue
        elif name.startswith("Body_handler"):
            # Obtain file arguments for the session
            for arg in schema["required"]:
                fp = Path(input(f"Provide path to {arg} file: "))
                files[arg] = (fp.name, open(fp, "rb"))
        else:
            # Obtain selection arguments for the session
            choices = schema["enum"]
            for ix, choice in enumerate(choices):
                print(f"\t{ix}. {choice}")
            choice = int( input(f"Select an option for {name}: ") )
            data[name] = choices[choice]
    # Create session
    url = f"{API_BASE_URL}/api/v1/usecases/{usecase}/session"
    response = requests.post(url, headers=headers, files=files, params=data)
    response.raise_for_status()
    return response.text

print(get_session_id(usecase))

Ensure that your environment has the requests library installed.

Update API_BASE_URL, API_KEY, and usecase variables in the script and run it. Follow the prompts to configure the session. You should receive a session ID at the end.

python configure_session.py

2. Make an API call using the session ID

Now that you have the session ID, you can use it to make API calls. Copy the code below and save it as make_api_call.py:

import requests
import json

session_id = # replace with your session id

usecase = # add the name of your usecase

payload = {
  "model": usecase,
}

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

response.raise_for_status()
print(response.json())

Replace the session_id variable with the session ID you obtained in the previous step. Run the script to make an API call:

python make_api_call.py

If the call is successful, you should see the response data printed to the console.

Getting Help

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