48 lines
1.1 KiB
Python
Executable File
48 lines
1.1 KiB
Python
Executable File
import os
|
|
import wikipedia
|
|
import wolframalpha
|
|
|
|
import help_methods
|
|
|
|
|
|
def answer_question(message):
|
|
"""
|
|
answer_question(message)
|
|
|
|
Submits a request to the wolfram API and returns the response
|
|
If no answer is found, tries wikipedia. If that fails, apologizes
|
|
"""
|
|
|
|
if len(message.split()) > 1:
|
|
client = wolframalpha.Client(os.getenv("wolfram_token"))
|
|
question = " ".join(message.split()[1:])
|
|
try:
|
|
res = client.query(question)
|
|
return next(res.results).text
|
|
except Exception:
|
|
try:
|
|
return wikipedia.summary(question, sentences=5)
|
|
except Exception:
|
|
return "Sorry, I'm unable to answer that"
|
|
|
|
return help_methods.get_help_message("message")
|
|
|
|
|
|
def open_ai(prompt):
|
|
import os
|
|
import openai
|
|
|
|
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
response = openai.Completion.create(
|
|
model="text-ada-001",
|
|
prompt=prompt,
|
|
temperature=0.7,
|
|
max_tokens=256,
|
|
top_p=1,
|
|
frequency_penalty=0,
|
|
presence_penalty=0,
|
|
)["choices"][0]["text"]
|
|
|
|
return response
|