Spaces:
Sleeping
Sleeping
Create AgentGPT.py
Browse files- AgentGPT.py +107 -0
AgentGPT.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import asyncio
|
3 |
+
import websockets
|
4 |
+
import sqlite3
|
5 |
+
import datetime
|
6 |
+
import fireworks.client
|
7 |
+
import streamlit as st
|
8 |
+
import threading
|
9 |
+
import conteneiro
|
10 |
+
from langchain.agents import load_tools
|
11 |
+
from langchain.agents import initialize_agent
|
12 |
+
from langchain.agents import AgentType
|
13 |
+
from langchain.llms import HuggingFaceHub
|
14 |
+
from langchain.llms.fireworks import Fireworks
|
15 |
+
from langchain.chat_models.fireworks import ChatFireworks
|
16 |
+
|
17 |
+
client_ports = []
|
18 |
+
|
19 |
+
# Define the websocket client class
|
20 |
+
class AgentsGPT:
|
21 |
+
def __init__(self):
|
22 |
+
|
23 |
+
self.status = st.sidebar.status(label="AgentsGPT", state="complete", expanded=False)
|
24 |
+
|
25 |
+
async def get_response(self, question):
|
26 |
+
|
27 |
+
os.environ["GOOGLE_CSE_ID"] = GOOGLE_CSE_ID
|
28 |
+
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
29 |
+
os.environ["FIREWORKS_API_KEY"] = FIREWORKS_API_KEY
|
30 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACEHUB_API_TOKEN
|
31 |
+
|
32 |
+
llm = Fireworks(model="accounts/fireworks/models/llama-v2-70b-chat", model_kwargs={"temperature":0, "max_tokens":1500, "top_p":1.0}, streaming=True)
|
33 |
+
tools = load_tools(["google-search", "llm-math"], llm=llm)
|
34 |
+
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)
|
35 |
+
|
36 |
+
response = agent({"input": question})
|
37 |
+
output = response["output"]
|
38 |
+
steps = response["intermediate_steps"]
|
39 |
+
serverResponse = f"AgentsGPT: {output}"
|
40 |
+
responseSteps = f"intermediate steps: {steps}"
|
41 |
+
answer = f"Main output: {output}. Intermediate steps: {steps}"
|
42 |
+
print(serverResponse)
|
43 |
+
print(responseSteps)
|
44 |
+
output_Msg = st.chat_message("ai")
|
45 |
+
output_Msg.markdown(serverResponse)
|
46 |
+
output_steps = st.chat_message("assistant")
|
47 |
+
output_steps.markdown(responseSteps)
|
48 |
+
|
49 |
+
return answer
|
50 |
+
|
51 |
+
|
52 |
+
# Define a function that will run the client in a separate thread
|
53 |
+
def run(self):
|
54 |
+
# Create a thread object
|
55 |
+
self.thread = threading.Thread(target=self.run_client)
|
56 |
+
# Start the thread
|
57 |
+
self.thread.start()
|
58 |
+
|
59 |
+
# Define a function that will run the client using asyncio
|
60 |
+
def run_client(self):
|
61 |
+
# Get the asyncio event loop
|
62 |
+
loop = asyncio.new_event_loop()
|
63 |
+
# Set the event loop as the current one
|
64 |
+
asyncio.set_event_loop(loop)
|
65 |
+
# Run the client until it is stopped
|
66 |
+
loop.run_until_complete(self.client())
|
67 |
+
|
68 |
+
# Stop the WebSocket client
|
69 |
+
async def stop_client():
|
70 |
+
global ws
|
71 |
+
# Close the connection with the server
|
72 |
+
await ws.close()
|
73 |
+
client_ports.pop()
|
74 |
+
print("Stopping WebSocket client...")
|
75 |
+
|
76 |
+
# Define a coroutine that will connect to the server and exchange messages
|
77 |
+
async def startClient(self, clientPort):
|
78 |
+
|
79 |
+
self.uri = f'ws://localhost:{clientPort}'
|
80 |
+
self.name = f"Chaindesk client port: {clientPort}"
|
81 |
+
conteneiro.clients.append(self.name)
|
82 |
+
status = self.status
|
83 |
+
# Connect to the server
|
84 |
+
async with websockets.connect(self.uri) as websocket:
|
85 |
+
# Loop forever
|
86 |
+
while True:
|
87 |
+
status.update(label=self.name, state="running", expanded=True)
|
88 |
+
# Listen for messages from the server
|
89 |
+
input_message = await websocket.recv()
|
90 |
+
print(f"Server: {input_message}")
|
91 |
+
input_Msg = st.chat_message("assistant")
|
92 |
+
input_Msg.markdown(input_message)
|
93 |
+
try:
|
94 |
+
response = await self.get_response(input_message)
|
95 |
+
res1 = f"Client: {response}"
|
96 |
+
output_Msg = st.chat_message("ai")
|
97 |
+
output_Msg.markdown(res1)
|
98 |
+
await websocket.send(res1)
|
99 |
+
status.update(label=self.name, state="complete", expanded=True)
|
100 |
+
|
101 |
+
except websockets.ConnectionClosed:
|
102 |
+
print("client disconnected")
|
103 |
+
continue
|
104 |
+
|
105 |
+
except Exception as e:
|
106 |
+
print(f"Error: {e}")
|
107 |
+
continue
|