@@ -0,0 +1,2865 @@
+from json import JSONDecodeError%0Aimport requests%0A%0A# where to find the API%0AapiRoot = %22https://api.hutoma.com/v1/%22%0A%0A# Your Client Key%0A# This can be found in the AI setting page%0Aauth = 'YOUR_AUTH_KEY_HERE'%0A%0A# The AI id%0A# This can be also found in the AI setting page%0AaiId = 'YOUR_AI_ID_HERE'%0A%0A%0A# Chat response wrapper%0Aclass ChatResponse:%0A    def __init__(self, callResult):%0A        self.success = False%0A        try:%0A            # decode from json if possible%0A            result = callResult.json()%0A            # if there is a status then the response came from the API server%0A            if result%5B'status'%5D:%0A                # so store a copy of the json%0A                self.response = result%0A                # copy the json result code%0A                code = result%5B'status'%5D%5B'code'%5D%0A                # assemble a description of the result%0A                self.text = str(code) + ': ' + result%5B'status'%5D%5B'info'%5D%0A                # if we got a 200 OK then the the json object has the answer data%0A                if code == 200:%0A                    self.success = True%0A            else:%0A                # otherwise, assemble a description from the HTTP results%0A                self.text = 'Error ' + str(callResult.status_code) + ': ' + callResult.reason%0A        except JSONDecodeError:%0A            self.text = 'Error ' + str(callResult.status_code) + ': ' + callResult.reason%0A%0A%0A# chat API call%0Adef chat(apiRoot, auth, aiid, sayWhat, chatID=%22%22):%0A    # build the query%0A    query = %7B'q': sayWhat%7D%0A    if chatID != %22%22:%0A        query%5B'chatId'%5D = chatID%0A    # add the auth header%0A    headers = %7B'Authorization': %22Bearer %22 + auth%7D%0A    # make the http call and put the result into a wrapper%0A    return ChatResponse(requests.get(apiRoot + %22ai/%22 + aiid + %22/chat%22, params=query, headers=headers))%0A%0A%0A# history and chatId are stored between calls to keep track of the conversation%0Ahistory = %22%22%0AchatId = %22%22%0A%0Awhile True:%0A    # user input%0A    q = input(%22Human: %22)%0A    # make the http call%0A    chatResponse = chat(apiRoot, auth, aiId, q, chatID=chatId)%0A    # if the call succeeded then print the answer and store the chatId%0A    if chatResponse.success:%0A%0A        # gets the current chat score%0A        score = float(chatResponse.response%5B%22result%22%5D%5B%22score%22%5D)%0A        # if the score if greater than 0.5 output the AI response. %0A        # if not, redirect the conversation to a human agent%0A        if (score %3E 0.5):%0A            print(%22Ai: %22, chatResponse.response%5B%22result%22%5D%5B%22answer%22%5D)%0A            chatId = chatResponse.response%5B%22chatId%22%5D%0A        else:%0A            print(%22Ai: Looks like I am not understanding our conversation. Let me connect you with an agent. Please wait.%22)%0A            chatId = ''%0A            # TODO: Add here the code that will connect with your backend%0A    else:%0A        # otherwise, tell the user what went wrong and exit%0A        print(chatResponse.text)%0A        break%0A

Create conversation-redirect.py