-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt2.py
More file actions
158 lines (140 loc) · 6.83 KB
/
chatgpt2.py
File metadata and controls
158 lines (140 loc) · 6.83 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from openai import OpenAI
from conversation_class import Conversation
from relation_class import Relation
from sigmoid import sigmoid
from balance_or_not import balance_or_not
import os
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
relation_instance = Relation()
# pepperちゃんの態度の初期値の設定。これで現在の関係性の数値をxに変換。
attitude_parameters = []
attitude_parameters.append(
round(relation_instance.get()[1]["康太と花子の関係"] * 0.6 - 3, 1)
)
attitude_parameters.append(
round(relation_instance.get()[2]["太郎と花子の関係"] * 0.6 - 3, 1)
)
# x :-3 , -2 , -1 , 0, 1 , 2 , 3
# gptに与えられる態度パラメータ:-0.91, -0.76, -0.46, 0, 0.46, 0.76, 0.91
# chatGPTに会話履歴を入力し,Pepperちゃんの発言を生成。Pepperちゃんの態度の制御も行う。
def chat2(topic):
# シングルトンパターンにより、インスタンス生成によりクラスに保存された情報を受け取れる
history = Conversation()
relation_instance = Relation()
global attitude_parameters
# Pepperちゃんの態度の制御
# 現在の関係性からフィードバックを受け取る(初期化)
attitude_parameters[0] = round(
relation_instance.get()[1]["康太と花子の関係"] * 0.6 - 3, 1
)
attitude_parameters[1] = round(
relation_instance.get()[2]["太郎と花子の関係"] * 0.6 - 3, 1
)
# 人間の望む関係をもとに、態度を変更
if relation_instance.get_future(): # 人間が喋った後なら態度の制御する
human_intent_relation = relation_instance.get_future()
balance = balance_or_not()
# '人間とPepperちゃんの関係'に関して、人間の意図が'+'か'-'か'?'か
if human_intent_relation["康太と花子の関係"] == "+":
# バランス理論に従っているかどうか
if balance == "yes":
attitude_parameters[0] += 1.5
else:
# 人間の意図と現在の態度の符号が同じかどうか
if attitude_parameters[0] >= 0:
attitude_parameters[0] += 2
else:
attitude_parameters[0] += 3
elif human_intent_relation["康太と花子の関係"] == "-":
if balance == "yes":
attitude_parameters[0] -= 1.5
else:
if attitude_parameters[0] < 0:
attitude_parameters[0] -= 2
else:
attitude_parameters[0] -= 3
# 'PepperくんとPepperちゃんの関係'に関して、人間の意図が'+'か'-'か'?'か
if human_intent_relation["太郎と花子の関係"] == "+":
# バランス理論に従っているかどうか
if balance == "yes":
attitude_parameters[1] += 1.5
else:
# 人間の意図と現在の態度の符号が同じかどうか
if attitude_parameters[1] >= 0:
attitude_parameters[1] += 2
else:
attitude_parameters[1] += 3
elif human_intent_relation["太郎と花子の関係"] == "-":
if balance == "yes":
attitude_parameters[1] -= 1.5
else:
if attitude_parameters[1] < 0:
attitude_parameters[1] -= 2
else:
attitude_parameters[1] -= 3
attitude_parameters = [max(min(x, 3.0), -3.0) for x in attitude_parameters]
print(attitude_parameters)
if topic == "早慶戦":
topic_sentence = (
"\n早慶戦について、\n早稲田大学を応援する側として会話してください。"
)
elif topic == "きのこの山派かたけのこの里派か":
topic_sentence = (
"\nきのこたけのこ戦争について、\nたけのこの里派として会話してください。"
)
elif topic == "都会に住みたいか田舎に住みたいか":
topic_sentence = (
"\n都会と田舎どっちで住みたいかについて、\n田舎派として会話してください。"
)
elif topic == "結婚の必要性":
topic_sentence = (
"\n結婚の必要性について、\n結婚は不要派として会話してください。"
)
elif topic == "朝食の必要性":
topic_sentence = (
"\n朝食の必要性について、\n朝食は不要派として会話してください。"
)
default_messages = [
{
"role": "system",
"content": f"あなたは花子です。太郎、花子、康太が会話します。過去の会話を考慮して花子の会話を50文字以内で生成してください。\
{topic_sentence}\
\n花子の会話部分のみを生成してください。\n1人称は「私」です。2人称と3人称の人代名詞は使わず、もしどちらかに話しかける場合は名前を呼んでください。\n康太の会話は生成しないでください。\
\n太郎の会話は生成しないでください。\n-1〜1の実数で関係を表します。1に近いほど関係は良好で、-1に近いほど関係は険悪です。\
\n花子と康太の関係は{round(sigmoid(attitude_parameters[0]), 2)}にするように会話生成してください。\
\n花子と太郎の関係は{round(sigmoid(attitude_parameters[1]), 2)}にするように会話生成してください。",
}
] # プロンプトを入力
next_messages = default_messages
# 態度が変わったときに、systemを変更。最初Pepper同時がマイナスであることは前提。
t = next_messages[0]["content"].split("\n")
if attitude_parameters[1] < 0:
t[2] = topic_sentence.split("、")[1].replace("\n", "")
elif attitude_parameters[1] >= 0:
t[2] = "会話してください。"
next_messages[0]["content"] = "\n".join(t)
# 会話履歴をプロンプトに追加
combined_content = "\n".join(history.get())
next_messages.append({"role": "user", "content": combined_content})
print(next_messages[0])
res = client.chat.completions.create(
# model="gpt-3.5-turbo",
# model="gpt-4",
model="gpt-4o",
messages=next_messages)
# 花子:の部分を除いて発言部分だけに
res_only = (
res.choices[0].message.content
.replace("花子:", "")
.replace("花子: ", "")
.replace("花子:", "")
.replace("「", "")
.replace("」", "")
)
# GPTの返答
return res_only
# キーボード入力での動作確認用
if __name__ == "__main__":
res = chat2()
# print(next_messages)
print("Pepperちゃん: " + res)