Arcypojeb commited on
Commit
8d7e35f
1 Parent(s): 21ae628

Update ServChar.py

Browse files
Files changed (1) hide show
  1. ServChar.py +44 -13
ServChar.py CHANGED
@@ -6,28 +6,59 @@ import streamlit as st
6
  from PyCharacterAI import Client
7
 
8
  class WebSocketServer2:
9
- def __init__(self, host, port):
10
- self.host = host
11
- self.port = port
12
- self.server = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  async def handler(self, websocket):
 
15
  client = Client()
16
  status = st.sidebar.status(label="runs", state="complete", expanded=False)
17
- if "tokenChar" not in st.session_state:
18
- st.session_state.tokenChar = ""
19
- if "character_ID" not in st.session_state:
20
- st.session_state.character_ID = ""
21
 
22
- await client.authenticate_with_token(st.session_state.tokenChar)
23
- char_id = st.session_state.character_ID
24
- chat = await client.create_or_continue_chat(char_id)
25
  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)"
26
  print('New connection')
27
  await websocket.send(instruction)
 
28
  while True:
29
  status.update(label="runs", state="running", expanded=True)
30
  # Receive a message from the client
 
31
  message = await websocket.recv()
32
  # Print the message
33
  print(f"Server received: {message}")
@@ -58,11 +89,11 @@ class WebSocketServer2:
58
  except Exception as e:
59
  print(f"Error: {e}")
60
 
61
- async def start_server(self):
62
  self.server = await websockets.serve(
63
  self.handler,
64
  self.host,
65
- self.port
66
  )
67
  print(f"WebSocket server started at ws://{self.host}:{self.port}")
68
 
 
6
  from PyCharacterAI import Client
7
 
8
  class WebSocketServer2:
9
+ def __init__(self, token, characterID):
10
+
11
+ if "tokenChar" not in st.session_state:
12
+ st.session_state.tokenChar = ""
13
+ if "character_ID" not in st.session_state:
14
+ st.session_state.character_ID = ""
15
+
16
+ st.session_state.tokenChar = token
17
+ st.session_state.character_ID = characterID
18
+
19
+ async def askCharacter(self, question):
20
+ self.client = Client()
21
+ db = sqlite3.connect('chat-hub.db')
22
+ client = Client()
23
+ input_Msg = st.chat_message("human")
24
+ input_Msg.markdown(question)
25
+ await client.authenticate_with_token(st.session_state.tokenChar)
26
+ timestamp = datetime.datetime.now().isoformat()
27
+ sender = 'client'
28
+ db = sqlite3.connect('chat-hub.db')
29
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
30
+ (sender, question, timestamp))
31
+ db.commit()
32
+ try:
33
+ chat = await client.create_or_continue_chat(st.session_state.character_ID)
34
+ answer = await chat.send_message(question)
35
+ response = f"{answer.src_character_name}: {answer.text}"
36
+ output_Msg = st.chat_message("ai")
37
+ output_Msg.markdown(response)
38
+ timestamp = datetime.datetime.now().isoformat()
39
+ serverSender = 'server'
40
+ db = sqlite3.connect('chat-hub.db')
41
+ db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)',
42
+ (serverSender, response, timestamp))
43
+ db.commit()
44
+ return response
45
+ except Exception as e:
46
+ print(f"Error: {e}")
47
 
48
  async def handler(self, websocket):
49
+
50
  client = Client()
51
  status = st.sidebar.status(label="runs", state="complete", expanded=False)
 
 
 
 
52
 
53
+ await client.authenticate_with_token(st.session_state.tokenChar)
 
 
54
  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)"
55
  print('New connection')
56
  await websocket.send(instruction)
57
+
58
  while True:
59
  status.update(label="runs", state="running", expanded=True)
60
  # Receive a message from the client
61
+ chat = await client.create_or_continue_chat(st.session_state.character_ID)
62
  message = await websocket.recv()
63
  # Print the message
64
  print(f"Server received: {message}")
 
89
  except Exception as e:
90
  print(f"Error: {e}")
91
 
92
+ async def start_server(self, serverPort):
93
  self.server = await websockets.serve(
94
  self.handler,
95
  self.host,
96
+ serverPort
97
  )
98
  print(f"WebSocket server started at ws://{self.host}:{self.port}")
99