Arcypojeb commited on
Commit
6c82d64
1 Parent(s): 02dc189

Delete clientG4F.py

Browse files
Files changed (1) hide show
  1. clientG4F.py +0 -88
clientG4F.py DELETED
@@ -1,88 +0,0 @@
1
- import asyncio
2
- import websockets
3
- import threading
4
- import sqlite3
5
- import g4f
6
- import streamlit as st
7
-
8
- # Define the websocket client class
9
- class WebSocketClient1:
10
- def __init__(self, uri):
11
- # Initialize the uri attribute
12
- self.uri = uri
13
-
14
- async def askQuestion(self, question):
15
- 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."
16
- try:
17
- db = sqlite3.connect('chat-hub.db')
18
- cursor = db.cursor()
19
- cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 30")
20
- messages = cursor.fetchall()
21
- messages.reverse()
22
-
23
- past_user_inputs = []
24
- generated_responses = []
25
-
26
- for message in messages:
27
- if message[1] == 'server':
28
- past_user_inputs.append(message[2])
29
- else:
30
- generated_responses.append(message[2])
31
-
32
- response = await g4f.ChatCompletion.create_async(
33
- model=g4f.models.gpt_4,
34
- provider=g4f.Provider.Bing,
35
- messages=[
36
- {"role": "system", "content": system_instruction},
37
- *[{"role": "user", "content": message} for message in past_user_inputs],
38
- *[{"role": "assistant", "content": message} for message in generated_responses],
39
- {"role": "user", "content": question}
40
- ])
41
-
42
- print(response)
43
- return response
44
-
45
- except Exception as e:
46
- print(e)
47
-
48
- # Define a function that will run the client in a separate thread
49
- def run(self):
50
- # Create a thread object
51
- self.thread = threading.Thread(target=self.run_client)
52
- # Start the thread
53
- self.thread.start()
54
-
55
- # Define a function that will run the client using asyncio
56
- def run_client(self):
57
- # Get the asyncio event loop
58
- loop = asyncio.new_event_loop()
59
- # Set the event loop as the current one
60
- asyncio.set_event_loop(loop)
61
- # Run the client until it is stopped
62
- loop.run_until_complete(self.client())
63
-
64
- # Define a coroutine that will connect to the server and exchange messages
65
- async def startClient(self):
66
- # Connect to the server
67
- async with websockets.connect(self.uri) as websocket:
68
- # Loop forever
69
- while True:
70
- # Listen for messages from the server
71
- input_message = await websocket.recv()
72
- print(f"Server: {input_message}")
73
- input_Msg = st.chat_message("assistant")
74
- input_Msg.markdown(input_message)
75
- try:
76
- response = await self.askQuestion(input_message)
77
- res1 = f"Client: {response}"
78
- output_Msg = st.chat_message("ai")
79
- output_Msg.markdown(res1)
80
- await websocket.send(res1)
81
-
82
- except websockets.ConnectionClosed:
83
- print("client disconnected")
84
- continue
85
-
86
- except Exception as e:
87
- print(f"Error: {e}")
88
- continue