Skip to content

Command Line Test Script for Development

The Genie framework creates an agentic API. There are numerous ways of interacting with an API. When you are developing agents, and you do not always want to carry the burden of spinning up a graphical user interface, we suggest using our command-line test script.

You will first have to install requests using the command pip install requests.

Create a file called test_dialogue.py and paste the following content:

test_dialogue.py
import time

import requests


HOST = 'http://127.0.0.1:8000'
BASE_URL = HOST + "/v1/ai/my_first"

response = requests.get(f"{BASE_URL}/start_session")
ai_response = response.json()
session_id = ai_response["session_id"]

while True:
    if ai_response["error"]:
        print("===")
        print(ai_response["error"])
        print("===")
        break

    if ai_response["response"]:
        print("\n---")
        print(ai_response["response"])

    if "user_input" in ai_response["next_actions"]:
        user_input = input("\n >> ")
        event = dict(
            session_id=session_id,
            event="user_input",
            event_input=user_input,
        )

    elif "poll" in ai_response["next_actions"]:
        time.sleep(1)
        print(".", end="", flush=True)
        event = dict(
            session_id=session_id,
            event="poll",
            event_input="",
        )

    elif "advance" in ai_response["next_actions"]:
        event = dict(
            session_id=session_id,
            event="advance",
            event_input="",
        )

    elif len(ai_response["next_actions"]) == 0:
        print("***")
        break

    else:
        print("BLOOP")
        break

    response = requests.post(f"{BASE_URL}/event", json=event)
    response.raise_for_status()
    ai_response = response.json()

As you can see, this script:

  1. Starts a new session with an agent registered under the model key my_first, running on localhost
  2. Enter a loop that
    1. Prints any errors that are caught
    2. Prints any response retrieved from the agent
    3. Checks what the next actions are that we can do
    4. if user_input, then we ask the user for the next user input
    5. if poll, then we send a poll request to see if the server has a response for us
    6. if advance, then we tell the server to advance to the next step
    7. if nothing remains to be done, we break out of the loop
    8. else we, well… error out

This will work well with our question / answer dialogue.