-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatGPT_api_example.py
More file actions
89 lines (69 loc) · 2.99 KB
/
chatGPT_api_example.py
File metadata and controls
89 lines (69 loc) · 2.99 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
from chatGPT_api import get_GPT_response
import json
import pandas as pd
# Get ChatGPT to choose a character
# Let's try the letter R
l = 'R'
#VERSION 1 - SIMPLE PROMPT
prompt = (f"Create a list of no more than 3 very famous characters, either real or fictional, beginning with the letter {l}. They must be well-known to be used to play a famous character guessing game"
f"Use only the character's names."
)
famous = get_GPT_response(prompt)
'''
1. Romeo
2. Robin Hood
3. Ronald McDonald
'''
# VERSION 2 - ADD SYSTEM ROLE AND MORE GUIDANCE ON OUTPUT
prompt = (f"Create a list of no more than 3 very famous characters, either real or fictional, beginning with the letter {l}. They must be well-known to be used to play a famous character guessing game"
f"Use only the character's names and do not output any other information or numbering."
)
systemRole = "you are a bot that only knows French names"
famous = get_GPT_response(prompt, systemRole)
'''
Robin des Bois
Raimundo
Ratatouille
'''
# VERSION 3 - ADD REQUEST FOR JSON RESPONSE
prompt = (f"Create a list of no more than 3 very famous characters, either real or fictional, beginning with the letter {l}. They must be well-known to be used to play a famous character guessing game"
f"Use only the character's names and do not output any other information or numbering. Provide output in curly brackets as json eg 'characters':['Richard','Robert']"
)
systemRole = "you are a bot that plays an online version of the 'who am i' game"
famous = get_GPT_response(prompt, systemRole)
'''
{'characters':['Robin Hood','Ron Weasley','R2-D2']}
'''
# VERSION 3 - ADD json_schema to control output
prompt = (f"Create a list of no more than 3 very famous characters, either real or fictional, beginning with the letter {l}. They must be well-known to be used to play a famous character guessing game. "
f"Use only the character's names and output as json."
)
systemRole = "you are a bot that plays an online version of the 'who am i' game"
json_schema = {
"type": "object",
"properties":
{
"characterList":
{
"type": "array",
"items":
{
"type": "object",
"properties":
{
"name": {"type": "string"},
},
}
}
}
}
famous = get_GPT_response(prompt, systemRole, reponseFormat = "json_schema", json_schema = json_schema)
'''
{"characterList":[{"name":"Robin Hood"},{"name":"Ron Weasley"},{"name":"R2-D2"}]}
'''
try:
famousList = [x['name'] for x in json.loads(famous)['characterList']]
print(famousList)
except:
print("error loading json")