-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.py
195 lines (155 loc) · 5.94 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import sys
from taipy.gui import Gui, State, notify
import openai
from dotenv import load_dotenv
client = None
context = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today? "
conversation = {
"Conversation": ["Who are you?", "Hi! I am GPT-4. How can I help you today?"]
}
current_user_message = ""
past_conversations = []
selected_conv = None
selected_row = [1]
def on_init(state: State) -> None:
"""
Initialize the app.
Args:
- state: The current state of the app.
"""
state.context = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today? "
state.conversation = {
"Conversation": ["Who are you?", "Hi! I am GPT-4. How can I help you today?"]
}
state.current_user_message = ""
state.past_conversations = []
state.selected_conv = None
state.selected_row = [1]
def request(state: State, prompt: str) -> str:
"""
Send a prompt to the GPT-4 API and return the response.
Args:
- state: The current state of the app.
- prompt: The prompt to send to the API.
Returns:
The response from the API.
"""
response = state.client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"{prompt}",
}
],
model="gpt-4-turbo-preview",
)
return response.choices[0].message.content
def update_context(state: State) -> None:
"""
Update the context with the user's message and the AI's response.
Args:
- state: The current state of the app.
"""
state.context += f"Human: \n {state.current_user_message}\n\n AI:"
answer = request(state, state.context).replace("\n", "")
state.context += answer
state.selected_row = [len(state.conversation["Conversation"]) + 1]
return answer
def send_message(state: State) -> None:
"""
Send the user's message to the API and update the context.
Args:
- state: The current state of the app.
"""
notify(state, "info", "Sending message...")
answer = update_context(state)
conv = state.conversation._dict.copy()
conv["Conversation"] += [state.current_user_message, answer]
state.current_user_message = ""
state.conversation = conv
notify(state, "success", "Response received!")
def style_conv(state: State, idx: int, row: int) -> str:
"""
Apply a style to the conversation table depending on the message's author.
Args:
- state: The current state of the app.
- idx: The index of the message in the table.
- row: The row of the message in the table.
Returns:
The style to apply to the message.
"""
if idx is None:
return None
elif idx % 2 == 0:
return "user_message"
else:
return "gpt_message"
def on_exception(state, function_name: str, ex: Exception) -> None:
"""
Catches exceptions and notifies user in Taipy GUI
Args:
state (State): Taipy GUI state
function_name (str): Name of function where exception occured
ex (Exception): Exception
"""
notify(state, "error", f"An error occured in {function_name}: {ex}")
def reset_chat(state: State) -> None:
"""
Reset the chat by clearing the conversation.
Args:
- state: The current state of the app.
"""
state.past_conversations = state.past_conversations + [
[len(state.past_conversations), state.conversation]
]
state.conversation = {
"Conversation": ["Who are you?", "Hi! I am GPT-4. How can I help you today?"]
}
def tree_adapter(item: list) -> [str, str]:
"""
Converts element of past_conversations to id and displayed string
Args:
item: element of past_conversations
Returns:
id and displayed string
"""
identifier = item[0]
if len(item[1]["Conversation"]) > 3:
return (identifier, item[1]["Conversation"][2][:50] + "...")
return (item[0], "Empty conversation")
def select_conv(state: State, var_name: str, value) -> None:
"""
Selects conversation from past_conversations
Args:
state: The current state of the app.
var_name: "selected_conv"
value: [[id, conversation]]
"""
state.conversation = state.past_conversations[value[0][0]][1]
state.context = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today? "
for i in range(2, len(state.conversation["Conversation"]), 2):
state.context += f"Human: \n {state.conversation['Conversation'][i]}\n\n AI:"
state.context += state.conversation["Conversation"][i + 1]
state.selected_row = [len(state.conversation["Conversation"]) + 1]
past_prompts = []
page = """
<|layout|columns=300px 1|
<|part|class_name=sidebar|
# Taipy **Chat**{: .color-primary} # {: .logo-text}
<|New Conversation|button|class_name=fullwidth plain|id=reset_app_button|on_action=reset_chat|>
### Previous activities ### {: .h5 .mt2 .mb-half}
<|{selected_conv}|tree|lov={past_conversations}|class_name=past_prompts_list|multiple|adapter=tree_adapter|on_change=select_conv|>
|>
<|part|class_name=p2 align-item-bottom table|
<|{conversation}|table|style=style_conv|show_all|selected={selected_row}|rebuild|>
<|part|class_name=card mt1|
<|{current_user_message}|input|label=Write your message here...|on_action=send_message|class_name=fullwidth|change_delay=-1|>
|>
|>
|>
"""
if __name__ == "__main__":
load_dotenv()
client = openai.Client(api_key=os.getenv("OPENAI_API_KEY"))
Gui(page).run(debug=True, dark_mode=True, use_reloader=True, title="💬 Taipy Chat")