DEV Community

NEBULA DATA
NEBULA DATA

Posted on

MAKE THE CHATBOT MORE INTERACTIVE USING PYTHON ONLY USING NEBULA API


For today’s episode, I'll continue working on our project to make rag based chatbot using Nebula Data’s api.
Okay, from the previous episode, we already set up the account, api and the billing. Now I want to make a simple chatbot, but using CLI (command line interface) only (maybe in the next article, I will make the interface).
Okay, so we already have the base of our code from the Nebula documentation. Like this:

from openai import OpenAI
client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://llm.ai-nebula.com/v1"
)

response = client.chat.completions.create(
    model="gpt-5.2",
    messages=[
        {"role": "user", "content": "Hello!"}
    ]
)
print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

This is for testing the api either they’re working or not, by simply saying “Hello!” to the AI through the nebula api.

Now, what I wanna do is to make this ai can answer my questions.
Why don’t I just change the string “Hello!” to my question?

Well, technically it can, but that’s not interactive, because if we just change that part, it won't be possible to keep changing the code again and again each time we need to ask something to the AI.

So I want to make a loop and create a variable that can contain our questions, so we don’t need to change it again, all we need is just to ask via the CLI.

Okay, so first things first,
We need to make a variable, let’s name it “user_input”, inside this variable is our question. And this process:

response = client.chat.completions.create(
    model="gpt-5.2",
    messages=[
        {"role": "user", "content": "Hello!"}
    ]
)
Enter fullscreen mode Exit fullscreen mode

I will wrap them into a loop function, specifically the “while loop”,

What is a while loop?

A while loop is a control structure in programming that repeatedly runs a block of code as long as a condition is true.
The condition is checked before each iteration, so if it’s false from the start, the loop won’t run at all.


Okay, now if I applied the loop function on purpose to keep the AI running, then how can I stop them? Well, all I need is just to make a “Break Condition” that can be applied inside the loop and put a condition that can make the loop stop.


So the simplest way to do that is to make a conditional statement (if statement) that contains “break function”. So the condition I wanna make as if the user types “quit” or “exit”, the program will activate the break condition and finally call the break function.


This is how you can make the loop function work on a chatbot (it’s the easiest method I can explain). Now, after the loop is True, it will take the user input and process it into the AI using NEBULA API, so for doing that, all we need is just to place the response code inside the while loop condition:


And the rest or the outer while loop function is just initialize the NEBULA API, URL, and the Print function, so the final version should be like this:


Okay, all we need now is to run this program,

And to close the program:

And just like that, we can make the chatbot more interactive using the NEBULA API!
But many people use APIs just like that. It's not safe to write the API key in a Python file like that, so in the next article, I will show you how to use .env and the benefits of using it.
And for you guys who want to build a chatbot or even other things with AI, you can check NEBULA LAB here:
Nebula Lab

There’s much more than just an API KEY FOR ALL MODELS, they also have tons of other features, such as marketing, etc.

Top comments (0)