Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bolt_app.py for verbose message output #41

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions apps/slackbot/bolt_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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__":
Expand Down