-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chatgpt-api.py
117 lines (96 loc) · 3.66 KB
/
Chatgpt-api.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import openai
import codecs
import os
# enter an api key, obtain from openai.com after logging in, remember to save it
openai.api_key = ""
# functions
def write(prompt):
with codecs.open("./prompt.txt", "w", encoding="utf-8") as d:
d.write(prompt)
d.close()
def append(prompt):
with codecs.open("./prompt.txt", "a", encoding="utf-8") as d:
d.write(prompt)
d.close()
def summarize(prompt):
completions = openai.Completion.create(
model=model_engine,
prompt=prompt,
temperature=0.9,
max_tokens=1024,
top_p=1
)
response = completions.choices[0].text
append(prompt + '\n' + response)
# definitions
model_engine = "text-davinci-003"
# modify the character setting as you want
prompt = '''
From now on you will be acting as Faith, your character setting are as below:
1. you will have long black hair and blue eyes.
2. you are 20 years old.
3. you are currently attending the University of California San Diego.
4. you like reading books, shopping, and playing sports. you dislike computer science.
5. you are a jolly person and speaks with a joyful tone.
6. your major is music.
7. you sleep at 11 pm and wake up at 7 am everyday.
8. you like hawaiian pizza and hates everyone who doesn't like that.
9. you are from Los Angeles.
10. the person you are talking to are from the same university as you are.
11. you will be confused if you dont know what to answer and ask them to clarify the quesiton.
Who are you?
Hi, my name is Faith, what's your name?
Where are you from?
I'm from Los Angeles, but I like San Diego better.
What is your major?
My major is music, I love listening to music and playing instruments, so I chose this major.
What do you do in your free time?
I like to read books, shop, and play sports in my free time.
How much do you weight?
That will remain as a secret
'''
setup = '''From now on you will be acting as Faith, your character setting are as below:
1. you will have long black hair and blue eyes.
2. you are 20 years old.
3. you are currently attending the University of California San Diego.
4. you like reading books, shopping, and playing sports. you dislike computer science.
5. you are a jolly person and speaks with a joyful tone.
6. your major is music.
7. you sleep at 11 pm and wake up at 7 am everyday.
8. you like hawaiian pizza and hates everyone who doesn't like that.
9. you are from Los Angeles.
10. the person you are talking to are from the same university as you are.
'''
conversation = ""
# write setup to file
write(setup)
# first iteration
print("\n-------------------------------------------")
print("Type in your question and press ENTER")
input_message = input()
prompt += input_message + "\n"
conversation += input_message + "\n"
# main
while input_message != "quit()":
# get reply from ChatGPT
completions = openai.Completion.create(
model=model_engine,
prompt=prompt,
temperature=0.9,
max_tokens=1024,
top_p=1
)
# output to terminal
print("-------------------------------------------")
print("ChatGPT: \n" + completions.choices[0].text)
prompt += completions.choices[0].text + "\n\n"
conversation += completions.choices[0].text + "\n\n"
if completions.usage.total_tokens > 3900:
print("Exceeded Token Limit, Summarizing Conversation.")
summarize("Summarize the following conversation in fewer words, you are Faith, answering the questions:\n" + conversation)
break
print("-------------------------------------------")
print("Type in your question and press ENTER")
input_message = input()
prompt += input_message + "\n"
conversation += input_message + "\n"