EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

KB486763: Custom MicroStrategy AI Bot Interface run in Terminal (with REST API and Python)


Robert Prochowicz

Manager, Sales Engineering • MicroStrategy


MicroStrategy REST APIs allow building custom AI chat bots according to user requirements

Strategy Auto Bot is a great way to use AI to query data for insights. The Auto Bot has a user friendly interface with many useful features that improve user experience. In some cases, however, users would like to build their own custom interface for this functionality with a modern framework like React or Angular.
This demo is utilizing the recently introduced Strategy REST APIs for Chatbots. It is based on a Python script run in a terminal command window. The script is using three simple functions for interacting with the bot: 
- one that creates a bot instance
- one that posts a question entered by the user
- one that retrieves the answer generated by the chat bot
The script is run in terminal window.

ka0PW0000002Bx3YAE_0EMPW000006e1pz.jpeg

Video of the solution
Please note - Color formatting has been added in the code below (http://hilite.me/ ). Code copied from here might not work due to indentation issues. In that case, use attached file with sample code. Also this CRM is adding ';' after URLs. 
 


from mstrio.connection import Connection as mstrConnection
from time import sleep
from getpass import getpass
from time import sleep

base_url = 'https://SERVER/StrategyLibraryInsights/api'
login_mode = 1


welcome1="""
__        __   _                            _                   
\ \      / /__| | ___ ___  _ __ ___   ___  | |_ ___             
 \ \ /\ / / _ \ |/ __/ _ \| '_ ` _ \ / _ \ | __/ _ \            
  \ V  V /  __/ | (_| (_) | | | | | |  __/ | || (_) |           
   \_/\_/ \___|_|\___\___/|_| |_| |_|\___|  \__\___/            
 __  __ _               ____  _             _                   
|  \/  (_) ___ _ __ ___/ ___|| |_ _ __ __ _| |_ ___  __ _ _   _ 
| |\/| | |/ __| '__/ _ \___ \| __| '__/ _` | __/ _ \/ _` | | | |
| |  | | | (__| | | (_) |__) | |_| | | (_| | ||  __/ (_| | |_| |
|_|  |_|_|\___|_|  \___/____/ \__|_|  \__,_|\__\___|\__, |\__, |
 ____        _   _                                  |___/ |___/ 
| __ )  ___ | |_| |                                             
|  _ \ / _ \| __| |                                             
| |_) | (_) | |_|_|                                             
|____/ \___/ \__(_)                                                                                        

"""

ascii_car='''
              ______--------___
             /|             / |
  o___________|_\__________/__|
 ]|___     |  |=   ||  =|___  |"
 //   \\    |  |____||_///   \\|"
|  X  |\--------------/|  X  |\"
 \___/  Strategy   \___/
 
'''
for line in welcome1.split('\n'):
    print(line)
    sleep(0.05)

mstr_username= input("Enter username: ")
mstr_password = getpass()
project_id = "B7CA92F04B9FAE8D941C3E9B7E0CD754"  # Replace with your project ID
bot_id="91B3A1DD094A49C235C43994DA546868"        # Replace with your Bot ID

# Create Bot Instance
def post_bot_instance(connection, botId):
    url_add=f"/api/bots/{botId}/instances"
    res=connection.post(url=connection.base_url+url_add)
    return res.json()['id']

# Post a question
def post_question(connection, botId, instanceId, question):
    header_prefer = {"Prefer": "respond-async"}
    url_add=f"/api/bots/{botId}/instances/{instanceId}/questions"
    res=connection.post(url=connection.base_url+url_add, json=question, headers=header_prefer) #json!!
    return res.json()['id']

# Retrieve the answer
def get_question(connection, botId, questionId):
    url_add=f"/api/bots/{botId}/questions/{questionId}"
    res=connection.get(url=connection.base_url+url_add)
    return res.json()['answers']

# Establish Connection
conn = mstrConnection(base_url, mstr_username, mstr_password, login_mode=login_mode, verbose=False)
conn.select_project(project_id=project_id)

# Create Bot Instance
inst_id = post_bot_instance(conn, bot_id)

qtext="connected"
while qtext != "0":
    qtext = input("\nEnter your question or 0 to close: ")  # What To Do ???
    print()

    if qtext == "0":
        print("Good bye!")
        print(ascii_car)
    else:
        question={'text': qtext}
        qr=post_question(conn, bot_id, inst_id, question)

        answ=get_question(conn, bot_id, qr)
        while not answ:    
            print("...")
            sleep(3)
            answ=get_question(conn, bot_id, qr)
        print(answ[0]['text'])

conn.close()

Customization Warning:
This customization is provided as a convenience to Strategy users and is only directly applicable to the version stated. While this code may apply to other releases directly, Strategy Technical Support makes no guarantees that the code provided will apply to any future or previous builds. In the event of a code change in future builds, Strategy Technical Support makes no guarantee that an updated version of this particular customization will be provided. For enhancements to this customization or to incorporate similar functionality into other versions, contact your Account Executive to inquire about Strategy Consulting assistance.


Comment

0 comments

Details

Knowledge Article

Published:

June 24, 2024

Last Updated:

June 24, 2024