chatgpt: Write a simple app using Python
chatgpt is here, will take our jobs? Will enslave us? will make us lazy without any critical thinking capabilities? Only time will know…
chatgpt is here, will take our jobs? Will enslave us? will make us lazy without any critical thinking capabilities? Only time will know, but for now, let’s explore how we can use it to do some calculations inside a Python script!
Things to take care
You would also need to generate an API key from openai from here after logging
https://beta.openai.com/overviewThen you need to install the openai lib
pip3 install openaiThe code
Save the following as chatgpt1.py openai
import openai
if __name__ == '__main__':
openai.api_key = "YOUR-OPENAI-KEY"
GENDER = "male"
WEIGHT_KG = 82
HEIGHT_CM = 178
AGE = 42
excerises = "run 5km in 30 minutes, 3 sets of biceps 12 reps each, 3 sets of triceps 12 reps each."
query = "I am a %s \
years old %s \
with a height of %s cm \
and weight %s kg, \
the excerises i did are %s. \
calculate in a comma seperated way 'exercise name','distance','reps/duration','calories burned' with each excercise a new line, last line is calorie sum"
query = query%(AGE,GENDER,HEIGHT_CM,WEIGHT_KG,excerises)
response = openai.Completion.create( model="text-davinci-003", prompt=query, temperature=0.1,max_tokens=1024)
res = response['choices'][0]['text']
for line in res.split("\n"):
print(line)This script does the following, it creates a chatgpt prompt like this
I am a 42 years old male with a height of 178 cm and weight 82 kg,
the excerises i did are run 5km in 30 minutes,
3 sets of biceps 12 reps each, 3 sets of triceps 12 reps each.
calculate in a comma seperated way
'exercise name','distance','reps/duration','calories burned'
with each excercise a new line, last line is calorie sumAnd then queries chatgpt. The core line that does the query and gets back the result is this one
response = openai.Completion.create( model="text-davinci-003", prompt=query, temperature=0.1,max_tokens=1024)- The model we use is ‘text-davinci-003’
- temperature: The temperature controls how much randomness is in the output.
Executing this produces the following output.
Since we had asked to format the output is very easy to print it
res = response['choices'][0]['text']
for line in res.split("\n"):
print(line)Output
Run,5km,30 minutes,400 calories,
Biceps,12 reps,3 sets,150 calories,
Triceps,12 reps,3 sets,150 calories,
Total, , ,700 caloriesImportant note:
If you run the script many times with the same data you will notice that the output varies a bit, this is happening because the calculations are done each time in a different manner based on how the model decides to calculate, this can be modified by the temperature parameter, in our script is set to 0.1 this is way most of the times produces the same results but not always, try to see what will happen if change the value to 0 or to near 1.
Conclusion
You see how easy was to have chatgpt to do all the calculations for us, the possibilities are endless you could create something that gives input from a microphone and writes the data to a google sheet and whatever you can think of!