Skip to content

Instantly share code, notes, and snippets.

@mottle-ai
Last active June 13, 2024 07:00
Show Gist options
  • Save mottle-ai/0567970193e64795650d4a925d06d5b1 to your computer and use it in GitHub Desktop.
Save mottle-ai/0567970193e64795650d4a925d06d5b1 to your computer and use it in GitHub Desktop.
How to insert a conversation history into the API Mottle
import json
import os
import requests
from dotenv import load_dotenv
from yaspin import yaspin
load_dotenv()
BOT_ID = os.getenv("BOT_ID")
BOT_SECRET = os.getenv("BOT_SECRET")
url = f"https://api.mottle.com/connect?bot={BOT_ID}"
headers = {"Content-Type": "application/json"}
data = {
"secret": BOT_SECRET,
"stream": True,
"temperature": 0, # You can modify to be within 0 and 2 (though 0 is recommeded).
"conversation": [
{"isBot": True, "msg": "Hi there, how can I help you with Mottle"},
{"isBot": False, "msg": "wat services do you offer"},
{
"isBot": True,
"msg": "Hi! At Mottle, we offer a chatbot creation tool that allows you to create a chatbot from your existing documentation by simply uploading files.",
},
{"isBot": False, "msg": "$$?"},
],
}
bot_response = ""
response = None
with yaspin(text="Calling API", spinner="dots") as spinner:
response = requests.post(url, headers=headers, json=data, stream=True)
if response.status_code != 200:
spinner.fail(f"❌: {response.status_code} Error")
else:
spinner.ok("✅")
for line in response.iter_lines():
if not line:
continue
decoded_line = line.decode("utf-8")
if decoded_line.startswith("{") or not decoded_line.find(": ") > 0:
print(decoded_line)
break
field, value = decoded_line.split(": ", 1)
if field == "data":
data = json.loads(value)
msg = data.get("message")
bot_response += msg
print(msg, end="")
if data.get("isFinished"):
break
# The varibale `bot_response` now contains the response from the bot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment