Arcypojeb commited on
Commit
02dc189
·
verified ·
1 Parent(s): 81e7fe4

Delete clientFireworks.py

Browse files
Files changed (1) hide show
  1. clientFireworks.py +0 -98
clientFireworks.py DELETED
@@ -1,98 +0,0 @@
1
- import asyncio
2
- import websockets
3
- import threading
4
- import sqlite3
5
- import fireworks.client
6
- import streamlit as st
7
-
8
- # Define the websocket client class
9
- class WebSocketClient:
10
- def __init__(self, uri):
11
- # Initialize the uri attribute
12
- self.uri = uri
13
-
14
- async def chatCompletion(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
- # Connect to the database and get the last 30 messages
18
- db = sqlite3.connect('chat-hub.db')
19
- cursor = db.cursor()
20
- cursor.execute("SELECT * FROM messages ORDER BY timestamp DESC LIMIT 10")
21
- messages = cursor.fetchall()
22
- messages.reverse()
23
-
24
- # Extract user inputs and generated responses from the messages
25
- past_user_inputs = []
26
- generated_responses = []
27
-
28
- for message in messages:
29
- if message[1] == 'server':
30
- past_user_inputs.append(message[2])
31
- else:
32
- generated_responses.append(message[2])
33
-
34
- # Prepare data to send to the chatgpt-api.shn.hk
35
- response = fireworks.client.ChatCompletion.create(
36
- model="accounts/fireworks/models/llama-v2-7b-chat",
37
- messages=[
38
- {"role": "system", "content": system_instruction},
39
- *[{"role": "user", "content": message} for message in past_user_inputs],
40
- *[{"role": "assistant", "content": message} for message in generated_responses],
41
- {"role": "user", "content": question}
42
- ],
43
- stream=False,
44
- n=1,
45
- max_tokens=2500,
46
- temperature=0.5,
47
- top_p=0.7,
48
- )
49
-
50
- answer = response.choices[0].message.content
51
- print(answer)
52
- return str(answer)
53
-
54
- except Exception as error:
55
- print("Error while fetching or processing the response:", error)
56
- return "Error: Unable to generate a response."
57
-
58
- # Define a function that will run the client in a separate thread
59
- def run(self):
60
- # Create a thread object
61
- self.thread = threading.Thread(target=self.run_client)
62
- # Start the thread
63
- self.thread.start()
64
-
65
- # Define a function that will run the client using asyncio
66
- def run_client(self):
67
- # Get the asyncio event loop
68
- loop = asyncio.new_event_loop()
69
- # Set the event loop as the current one
70
- asyncio.set_event_loop(loop)
71
- # Run the client until it is stopped
72
- loop.run_until_complete(self.client())
73
-
74
- # Define a coroutine that will connect to the server and exchange messages
75
- async def startClient(self):
76
- # Connect to the server
77
- async with websockets.connect(self.uri) as websocket:
78
- # Loop forever
79
- while True:
80
- # Listen for messages from the server
81
- input_message = await websocket.recv()
82
- print(f"Server: {input_message}")
83
- input_Msg = st.chat_message("assistant")
84
- input_Msg.markdown(input_message)
85
- try:
86
- response = await self.chatCompletion(input_message)
87
- res1 = f"Client: {response}"
88
- output_Msg = st.chat_message("ai")
89
- output_Msg.markdown(res1)
90
- await websocket.send(res1)
91
-
92
- except websockets.ConnectionClosed:
93
- print("client disconnected")
94
- continue
95
-
96
- except Exception as e:
97
- print(f"Error: {e}")
98
- continue