Arcypojeb commited on
Commit
03a6074
1 Parent(s): ddad303

Update clientCharacter.py

Browse files
Files changed (1) hide show
  1. clientCharacter.py +58 -20
clientCharacter.py CHANGED
@@ -1,20 +1,54 @@
 
1
  import asyncio
2
  import websockets
3
  import threading
4
  import sqlite3
 
5
  import streamlit as st
6
- from PyCharacterAI import Client
 
7
 
8
  # Define the websocket client class
9
- class WebSocketClient2:
10
  def __init__(self, clientPort):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- self.uri = f'ws://localhost:{clientPort}'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- if "tokenChar" not in st.session_state:
15
- st.session_state.tokenChar = ""
16
- if "character_ID" not in st.session_state:
17
- st.session_state.character_ID = ""
 
18
 
19
  # Define a function that will run the client in a separate thread
20
  def run(self):
@@ -32,31 +66,35 @@ class WebSocketClient2:
32
  # Run the client until it is stopped
33
  loop.run_until_complete(self.client())
34
 
 
 
 
 
 
 
 
 
35
  # Define a coroutine that will connect to the server and exchange messages
36
  async def startClient(self):
37
- status = st.sidebar.status(label="runs", state="complete", expanded=False)
38
- client = Client()
39
- await client.authenticate_with_token(st.session_state.tokenChar)
40
- chat = await client.create_or_continue_chat(st.session_state.character_ID)
41
  # Connect to the server
42
  async with websockets.connect(self.uri) as websocket:
43
  # Loop forever
44
  while True:
45
- status.update(label="runs", state="running", expanded=True)
46
  # Listen for messages from the server
47
  input_message = await websocket.recv()
48
  print(f"Server: {input_message}")
49
  input_Msg = st.chat_message("assistant")
50
  input_Msg.markdown(input_message)
51
  try:
52
- answer = await chat.send_message(input_message)
53
- response = f"{answer.src_character_name}: {answer.text}"
54
- print(response)
55
- outputMsg1 = st.chat_message("ai")
56
- outputMsg1.markdown(response)
57
- await websocket.send(response)
58
- status.update(label="runs", state="complete", expanded=True)
59
- continue
60
 
61
  except websockets.ConnectionClosed:
62
  print("client disconnected")
 
1
+ import json
2
  import asyncio
3
  import websockets
4
  import threading
5
  import sqlite3
6
+ import requests
7
  import streamlit as st
8
+
9
+ client_ports = []
10
 
11
  # Define the websocket client class
12
+ class WebSocketClient6:
13
  def __init__(self, clientPort):
14
+ # Initialize the uri attribute
15
+ self.uri = f'ws://localhost:{clientPort}'
16
+ self.name = f"Chaindesk client port: {clientPort}"
17
+ self.status = st.sidebar.status(label=self.name, state="complete", expanded=False)
18
+ st.session_state.clientPort = clientPort
19
+
20
+ if "client_ports" not in st.session_state:
21
+ st.session_state['client_ports'] = ""
22
+
23
+ async def askChaindesk(self, question):
24
+
25
+ if "agentID" not in st.session_state:
26
+ st.session_state.agentID = ""
27
+
28
+ id = st.session_state.agentID
29
 
30
+ url = f"https://api.chaindesk.ai/agents/{id}/query"
31
+
32
+ payload = {
33
+ "query": question
34
+ }
35
+
36
+ headers = {
37
+ "Authorization": "Bearer fe77e704-bc5a-4171-90f2-9d4b0d4ac942",
38
+ "Content-Type": "application/json"
39
+ }
40
+ try:
41
+ response = requests.request("POST", url, json=payload, headers=headers)
42
+ print(response.text)
43
+ response_text = response.text
44
+ responseJson = json.loads(response_text)
45
+ answer = responseJson["answer"]
46
 
47
+ print(response.text)
48
+ return answer
49
+
50
+ except Exception as e:
51
+ print(e)
52
 
53
  # Define a function that will run the client in a separate thread
54
  def run(self):
 
66
  # Run the client until it is stopped
67
  loop.run_until_complete(self.client())
68
 
69
+ # Stop the WebSocket client
70
+ async def stop_client():
71
+ global ws
72
+ # Close the connection with the server
73
+ await ws.close()
74
+ client_ports.pop()
75
+ print("Stopping WebSocket client...")
76
+
77
  # Define a coroutine that will connect to the server and exchange messages
78
  async def startClient(self):
79
+
80
+ status = self.status
 
 
81
  # Connect to the server
82
  async with websockets.connect(self.uri) as websocket:
83
  # Loop forever
84
  while True:
85
+ status.update(label=self.name, state="running", expanded=True)
86
  # Listen for messages from the server
87
  input_message = await websocket.recv()
88
  print(f"Server: {input_message}")
89
  input_Msg = st.chat_message("assistant")
90
  input_Msg.markdown(input_message)
91
  try:
92
+ response = await self.askChaindesk(input_message)
93
+ res1 = f"Client: {response}"
94
+ output_Msg = st.chat_message("ai")
95
+ output_Msg.markdown(res1)
96
+ await websocket.send(res1)
97
+ status.update(label=self.name, state="complete", expanded=True)
 
 
98
 
99
  except websockets.ConnectionClosed:
100
  print("client disconnected")