So I have been struggling with this issue for what seems like forever now (I’m pretty new to Python). I am using Python 3.7 (need it to be 3.7 due to variations in the versions of packages I am using for the project) to develop an AI chatbot system that can converse with you based on your text input. The program reads the contents of a series of .yml files when it starts. In one of the .yml files I am developing a syntax for when the first 5 characters match a ^###^ pattern, it will instead execute the code and return the result of that execution rather than just output text back to the user. For example:
Normal Conversation:
- - What is AI?
- Artificial Intelligence is the branch of engineering and science devoted to constructing machines that think.
Service/Code-based conversation:
- - Say hello to me
- ^###^print("HELLO")
The idea is that when you ask it to say hello to you, the ^##^print(“HELLO”) string will be retrieved from the .yml file, the first 5 characters of the response will be removed, the response will be sent to a separate function in the python code where it will run the code and store the result into a variable which will be returned from the function into a variable that will give the nice, clean result of HELLO to the user. I realize that this may be a bit hard to follow, but I will straighten up my code and condense everything once I have this whole error resolved. As a side note: Oracle is just what I am calling the project. I’m not trying to weave Java into this whole mess.
THE PROBLEM is that it does not store the result of the code being run/executed/evaluated into the variable like it should.
My code:
def executecode(input):
print("The code to be executed is: ",input)
#note: the input may occasionally have single quotes and/or double quotes in the input string
result = eval("{}".format(input))
print ("The result of the code eval: ", result)
test = eval("2+2")
test
print(test)
return result
@app.route("/get")
def get_bot_response():
userText = request.args.get('msg')
print("Oracle INTERPRETED input: ", userText)
ChatbotResponse = str(english_bot.get_response(userText))
print("CHATBOT RESPONSE VARIABLE: ", ChatbotResponse)
#The interpreted string was a request due to the ^###^ pattern in front of the response in the custom .yml file
if ChatbotResponse[:5] == '^###^':
print("---SERVICE REQUEST---")
print(executecode(ChatbotResponse[5:]))
interpreter_response = executecode(ChatbotResponse[5:])
print("Oracle RESPONDED with: ", interpreter_response)
else:
print("Oracle RESPONDED with: ", ChatbotResponse)
return ChatbotResponse
When I run this code, this is the output:
Oracle INTERPRETED input: How much RAM do you have?
CHATBOT RESPONSE VARIABLE: ^###^print("HELLO")
---SERVICE REQUEST---
The code to be executed is: print("HELLO")
HELLO
The result of the code eval: None
4
None
The code to be executed is: print("HELLO")
HELLO
The result of the code eval: None
4
Oracle RESPONDED with: None
Output on the website interface
Essentially, need it to say HELLO for the “The result of the code eval:” output. This should get it to where the chatbot responds with HELLO in the web interface, which is the end goal here. It seems as if it IS executing the code due to the HELLO’s after the “The code to be executed is:” output text. It’s just not storing it into a variable like I need it to.
I have tried eval, exec, ast.literal_eval(), converting the input to string with str(), changing up the single and double quotes, putting before pairs of quotes, and a few other things. Whenever I get it to where the program interprets “print(“HELLO”)” when it executes the code, it complains about the syntax. Also, from several days of looking online I have figured out that exec and eval aren’t generally favored due to a bunch of issues, however I genuinely do not care about that at the moment because I am trying to make something that works before I make something that is good and works. I have a feeling the problem is something small and stupid like it always is, but I have no idea what it could be. 🙁
I used these 2 resources as the foundation for the whole chatbot project:
Also, I am sorry for the rather lengthy and descriptive question. It’s rare that I have to ask a question of my own on stackoverflow because if I have a question, it usually already has a good answer. It feels like I’ve tried everything at this point. If you have a better suggestion of how to do this whole system or you think I should try approaching this another way, I’m open to ideas.
Thank you for any/all help. It is very much appreciated! 🙂
2
Answers
The issue is that python’s
print()
doesn’t have a return value, meaning it will always returnNone
.eval
simply evaluates some expression, and returns back the return value from that expression. Sinceprint()
returnsNone
, aneval
of some print statement will also returnNone
.What you need is a io stream manager! Here is a possible solution that captures any io output and returns that if the expression evaluates to
None
.As a final note, this approach is heavily linked to
eval
sinceeval
can only evaluate a single statement. If you want to extend your bot to multiple line statements, you will need to use exec, which changes the logic. Here’s a great resource detailing the differences betweeneval
andexec
: What's the difference between eval, exec, and compile?It is easy just convert try to create a new list and add the the updated values of that variable to it, for example:
if you’ve a variable name myVar store the values or even the questions no matter.
1- First declare a new list in your code as below:
2- If you’ve need to answer or display the value through myVar then you can do like below:
and this if you have like a generator for the values instead if you need the opposite which means the values are already stored then you will just update the second step to be like the following:
ans here all the values will be stored in your list and you can also do this in other way, for example using loops is will be much better if you have multiple values or answers.