forked from ai-powered-coder/trigger-apis-using-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (41 loc) · 1.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import openai
import requests
import json
def lights_on():
print("Lights on")
def lights_off():
print("Lights off")
def do_not_disturb():
print("Do not disturb")
def bitcoin_price():
url = "https://api.coindesk.com/v1/bpi/currentprice.json"
response = requests.get(url)
data = json.loads(response.text)
return data['bpi']['USD']['rate_float']
def main():
commands = ['lights_on', 'lights_off', 'do_not_disturb', 'bitcoin_price']
commandsWithDashInFront = [f'-{command}' for command in commands]
commandsAsString = '\n'.join(commandsWithDashInFront)
statement = input(f'What would you like to do?')
prompt = f'''
Statement:
{statement}
----
Choose a command that matches the statement from the list below:
{commandsAsString}
You can choose "nothing" if there is no match.
-----
Chosen command:
'''
completion = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=200)
if completion.choices[0].text == 'lights_on':
lights_on()
elif completion.choices[0].text == 'lights_off':
lights_off()
elif completion.choices[0].text == 'do_not_disturb':
do_not_disturb()
elif completion.choices[0].text == 'bitcoin_price':
print(bitcoin_price())
elif completion.choices[0].text == 'nothing':
print('No command matched')
main()