We can easily create a chatbot command-line interface (CLI) using neets.ai’s Text-To-Speech (TTS) and Large Language Model (LLM) APIs. This guide is compatible with Windows Terminal, but the steps can be applied to any terminal interface.
Prerequisites
- Python installation: Ensure you have Python version 3.8 or newer installed.
- MPV: Install MPV to handle audio playback. If you are on Windows/Linux, visit <https://mpv.io/installation>. If you are on Mac, run:
brew install mpv
Setting Up
- Open your terminal: Launch your terminal and navigate to your desired directory where you want to setup the project.
- Install Python dependencies: You need to install two Python packages. Execute the following commands in your terminal:
pip install requests
pip install openai
Creating Your Chatbot
- Code preparation: Open your chosen code editor. Copy and paste the following code from our API documentation below:
import shutil
import subprocess
from typing import Iterator
import requests
from openai import OpenAI
def play_stream(audio_stream: Iterator[bytes]) -> bytes:
process = subprocess.Popen(
["mpv", "--no-cache", "--no-terminal", "--", "fd://0"],
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
audio = b""
for chunk in audio_stream:
if chunk is not None:
process.stdin.write(chunk)
process.stdin.flush()
audio += chunk
if process.stdin:
process.stdin.close()
process.wait()
def say(text: str, api_key: str):
response = requests.request(
method="POST",
url="https://api.neets.ai/v1/tts",
headers={
"Content-Type": "application/json",
"X-API-Key": api_key
},
json={
"text": text,
"voice_id": "us-female-2",
"params": {
"model": "style-diff-500"
}
}
)
audio_stream = response.iter_content(chunk_size=None)
play_stream(audio_stream)
api_key = input("Neets API Key: ")
messages = []
client = OpenAI(
api_key=api_key,
base_url="https://api.neets.ai/v1"
)
while True:
user_message = input("Type a message: ")
messages.append({
"role": "user",
"content": user_message
})
response = client.chat.completions.create(
model="mistralai/Mixtral-8X7B-Instruct-v0.1",
messages=messages
)
bot_response = response.choices[0].message.content
messages.append({
"role": "assistant",
"content": bot_response
})
print("Bot: " + bot_response)
say(bot_response, api_key)
- Save your script: Ensure that you save the file with a clear name. For example:
chatbot.py
Running Your Chatbot
- Execute your script: In your terminal, run the script using the command
python chatbot.py
- Enter your API Key: When prompted, enter your
NEETS_API_KEY
. You can find this key on your API Keys page.
Now you're all set! Follow these steps to build your own Chatbot CLI using neets.ai's easy to use APIs.