-
Notifications
You must be signed in to change notification settings - Fork 24
/
Chat.py
89 lines (73 loc) · 2.85 KB
/
Chat.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
import sys
import os
base_dir = os.path.dirname(os.path.abspath(__file__))
venv_site_packages = os.path.join(base_dir, 'venv', 'Lib', 'site-packages')
sys.path.append(venv_site_packages)
try:
import autogen
except ImportError:
import sys
import os
# Determine the correct path based on the operating system
if os.name == 'posix':
site_packages = os.path.join(sys.prefix, 'lib', 'python{}.{}/site-packages'.format(sys.version_info.major, sys.version_info.minor))
else: # For Windows
site_packages = os.path.join(sys.prefix, 'Lib', 'site-packages')
sys.path.append(site_packages)
import autogen
import json
class Chat:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"Task": ("STRING", {
"multiline": True,
"default": "Task here"
}),
"User": ("User",),
"Agent": ("Agent",),
"Instructions": ("STRING", {
"multiline": True,
"default": "Instructions:\nDo not write anything here!\nType below in the Input \nNEW for a new chat \nSEND to send a message if asked \nAsk if you have a follow up question"
}),
"Input": ("STRING", {"default": "NEW"})
}
}
RETURN_TYPES = ("TEXT",)
FUNCTION = "execute"
CATEGORY = "AutoGen"
def execute(self, Task, User, Agent, Input, Instructions=None):
user_object = User['User'] # Extract the actual user object from the dictionary
agent_object = Agent['Agent']
print(agent_object)
conversations = {}
# autogen.ChatCompletion.start_logging(conversations)
if Input=="SEND":
user_object.send(
recipient=agent_object,
message=Task,
)
else:
user_object.initiate_chat(
agent_object,
message=Task,
)
# Check if the first key of the dictionary is a string representation of a list
if isinstance(list(conversations.keys())[0], str):
# Convert the string representation to an actual list
content_list_str = list(conversations.keys())[0]
content_list = json.loads(content_list_str)
else:
raise ValueError("Unexpected format of 'text'")
# Extract the content from each dictionary in the list
all_contents = [item['content'] for item in content_list if 'content' in item]
# Get the last content from the list (or None if the list is empty)
last_content = all_contents[-1] if all_contents else None
return ({"TEXT": last_content, "LOG": conversations},)
NODE_CLASS_MAPPINGS = {
"Chat": Chat,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Chat": "Chat"
}