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/
Wolfram Alpha now available as a $2.99 PC app - The Verge
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's API seems enough to solve a math problem(while Chatgpt 3.5 cannot solve this problem correctly). But in fact, its natural language interpretion is much dumber than Chatgpt. Wolfram Alpha would say "Wolfram Alpha wasn't able to answer it" in these cases:
  
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

Popular Posts