diff --git a/apps/slackbot/bolt_app.py b/apps/slackbot/bolt_app.py index 96f711b0..4b64d802 100644 --- a/apps/slackbot/bolt_app.py +++ b/apps/slackbot/bolt_app.py @@ -61,7 +61,7 @@ def event_test(client, say, event): previous_messages = replies['messages'][:-1] results = get_response(question, previous_messages) - + say(results, thread_ts=thread_ts) @app.event("app_home_opened") @@ -128,6 +128,38 @@ def hello(): if 'PINECONE_API_KEY'not in os.environ: print("Warning: Pinecone API key not specified. Using local Chroma database.") local_memory = LocalChromaStore.from_folder('files', OPENAI_KEY).as_retriever() + +# ---- add this for verbose output --- # +def contains_verbose(query: str) -> bool: + '''looks for -verbose in the question and returns True or False''' + return "-verbose" in query.lower() + +def log_formatter(logger): + '''Formats the logger into readable string''' + log_strings = [] + for log in logger: + + reply = log["reply"] + if "thoughts" in reply: + # reply = json.loads(reply) + formatted_reply = f"""-- Step: {log["Step"]} -- \nThoughts: \n {reply["thoughts"]} """ + + if "command" in reply: # add command if it exists + formatted_reply += f"""\nCommand: \n {reply["command"]}""" + + log_strings.append(formatted_reply) + + else: # for final response + formatted_reply = f"""-- Step: {log["Step"]} -- \nFinal Response: \n {reply}""" + log_strings.append(formatted_reply) + + log_string = "\n".join(log_strings) + return log_string + +def remove_verbose(input_string): + # Split the string at the first occurrence of "#verbose" and everything after it + parts = input_string.split("#verbose", 1) + return parts[0] def get_response(question, previous_messages): llm = ChatOpenAI( @@ -146,14 +178,34 @@ def get_response(question, previous_messages): tools=get_tools(memory) ai_name='Sherpa' - ai_id = bot['user_id'] - question = question.replace(f'@{ai_id}', f'@{ai_name}') - - task_agent = TaskAgent.from_llm_and_tools(ai_name="Sherpa", ai_role="assistant", ai_id=bot['user_id'], memory=memory, tools=tools, previous_messages = previous_messages, llm=llm) - return task_agent.run(question) - + + task_agent = TaskAgent.from_llm_and_tools(ai_name="Sherpa", + ai_role="assistant", + ai_id=bot['user_id'], + memory=memory, tools=tools, + previous_messages = previous_messages, + llm=llm) + + if contains_verbose(query = question): + print("Verbose mode is on") + question = question.replace(f'@{ai_id}', f'@{ai_name}') + question = question.replace('-verbose', '') + response = task_agent.run(question) + logger = task_agent.logger # logger is updated after running task_agent.run(question) + try: # in case log_formatter fails + verbose_response = response + ' \n #verbose \n ' + log_formatter(logger) + except: + verbose_response = response + ' \n #verbose \n ' + str(logger) + + return verbose_response + + else: + print("Verbose mode is off") + question = question.replace(f'@{ai_id}', f'@{ai_name}') + response = task_agent.run(question) + return response # Start the server on port 3000 if __name__ == "__main__":