I have an API that generates random educational questions for a user, however it is quite hard to understand since its not in a readable format. E.g, it generates questions like:
‘Let ( z ) be a complex number such that ( |z – i| = 2 ) and ( text{arg}(z) = frac{pi}{6} ). Find all possible values of ( z ) in the form ( a + bi ), where ( a ) and ( b ) are real numbers.
“frac{-bpm sqrt{b^{2}-4ac}}{2a}”
This is quite hard to read, and i don’t know how to make it look way nicer, i have tried laTeX formatting and sympy, but it was either confusing to implement or it did not work. I want it to turn into this:
https://i.sstatic.net/4Yr9pVLj.png
import requests, re
import sympy as sp
API_KEY = ‘sk…’
ENDPOINT = ‘https://api.openai.com/v1/chat/completions’
Generate a random math question
generate_question_message = [
{“role”: “system”, “content”: “You are a helpful assistant that generates math questions.”},
{“role”: “user”, “content”: “Generate a complex A-Level Mathematics question.”}
]
data_generate = {
“model”: “gpt-3.5-turbo”,
“messages”: generate_question_message,
“temperature”: 1,
“max_tokens”: 100
}
headers = {
“Content-Type”: “application/json”,
“Authorization”: f”Bearer {API_KEY}”
}
response_generate = requests.post(ENDPOINT, json=data_generate, headers=headers)
if response_generate.status_code == 200:
generated_question = response_generate.json()[‘choices’][0][‘message’][‘content’]
latex_question = f”text{{Question: }} {generated_question}”
print(latex_question)
user_answer = input("What is your answer to the question? ")
# Verify the user's answer
answer_verification_message = [
{"role": "system", "content": "You are a helpful assistant that verifies math answers."},
{"role": "user", "content": f"{generated_question}"},
{"role": "user", "content": f"Answer: {user_answer}"}
]
data_answer = {
"model": "gpt-3.5-turbo",
"messages": answer_verification_message,
"temperature": 1,
"max_tokens": 400
}
response_answer = requests.post(ENDPOINT, json=data_answer, headers=headers)
if response_answer.status_code == 200:
model_response = response_answer.json()['choices'][0]['message']['content']
print("Model's response:", model_response)
else:
print("Error:", response_answer.text)
else:
print(“Error:”, response_generate.text)
Generated Output:
Question: Solve the following trigonometric equation for ( 0 leq x leq 2pi )
[ 2 sin(2x) + sqrt{3} = 0 ]
Acrilyce is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.