-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.py
executable file
·82 lines (59 loc) · 2.21 KB
/
tasks.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
from robocorp import vault
from robocorp.tasks import task
from RPA.Assistant.types import WindowLocation, Size
import RPA.Assistant
from openai import OpenAI
assistant = RPA.Assistant.Assistant()
gpt_conversation_display = []
gpt_conversation_internal = []
gpt_model = "gpt-3.5-turbo"
openai_client = None
@task
def display_window():
authorize_openai()
display_conversation()
assistant.run_dialog(
timeout=1800, title="AI Chat", on_top=True, location=WindowLocation.Center
)
def authorize_openai():
global openai_client
secrets_container = vault.get_secret("openai")
openai_client = OpenAI(api_key=secrets_container["key"])
def show_spinner():
assistant.clear_dialog()
assistant.add_loading_spinner(name="spinner", width=60, height=60, stroke_width=8)
assistant.refresh_dialog()
def ask_gpt(form_data: dict):
global gpt_conversation_internal
show_spinner()
gpt_conversation_internal.append({"role": "user", "content": form_data["input"]})
response = openai_client.chat.completions.create(
model=gpt_model,
messages=gpt_conversation_internal,
temperature=1,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
text = response.choices[0].message.content
gpt_conversation_internal.append({"role": "assistant", "content": text})
gpt_conversation_display.append((form_data["input"], text))
display_conversation()
assistant.refresh_dialog()
def display_conversation():
assistant.clear_dialog()
assistant.add_heading("Conversation")
for reply in gpt_conversation_display:
assistant.add_text("You:", size=Size.Small)
assistant.open_container(background_color="#C091EF", margin=2)
assistant.add_text(reply[0])
assistant.close_container()
assistant.add_text("GPT:", size=Size.Small)
assistant.open_container(background_color="#A5AACD", margin=2)
assistant.add_text(reply[1])
assistant.close_container()
display_buttons()
def display_buttons():
assistant.add_text_input("input", placeholder="Send a message", minimum_rows=3)
assistant.add_next_ui_button("Send", ask_gpt)
assistant.add_submit_buttons("Close", default="Close")