Chatgpt + Wolfram Alpha
Preparation:
1 get a Chatgpt API (open OpenAI account)
2 get a Wolfram Alpha API (100% Free! You need to sign up here:
https://developer.wolframalpha.com/)
https://developer.wolframalpha.com/)
![Wolfram Alpha now available as a $2.99 PC app - The Verge](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcdn.vox-cdn.com%2Fthumbor%2FdkYJxU-jqX6YWV8LHoL5yZoervY%3D%2F0x5%3A533x360%2F1200x800%2Ffilters%3Afocal(0x5%3A533x360)%2Fcdn.vox-cdn.com%2Fassets%2F1084845%2Fwa-logo-stacked1-large.jpeg&f=1&nofb=1&ipt=c07167716fa61ce2377525704df39d779b639aeba0c71b86994f0e05f3640ac0&ipo=images)
3 download relevant packages
pip install openai langchain wolframalpha
Firstly, we set some basic environment
import os
import openai, langchain, wolframalpha
from langchain.llms import OpenAI
# Your API_key for Chatgpt (eg."sk-AORoxxxxxxxxxxxxxxxxxx")
os.environ["OPENAI_API_KEY"]=API.chatgpt
# btw I use API.chatgpt because I store all my keys in an "API.py"
# 这是代理,翻墙软件一般会告诉你端口 non-Chinese don't have to set this:
os.environ["OPENAI_PROXY"] = "http://192.168.0.123:6666"
# You API_key for WolframAlpha (eg.X0004ET-TU200000J)
os.environ["WOLFRAM_ALPHA_APPID"]=API.wolfram_show_steps
With "WolframAlphaAPIWrapper", we can ask wolfram to solve maths problems
from langchain.utilities.wolfram_alpha import WolframAlphaAPIWrapper
wolfram = WolframAlphaAPIWrapper()
wolfram.run("intergral of 2xe^x")
'''Assumption: integral2 x e^x dx = 2 e^x (x - 1) + constant
Answer: integral2 x e^x dx = 2 e^x (x - 1) + constant'''
wolfram.run("Question 1: how to calculate \int 2xe^xdx")
wolfram.run("GOD pls help me calculate \int 2xe^xdx")
wolfram.run("I learnt a function 2xe^x in my class, but I don't know its intergral")
"Wolfram Alpha wasn't able to answer it"
We can use Chatgpt to help us translate our request into a direct maths question
from langchain.prompts import PromptTemplate
Chatgpt=OpenAI(temperature=0,model_name="text-davinci-003")
prompt = PromptTemplate.from_template('''
help me turn this prompt
into a direct maths problem
for wolfram to solve{question}'''
# Wolfram can't solve this alone:
my_question="I learnt a function 2xe^x in my class, but I don't know its intergral"
prompt_for_Wolfram=Chatgpt(prompt.format(question=my_question))
# Now wolfram solve 'Find the integral of 2x*e^x.' instead
wolfram.run(prompt_for_Wolfram)
'''
Assumption: integral2 x e^x dx = 2 e^x (x - 1) + constant
Answer: integral2 x e^x dx = 2 e^x (x - 1) + constant
'''
We can also write a function called SuperWolfram
def SuperWolfram(prompt):
Chatgpt=OpenAI(temperature=0.2,model_name="gpt-3.5-turbo")
# you can change this template as you like
template1 = PromptTemplate.from_template('''
find out ONE most difficult math problem in this question without
telling me and turn it into a direct, simple one maths calculation
problem for wolfram to solve(All in ENGLIGH and JUST output ONE
DIRECT SIMPLIST CALCULATION PROBLEM): {question}.
so if i input "v=2m/s,t=3s,s=?" you only output: "What is 2*3?"
''')
# Combine template with prompt
prompt_for_Wolfram=Chatgpt(template1.format(question=prompt))
print("Wolfram's task: "+prompt_for_Wolfram+"\n")
# let Wolfram solve this new task
output_Wolfram=wolfram.run(prompt_for_Wolfram)
if output_Wolfram=="Wolfram Alpha wasn't able to answer it":
return "Wolfram dies"
else:
# Search for xxxx in "Assumption:wwww/nAnswer:xxxx"
output_Wolfram=wolfram.run(prompt_for_Wolfram)
index = output_Wolfram.index("Answer:")
output_Wolfram = output_Wolfram[index + len("Answer:") + 1:]
print("Wolfram's Answer: "+output_Wolfram+"\n")
# let Chatgpt to give a final output by giving it origin prompt and output of Wolfram
template2 = PromptTemplate.from_template('''{question}
, given that WolframAlpha's solution to "{prompt_for_Wolfram}"
is "{output_Wolfram}".please give me a humanized output ''')
# we increase the "temperature" so every result differs
Chatgpt=OpenAI(temperature=0.8,model_name="text-davinci-003")
final_output=Chatgpt(template2.format(question=prompt,prompt_for_Wolfram=prompt_for_Wolfram,output_Wolfram=output_Wolfram))
print("Answered by Chatgpt x WolframAlpha: "+final_output)
It is convenient now to ask a question that contains a complex maths problem
SuperWolfram("If I walk \int_0^1 x^2 e^xdx meters per second, how far do I get after 36s")
Wolfram's task: What is the integral of x^2 e^x from 0 to 1? Wolfram's Answer: integral_0^1 x^2 e^x dx = e - 2≈0.71828 Answered by Chatgpt x WolframAlpha: After 36 seconds, you will have traveled 25.8 meters.
SuperWolfram('''I want to buy a gift for my neighbor. Her age is the variance of 25, 33, 23, 25 and 28. How old is she? Is she considered young, middle-aged, or elderly based on her age? What kind of gift would be more suitable for her age?''')
Wolfram's task: What is the variance of 25, 33, 23, 25, and 28? Wolfram's Answer: 76/5 = 15.2 Answered by Chatgpt x WolframAlpha: She is approximately 15 years old. She is considered to be young. A suitable gift for her would be something personalized that reflects her interests, such as a book, a piece of jewelry, or a framed photo.
Comments
Post a Comment