-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample2.py
More file actions
84 lines (69 loc) · 1.93 KB
/
sample2.py
File metadata and controls
84 lines (69 loc) · 1.93 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
# To execute this sample, please install streamchat-agent from PyPI as follows.
# $ pip install streamchat-agent
import sys
import time
import math
import re
import datetime
import StreamChatAgent as sca
import ChatAIAgent as ca
def print_incremental(st, interval_sec):
for i in range(len(st)):
if not running:
break
print(f"{st[i]}", end='')
sys.stdout.flush()
interruptible_sleep(interval_sec)
def interruptible_sleep(time_sec):
counter = math.floor(time_sec / 0.1)
frac = time_sec - (counter * 0.1)
for i in range(counter):
if not running:
break
time.sleep(0.1)
if not running:
return
time.sleep(frac)
def answer_cb(user_message, completion):
print(f"\n[{user_message.extern.author.name} {user_message.extern.datetime}] {user_message.message}\n")
interruptible_sleep(3)
time_str = datetime.datetime.now().strftime ('%H:%M:%S')
message = completion.choices[0]["message"]["content"]
print(f"[ChatGPT {time_str}] ", end='')
print_incremental(message, 0.05)
print("\n")
interruptible_sleep(5)
def get_item_cb(c):
ai_agent.put_message(ca.userMessage(message=c.message, extern=c))
def pre_filter_cb(c):
return None if re.match(r'^(:[^:]+:)+$', c.message) else c
def post_filter_cb(c):
c.message = re.sub(r':[^:]+:','', c.message)
return c
running = False
if len(sys.argv) <= 2:
exit(0)
sca_params = sca.params(
video_id=sys.argv[1],
get_item_cb=get_item_cb,
pre_filter_cb=pre_filter_cb,
post_filter_cb=post_filter_cb
)
stream_agent = sca.StreamChatAgent( sca_params )
ca_params = ca.params(
api_key=sys.argv[2],
system_role="You are a cheerful assistant who speek English and can get conversation exciting with user.",
answer_cb=answer_cb
)
ai_agent = ca.ChatAIAgent( ca_params )
running = True
stream_agent.start()
ai_agent.start()
input()
running=False
ai_agent.disconnect()
stream_agent.disconnect()
ai_agent.join()
stream_agent.join()
del stream_agent
del ai_agent