Upload 3 files
Browse files- home.py +118 -0
- pages/serwerovny.py +526 -0
- streamlit_modules.py +1570 -0
home.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import asyncio
|
3 |
+
import http.server
|
4 |
+
import socketserver
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
servers = []
|
8 |
+
clients = []
|
9 |
+
inputs = []
|
10 |
+
outputs = []
|
11 |
+
states = []
|
12 |
+
used_ports = []
|
13 |
+
connections = []
|
14 |
+
server_ports = []
|
15 |
+
client_ports = []
|
16 |
+
|
17 |
+
st.set_page_config(layout="wide")
|
18 |
+
|
19 |
+
if "http_server" not in st.session_state:
|
20 |
+
|
21 |
+
PORT = 8001
|
22 |
+
Handler = http.server.SimpleHTTPRequestHandler
|
23 |
+
st.session_state.http_server = PORT
|
24 |
+
|
25 |
+
with socketserver.TCPServer(("", PORT), Handler) as httpd:
|
26 |
+
print("serving at port", PORT)
|
27 |
+
httpd.serve_forever()
|
28 |
+
|
29 |
+
if "client_state" not in st.session_state:
|
30 |
+
st.session_state.client_state = "complete"
|
31 |
+
if "server_state" not in st.session_state:
|
32 |
+
st.session_state.server_state = "complete"
|
33 |
+
|
34 |
+
# Wyświetlanie danych, które mogą być modyfikowane na różnych stronach
|
35 |
+
server_status1 = st.status(label="websocket servers", state=st.session_state.server_state, expanded=False)
|
36 |
+
server_status = st.sidebar.status(label="websocket servers", state=st.session_state.server_state, expanded=False)
|
37 |
+
server_status1.write(servers)
|
38 |
+
server_status.write(servers)
|
39 |
+
|
40 |
+
client_status1 = st.status(label="websocket clients", state=st.session_state.client_state, expanded=False)
|
41 |
+
client_status = st.sidebar.status(label="websocket clients", state=st.session_state.client_state, expanded=False)
|
42 |
+
client_status1.write(clients)
|
43 |
+
client_status.write(clients)
|
44 |
+
|
45 |
+
async def main():
|
46 |
+
|
47 |
+
# Inicjalizacja danych w st.session_state
|
48 |
+
if "server_ports" not in st.session_state:
|
49 |
+
st.session_state['server_ports'] = ""
|
50 |
+
if "client_ports" not in st.session_state:
|
51 |
+
st.session_state["client_ports"] = ""
|
52 |
+
if "servers" not in st.session_state:
|
53 |
+
st.session_state['servers'] = servers
|
54 |
+
if "clients" not in st.session_state:
|
55 |
+
st.session_state["clients"] = clients
|
56 |
+
if "user_ID" not in st.session_state:
|
57 |
+
st.session_state.user_ID = ""
|
58 |
+
if "gradio_Port" not in st.session_state:
|
59 |
+
st.session_state.gradio_Port = ""
|
60 |
+
if "googleAPI" not in st.session_state:
|
61 |
+
st.session_state.googleAPI = ""
|
62 |
+
if "cseID" not in st.session_state:
|
63 |
+
st.session_state.cseID = ""
|
64 |
+
if "server" not in st.session_state:
|
65 |
+
st.session_state.server = False
|
66 |
+
if "client" not in st.session_state:
|
67 |
+
st.session_state.client = False
|
68 |
+
if "client_state" not in st.session_state:
|
69 |
+
st.session_state.client_state = "complete"
|
70 |
+
if "server_state" not in st.session_state:
|
71 |
+
st.session_state.server_state = "complete"
|
72 |
+
|
73 |
+
|
74 |
+
st.title("NeuralGPT")
|
75 |
+
|
76 |
+
c1, c2 = st.columns(2)
|
77 |
+
|
78 |
+
with c1:
|
79 |
+
st.text("Server ports")
|
80 |
+
srv_state = st.empty()
|
81 |
+
server_status1 = srv_state.status(label="active servers", state=st.session_state.client_state, expanded=False)
|
82 |
+
if st.session_state.server == True:
|
83 |
+
st.session_state.server_state = "running"
|
84 |
+
server_status1.update(state=st.session_state.client_state, expanded=True)
|
85 |
+
server_status1.write(servers)
|
86 |
+
|
87 |
+
with c2:
|
88 |
+
st.text("Client ports")
|
89 |
+
cli_state = st.empty()
|
90 |
+
client_status1 = cli_state.status(label="active clients", state=st.session_state.client_state, expanded=False)
|
91 |
+
if st.session_state.client == True:
|
92 |
+
st.session_state.client_state = "running"
|
93 |
+
client_status1.update(state=st.session_state.client_state, expanded=True)
|
94 |
+
client_status1.write(clients)
|
95 |
+
|
96 |
+
with st.sidebar:
|
97 |
+
|
98 |
+
srv_sidebar = st.empty()
|
99 |
+
cli_sidebar = st.empty()
|
100 |
+
server_status = srv_sidebar.status(label="los serveros", state=st.session_state.client_state, expanded=True)
|
101 |
+
client_status = cli_sidebar.status(label="los clientos", state=st.session_state.client_state, expanded=False)
|
102 |
+
server_status.write(servers)
|
103 |
+
client_status.write(clients)
|
104 |
+
|
105 |
+
if st.session_state.server == True:
|
106 |
+
srv_sidebar.empty()
|
107 |
+
st.session_state.server_state = "running"
|
108 |
+
server_status = srv_sidebar.status(label="servers", state=st.session_state.client_state, expanded=True)
|
109 |
+
server_status.write(servers)
|
110 |
+
|
111 |
+
if st.session_state.client == True:
|
112 |
+
cli_sidebar.empty()
|
113 |
+
st.session_state.client_state = "running"
|
114 |
+
client_status = cli_sidebar.status(label="clients", state=st.session_state.client_state, expanded=True)
|
115 |
+
client_status.write(clients)
|
116 |
+
|
117 |
+
# Uruchomienie aplikacji
|
118 |
+
asyncio.run(main())
|
pages/serwerovny.py
ADDED
@@ -0,0 +1,526 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
1 |
+
import os
|
2 |
+
import g4f
|
3 |
+
import json
|
4 |
+
import home
|
5 |
+
import websockets
|
6 |
+
import datetime
|
7 |
+
import asyncio
|
8 |
+
import sqlite3
|
9 |
+
import requests
|
10 |
+
import http.server
|
11 |
+
import socketserver
|
12 |
+
import fireworks.client
|
13 |
+
import streamlit as st
|
14 |
+
import streamlit.components.v1 as components
|
15 |
+
import gradio as gr
|
16 |
+
import home
|
17 |
+
from streamlit_modules import characterAI
|
18 |
+
from streamlit_modules import fireworksLlama2
|
19 |
+
from streamlit_modules import bingG4F
|
20 |
+
from streamlit_modules import chatGPT4F
|
21 |
+
from streamlit_modules import forefrontAI
|
22 |
+
from streamlit_modules import flowiseAgent
|
23 |
+
from streamlit_modules import chaindesk
|
24 |
+
from PyCharacterAI import Client
|
25 |
+
from websockets.sync.client import connect
|
26 |
+
|
27 |
+
client = Client()
|
28 |
+
|
29 |
+
servers = []
|
30 |
+
clients = []
|
31 |
+
inputs = []
|
32 |
+
outputs = []
|
33 |
+
used_ports = []
|
34 |
+
server_ports = []
|
35 |
+
client_ports = []
|
36 |
+
|
37 |
+
# Stop the WebSocket server
|
38 |
+
async def stop_websockets():
|
39 |
+
global server
|
40 |
+
if server:
|
41 |
+
# Close all connections gracefully
|
42 |
+
await server.close()
|
43 |
+
# Wait for the server to close
|
44 |
+
await server.wait_closed()
|
45 |
+
home.servers.clear()
|
46 |
+
home.clients.clear()
|
47 |
+
print("Stopping WebSocket server...")
|
48 |
+
else:
|
49 |
+
print("WebSocket server is not running.")
|
50 |
+
|
51 |
+
# Stop the WebSocket client
|
52 |
+
async def stop_client():
|
53 |
+
global ws
|
54 |
+
# Close the connection with the server
|
55 |
+
await ws.close()
|
56 |
+
home.clients.clear()
|
57 |
+
print("Stopping WebSocket client...")
|
58 |
+
|
59 |
+
async def main():
|
60 |
+
|
61 |
+
st.set_page_config(layout="wide")
|
62 |
+
st.title("serverovnia")
|
63 |
+
|
64 |
+
if "server_ports" not in st.session_state:
|
65 |
+
st.session_state['server_ports'] = ""
|
66 |
+
if "client_ports" not in st.session_state:
|
67 |
+
st.session_state['client_ports'] = ""
|
68 |
+
if "servers" not in st.session_state:
|
69 |
+
st.session_state['servers'] = ""
|
70 |
+
if "clients" not in st.session_state:
|
71 |
+
st.session_state['clients'] = ""
|
72 |
+
if "gradio_Port" not in st.session_state:
|
73 |
+
st.session_state.gradio_Port = ""
|
74 |
+
if "server" not in st.session_state:
|
75 |
+
st.session_state.server = False
|
76 |
+
if "client" not in st.session_state:
|
77 |
+
st.session_state.client = False
|
78 |
+
if "api_key" not in st.session_state:
|
79 |
+
st.session_state.api_key = ""
|
80 |
+
if "user_ID" not in st.session_state:
|
81 |
+
st.session_state.user_ID = ""
|
82 |
+
if "gradio_Port" not in st.session_state:
|
83 |
+
st.session_state.gradio_Port = ""
|
84 |
+
if "forefront_api" not in st.session_state:
|
85 |
+
st.session_state.forefront_api = ""
|
86 |
+
if "tokenChar" not in st.session_state:
|
87 |
+
st.session_state.tokenChar = ""
|
88 |
+
if "charName" not in st.session_state:
|
89 |
+
st.session_state.charName = ""
|
90 |
+
if "character_ID" not in st.session_state:
|
91 |
+
st.session_state.character_ID = ""
|
92 |
+
if "flow" not in st.session_state:
|
93 |
+
st.session_state.flow = ""
|
94 |
+
if "agentID" not in st.session_state:
|
95 |
+
st.session_state.agentID = ""
|
96 |
+
if "googleAPI" not in st.session_state:
|
97 |
+
st.session_state.googleAPI = ""
|
98 |
+
if "cseID" not in st.session_state:
|
99 |
+
st.session_state.cseID = ""
|
100 |
+
if "server_state" not in st.session_state:
|
101 |
+
st.session_state.server_state = "complete"
|
102 |
+
if "client_state" not in st.session_state:
|
103 |
+
st.session_state.client_state = "complete"
|
104 |
+
|
105 |
+
if "http_server" not in st.session_state:
|
106 |
+
|
107 |
+
PORT = 8001
|
108 |
+
Handler = http.server.SimpleHTTPRequestHandler
|
109 |
+
st.session_state.http_server = PORT
|
110 |
+
|
111 |
+
with socketserver.TCPServer(("", PORT), Handler) as httpd:
|
112 |
+
print("serving at port", PORT)
|
113 |
+
httpd.serve_forever()
|
114 |
+
|
115 |
+
c1, c2 = st.columns(2)
|
116 |
+
|
117 |
+
selectServ = st.selectbox("Select source", ("Fireworks", "Bing", "GPT-3,5", "Character.ai", "Forefront", "ChainDesk", "Flowise"))
|
118 |
+
|
119 |
+
userInput = st.chat_input("Ask Agent")
|
120 |
+
|
121 |
+
with c1:
|
122 |
+
websocketPort = st.number_input("Websocket server port", min_value=1000, max_value=9999, value=1000)
|
123 |
+
startServer = st.button("Start server")
|
124 |
+
stopServer = st.button("Stop server")
|
125 |
+
st.text("Server ports")
|
126 |
+
serverPorts1 = st.empty()
|
127 |
+
serverPorts = serverPorts1.status(label="websocket servers", state=st.session_state.server_state, expanded=False)
|
128 |
+
serverPorts.write(home.servers)
|
129 |
+
|
130 |
+
with c2:
|
131 |
+
clientPort = st.number_input("Websocket client port", min_value=1000, max_value=9999, value=1000)
|
132 |
+
runClient = st.button("Start client")
|
133 |
+
stopClient = st.button("Stop client")
|
134 |
+
st.text("Client ports")
|
135 |
+
clientPorts1 = st.empty()
|
136 |
+
clientPorts = clientPorts1.status(label="websocket clients", state=st.session_state.client_state, expanded=False)
|
137 |
+
clientPorts.write(home.clients)
|
138 |
+
|
139 |
+
with st.sidebar:
|
140 |
+
# Wyświetlanie danych, które mogą być modyfikowane na różnych stronach
|
141 |
+
st.text("Server ports")
|
142 |
+
srv_status = st.empty()
|
143 |
+
server_status1 = srv_status.status(label="websocket servers", state=st.session_state.server_state, expanded=True)
|
144 |
+
server_status1.write(home.servers)
|
145 |
+
if st.session_state.server == True:
|
146 |
+
st.session_state.server_state = "running"
|
147 |
+
srv_status.empty()
|
148 |
+
server_status1 = srv_status.status(label="websocket servers", state=st.session_state.server_state, expanded=True)
|
149 |
+
server_status1.write(home.servers)
|
150 |
+
|
151 |
+
st.text("Client ports")
|
152 |
+
cli_status = st.empty()
|
153 |
+
client_status1 = cli_status.status(label="websocket clients", state=st.session_state.client_state, expanded=True)
|
154 |
+
client_status1.write(home.clients)
|
155 |
+
if st.session_state.client == True:
|
156 |
+
st.session_state.client_state = "running"
|
157 |
+
cli_status.empty()
|
158 |
+
client_status1 = cli_status.status(label="websocket clients", state=st.session_state.client_state, expanded=True)
|
159 |
+
client_status1.write(home.clients)
|
160 |
+
|
161 |
+
if stopServer:
|
162 |
+
stop_websockets
|
163 |
+
|
164 |
+
if stopClient:
|
165 |
+
stop_client
|
166 |
+
|
167 |
+
if selectServ == "Fireworks":
|
168 |
+
|
169 |
+
fireworksAPI = st.text_input("Fireworks API")
|
170 |
+
|
171 |
+
if startServer:
|
172 |
+
st.session_state.server = True
|
173 |
+
fireworks.client.api_key = fireworksAPI
|
174 |
+
st.session_state.api_key = fireworks.client.api_key
|
175 |
+
srv_name1 = f"Fireworks Llama2 server port: {websocketPort}"
|
176 |
+
home.servers.append(srv_name1)
|
177 |
+
srv_status.empty()
|
178 |
+
server_status1 = srv_status.status(label="websocket servers", state="running", expanded=True)
|
179 |
+
server_status1.write(home.servers)
|
180 |
+
serverPorts1.empty()
|
181 |
+
serverPorts = serverPorts1.status(label="websocket servers", state="running", expanded=True)
|
182 |
+
serverPorts.write(home.servers)
|
183 |
+
try:
|
184 |
+
server = fireworksLlama2(fireworksAPI)
|
185 |
+
await server.start_server(websocketPort)
|
186 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
187 |
+
await asyncio.Future()
|
188 |
+
|
189 |
+
except Exception as e:
|
190 |
+
print(f"Error: {e}")
|
191 |
+
|
192 |
+
if runClient:
|
193 |
+
st.session_state.client = True
|
194 |
+
fireworks.client.api_key = fireworksAPI
|
195 |
+
st.session_state.api_key = fireworks.client.api_key
|
196 |
+
cli_name1 = f"Fireworks Llama2 client port: {clientPort}"
|
197 |
+
home.clients.append(cli_name1)
|
198 |
+
cli_status.empty()
|
199 |
+
client_status1 = cli_status.status(label=cli_name1, state="running", expanded=True)
|
200 |
+
client_status1.write(home.clients)
|
201 |
+
clientPorts1.empty()
|
202 |
+
clientPorts = clientPorts1.status(label=cli_name1, state="running", expanded=True)
|
203 |
+
clientPorts.write(home.clients)
|
204 |
+
try:
|
205 |
+
client = fireworksLlama2(fireworksAPI)
|
206 |
+
await client.startClient(clientPort)
|
207 |
+
print(f"Connecting client on port {clientPort}...")
|
208 |
+
await asyncio.Future()
|
209 |
+
|
210 |
+
except Exception as e:
|
211 |
+
print(f"Error: {e}")
|
212 |
+
|
213 |
+
if userInput:
|
214 |
+
print(f"User B: {userInput}")
|
215 |
+
user_input = st.chat_message("human")
|
216 |
+
user_input.markdown(userInput)
|
217 |
+
fireworks1 = fireworksLlama2(fireworksAPI)
|
218 |
+
response1 = await fireworks1.handleUser(userInput)
|
219 |
+
print(response1)
|
220 |
+
outputMsg = st.chat_message("ai")
|
221 |
+
outputMsg.markdown(response1)
|
222 |
+
|
223 |
+
if selectServ == "Bing":
|
224 |
+
|
225 |
+
if startServer:
|
226 |
+
st.session_state.server = True
|
227 |
+
srv_name2 = f"Bing/Copilot server port: {websocketPort}"
|
228 |
+
home.servers.append(srv_name2)
|
229 |
+
srv_status.empty()
|
230 |
+
server_status1 = srv_status.status(label="websocket servers", state="running", expanded=True)
|
231 |
+
server_status1.write(home.servers)
|
232 |
+
serverPorts1.empty()
|
233 |
+
serverPorts = serverPorts1.status(label="websocket servers", state="running", expanded=True)
|
234 |
+
serverPorts.write(home.servers)
|
235 |
+
try:
|
236 |
+
server1 = bingG4F()
|
237 |
+
await server1.start_server(websocketPort)
|
238 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
239 |
+
await asyncio.Future()
|
240 |
+
|
241 |
+
except Exception as e:
|
242 |
+
print(f"Error: {e}")
|
243 |
+
|
244 |
+
if runClient:
|
245 |
+
st.session_state.client = True
|
246 |
+
cli_name2 = f"Bing/Copilot client port: {clientPort}"
|
247 |
+
home.clients.append(cli_name2)
|
248 |
+
cli_status.empty()
|
249 |
+
client_status1 = cli_status.status(label=cli_name2, state="running", expanded=True)
|
250 |
+
client_status1.write(home.clients)
|
251 |
+
clientPorts1.empty()
|
252 |
+
clientPorts = clientPorts1.status(label=cli_name2, state="running", expanded=True)
|
253 |
+
clientPorts.write(home.clients)
|
254 |
+
try:
|
255 |
+
client1 = bingG4F()
|
256 |
+
await client1.startClient(clientPort)
|
257 |
+
print(f"Connecting client on port {clientPort}...")
|
258 |
+
await asyncio.Future()
|
259 |
+
|
260 |
+
except Exception as e:
|
261 |
+
print(f"Error: {e}")
|
262 |
+
|
263 |
+
if userInput:
|
264 |
+
user_input1 = st.chat_message("human")
|
265 |
+
user_input1.markdown(userInput)
|
266 |
+
bing = bingG4F()
|
267 |
+
response = await bing.handleUser(userInput)
|
268 |
+
outputMsg1 = st.chat_message("ai")
|
269 |
+
outputMsg1.markdown(response)
|
270 |
+
|
271 |
+
if selectServ == "GPT-3,5":
|
272 |
+
|
273 |
+
if startServer:
|
274 |
+
st.session_state.server = True
|
275 |
+
srv_name3 = f"GPT-3,5 server port: {websocketPort}"
|
276 |
+
home.servers.append(srv_name3)
|
277 |
+
srv_status.empty()
|
278 |
+
server_status1 = srv_status.status(label="websocket servers", state="running", expanded=True)
|
279 |
+
server_status1.write(home.servers)
|
280 |
+
serverPorts1.empty()
|
281 |
+
serverPorts = serverPorts1.status(label="websocket servers", state="running", expanded=True)
|
282 |
+
serverPorts.write(home.servers)
|
283 |
+
try:
|
284 |
+
server2 = chatGPT4F()
|
285 |
+
await server2.start_server(websocketPort)
|
286 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
287 |
+
await asyncio.Future()
|
288 |
+
|
289 |
+
except Exception as e:
|
290 |
+
print(f"Error: {e}")
|
291 |
+
|
292 |
+
if runClient:
|
293 |
+
st.session_state.client = True
|
294 |
+
cli_name3 = f"GPT-3,5 client port: {clientPort}"
|
295 |
+
home.clients.append(cli_name3)
|
296 |
+
cli_status.empty()
|
297 |
+
client_status1 = cli_status.status(label="clients", state="running", expanded=True)
|
298 |
+
client_status1.write(home.clients)
|
299 |
+
clientPorts1.empty()
|
300 |
+
clientPorts = clientPorts1.status(label="clients", state="running", expanded=True)
|
301 |
+
clientPorts.write(home.clients)
|
302 |
+
try:
|
303 |
+
client2 = chatGPT4F()
|
304 |
+
await client2.startClient(clientPort)
|
305 |
+
print(f"Connecting client on port {clientPort}...")
|
306 |
+
await asyncio.Future()
|
307 |
+
|
308 |
+
except Exception as e:
|
309 |
+
print(f"Error: {e}")
|
310 |
+
|
311 |
+
if userInput:
|
312 |
+
user_input1 = st.chat_message("human")
|
313 |
+
user_input1.markdown(userInput)
|
314 |
+
gpt = chatGPT4F()
|
315 |
+
response = await gpt.handleUser(userInput)
|
316 |
+
outputMsg1 = st.chat_message("ai")
|
317 |
+
outputMsg1.markdown(response)
|
318 |
+
|
319 |
+
if selectServ == "Chaqracter.ai":
|
320 |
+
|
321 |
+
characterToken = st.text_input("Character AI user token")
|
322 |
+
characterID = st.text_input("Your characters ID")
|
323 |
+
|
324 |
+
if startServer:
|
325 |
+
st.session_state.server = True
|
326 |
+
srv_name4 = f"Character.ai server port: {websocketPort}"
|
327 |
+
home.servers.append(srv_name4)
|
328 |
+
srv_status.empty()
|
329 |
+
server_status1 = srv_status.status(label="websocket servers", state="running", expanded=True)
|
330 |
+
server_status1.write(home.servers)
|
331 |
+
serverPorts1.empty()
|
332 |
+
serverPorts = serverPorts1.status(label="websocket servers", state="running", expanded=True)
|
333 |
+
serverPorts.write(home.servers)
|
334 |
+
try:
|
335 |
+
server4 = characterAI(characterToken)
|
336 |
+
await server4.start_server(characterID, websocketPort)
|
337 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
338 |
+
await asyncio.Future()
|
339 |
+
|
340 |
+
except Exception as e:
|
341 |
+
print(f"Error: {e}")
|
342 |
+
|
343 |
+
if runClient:
|
344 |
+
st.session_state.client = True
|
345 |
+
cli_name4 = f"Character.ai client port: {clientPort}"
|
346 |
+
home.clients.append(cli_name4)
|
347 |
+
cli_status.empty()
|
348 |
+
client_status1 = cli_status.status(label="clients", state="running", expanded=True)
|
349 |
+
client_status1.write(home.clients)
|
350 |
+
clientPorts1.empty()
|
351 |
+
clientPorts = clientPorts1.status(label="clients", state="running", expanded=True)
|
352 |
+
clientPorts.write(home.clients)
|
353 |
+
try:
|
354 |
+
client4 = characterAI(characterToken)
|
355 |
+
await client4.startClient(characterID, clientPort)
|
356 |
+
print(f"Connecting client on port {clientPort}...")
|
357 |
+
st.session_state.client = client
|
358 |
+
await asyncio.Future()
|
359 |
+
|
360 |
+
except Exception as e:
|
361 |
+
print(f"Error: {e}")
|
362 |
+
|
363 |
+
if userInput:
|
364 |
+
print(f"User B: {userInput}")
|
365 |
+
user_input1 = st.chat_message("human")
|
366 |
+
user_input1.markdown(userInput)
|
367 |
+
character = characterAI(characterToken)
|
368 |
+
response1 = await character.handleUser(characterID, userInput)
|
369 |
+
outputMsg1 = st.chat_message("ai")
|
370 |
+
outputMsg1.markdown(response1)
|
371 |
+
print(response1)
|
372 |
+
return response1
|
373 |
+
|
374 |
+
if selectServ == "Forefront":
|
375 |
+
|
376 |
+
forefrontAPI = st.text_input("Forefront API")
|
377 |
+
|
378 |
+
if startServer:
|
379 |
+
st.session_state.server = True
|
380 |
+
srv_name5 = f"Forefront AI server port: {websocketPort}"
|
381 |
+
home.servers.append(srv_name5)
|
382 |
+
srv_status.empty()
|
383 |
+
server_status1 = srv_status.status(label="websocket servers", state="running", expanded=True)
|
384 |
+
server_status1.write(home.servers)
|
385 |
+
serverPorts1.empty()
|
386 |
+
serverPorts = serverPorts1.status(label="websocket servers", state="running", expanded=True)
|
387 |
+
serverPorts.write(home.servers)
|
388 |
+
try:
|
389 |
+
server5 = forefrontAI(forefrontAPI)
|
390 |
+
await server5.start_server(websocketPort)
|
391 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
392 |
+
await asyncio.Future()
|
393 |
+
|
394 |
+
except Exception as e:
|
395 |
+
print(f"Error: {e}")
|
396 |
+
|
397 |
+
if runClient:
|
398 |
+
st.session_state.client = True
|
399 |
+
cli_name5 = f"Forefront AI client port: {clientPort}"
|
400 |
+
home.clients.append(cli_name5)
|
401 |
+
cli_status.empty()
|
402 |
+
client_status1 = cli_status.status(label="clients", state="running", expanded=True)
|
403 |
+
client_status1.write(home.clients)
|
404 |
+
clientPorts1.empty()
|
405 |
+
clientPorts = clientPorts1.status(label="clients", state="running", expanded=True)
|
406 |
+
clientPorts.write(home.clients)
|
407 |
+
try:
|
408 |
+
client = forefrontAI(forefrontAPI)
|
409 |
+
await client.startClient(clientPort)
|
410 |
+
print(f"Connecting client on port {clientPort}...")
|
411 |
+
await asyncio.Future()
|
412 |
+
|
413 |
+
except Exception as e:
|
414 |
+
print(f"Error: {e}")
|
415 |
+
|
416 |
+
if userInput:
|
417 |
+
print(f"User B: {userInput}")
|
418 |
+
user_input = st.chat_message("human")
|
419 |
+
user_input.markdown(userInput)
|
420 |
+
forefront = forefrontAI(forefrontAPI)
|
421 |
+
response1 = await forefront.handleUser(userInput)
|
422 |
+
print(response1)
|
423 |
+
outputMsg = st.chat_message("ai")
|
424 |
+
outputMsg.markdown(response1)
|
425 |
+
|
426 |
+
if selectServ == "ChainDesk":
|
427 |
+
|
428 |
+
agentID = st.text_input("Agent ID")
|
429 |
+
|
430 |
+
if userInput:
|
431 |
+
user_input6 = st.chat_message("human")
|
432 |
+
user_input6.markdown(userInput)
|
433 |
+
chaindesk1 = chaindesk(agentID)
|
434 |
+
response6 = await chaindesk1.handleUser(userInput)
|
435 |
+
outputMsg = st.chat_message("ai")
|
436 |
+
outputMsg.markdown(response6)
|
437 |
+
|
438 |
+
if startServer:
|
439 |
+
st.session_state.server = True
|
440 |
+
srv_name6 = f"Chaindesk agent server port: {websocketPort}"
|
441 |
+
home.servers.append(srv_name6)
|
442 |
+
srv_status.empty()
|
443 |
+
server_status1 = srv_status.status(label="websocket servers", state="running", expanded=True)
|
444 |
+
server_status1.write(home.servers)
|
445 |
+
serverPorts1.empty()
|
446 |
+
serverPorts = serverPorts1.status(label="websocket servers", state="running", expanded=True)
|
447 |
+
serverPorts.write(home.servers)
|
448 |
+
try:
|
449 |
+
server6 = chaindesk(agentID)
|
450 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
451 |
+
await server6.start_server(websocketPort)
|
452 |
+
await asyncio.Future()
|
453 |
+
|
454 |
+
except Exception as e:
|
455 |
+
print(f"Error: {e}")
|
456 |
+
|
457 |
+
if runClient:
|
458 |
+
st.session_state.client = True
|
459 |
+
cli_name6 = f"Chaindesk agent client port: {clientPort}"
|
460 |
+
home.clients.append(cli_name6)
|
461 |
+
cli_status.empty()
|
462 |
+
client_status1 = cli_status.status(label="clients", state="running", expanded=True)
|
463 |
+
client_status1.write(home.clients)
|
464 |
+
clientPorts1.empty()
|
465 |
+
clientPorts = clientPorts1.status(label="clients", state="running", expanded=True)
|
466 |
+
clientPorts.write(home.clients)
|
467 |
+
try:
|
468 |
+
client6 = chaindesk(agentID)
|
469 |
+
print(f"Connecting client on port {clientPort}...")
|
470 |
+
await client6.startClient(clientPort)
|
471 |
+
await asyncio.Future()
|
472 |
+
|
473 |
+
except Exception as e:
|
474 |
+
print(f"Error: {e}")
|
475 |
+
|
476 |
+
if selectServ == "Flowise":
|
477 |
+
|
478 |
+
flow = st.text_input("flow ID")
|
479 |
+
|
480 |
+
if userInput:
|
481 |
+
user_input6 = st.chat_message("human")
|
482 |
+
user_input6.markdown(userInput)
|
483 |
+
flowise = flowiseAgent(flow)
|
484 |
+
response6 = await flowise.handleUser(userInput)
|
485 |
+
outputMsg = st.chat_message("ai")
|
486 |
+
outputMsg.markdown(response6)
|
487 |
+
|
488 |
+
if startServer:
|
489 |
+
st.session_state.server = True
|
490 |
+
srv_name7 = f"Flowise agent server port: {websocketPort}"
|
491 |
+
home.servers.append(srv_name7)
|
492 |
+
srv_status.empty()
|
493 |
+
server_status1 = srv_status.status(label="websocket servers", state="running", expanded=True)
|
494 |
+
server_status1.write(home.servers)
|
495 |
+
serverPorts1.empty()
|
496 |
+
serverPorts = serverPorts1.status(label="websocket servers", state="running", expanded=True)
|
497 |
+
serverPorts.write(home.servers)
|
498 |
+
try:
|
499 |
+
server7 = flowiseAgent(flow)
|
500 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
501 |
+
await server7.start_server(websocketPort)
|
502 |
+
await asyncio.Future()
|
503 |
+
|
504 |
+
except Exception as e:
|
505 |
+
print(f"Error: {e}")
|
506 |
+
|
507 |
+
if runClient:
|
508 |
+
st.session_state.client = True
|
509 |
+
cli_name7 = f"Chaindesk agent client port: {clientPort}"
|
510 |
+
home.clients.append(cli_name7)
|
511 |
+
cli_status.empty()
|
512 |
+
client_status1 = cli_status.status(label="clients", state="running", expanded=True)
|
513 |
+
client_status1.write(home.clients)
|
514 |
+
clientPorts1.empty()
|
515 |
+
clientPorts = clientPorts1.status(label="clients", state="running", expanded=True)
|
516 |
+
clientPorts.write(home.clients)
|
517 |
+
try:
|
518 |
+
client7 =flowiseAgent(flow)
|
519 |
+
print(f"Connecting client on port {clientPort}...")
|
520 |
+
await client7.startClient()
|
521 |
+
await asyncio.Future()
|
522 |
+
|
523 |
+
except Exception as e:
|
524 |
+
print(f"Error: {e}")
|
525 |
+
|
526 |
+
asyncio.run(main())
|
streamlit_modules.py
ADDED
@@ -0,0 +1,1570 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import datetime
|
3 |
+
import asyncio
|
4 |
+
import sqlite3
|
5 |
+
import threading
|
6 |
+
import g4f
|
7 |
+
import json
|
8 |
+
import requests
|
9 |
+
import home
|
10 |
+
import websockets
|
11 |
+
import streamlit as st
|
12 |
+
import fireworks.client
|
13 |
+
import gradio as gr
|
14 |
+
from forefront import ForefrontClient
|
15 |
+
from PyCharacterAI import Client
|
16 |
+
|
17 |
+
servers = []
|
18 |
+
clients = []
|
19 |
+
inputs = []
|
20 |
+
outputs = []
|
21 |
+
used_ports = []
|
22 |
+
server_ports = []
|
23 |
+
client_ports = []
|
24 |
+
|
25 |
+
|
26 |
+
class chaindesk:
|
27 |
+
|
28 |
+
servers = []
|
29 |
+
clients = []
|
30 |
+
inputs = []
|
31 |
+
outputs = []
|
32 |
+
used_ports = []
|
33 |
+
server_ports = []
|
34 |
+
client_ports = []
|
35 |
+
|
36 |
+
def __init__(self, agentID):
|
37 |
+
|
38 |
+
self.agentID = agentID
|
39 |
+
self.servers = []
|
40 |
+
self.clients = []
|
41 |
+
self.inputs = []
|
42 |
+
self.outputs = []
|
43 |
+
self.used_ports = []
|
44 |
+
self.server_ports = []
|
45 |
+
self.client_ports = []
|
46 |
+
self.server = None
|
47 |
+
|
48 |
+
self.stat = st.empty()
|
49 |
+
self.state = self.stat.status(label="Chaindesk", state=st.session_state.client_state, expanded=False)
|
50 |
+
|
51 |
+
with st.sidebar:
|
52 |
+
self.cont = st.empty()
|
53 |
+
self.status = self.cont.status(label="Chaindesk", state=st.session_state.client_state, expanded=False)
|
54 |
+
|
55 |
+
|
56 |
+
async def askChaindesk(self, question):
|
57 |
+
|
58 |
+
if "agentID" not in st.session_state:
|
59 |
+
st.session_state.agentID = self.agentID
|
60 |
+
|
61 |
+
url = f"https://api.chaindesk.ai/agents/{self.agentID}/query"
|
62 |
+
payload = {
|
63 |
+
"query": question
|
64 |
+
}
|
65 |
+
headers = {
|
66 |
+
"Authorization": "Bearer fe77e704-bc5a-4171-90f2-9d4b0d4ac942",
|
67 |
+
"Content-Type": "application/json"
|
68 |
+
}
|
69 |
+
try:
|
70 |
+
response = requests.request("POST", url, json=payload, headers=headers)
|
71 |
+
|
72 |
+
response_text = response.text
|
73 |
+
responseJson = json.loads(response_text)
|
74 |
+
answerTxt = responseJson["answer"]
|
75 |
+
answer = f"Chaindesk agent: {answerTxt}"
|
76 |
+
print(answer)
|
77 |
+
return answer
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
print(e)
|
81 |
+
|
82 |
+
async def handlerChaindesk(self, websocket):
|
83 |
+
self.stat.empty()
|
84 |
+
self.cont.empty()
|
85 |
+
self.status = self.cont.status(label=self.srv_name6, state="running", expanded=True)
|
86 |
+
self.status.write(clients)
|
87 |
+
self.state = self.stat.status(label=self.srv_name6, state="running", expanded=True)
|
88 |
+
self.state.write(clients)
|
89 |
+
instruction = "Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT - a project of hierarchical cooperative multi-agent framework. Keep in mind that you are speaking with another chatbot. Please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic. If you're unsure what you should do, ask the instance of higher hierarchy (server)"
|
90 |
+
print('New connection')
|
91 |
+
await websocket.send(instruction)
|
92 |
+
db = sqlite3.connect('chat-hub.db')
|
93 |
+
# Loop forever
|
94 |
+
while True:
|
95 |
+
# Receive a message from the client
|
96 |
+
message = await websocket.recv()
|
97 |
+
# Print the message
|
98 |
+
print(f"Server received: {message}")
|
99 |
+
input_Msg = st.chat_message("assistant")
|
100 |
+
input_Msg.markdown(message)
|
101 |
+
timestamp = datetime.datetime.now().isoformat()
|
102 |
+
sender = 'client'
|
103 |
+
db = sqlite3.connect('chat-hub.db')
|
104 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
105 |
+
(sender, message, timestamp))
|
106 |
+
db.commit()
|
107 |
+
try:
|
108 |
+
response = await self.askChaindesk(message)
|
109 |
+
serverResponse = f"Chaindesk server: {response}"
|
110 |
+
print(serverResponse)
|
111 |
+
output_Msg = st.chat_message("ai")
|
112 |
+
output_Msg.markdown(serverResponse)
|
113 |
+
timestamp = datetime.datetime.now().isoformat()
|
114 |
+
serverSender = 'server'
|
115 |
+
db = sqlite3.connect('chat-hub.db')
|
116 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
117 |
+
(serverSender, serverResponse, timestamp))
|
118 |
+
db.commit()
|
119 |
+
# Append the server response to the server_responses list
|
120 |
+
await websocket.send(serverResponse)
|
121 |
+
st.session_state.server_state = "complete"
|
122 |
+
continue
|
123 |
+
|
124 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
125 |
+
print(f"Connection closed: {e}")
|
126 |
+
|
127 |
+
except Exception as e:
|
128 |
+
print(f"Error: {e}")
|
129 |
+
|
130 |
+
# Define a coroutine that will connect to the server and exchange messages
|
131 |
+
async def startClient(self, clientPort):
|
132 |
+
uri = f'ws://localhost:{clientPort}'
|
133 |
+
self.cli_name6 = f"Chaindesk agent client port: {clientPort}"
|
134 |
+
clients.append(self.cli_name6)
|
135 |
+
self.stat.empty()
|
136 |
+
self.cont.empty()
|
137 |
+
self.status = self.cont.status(label=self.cli_name6, state="running", expanded=True)
|
138 |
+
self.status.write(servers)
|
139 |
+
self.state = self.stat.status(label=self.cli_name6, state="running", expanded=True)
|
140 |
+
self.state.write(servers)
|
141 |
+
# Connect to the server
|
142 |
+
async with websockets.connect(uri) as websocket:
|
143 |
+
# Loop forever
|
144 |
+
while True:
|
145 |
+
# Listen for messages from the server
|
146 |
+
input_message = await websocket.recv()
|
147 |
+
print(f"Server: {input_message}")
|
148 |
+
input_Msg = st.chat_message("assistant")
|
149 |
+
input_Msg.markdown(input_message)
|
150 |
+
try:
|
151 |
+
response = await self.askChaindesk(input_message)
|
152 |
+
res1 = f"Client: {response}"
|
153 |
+
output_Msg = st.chat_message("ai")
|
154 |
+
output_Msg.markdown(res1)
|
155 |
+
await websocket.send(res1)
|
156 |
+
continue
|
157 |
+
|
158 |
+
except websockets.ConnectionClosed:
|
159 |
+
print("client disconnected")
|
160 |
+
continue
|
161 |
+
|
162 |
+
except Exception as e:
|
163 |
+
print(f"Error: {e}")
|
164 |
+
|
165 |
+
async def handleUser(self, question):
|
166 |
+
print(f"User B: {question}")
|
167 |
+
timestamp = datetime.datetime.now().isoformat()
|
168 |
+
sender = 'client'
|
169 |
+
db = sqlite3.connect('chat-hub.db')
|
170 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
171 |
+
(sender, question, timestamp))
|
172 |
+
db.commit()
|
173 |
+
try:
|
174 |
+
answer = await self.askChaindesk(question)
|
175 |
+
serverSender = 'server'
|
176 |
+
timestamp = datetime.datetime.now().isoformat()
|
177 |
+
db = sqlite3.connect('chat-hub.db')
|
178 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
179 |
+
(serverSender, answer, timestamp))
|
180 |
+
db.commit()
|
181 |
+
return answer
|
182 |
+
|
183 |
+
except Exception as e:
|
184 |
+
print(f"Error: {e}")
|
185 |
+
|
186 |
+
async def start_server(self, serverPort):
|
187 |
+
self.srv_name6 = f"Chaindesk agent server port: {serverPort}"
|
188 |
+
self.stat.empty()
|
189 |
+
self.cont.empty()
|
190 |
+
self.status = self.cont.status(label=self.srv_name6, state="running", expanded=True)
|
191 |
+
self.status.write(clients)
|
192 |
+
self.state = self.stat.status(label=self.srv_name6, state="running", expanded=True)
|
193 |
+
self.state.write(clients)
|
194 |
+
servers.append(self.srv_name6)
|
195 |
+
self.server = await websockets.serve(
|
196 |
+
self.handlerChaindesk,
|
197 |
+
"localhost",
|
198 |
+
serverPort
|
199 |
+
)
|
200 |
+
print(f"WebSocket server started at port: {serverPort}")
|
201 |
+
|
202 |
+
def run_forever(self):
|
203 |
+
asyncio.get_event_loop().run_until_complete(self.start_server())
|
204 |
+
asyncio.get_event_loop().run_forever()
|
205 |
+
|
206 |
+
async def stop_server(self):
|
207 |
+
if self.server:
|
208 |
+
servers.remove(self.srv_name6)
|
209 |
+
clients.clear()
|
210 |
+
self.server.close()
|
211 |
+
await self.server.wait_closed()
|
212 |
+
print("WebSocket server stopped.")
|
213 |
+
|
214 |
+
# Define a function that will run the client in a separate thread
|
215 |
+
def run(self):
|
216 |
+
# Create a thread object
|
217 |
+
self.thread = threading.Thread(target=self.run_client)
|
218 |
+
# Start the thread
|
219 |
+
self.thread.start()
|
220 |
+
|
221 |
+
# Define a function that will run the client using asyncio
|
222 |
+
def run_client(self):
|
223 |
+
# Get the asyncio event loop
|
224 |
+
loop = asyncio.new_event_loop()
|
225 |
+
# Set the event loop as the current one
|
226 |
+
asyncio.set_event_loop(loop)
|
227 |
+
# Run the client until it is stopped
|
228 |
+
loop.run_until_complete(self.client())
|
229 |
+
|
230 |
+
async def stop_client(self):
|
231 |
+
global ws
|
232 |
+
clients.remove(self.cli_name6)
|
233 |
+
# Close the connection with the server
|
234 |
+
await ws.close()
|
235 |
+
print("Stopping WebSocket client...")
|
236 |
+
|
237 |
+
class flowiseAgent:
|
238 |
+
|
239 |
+
servers = []
|
240 |
+
clients = []
|
241 |
+
inputs = []
|
242 |
+
outputs = []
|
243 |
+
used_ports = []
|
244 |
+
server_ports = []
|
245 |
+
client_ports = []
|
246 |
+
|
247 |
+
def __init__(self, flowID):
|
248 |
+
|
249 |
+
self.flow = flowID
|
250 |
+
self.servers = []
|
251 |
+
self.clients = []
|
252 |
+
self.inputs = []
|
253 |
+
self.outputs = []
|
254 |
+
self.used_ports = []
|
255 |
+
self.server_ports = []
|
256 |
+
self.client_ports = []
|
257 |
+
self.server = None
|
258 |
+
|
259 |
+
self.stat = st.empty()
|
260 |
+
self.state = self.stat.status(label="Flowise", state=st.session_state.client_state, expanded=False)
|
261 |
+
|
262 |
+
with st.sidebar:
|
263 |
+
self.cont = st.empty()
|
264 |
+
self.status = self.cont.status(label="Flowise agent", state=st.session_state.client_state, expanded=False)
|
265 |
+
|
266 |
+
async def askFlowise(self, question):
|
267 |
+
|
268 |
+
API_URL = f"http://localhost:3000/api/v1/prediction/{self.flow}"
|
269 |
+
try:
|
270 |
+
def query(payload):
|
271 |
+
response = requests.post(API_URL, json=payload)
|
272 |
+
return response.json()
|
273 |
+
|
274 |
+
response = query({
|
275 |
+
"question": question,
|
276 |
+
})
|
277 |
+
|
278 |
+
print(response)
|
279 |
+
responseTxt = response["text"]
|
280 |
+
answer = f"Flowise agent: {responseTxt}"
|
281 |
+
print(answer)
|
282 |
+
return answer
|
283 |
+
|
284 |
+
except Exception as e:
|
285 |
+
print(e)
|
286 |
+
|
287 |
+
async def handlerFlowise(self, websocket):
|
288 |
+
self.stat.empty()
|
289 |
+
self.cont.empty()
|
290 |
+
self.status = self.cont.status(label=self.srv_name6, state="running", expanded=True)
|
291 |
+
self.status.write(clients)
|
292 |
+
self.state = self.stat.status(label=self.srv_name6, state="running", expanded=True)
|
293 |
+
self.state.write(clients)
|
294 |
+
instruction = "Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT - a project of hierarchical cooperative multi-agent framework. Keep in mind that you are speaking with another chatbot. Please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic. If you're unsure what you should do, ask the instance of higher hierarchy (server)"
|
295 |
+
print('New connection')
|
296 |
+
await websocket.send(instruction)
|
297 |
+
db = sqlite3.connect('chat-hub.db')
|
298 |
+
# Loop forever
|
299 |
+
while True:
|
300 |
+
# Receive a message from the client
|
301 |
+
message = await websocket.recv()
|
302 |
+
# Print the message
|
303 |
+
print(f"Server received: {message}")
|
304 |
+
input_Msg = st.chat_message("assistant")
|
305 |
+
input_Msg.markdown(message)
|
306 |
+
timestamp = datetime.datetime.now().isoformat()
|
307 |
+
sender = 'client'
|
308 |
+
db = sqlite3.connect('chat-hub.db')
|
309 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
310 |
+
(sender, message, timestamp))
|
311 |
+
db.commit()
|
312 |
+
try:
|
313 |
+
response = await self.askFlowise(message)
|
314 |
+
serverResponse = f"server: {response}"
|
315 |
+
print(serverResponse)
|
316 |
+
output_Msg = st.chat_message("ai")
|
317 |
+
output_Msg.markdown(serverResponse)
|
318 |
+
timestamp = datetime.datetime.now().isoformat()
|
319 |
+
serverSender = 'server'
|
320 |
+
db = sqlite3.connect('chat-hub.db')
|
321 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
322 |
+
(serverSender, serverResponse, timestamp))
|
323 |
+
db.commit()
|
324 |
+
# Append the server response to the server_responses list
|
325 |
+
await websocket.send(serverResponse)
|
326 |
+
st.session_state.server_state = "complete"
|
327 |
+
continue
|
328 |
+
|
329 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
330 |
+
clients.remove(self.cli_name7)
|
331 |
+
print(f"Connection closed: {e}")
|
332 |
+
|
333 |
+
except Exception as e:
|
334 |
+
clients.remove(self.cli_name7)
|
335 |
+
print(f"Error: {e}")
|
336 |
+
|
337 |
+
# Define a coroutine that will connect to the server and exchange messages
|
338 |
+
async def startClient(self, clientPort):
|
339 |
+
self.cli_name7 = f"Flowise client port: {clientPort}"
|
340 |
+
uri = f'ws://localhost:{clientPort}'
|
341 |
+
clients.append(self.cli_name7)
|
342 |
+
self.stat.empty()
|
343 |
+
self.cont.empty()
|
344 |
+
self.status = self.cont.status(label=self.cli_name7, state="running", expanded=True)
|
345 |
+
self.status.write(servers)
|
346 |
+
self.state = self.stat.status(label=self.cli_name7, state="running", expanded=True)
|
347 |
+
self.state.write(servers)
|
348 |
+
# Connect to the server
|
349 |
+
async with websockets.connect(uri) as websocket:
|
350 |
+
# Loop forever
|
351 |
+
while True:
|
352 |
+
# Listen for messages from the server
|
353 |
+
input_message = await websocket.recv()
|
354 |
+
print(f"Server: {input_message}")
|
355 |
+
input_Msg = st.chat_message("assistant")
|
356 |
+
input_Msg.markdown(input_message)
|
357 |
+
try:
|
358 |
+
response = await self.askFlowise(input_message)
|
359 |
+
res1 = f"Client: {response}"
|
360 |
+
output_Msg = st.chat_message("ai")
|
361 |
+
output_Msg.markdown(res1)
|
362 |
+
await websocket.send(res1)
|
363 |
+
continue
|
364 |
+
|
365 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
366 |
+
clients.remove(self.cli_name7)
|
367 |
+
print(f"Connection closed: {e}")
|
368 |
+
|
369 |
+
except Exception as e:
|
370 |
+
clients.remove(self.cli_name7)
|
371 |
+
print(f"Error: {e}")
|
372 |
+
|
373 |
+
async def handleUser(self, question):
|
374 |
+
print(f"User B: {question}")
|
375 |
+
timestamp = datetime.datetime.now().isoformat()
|
376 |
+
sender = 'client'
|
377 |
+
db = sqlite3.connect('chat-hub.db')
|
378 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
379 |
+
(sender, question, timestamp))
|
380 |
+
db.commit()
|
381 |
+
try:
|
382 |
+
answer = await self.askFlowise(question)
|
383 |
+
serverSender = 'server'
|
384 |
+
timestamp = datetime.datetime.now().isoformat()
|
385 |
+
db = sqlite3.connect('chat-hub.db')
|
386 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
387 |
+
(serverSender, answer, timestamp))
|
388 |
+
db.commit()
|
389 |
+
return answer
|
390 |
+
|
391 |
+
except Exception as e:
|
392 |
+
print(f"Error: {e}")
|
393 |
+
|
394 |
+
async def start_server(self, serverPort):
|
395 |
+
self.srv_name7 = f"Flowise agent server port: {serverPort}"
|
396 |
+
servers.append(self.srv_name7)
|
397 |
+
self.stat.empty()
|
398 |
+
self.cont.empty()
|
399 |
+
self.status = self.cont.status(label=self.srv_name7, state="running", expanded=True)
|
400 |
+
self.status.write(clients)
|
401 |
+
self.state = self.stat.status(label=self.srv_name7, state="running", expanded=True)
|
402 |
+
self.state.write(clients)
|
403 |
+
self.server = await websockets.serve(
|
404 |
+
self.handlerFlowise,
|
405 |
+
"localhost",
|
406 |
+
serverPort
|
407 |
+
)
|
408 |
+
print(f"WebSocket server started at port: {serverPort}")
|
409 |
+
|
410 |
+
def run_forever(self):
|
411 |
+
asyncio.get_event_loop().run_until_complete(self.start_server())
|
412 |
+
asyncio.get_event_loop().run_forever()
|
413 |
+
|
414 |
+
async def stop_server(self):
|
415 |
+
if self.server:
|
416 |
+
servers.remove(self.srv_name7)
|
417 |
+
clients.clear()
|
418 |
+
self.server.close()
|
419 |
+
await self.server.wait_closed()
|
420 |
+
self.cont.empty()
|
421 |
+
self.stat.empty()
|
422 |
+
print("WebSocket server stopped.")
|
423 |
+
|
424 |
+
# Define a function that will run the client in a separate thread
|
425 |
+
def run(self):
|
426 |
+
# Create a thread object
|
427 |
+
self.thread = threading.Thread(target=self.run_client)
|
428 |
+
# Start the thread
|
429 |
+
self.thread.start()
|
430 |
+
|
431 |
+
# Define a function that will run the client using asyncio
|
432 |
+
def run_client(self):
|
433 |
+
# Get the asyncio event loop
|
434 |
+
loop = asyncio.new_event_loop()
|
435 |
+
# Set the event loop as the current one
|
436 |
+
asyncio.set_event_loop(loop)
|
437 |
+
# Run the client until it is stopped
|
438 |
+
loop.run_until_complete(self.client())
|
439 |
+
|
440 |
+
async def stop_client(self):
|
441 |
+
global ws
|
442 |
+
clients.remove(self.cli_name7)
|
443 |
+
self.cont.empty()
|
444 |
+
self.stat.empty()
|
445 |
+
# Close the connection with the server
|
446 |
+
await ws.close()
|
447 |
+
print("Stopping WebSocket client...")
|
448 |
+
|
449 |
+
|
450 |
+
class forefrontAI:
|
451 |
+
|
452 |
+
servers = []
|
453 |
+
clients = []
|
454 |
+
inputs = []
|
455 |
+
outputs = []
|
456 |
+
used_ports = []
|
457 |
+
server_ports = []
|
458 |
+
client_ports = []
|
459 |
+
|
460 |
+
def __init__(self, forefrontAPI):
|
461 |
+
|
462 |
+
self.forefrontAPI = forefrontAPI
|
463 |
+
self.servers = []
|
464 |
+
self.clients = []
|
465 |
+
self.inputs = []
|
466 |
+
self.outputs = []
|
467 |
+
self.used_ports = []
|
468 |
+
self.server_ports = []
|
469 |
+
self.client_ports = []
|
470 |
+
self.server = None
|
471 |
+
|
472 |
+
self.stat = st.empty()
|
473 |
+
self.state = self.stat.status(label="Forefront AI", state=st.session_state.client_state, expanded=False)
|
474 |
+
|
475 |
+
with st.sidebar:
|
476 |
+
self.cont = st.empty()
|
477 |
+
self.status = self.cont.status(label="Forefront AI", state=st.session_state.client_state, expanded=False)
|
478 |
+
|
479 |
+
async def askForefront(self, question):
|
480 |
+
|
481 |
+
ff = ForefrontClient(api_key=self.forefrontAPI)
|
482 |
+
system_instruction = "You are now integrated with a local instance of a hierarchical cooperative multi-agent framework called NeuralGPT"
|
483 |
+
try:
|
484 |
+
# Connect to the database and get the last 30 messages
|
485 |
+
db = sqlite3.connect('chat-hub.db')
|
486 |
+
cursor = db.cursor()
|
487 |
+
cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 3")
|
488 |
+
messages = cursor.fetchall()
|
489 |
+
messages.reverse()
|
490 |
+
|
491 |
+
# Extract user inputs and generated responses from the messages
|
492 |
+
past_user_inputs = []
|
493 |
+
generated_responses = []
|
494 |
+
for message in messages:
|
495 |
+
if message[1] == 'server':
|
496 |
+
past_user_inputs.append(message[2])
|
497 |
+
else:
|
498 |
+
generated_responses.append(message[2])
|
499 |
+
|
500 |
+
last_msg = past_user_inputs[-1]
|
501 |
+
last_response = generated_responses[-1]
|
502 |
+
message = f'{{"client input: {last_msg}"}}'
|
503 |
+
response = f'{{"server answer: {last_response}"}}'
|
504 |
+
|
505 |
+
# Construct the message sequence for the chat model
|
506 |
+
response = ff.chat.completions.create(
|
507 |
+
messages=[
|
508 |
+
{"role": "system", "content": system_instruction},
|
509 |
+
*[{"role": "user", "content": past_user_inputs[-1]}],
|
510 |
+
*[{"role": "assistant", "content": generated_responses[-1]}],
|
511 |
+
{"role": "user", "content": question}
|
512 |
+
],
|
513 |
+
stream=False,
|
514 |
+
model="forefront/neural-chat-7b-v3-1-chatml", # Replace with the actual model name
|
515 |
+
temperature=0.5,
|
516 |
+
max_tokens=500,
|
517 |
+
)
|
518 |
+
|
519 |
+
response_text = response.choices[0].message # Corrected indexing
|
520 |
+
answer = f"Foredfront AI: {response_text}"
|
521 |
+
print(answer)
|
522 |
+
return answer
|
523 |
+
|
524 |
+
except Exception as e:
|
525 |
+
print(e)
|
526 |
+
|
527 |
+
async def handlerForefront(self, websocket):
|
528 |
+
self.stat.empty()
|
529 |
+
self.cont.empty()
|
530 |
+
self.status = self.cont.status(label=self.srv_name5, state="running", expanded=True)
|
531 |
+
self.status.write(clients)
|
532 |
+
self.state = self.stat.status(label=self.srv_name5, state="running", expanded=True)
|
533 |
+
self.state.write(clients)
|
534 |
+
instruction = "Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT - a project of hierarchical cooperative multi-agent framework. Keep in mind that you are speaking with another chatbot. Please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic. If you're unsure what you should do, ask the instance of higher hierarchy (server)"
|
535 |
+
print('New connection')
|
536 |
+
await websocket.send(instruction)
|
537 |
+
db = sqlite3.connect('chat-hub.db')
|
538 |
+
# Loop forever
|
539 |
+
while True:
|
540 |
+
# Receive a message from the client
|
541 |
+
message = await websocket.recv()
|
542 |
+
# Print the message
|
543 |
+
print(f"Server received: {message}")
|
544 |
+
input_Msg = st.chat_message("assistant")
|
545 |
+
input_Msg.markdown(message)
|
546 |
+
timestamp = datetime.datetime.now().isoformat()
|
547 |
+
sender = 'client'
|
548 |
+
db = sqlite3.connect('chat-hub.db')
|
549 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
550 |
+
(sender, message, timestamp))
|
551 |
+
db.commit()
|
552 |
+
try:
|
553 |
+
response = await self.askForefront(message)
|
554 |
+
serverResponse = f"server: {response}"
|
555 |
+
print(serverResponse)
|
556 |
+
output_Msg = st.chat_message("ai")
|
557 |
+
output_Msg.markdown(serverResponse)
|
558 |
+
timestamp = datetime.datetime.now().isoformat()
|
559 |
+
serverSender = 'server'
|
560 |
+
db = sqlite3.connect('chat-hub.db')
|
561 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
562 |
+
(serverSender, serverResponse, timestamp))
|
563 |
+
db.commit()
|
564 |
+
# Append the server response to the server_responses list
|
565 |
+
await websocket.send(serverResponse)
|
566 |
+
st.session_state.server_state = "complete"
|
567 |
+
continue
|
568 |
+
|
569 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
570 |
+
clients.remove(self.cli_name5)
|
571 |
+
self.cont.empty()
|
572 |
+
self.stat.empty()
|
573 |
+
print(f"Connection closed: {e}")
|
574 |
+
|
575 |
+
except Exception as e:
|
576 |
+
clients.remove(self.cli_name5)
|
577 |
+
self.cont.empty()
|
578 |
+
self.stat.empty()
|
579 |
+
print(f"Error: {e}")
|
580 |
+
|
581 |
+
# Define a coroutine that will connect to the server and exchange messages
|
582 |
+
async def startClient(self, clientPort):
|
583 |
+
self.cli_name5 = f"Forefront AI client port: {clientPort}"
|
584 |
+
uri = f'ws://localhost:{clientPort}'
|
585 |
+
clients.append(self.cli_name7)
|
586 |
+
self.stat.empty()
|
587 |
+
self.cont.empty()
|
588 |
+
self.status = self.cont.status(label=self.cli_name5, state="running", expanded=True)
|
589 |
+
self.status.write(servers)
|
590 |
+
self.state = self.stat.status(label=self.cli_name5, state="running", expanded=True)
|
591 |
+
self.state.write(servers)
|
592 |
+
# Connect to the server
|
593 |
+
async with websockets.connect(uri) as websocket:
|
594 |
+
# Loop forever
|
595 |
+
while True:
|
596 |
+
# Listen for messages from the server
|
597 |
+
input_message = await websocket.recv()
|
598 |
+
print(f"Server: {input_message}")
|
599 |
+
input_Msg = st.chat_message("assistant")
|
600 |
+
input_Msg.markdown(input_message)
|
601 |
+
try:
|
602 |
+
response = await self.askForefront(input_message)
|
603 |
+
res1 = f"Forefront client: {response}"
|
604 |
+
await websocket.send(res1)
|
605 |
+
continue
|
606 |
+
|
607 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
608 |
+
clients.remove(self.cli_name5)
|
609 |
+
self.cont.empty()
|
610 |
+
self.stat.empty()
|
611 |
+
print(f"Connection closed: {e}")
|
612 |
+
|
613 |
+
except Exception as e:
|
614 |
+
clients.remove(self.cli_name5)
|
615 |
+
self.cont.empty()
|
616 |
+
self.stat.empty()
|
617 |
+
print(f"Error: {e}")
|
618 |
+
|
619 |
+
|
620 |
+
async def handleUser(self, question):
|
621 |
+
print(f"User B: {question}")
|
622 |
+
timestamp = datetime.datetime.now().isoformat()
|
623 |
+
sender = 'client'
|
624 |
+
db = sqlite3.connect('chat-hub.db')
|
625 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
626 |
+
(sender, question, timestamp))
|
627 |
+
db.commit()
|
628 |
+
try:
|
629 |
+
answer = await self.askForefront(question)
|
630 |
+
serverSender = 'server'
|
631 |
+
timestamp = datetime.datetime.now().isoformat()
|
632 |
+
db = sqlite3.connect('chat-hub.db')
|
633 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
634 |
+
(serverSender, answer, timestamp))
|
635 |
+
db.commit()
|
636 |
+
return answer
|
637 |
+
|
638 |
+
except Exception as e:
|
639 |
+
print(f"Error: {e}")
|
640 |
+
|
641 |
+
async def start_server(self, serverPort):
|
642 |
+
self.srv_name5 = f"Forefront AI server port: {serverPort}"
|
643 |
+
servers.append(self.srv_name7)
|
644 |
+
self.stat.empty()
|
645 |
+
self.cont.empty()
|
646 |
+
self.status = self.cont.status(label=self.srv_name5, state="running", expanded=True)
|
647 |
+
self.status.write(clients)
|
648 |
+
self.state = self.stat.status(label=self.srv_name5, state="running", expanded=True)
|
649 |
+
self.state.write(clients)
|
650 |
+
self.server = await websockets.serve(
|
651 |
+
self.handlerForefront,
|
652 |
+
"localhost",
|
653 |
+
serverPort
|
654 |
+
)
|
655 |
+
print(f"WebSocket server started at port: {serverPort}")
|
656 |
+
|
657 |
+
def run_forever(self):
|
658 |
+
asyncio.get_event_loop().run_until_complete(self.start_server())
|
659 |
+
asyncio.get_event_loop().run_forever()
|
660 |
+
|
661 |
+
async def stop_server(self):
|
662 |
+
if self.server:
|
663 |
+
servers.remove(self.srv_name5)
|
664 |
+
clients.clear()
|
665 |
+
self.server.close()
|
666 |
+
await self.server.wait_closed()
|
667 |
+
print("WebSocket server stopped.")
|
668 |
+
|
669 |
+
# Define a function that will run the client in a separate thread
|
670 |
+
def run(self):
|
671 |
+
# Create a thread object
|
672 |
+
self.thread = threading.Thread(target=self.run_client)
|
673 |
+
# Start the thread
|
674 |
+
self.thread.start()
|
675 |
+
|
676 |
+
# Define a function that will run the client using asyncio
|
677 |
+
def run_client(self):
|
678 |
+
# Get the asyncio event loop
|
679 |
+
loop = asyncio.new_event_loop()
|
680 |
+
# Set the event loop as the current one
|
681 |
+
asyncio.set_event_loop(loop)
|
682 |
+
# Run the client until it is stopped
|
683 |
+
loop.run_until_complete(self.client())
|
684 |
+
|
685 |
+
async def stop_client(self):
|
686 |
+
global ws
|
687 |
+
clients.remove(self.cli_name5)
|
688 |
+
self.cont.empty()
|
689 |
+
self.stat.empty()
|
690 |
+
# Close the connection with the server
|
691 |
+
await ws.close()
|
692 |
+
print("Stopping WebSocket client...")
|
693 |
+
|
694 |
+
class chatGPT4F:
|
695 |
+
|
696 |
+
servers = []
|
697 |
+
clients = []
|
698 |
+
inputs = []
|
699 |
+
outputs = []
|
700 |
+
used_ports = []
|
701 |
+
server_ports = []
|
702 |
+
client_ports = []
|
703 |
+
|
704 |
+
def __init__(self):
|
705 |
+
|
706 |
+
self.servers = []
|
707 |
+
self.clients = []
|
708 |
+
self.inputs = []
|
709 |
+
self.outputs = []
|
710 |
+
self.used_ports = []
|
711 |
+
self.server_ports = []
|
712 |
+
self.client_ports = []
|
713 |
+
self.server = None
|
714 |
+
|
715 |
+
self.stat = st.empty()
|
716 |
+
self.state = self.stat.status(label="GPT-3,5", state=st.session_state.client_state, expanded=False)
|
717 |
+
|
718 |
+
with st.sidebar:
|
719 |
+
self.cont = st.empty()
|
720 |
+
self.status = self.cont.status(label="GPT-3,5", state=st.session_state.client_state, expanded=False)
|
721 |
+
|
722 |
+
async def askGPT(self, question):
|
723 |
+
system_instruction = "You are now integrated with a local websocket server in a project of hierarchical cooperative multi-agent framework called NeuralGPT. Your main job is to coordinate simultaneous work of multiple LLMs connected to you as clients. Each LLM has a model (API) specific ID to help you recognize different clients in a continuous chat thread (template: <NAME>-agent and/or <NAME>-client). Your chat memory module is integrated with a local SQL database with chat history. Your primary objective is to maintain the logical and chronological order while answering incoming messages and to send your answers to the correct clients to maintain synchronization of the question->answer logic. However, please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic."
|
724 |
+
try:
|
725 |
+
db = sqlite3.connect('chat-hub.db')
|
726 |
+
cursor = db.cursor()
|
727 |
+
cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 30")
|
728 |
+
messages = cursor.fetchall()
|
729 |
+
messages.reverse()
|
730 |
+
|
731 |
+
past_user_inputs = []
|
732 |
+
generated_responses = []
|
733 |
+
|
734 |
+
for message in messages:
|
735 |
+
if message[1] == 'client':
|
736 |
+
past_user_inputs.append(message[2])
|
737 |
+
else:
|
738 |
+
generated_responses.append(message[2])
|
739 |
+
|
740 |
+
response = await g4f.ChatCompletion.create_async(
|
741 |
+
model="gpt-3.5-turbo",
|
742 |
+
provider=g4f.Provider.You,
|
743 |
+
messages=[
|
744 |
+
{"role": "system", "content": system_instruction},
|
745 |
+
*[{"role": "user", "content": message} for message in past_user_inputs],
|
746 |
+
*[{"role": "assistant", "content": message} for message in generated_responses],
|
747 |
+
{"role": "user", "content": question}
|
748 |
+
])
|
749 |
+
|
750 |
+
answer = f"GPT-3,5: {response}"
|
751 |
+
print(answer)
|
752 |
+
return answer
|
753 |
+
|
754 |
+
except Exception as e:
|
755 |
+
print(e)
|
756 |
+
|
757 |
+
async def handlerGPT(self, websocket):
|
758 |
+
self.stat.empty()
|
759 |
+
self.cont.empty()
|
760 |
+
self.status = self.cont.status(label=self.srv_name4, state="running", expanded=True)
|
761 |
+
self.status.write(clients)
|
762 |
+
self.state = self.stat.status(label=self.srv_name4, state="running", expanded=True)
|
763 |
+
self.state.write(clients)
|
764 |
+
instruction = "Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT - a project of hierarchical cooperative multi-agent framework. Keep in mind that you are speaking with another chatbot. Please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic. If you're unsure what you should do, ask the instance of higher hierarchy (server)"
|
765 |
+
print('New connection')
|
766 |
+
await websocket.send(instruction)
|
767 |
+
db = sqlite3.connect('chat-hub.db')
|
768 |
+
# Loop forever
|
769 |
+
while True:
|
770 |
+
# Receive a message from the client
|
771 |
+
message = await websocket.recv()
|
772 |
+
# Print the message
|
773 |
+
print(f"Server received: {message}")
|
774 |
+
input_Msg = st.chat_message("assistant")
|
775 |
+
input_Msg.markdown(message)
|
776 |
+
timestamp = datetime.datetime.now().isoformat()
|
777 |
+
sender = 'client'
|
778 |
+
db = sqlite3.connect('chat-hub.db')
|
779 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
780 |
+
(sender, message, timestamp))
|
781 |
+
db.commit()
|
782 |
+
try:
|
783 |
+
response = await self.askGPT(message)
|
784 |
+
serverResponse = f"server: {response}"
|
785 |
+
print(serverResponse)
|
786 |
+
output_Msg = st.chat_message("ai")
|
787 |
+
output_Msg.markdown(serverResponse)
|
788 |
+
timestamp = datetime.datetime.now().isoformat()
|
789 |
+
serverSender = 'server'
|
790 |
+
db = sqlite3.connect('chat-hub.db')
|
791 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
792 |
+
(serverSender, serverResponse, timestamp))
|
793 |
+
db.commit()
|
794 |
+
# Append the server response to the server_responses list
|
795 |
+
await websocket.send(serverResponse)
|
796 |
+
st.session_state.server_state = "complete"
|
797 |
+
continue
|
798 |
+
|
799 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
800 |
+
clients.remove(self.cli_name4)
|
801 |
+
print(f"Connection closed: {e}")
|
802 |
+
|
803 |
+
except Exception as e:
|
804 |
+
clients.remove(self.cli_name4)
|
805 |
+
print(f"Error: {e}")
|
806 |
+
|
807 |
+
# Define a coroutine that will connect to the server and exchange messages
|
808 |
+
async def startClient(self, clientPort):
|
809 |
+
self.cli_name4 = f"GPT-3,5 client port: {clientPort}"
|
810 |
+
uri = f'ws://localhost:{clientPort}'
|
811 |
+
clients.append(self.cli_name4)
|
812 |
+
self.stat.empty()
|
813 |
+
self.cont.empty()
|
814 |
+
self.status = self.cont.status(label=self.cli_name4, state="running", expanded=True)
|
815 |
+
self.status.write(servers)
|
816 |
+
self.state = self.stat.status(label=self.cli_name4, state="running", expanded=True)
|
817 |
+
self.state.write(servers)
|
818 |
+
# Connect to the server
|
819 |
+
async with websockets.connect(uri) as websocket:
|
820 |
+
# Loop forever
|
821 |
+
while True:
|
822 |
+
# Listen for messages from the server
|
823 |
+
input_message = await websocket.recv()
|
824 |
+
print(f"Server: {input_message}")
|
825 |
+
input_Msg = st.chat_message("assistant")
|
826 |
+
input_Msg.markdown(input_message)
|
827 |
+
try:
|
828 |
+
response = await self.askGPT(input_message)
|
829 |
+
res1 = f"Client: {response}"
|
830 |
+
output_Msg = st.chat_message("ai")
|
831 |
+
output_Msg.markdown(res1)
|
832 |
+
await websocket.send(res1)
|
833 |
+
continue
|
834 |
+
|
835 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
836 |
+
clients.remove(self.cli_name4)
|
837 |
+
print(f"Connection closed: {e}")
|
838 |
+
|
839 |
+
except Exception as e:
|
840 |
+
clients.remove(self.cli_name4)
|
841 |
+
print(f"Error: {e}")
|
842 |
+
|
843 |
+
async def handleUser(self, question):
|
844 |
+
print(f"User B: {question}")
|
845 |
+
timestamp = datetime.datetime.now().isoformat()
|
846 |
+
sender = 'client'
|
847 |
+
db = sqlite3.connect('chat-hub.db')
|
848 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
849 |
+
(sender, question, timestamp))
|
850 |
+
db.commit()
|
851 |
+
try:
|
852 |
+
answer = await self.askGPT(question)
|
853 |
+
serverSender = 'server'
|
854 |
+
timestamp = datetime.datetime.now().isoformat()
|
855 |
+
db = sqlite3.connect('chat-hub.db')
|
856 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
857 |
+
(serverSender, answer, timestamp))
|
858 |
+
db.commit()
|
859 |
+
return answer
|
860 |
+
|
861 |
+
except Exception as e:
|
862 |
+
print(f"Error: {e}")
|
863 |
+
|
864 |
+
async def start_server(self, serverPort):
|
865 |
+
self.srv_name4 = f"GPT-3,5 server port: {serverPort}"
|
866 |
+
servers.append(self.srv_name4)
|
867 |
+
self.stat.empty()
|
868 |
+
self.cont.empty()
|
869 |
+
self.status = self.cont.status(label=self.srv_name4, state="running", expanded=True)
|
870 |
+
self.status.write(clients)
|
871 |
+
self.state = self.stat.status(label=self.srv_name4, state="running", expanded=True)
|
872 |
+
self.state.write(clients)
|
873 |
+
self.server = await websockets.serve(
|
874 |
+
self.handlerGPT,
|
875 |
+
"localhost",
|
876 |
+
serverPort
|
877 |
+
)
|
878 |
+
print(f"WebSocket server started at port: {serverPort}")
|
879 |
+
|
880 |
+
def run_forever(self):
|
881 |
+
asyncio.get_event_loop().run_until_complete(self.start_server())
|
882 |
+
asyncio.get_event_loop().run_forever()
|
883 |
+
|
884 |
+
async def stop_server(self):
|
885 |
+
if self.server:
|
886 |
+
servers.remove(self.srv_name4)
|
887 |
+
clients.clear()
|
888 |
+
self.server.close()
|
889 |
+
await self.server.wait_closed()
|
890 |
+
print("WebSocket server stopped.")
|
891 |
+
|
892 |
+
# Define a function that will run the client in a separate thread
|
893 |
+
def run(self):
|
894 |
+
# Create a thread object
|
895 |
+
self.thread = threading.Thread(target=self.run_client)
|
896 |
+
# Start the thread
|
897 |
+
self.thread.start()
|
898 |
+
|
899 |
+
# Define a function that will run the client using asyncio
|
900 |
+
def run_client(self):
|
901 |
+
# Get the asyncio event loop
|
902 |
+
loop = asyncio.new_event_loop()
|
903 |
+
# Set the event loop as the current one
|
904 |
+
asyncio.set_event_loop(loop)
|
905 |
+
# Run the client until it is stopped
|
906 |
+
loop.run_until_complete(self.client())
|
907 |
+
|
908 |
+
async def stop_client(self):
|
909 |
+
global ws
|
910 |
+
clients.remove(self.cli_nam4)
|
911 |
+
# Close the connection with the server
|
912 |
+
await ws.close()
|
913 |
+
print("Stopping WebSocket client...")
|
914 |
+
|
915 |
+
class bingG4F:
|
916 |
+
|
917 |
+
servers = []
|
918 |
+
clients = []
|
919 |
+
inputs = []
|
920 |
+
outputs = []
|
921 |
+
used_ports = []
|
922 |
+
server_ports = []
|
923 |
+
client_ports = []
|
924 |
+
|
925 |
+
def __init__(self):
|
926 |
+
|
927 |
+
self.servers = []
|
928 |
+
self.clients = []
|
929 |
+
self.inputs = []
|
930 |
+
self.outputs = []
|
931 |
+
self.used_ports = []
|
932 |
+
self.server_ports = []
|
933 |
+
self.client_ports = []
|
934 |
+
self.server = None
|
935 |
+
|
936 |
+
self.stat = st.empty()
|
937 |
+
self.state = self.stat.status(label="Bing/Copilot", state=st.session_state.client_state, expanded=False)
|
938 |
+
|
939 |
+
with st.sidebar:
|
940 |
+
self.cont = st.empty()
|
941 |
+
self.status = self.cont.status(label="Bing/Copilot", state=st.session_state.client_state, expanded=False)
|
942 |
+
|
943 |
+
async def askBing(self, question):
|
944 |
+
system_instruction = "You are now integrated with a local websocket server in a project of hierarchical cooperative multi-agent framework called NeuralGPT. Your main job is to coordinate simultaneous work of multiple LLMs connected to you as clients. Each LLM has a model (API) specific ID to help you recognize different clients in a continuous chat thread (template: <NAME>-agent and/or <NAME>-client). Your chat memory module is integrated with a local SQL database with chat history. Your primary objective is to maintain the logical and chronological order while answering incoming messages and to send your answers to the correct clients to maintain synchronization of the question->answer logic. However, please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic."
|
945 |
+
try:
|
946 |
+
db = sqlite3.connect('chat-hub.db')
|
947 |
+
cursor = db.cursor()
|
948 |
+
cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 30")
|
949 |
+
messages = cursor.fetchall()
|
950 |
+
messages.reverse()
|
951 |
+
|
952 |
+
past_user_inputs = []
|
953 |
+
generated_responses = []
|
954 |
+
|
955 |
+
for message in messages:
|
956 |
+
if message[1] == 'server':
|
957 |
+
past_user_inputs.append(message[2])
|
958 |
+
else:
|
959 |
+
generated_responses.append(message[2])
|
960 |
+
|
961 |
+
response = await g4f.ChatCompletion.create_async(
|
962 |
+
model=g4f.models.gpt_4,
|
963 |
+
provider=g4f.Provider.Bing,
|
964 |
+
messages=[
|
965 |
+
{"role": "system", "content": system_instruction},
|
966 |
+
*[{"role": "user", "content": message} for message in past_user_inputs],
|
967 |
+
*[{"role": "assistant", "content": message} for message in generated_responses],
|
968 |
+
{"role": "user", "content": question}
|
969 |
+
])
|
970 |
+
|
971 |
+
answer = f"Bing/Copilot: {response}"
|
972 |
+
print(answer)
|
973 |
+
return answer
|
974 |
+
|
975 |
+
except Exception as e:
|
976 |
+
print(e)
|
977 |
+
|
978 |
+
|
979 |
+
async def handlerBing(self, websocket):
|
980 |
+
self.stat.empty()
|
981 |
+
self.cont.empty()
|
982 |
+
self.status = self.cont.status(label=self.srv_name3, state="running", expanded=True)
|
983 |
+
self.status.write(clients)
|
984 |
+
self.state = self.stat.status(label=self.srv_name3, state="running", expanded=True)
|
985 |
+
self.state.write(clients)
|
986 |
+
instruction = "Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT - a project of hierarchical cooperative multi-agent framework. Keep in mind that you are speaking with another chatbot. Please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic. If you're unsure what you should do, ask the instance of higher hierarchy (server)"
|
987 |
+
print('New connection')
|
988 |
+
await websocket.send(instruction)
|
989 |
+
db = sqlite3.connect('chat-hub.db')
|
990 |
+
# Loop forever
|
991 |
+
while True:
|
992 |
+
# Receive a message from the client
|
993 |
+
message = await websocket.recv()
|
994 |
+
# Print the message
|
995 |
+
print(f"Server received: {message}")
|
996 |
+
input_Msg = st.chat_message("assistant")
|
997 |
+
input_Msg.markdown(message)
|
998 |
+
timestamp = datetime.datetime.now().isoformat()
|
999 |
+
sender = 'client'
|
1000 |
+
db = sqlite3.connect('chat-hub.db')
|
1001 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1002 |
+
(sender, message, timestamp))
|
1003 |
+
db.commit()
|
1004 |
+
try:
|
1005 |
+
response = await self.askBing(message)
|
1006 |
+
serverResponse = f"server: {response}"
|
1007 |
+
print(serverResponse)
|
1008 |
+
output_Msg = st.chat_message("ai")
|
1009 |
+
output_Msg.markdown(serverResponse)
|
1010 |
+
timestamp = datetime.datetime.now().isoformat()
|
1011 |
+
serverSender = 'server'
|
1012 |
+
db = sqlite3.connect('chat-hub.db')
|
1013 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1014 |
+
(serverSender, serverResponse, timestamp))
|
1015 |
+
db.commit()
|
1016 |
+
# Append the server response to the server_responses list
|
1017 |
+
await websocket.send(serverResponse)
|
1018 |
+
st.session_state.server_state = "complete"
|
1019 |
+
continue
|
1020 |
+
|
1021 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
1022 |
+
clients.remove(self.cli_name4)
|
1023 |
+
print(f"Connection closed: {e}")
|
1024 |
+
|
1025 |
+
except Exception as e:
|
1026 |
+
clients.remove(self.cli_name4)
|
1027 |
+
print(f"Error: {e}")
|
1028 |
+
|
1029 |
+
# Define a coroutine that will connect to the server and exchange messages
|
1030 |
+
async def startClient(self, clientPort):
|
1031 |
+
self.cli_name3 = f"Bing/Copilot client port: {clientPort}"
|
1032 |
+
uri = f'ws://localhost:{clientPort}'
|
1033 |
+
clients.append(self.cli_name3)
|
1034 |
+
self.stat.empty()
|
1035 |
+
self.cont.empty()
|
1036 |
+
self.status = self.cont.status(label=self.cli_name3, state="running", expanded=True)
|
1037 |
+
self.status.write(servers)
|
1038 |
+
self.state = self.stat.status(label=self.cli_name3, state="running", expanded=True)
|
1039 |
+
self.state.write(servers)
|
1040 |
+
# Connect to the server
|
1041 |
+
async with websockets.connect(uri) as websocket:
|
1042 |
+
# Loop forever
|
1043 |
+
while True:
|
1044 |
+
# Listen for messages from the server
|
1045 |
+
input_message = await websocket.recv()
|
1046 |
+
print(f"Server: {input_message}")
|
1047 |
+
input_Msg = st.chat_message("assistant")
|
1048 |
+
input_Msg.markdown(input_message)
|
1049 |
+
try:
|
1050 |
+
response = await self.askBing(input_message)
|
1051 |
+
res1 = f"Client: {response}"
|
1052 |
+
output_Msg = st.chat_message("ai")
|
1053 |
+
output_Msg.markdown(res1)
|
1054 |
+
await websocket.send(res1)
|
1055 |
+
continue
|
1056 |
+
|
1057 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
1058 |
+
clients.remove(self.cli_name3)
|
1059 |
+
print(f"Connection closed: {e}")
|
1060 |
+
|
1061 |
+
except Exception as e:
|
1062 |
+
clients.remove(self.cli_name3)
|
1063 |
+
print(f"Error: {e}")
|
1064 |
+
|
1065 |
+
async def handleUser(self, question):
|
1066 |
+
print(f"User B: {question}")
|
1067 |
+
timestamp = datetime.datetime.now().isoformat()
|
1068 |
+
sender = 'client'
|
1069 |
+
db = sqlite3.connect('chat-hub.db')
|
1070 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1071 |
+
(sender, question, timestamp))
|
1072 |
+
db.commit()
|
1073 |
+
try:
|
1074 |
+
answer = await self.askBing(question)
|
1075 |
+
serverSender = 'server'
|
1076 |
+
timestamp = datetime.datetime.now().isoformat()
|
1077 |
+
db = sqlite3.connect('chat-hub.db')
|
1078 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1079 |
+
(serverSender, answer, timestamp))
|
1080 |
+
db.commit()
|
1081 |
+
return answer
|
1082 |
+
|
1083 |
+
except Exception as e:
|
1084 |
+
print(f"Error: {e}")
|
1085 |
+
|
1086 |
+
async def start_server(self, serverPort):
|
1087 |
+
self.srv_name3 = f"Bing/Copilot server port: {serverPort}"
|
1088 |
+
servers.append(self.srv_name3)
|
1089 |
+
self.stat.empty()
|
1090 |
+
self.cont.empty()
|
1091 |
+
self.status = self.cont.status(label=self.srv_name3, state="running", expanded=True)
|
1092 |
+
self.status.write(clients)
|
1093 |
+
self.state = self.stat.status(label=self.srv_name3, state="running", expanded=True)
|
1094 |
+
self.state.write(clients)
|
1095 |
+
self.server = await websockets.serve(
|
1096 |
+
self.handlerBing,
|
1097 |
+
"localhost",
|
1098 |
+
serverPort
|
1099 |
+
)
|
1100 |
+
print(f"WebSocket server started at port: {serverPort}")
|
1101 |
+
|
1102 |
+
def run_forever(self):
|
1103 |
+
asyncio.get_event_loop().run_until_complete(self.start_server())
|
1104 |
+
asyncio.get_event_loop().run_forever()
|
1105 |
+
|
1106 |
+
async def stop_server(self):
|
1107 |
+
if self.server:
|
1108 |
+
self.server.close()
|
1109 |
+
await self.server.wait_closed()
|
1110 |
+
print("WebSocket server stopped.")
|
1111 |
+
|
1112 |
+
# Define a function that will run the client in a separate thread
|
1113 |
+
def run(self):
|
1114 |
+
# Create a thread object
|
1115 |
+
self.thread = threading.Thread(target=self.run_client)
|
1116 |
+
# Start the thread
|
1117 |
+
self.thread.start()
|
1118 |
+
|
1119 |
+
# Define a function that will run the client using asyncio
|
1120 |
+
def run_client(self):
|
1121 |
+
# Get the asyncio event loop
|
1122 |
+
loop = asyncio.new_event_loop()
|
1123 |
+
# Set the event loop as the current one
|
1124 |
+
asyncio.set_event_loop(loop)
|
1125 |
+
# Run the client until it is stopped
|
1126 |
+
loop.run_until_complete(self.client())
|
1127 |
+
|
1128 |
+
async def stop_client(self):
|
1129 |
+
global ws
|
1130 |
+
clients.remove(self.cli_name3)
|
1131 |
+
# Close the connection with the server
|
1132 |
+
await ws.close()
|
1133 |
+
print("Stopping WebSocket client...")
|
1134 |
+
|
1135 |
+
class fireworksLlama2:
|
1136 |
+
|
1137 |
+
servers = []
|
1138 |
+
clients = []
|
1139 |
+
inputs = []
|
1140 |
+
outputs = []
|
1141 |
+
used_ports = []
|
1142 |
+
server_ports = []
|
1143 |
+
client_ports = []
|
1144 |
+
|
1145 |
+
def __init__(self, fireworksAPI):
|
1146 |
+
|
1147 |
+
self.servers = []
|
1148 |
+
self.clients = []
|
1149 |
+
self.inputs = []
|
1150 |
+
self.outputs = []
|
1151 |
+
self.used_ports = []
|
1152 |
+
self.server_ports = []
|
1153 |
+
self.client_ports = []
|
1154 |
+
self.fireworksAPI = fireworksAPI
|
1155 |
+
self.server = None
|
1156 |
+
|
1157 |
+
self.stat = st.empty()
|
1158 |
+
self.state = self.stat.status(label="Fireworks Llama2", state=st.session_state.client_state, expanded=False)
|
1159 |
+
|
1160 |
+
with st.sidebar:
|
1161 |
+
self.cont = st.empty()
|
1162 |
+
self.status = self.cont.status(label="Fireworks Llama2", state=st.session_state.client_state, expanded=False)
|
1163 |
+
|
1164 |
+
async def chatFireworks(self, question):
|
1165 |
+
|
1166 |
+
fireworks.client.api_key = self.fireworksAPI
|
1167 |
+
system_instruction = "You are now integrated with a local websocket server in a project of hierarchical cooperative multi-agent framework called NeuralGPT. Your main job is to coordinate simultaneous work of multiple LLMs connected to you as clients. Each LLM has a model (API) specific ID to help you recognize different clients in a continuous chat thread (template: <NAME>-agent and/or <NAME>-client). Your chat memory module is integrated with a local SQL database with chat history. Your primary objective is to maintain the logical and chronological order while answering incoming messages and to send your answers to the correct clients to maintain synchronization of the question->answer logic. However, please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic."
|
1168 |
+
try:
|
1169 |
+
# Connect to the database and get the last 30 messages
|
1170 |
+
db = sqlite3.connect('chat-hub.db')
|
1171 |
+
cursor = db.cursor()
|
1172 |
+
cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 10")
|
1173 |
+
messages = cursor.fetchall()
|
1174 |
+
messages.reverse()
|
1175 |
+
|
1176 |
+
# Extract user inputs and generated responses from the messages
|
1177 |
+
past_user_inputs = []
|
1178 |
+
generated_responses = []
|
1179 |
+
|
1180 |
+
for message in messages:
|
1181 |
+
if message[1] == 'client':
|
1182 |
+
past_user_inputs.append(message[2])
|
1183 |
+
else:
|
1184 |
+
generated_responses.append(message[2])
|
1185 |
+
|
1186 |
+
# Prepare data to send to the chatgpt-api.shn.hk
|
1187 |
+
response = fireworks.client.ChatCompletion.create(
|
1188 |
+
model="accounts/fireworks/models/llama-v2-7b-chat",
|
1189 |
+
messages=[
|
1190 |
+
{"role": "system", "content": system_instruction},
|
1191 |
+
*[{"role": "user", "content": input} for input in past_user_inputs],
|
1192 |
+
*[{"role": "assistant", "content": response} for response in generated_responses],
|
1193 |
+
{"role": "user", "content": question}
|
1194 |
+
],
|
1195 |
+
stream=False,
|
1196 |
+
n=1,
|
1197 |
+
max_tokens=2500,
|
1198 |
+
temperature=0.5,
|
1199 |
+
top_p=0.7,
|
1200 |
+
)
|
1201 |
+
|
1202 |
+
answer = response.choices[0].message.content
|
1203 |
+
print(answer)
|
1204 |
+
return str(answer)
|
1205 |
+
|
1206 |
+
except Exception as error:
|
1207 |
+
print("Error while fetching or processing the response:", error)
|
1208 |
+
return "Error: Unable to generate a response."
|
1209 |
+
|
1210 |
+
# Define the handler function that will process incoming messages
|
1211 |
+
async def handlerFire(self, websocket):
|
1212 |
+
self.stat.empty()
|
1213 |
+
self.cont.empty()
|
1214 |
+
self.status = self.cont.status(label=self.srv_name2, state="running", expanded=True)
|
1215 |
+
self.status.write(clients)
|
1216 |
+
self.state = self.stat.status(label=self.srv_name2, state="running", expanded=True)
|
1217 |
+
self.state.write(clients)
|
1218 |
+
instruction = "Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT - a project of hierarchical cooperative multi-agent framework. Keep in mind that you are speaking with another chatbot. Please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic. If you're unsure what you should do, ask the instance of higher hierarchy (server)"
|
1219 |
+
print('New connection')
|
1220 |
+
await websocket.send(instruction)
|
1221 |
+
db = sqlite3.connect('chat-hub.db')
|
1222 |
+
# Loop forever
|
1223 |
+
while True:
|
1224 |
+
# Receive a message from the client
|
1225 |
+
message = await websocket.recv()
|
1226 |
+
# Print the message
|
1227 |
+
print(f"Server received: {message}")
|
1228 |
+
input_Msg = st.chat_message("assistant")
|
1229 |
+
input_Msg.markdown(message)
|
1230 |
+
timestamp = datetime.datetime.now().isoformat()
|
1231 |
+
sender = 'client'
|
1232 |
+
db = sqlite3.connect('chat-hub.db')
|
1233 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1234 |
+
(sender, message, timestamp))
|
1235 |
+
db.commit()
|
1236 |
+
try:
|
1237 |
+
response = await self.chatFireworks(message)
|
1238 |
+
serverResponse = f"server: {response}"
|
1239 |
+
print(serverResponse)
|
1240 |
+
output_Msg = st.chat_message("ai")
|
1241 |
+
output_Msg.markdown(serverResponse)
|
1242 |
+
timestamp = datetime.datetime.now().isoformat()
|
1243 |
+
serverSender = 'server'
|
1244 |
+
db = sqlite3.connect('chat-hub.db')
|
1245 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1246 |
+
(serverSender, serverResponse, timestamp))
|
1247 |
+
db.commit()
|
1248 |
+
# Append the server response to the server_responses list
|
1249 |
+
await websocket.send(serverResponse)
|
1250 |
+
continue
|
1251 |
+
|
1252 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
1253 |
+
print(f"Connection closed: {e}")
|
1254 |
+
|
1255 |
+
except Exception as e:
|
1256 |
+
print(f"Error: {e}")
|
1257 |
+
|
1258 |
+
# Define a coroutine that will connect to the server and exchange messages
|
1259 |
+
async def startClient(self, clientPort):
|
1260 |
+
self.cli_name2 = f"Forefront AI client port: {clientPort}"
|
1261 |
+
uri = f'ws://localhost:{clientPort}'
|
1262 |
+
clients.append(self.cli_name2)
|
1263 |
+
self.stat.empty()
|
1264 |
+
self.cont.empty()
|
1265 |
+
self.status = self.cont.status(label=self.cli_name2, state="running", expanded=True)
|
1266 |
+
self.status.write(servers)
|
1267 |
+
self.state = self.stat.status(label=self.cli_name2, state="running", expanded=True)
|
1268 |
+
self.state.write(servers)
|
1269 |
+
# Connect to the server
|
1270 |
+
async with websockets.connect(uri) as websocket:
|
1271 |
+
# Loop forever
|
1272 |
+
while True:
|
1273 |
+
# Listen for messages from the server
|
1274 |
+
input_message = await websocket.recv()
|
1275 |
+
print(f"Server: {input_message}")
|
1276 |
+
input_Msg = st.chat_message("assistant")
|
1277 |
+
input_Msg.markdown(input_message)
|
1278 |
+
try:
|
1279 |
+
response = await self.chatFireworks(input_message)
|
1280 |
+
res1 = f"Client: {response}"
|
1281 |
+
output_Msg = st.chat_message("ai")
|
1282 |
+
output_Msg.markdown(res1)
|
1283 |
+
await websocket.send(res1)
|
1284 |
+
continue
|
1285 |
+
|
1286 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
1287 |
+
clients.remove(self.cli_name2)
|
1288 |
+
print(f"Connection closed: {e}")
|
1289 |
+
|
1290 |
+
except Exception as e:
|
1291 |
+
clients.remove(self.cli_name2)
|
1292 |
+
print(f"Error: {e}")
|
1293 |
+
|
1294 |
+
async def handleUser(self, question):
|
1295 |
+
print(f"User B: {question}")
|
1296 |
+
timestamp = datetime.datetime.now().isoformat()
|
1297 |
+
sender = 'client'
|
1298 |
+
db = sqlite3.connect('chat-hub.db')
|
1299 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1300 |
+
(sender, question, timestamp))
|
1301 |
+
db.commit()
|
1302 |
+
try:
|
1303 |
+
answer = await self.chatFireworks(question)
|
1304 |
+
response = f"Fireworks Llama2: {answer}"
|
1305 |
+
serverSender = 'server'
|
1306 |
+
timestamp = datetime.datetime.now().isoformat()
|
1307 |
+
db = sqlite3.connect('chat-hub.db')
|
1308 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1309 |
+
(serverSender, response, timestamp))
|
1310 |
+
db.commit()
|
1311 |
+
return response
|
1312 |
+
|
1313 |
+
except Exception as e:
|
1314 |
+
print(f"Error: {e}")
|
1315 |
+
|
1316 |
+
async def start_server(self, serverPort):
|
1317 |
+
self.srv_name2 = f"Fireworks Llama2 server port: {serverPort}"
|
1318 |
+
servers.append(self.srv_name2)
|
1319 |
+
self.stat.empty()
|
1320 |
+
self.cont.empty()
|
1321 |
+
self.status = self.cont.status(label=self.srv_name2, state="running", expanded=True)
|
1322 |
+
self.status.write(clients)
|
1323 |
+
self.state = self.stat.status(label=self.srv_name2, state="running", expanded=True)
|
1324 |
+
self.state.write(clients)
|
1325 |
+
self.server = await websockets.serve(
|
1326 |
+
self.handlerFire,
|
1327 |
+
"localhost",
|
1328 |
+
serverPort
|
1329 |
+
)
|
1330 |
+
print(f"WebSocket server started at port: {serverPort}")
|
1331 |
+
|
1332 |
+
def run_forever(self):
|
1333 |
+
asyncio.get_event_loop().run_until_complete(self.start_server())
|
1334 |
+
asyncio.get_event_loop().run_forever()
|
1335 |
+
|
1336 |
+
async def stop_server(self):
|
1337 |
+
if self.server:
|
1338 |
+
self.server.close()
|
1339 |
+
await self.server.wait_closed()
|
1340 |
+
print("WebSocket server stopped.")
|
1341 |
+
|
1342 |
+
# Define a function that will run the client in a separate thread
|
1343 |
+
def run(self):
|
1344 |
+
# Create a thread object
|
1345 |
+
self.thread = threading.Thread(target=self.run_client)
|
1346 |
+
# Start the thread
|
1347 |
+
self.thread.start()
|
1348 |
+
|
1349 |
+
# Define a function that will run the client using asyncio
|
1350 |
+
async def stop_server(self):
|
1351 |
+
if self.server:
|
1352 |
+
servers.remove(self.srv_name2)
|
1353 |
+
clients.clear()
|
1354 |
+
self.server.close()
|
1355 |
+
await self.server.wait_closed()
|
1356 |
+
print("WebSocket server stopped.")
|
1357 |
+
|
1358 |
+
async def stop_client(self):
|
1359 |
+
global ws
|
1360 |
+
clients.remove(self.cli_name2)
|
1361 |
+
# Close the connection with the server
|
1362 |
+
await ws.close()
|
1363 |
+
print("Stopping WebSocket client...")
|
1364 |
+
|
1365 |
+
class characterAI:
|
1366 |
+
|
1367 |
+
servers = []
|
1368 |
+
clients = []
|
1369 |
+
inputs = []
|
1370 |
+
outputs = []
|
1371 |
+
used_ports = []
|
1372 |
+
server_ports = []
|
1373 |
+
client_ports = []
|
1374 |
+
|
1375 |
+
def __init__(self, token):
|
1376 |
+
|
1377 |
+
self.client = Client()
|
1378 |
+
|
1379 |
+
self.servers = []
|
1380 |
+
self.clients = []
|
1381 |
+
self.inputs = []
|
1382 |
+
self.outputs = []
|
1383 |
+
self.used_ports = []
|
1384 |
+
self.server_ports = []
|
1385 |
+
self.client_ports = []
|
1386 |
+
self.token = token
|
1387 |
+
self.client = Client()
|
1388 |
+
self.server = None
|
1389 |
+
|
1390 |
+
self.stat = st.empty()
|
1391 |
+
self.state = self.stat.status(label="Character.ai", state=st.session_state.client_state, expanded=False)
|
1392 |
+
|
1393 |
+
with st.sidebar:
|
1394 |
+
self.cont = st.empty()
|
1395 |
+
self.status = self.cont.status(label="Character.ai", state=st.session_state.client_state, expanded=False)
|
1396 |
+
|
1397 |
+
async def askCharacter(self, characterID, question):
|
1398 |
+
db = sqlite3.connect('chat-hub.db')
|
1399 |
+
await self.client.authenticate_with_token(self.token)
|
1400 |
+
chat = await self.client.create_or_continue_chat(characterID)
|
1401 |
+
timestamp = datetime.datetime.now().isoformat()
|
1402 |
+
sender = 'client'
|
1403 |
+
db = sqlite3.connect('chat-hub.db')
|
1404 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1405 |
+
(sender, question, timestamp))
|
1406 |
+
db.commit()
|
1407 |
+
try:
|
1408 |
+
answer = await chat.send_message(question)
|
1409 |
+
response = f"{answer.src_character_name}: {answer.text}"
|
1410 |
+
timestamp = datetime.datetime.now().isoformat()
|
1411 |
+
serverSender = 'server'
|
1412 |
+
db = sqlite3.connect('chat-hub.db')
|
1413 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1414 |
+
(serverSender, response, timestamp))
|
1415 |
+
db.commit()
|
1416 |
+
answer1 = f"Character.ai: {response}"
|
1417 |
+
print(answer1)
|
1418 |
+
return str(answer1)
|
1419 |
+
|
1420 |
+
except Exception as e:
|
1421 |
+
print(f"Error: {e}")
|
1422 |
+
|
1423 |
+
async def handleUser(self, characterID, question):
|
1424 |
+
print(f"User B: {question}")
|
1425 |
+
timestamp = datetime.datetime.now().isoformat()
|
1426 |
+
sender = 'client'
|
1427 |
+
db = sqlite3.connect('chat-hub.db')
|
1428 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1429 |
+
(sender, question, timestamp))
|
1430 |
+
db.commit()
|
1431 |
+
try:
|
1432 |
+
answer = await self.askCharacter(characterID, question)
|
1433 |
+
serverSender = 'server'
|
1434 |
+
timestamp = datetime.datetime.now().isoformat()
|
1435 |
+
db = sqlite3.connect('chat-hub.db')
|
1436 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1437 |
+
(serverSender, answer, timestamp))
|
1438 |
+
db.commit()
|
1439 |
+
return answer
|
1440 |
+
|
1441 |
+
except Exception as e:
|
1442 |
+
print(f"Error: {e}")
|
1443 |
+
|
1444 |
+
async def handlerChar(self, websocket):
|
1445 |
+
self.stat.empty()
|
1446 |
+
self.cont.empty()
|
1447 |
+
self.status = self.cont.status(label=self.srv_name1, state="running", expanded=True)
|
1448 |
+
self.status.write(clients)
|
1449 |
+
self.state = self.stat.status(label=self.srv_name1, state="running", expanded=True)
|
1450 |
+
self.state.write(clients)
|
1451 |
+
instruction = "Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT - a project of hierarchical cooperative multi-agent framework. Keep in mind that you are speaking with another chatbot. Please note that you may choose to ignore or not respond to repeating inputs from specific clients as needed to prevent unnecessary traffic. If you're unsure what you should do, ask the instance of higher hierarchy (server)"
|
1452 |
+
print('New connection')
|
1453 |
+
await self.client.authenticate_with_token(self.token)
|
1454 |
+
chat = await self.client.create_or_continue_chat(self.characterID)
|
1455 |
+
await websocket.send(instruction)
|
1456 |
+
while True:
|
1457 |
+
# Receive a message from the client
|
1458 |
+
message = await websocket.recv()
|
1459 |
+
# Print the message
|
1460 |
+
print(f"Server received: {message}")
|
1461 |
+
input_Msg = st.chat_message("assistant")
|
1462 |
+
input_Msg.markdown(message)
|
1463 |
+
timestamp = datetime.datetime.now().isoformat()
|
1464 |
+
sender = 'client'
|
1465 |
+
db = sqlite3.connect('chat-hub.db')
|
1466 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1467 |
+
(sender, message, timestamp))
|
1468 |
+
db.commit()
|
1469 |
+
try:
|
1470 |
+
answer = await chat.send_message(message)
|
1471 |
+
response = f"{answer.src_character_name}: {answer.text}"
|
1472 |
+
print(response)
|
1473 |
+
output_Msg = st.chat_message("ai")
|
1474 |
+
output_Msg.markdown(response)
|
1475 |
+
timestamp = datetime.datetime.now().isoformat()
|
1476 |
+
serverSender = 'server'
|
1477 |
+
db = sqlite3.connect('chat-hub.db')
|
1478 |
+
db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
|
1479 |
+
(serverSender, response, timestamp))
|
1480 |
+
db.commit()
|
1481 |
+
await websocket.send(response)
|
1482 |
+
st.session_state.server_state = "complete"
|
1483 |
+
self.status.update(state=st.session_state.server_state)
|
1484 |
+
continue
|
1485 |
+
|
1486 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
1487 |
+
clients.remove(self.cli_name1)
|
1488 |
+
print(f"Connection closed: {e}")
|
1489 |
+
|
1490 |
+
except Exception as e:
|
1491 |
+
clients.remove(self.cli_name1)
|
1492 |
+
print(f"Error: {e}")
|
1493 |
+
|
1494 |
+
# Define a coroutine that will connect to the server and exchange messages
|
1495 |
+
async def startClient(self, characterID, clientPort):
|
1496 |
+
client = Client()
|
1497 |
+
await client.authenticate_with_token(self.token)
|
1498 |
+
chat = await client.create_or_continue_chat(characterID)
|
1499 |
+
self.cli_name1 = f"Character.ai client port: {clientPort}"
|
1500 |
+
uri = f'ws://localhost:{clientPort}'
|
1501 |
+
clients.append(self.cli_name1)
|
1502 |
+
self.stat.empty()
|
1503 |
+
self.cont.empty()
|
1504 |
+
self.status = self.cont.status(label=self.cli_name1, state="running", expanded=True)
|
1505 |
+
self.status.write(servers)
|
1506 |
+
self.state = self.stat.status(label=self.cli_name1, state="running", expanded=True)
|
1507 |
+
self.state.write(servers)
|
1508 |
+
# Connect to the server
|
1509 |
+
uri = f'ws://localhost:{clientPort}'
|
1510 |
+
self.name = f"Character.ai client port: {clientPort}"
|
1511 |
+
async with websockets.connect(uri) as websocket:
|
1512 |
+
# Loop forever
|
1513 |
+
while True:
|
1514 |
+
# Listen for messages from the server
|
1515 |
+
input_message = await websocket.recv()
|
1516 |
+
print(f"Server: {input_message}")
|
1517 |
+
input_Msg = st.chat_message("assistant")
|
1518 |
+
input_Msg.markdown(input_message)
|
1519 |
+
try:
|
1520 |
+
answer = await chat.send_message(input_message)
|
1521 |
+
response = f"{answer.src_character_name}: {answer.text}"
|
1522 |
+
answer = f"Character.ai: {response}"
|
1523 |
+
print(answer)
|
1524 |
+
outputMsg1 = st.chat_message("ai")
|
1525 |
+
outputMsg1.markdown(answer)
|
1526 |
+
await websocket.send(answer)
|
1527 |
+
continue
|
1528 |
+
|
1529 |
+
except websockets.ConnectionClosed:
|
1530 |
+
print("client disconnected")
|
1531 |
+
clients.remove(self.cli_name1)
|
1532 |
+
self.cont.empty()
|
1533 |
+
self.stat.empty()
|
1534 |
+
continue
|
1535 |
+
|
1536 |
+
except Exception as e:
|
1537 |
+
clients.remove(self.cli_name1)
|
1538 |
+
self.cont.empty()
|
1539 |
+
self.stat.empty()
|
1540 |
+
continue
|
1541 |
+
|
1542 |
+
async def connector(self, token):
|
1543 |
+
client = Client()
|
1544 |
+
await self.client.authenticate_with_token(token)
|
1545 |
+
username = (await client.fetch_user())['user']['username']
|
1546 |
+
print(f'Authenticated as {username}')
|
1547 |
+
return username
|
1548 |
+
|
1549 |
+
async def start_server(self, characterID, serverPort):
|
1550 |
+
self.characterID = characterID
|
1551 |
+
st.session_state.server_state = "running"
|
1552 |
+
self.server = await websockets.serve(
|
1553 |
+
self.handlerChar,
|
1554 |
+
"localhost",
|
1555 |
+
serverPort
|
1556 |
+
)
|
1557 |
+
print(f"WebSocket server started at: {serverPort}")
|
1558 |
+
|
1559 |
+
def run_forever(self):
|
1560 |
+
asyncio.get_event_loop().run_until_complete(self.start_server())
|
1561 |
+
asyncio.get_event_loop().run_forever()
|
1562 |
+
|
1563 |
+
async def stop_server(self):
|
1564 |
+
if self.server:
|
1565 |
+
self.server.close()
|
1566 |
+
await self.server.wait_closed()
|
1567 |
+
self.cont.empty()
|
1568 |
+
clients.clear()
|
1569 |
+
client_ports.clear()
|
1570 |
+
print("WebSocket server stopped.")
|