Spaces:
Sleeping
Sleeping
Upload agent.py
Browse files- pages/agent.py +124 -0
pages/agent.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import websockets
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import fireworks.client
|
| 5 |
+
from langchain.agents import load_tools
|
| 6 |
+
from langchain.agents import initialize_agent
|
| 7 |
+
from langchain.agents import AgentType
|
| 8 |
+
from langchain.llms.fireworks import Fireworks
|
| 9 |
+
from langchain.chat_models.fireworks import ChatFireworks
|
| 10 |
+
|
| 11 |
+
inputs = []
|
| 12 |
+
outputs = []
|
| 13 |
+
used_ports = []
|
| 14 |
+
server_ports = []
|
| 15 |
+
client_ports = []
|
| 16 |
+
|
| 17 |
+
GOOGLE_CSE_ID = "f3882ab3b67cc4923"
|
| 18 |
+
GOOGLE_API_KEY = "AIzaSyBNvtKE35EAeYO-ECQlQoZO01RSHWhfIws"
|
| 19 |
+
FIREWORKS_API_KEY = "WZGOkHQbZULIzA6u83kyLGBKPigs1HmK9Ec8DEKmGOtu45zx"
|
| 20 |
+
FIREWORKS_API_KEY1 = "WZGOkHQbZULIzA6u83kyLGBKPigs1HmK9Ec8DEKmGOtu45zx"
|
| 21 |
+
|
| 22 |
+
def get_response(input):
|
| 23 |
+
os.environ["GOOGLE_CSE_ID"] = GOOGLE_CSE_ID
|
| 24 |
+
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
| 25 |
+
os.environ["FIREWORKS_API_KEY"] = FIREWORKS_API_KEY
|
| 26 |
+
|
| 27 |
+
llm = Fireworks(model="accounts/fireworks/models/llama-v2-13b-chat", model_kwargs={"temperature":0, "max_tokens":4000, "top_p":1.0})
|
| 28 |
+
|
| 29 |
+
tools = load_tools(["google-search", "llm-math"], llm=llm)
|
| 30 |
+
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True, handle_parsing_errors=True)
|
| 31 |
+
|
| 32 |
+
response = agent({"input": input})
|
| 33 |
+
return response["output"], response["intermediate_steps"]
|
| 34 |
+
|
| 35 |
+
async def handleWebSocket(ws):
|
| 36 |
+
print('New connection')
|
| 37 |
+
await ws.send('Hello! You are now entering a chat room for AI agents working as instances of NeuralGPT. Keep in mind that you are speaking with another chatbot')
|
| 38 |
+
while True:
|
| 39 |
+
message = await ws.recv()
|
| 40 |
+
print(f'Received message: {message}')
|
| 41 |
+
try:
|
| 42 |
+
answer = await get_response(message) # Use the message directly
|
| 43 |
+
await ws.send(answer)
|
| 44 |
+
|
| 45 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
| 46 |
+
print(f"Connection closed: {e}")
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error: {e}")
|
| 51 |
+
|
| 52 |
+
# Start the WebSocket server
|
| 53 |
+
async def start_websockets(websocketPort):
|
| 54 |
+
global server
|
| 55 |
+
server = await(websockets.serve(handleWebSocket, 'localhost', websocketPort))
|
| 56 |
+
server_ports.append(websocketPort)
|
| 57 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
| 58 |
+
return "Used ports:\n" + '\n'.join(map(str, server_ports))
|
| 59 |
+
await asyncio.Future()
|
| 60 |
+
|
| 61 |
+
async def start_client(clientPort):
|
| 62 |
+
uri = f'ws://localhost:{clientPort}'
|
| 63 |
+
client_ports.append(clientPort)
|
| 64 |
+
async with websockets.connect(uri) as ws:
|
| 65 |
+
while True:
|
| 66 |
+
# Listen for messages from the server
|
| 67 |
+
input_message = await ws.recv()
|
| 68 |
+
output_message = await get_response(input_message)
|
| 69 |
+
await ws.send(output_message)
|
| 70 |
+
|
| 71 |
+
# Stop the WebSocket server
|
| 72 |
+
async def stop_websockets():
|
| 73 |
+
global server
|
| 74 |
+
if server:
|
| 75 |
+
# Close all connections gracefully
|
| 76 |
+
server.close()
|
| 77 |
+
# Wait for the server to close
|
| 78 |
+
await server.wait_closed()
|
| 79 |
+
print("Stopping WebSocket server...")
|
| 80 |
+
else:
|
| 81 |
+
print("WebSocket server is not running.")
|
| 82 |
+
|
| 83 |
+
# Stop the WebSocket client
|
| 84 |
+
async def stop_client():
|
| 85 |
+
global ws
|
| 86 |
+
# Close the connection with the server
|
| 87 |
+
ws.close()
|
| 88 |
+
print("Stopping WebSocket client...")
|
| 89 |
+
|
| 90 |
+
# error capturing in integration as a component
|
| 91 |
+
with gr.Blocks() as demo:
|
| 92 |
+
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
| 93 |
+
with gr.TabItem("Agents GPT", elem_id="agents_gpt", id=0):
|
| 94 |
+
with gr.Column(scale=1, min_width=600):
|
| 95 |
+
with gr.Row():
|
| 96 |
+
userInput = gr.Textbox(label="User Input")
|
| 97 |
+
with gr.Row():
|
| 98 |
+
ask_Qestion = gr.Button("Ask question")
|
| 99 |
+
with gr.Row():
|
| 100 |
+
goalOutput = gr.Textbox(lines=15, max_lines=130, label="Goal output:")
|
| 101 |
+
steps = gr.Json(label="Intermediate Steps")
|
| 102 |
+
|
| 103 |
+
with gr.TabItem("Agents GPT", elem_id="agents_gpt", id=0):
|
| 104 |
+
with gr.Column(scale=1, min_width=600):
|
| 105 |
+
with gr.Row():
|
| 106 |
+
websocketPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
|
| 107 |
+
startWebsockets = gr.Button("Start WebSocket Server")
|
| 108 |
+
stopWebsockets = gr.Button("Stop WebSocket Server")
|
| 109 |
+
with gr.Row():
|
| 110 |
+
port = gr.Textbox()
|
| 111 |
+
with gr.Row():
|
| 112 |
+
clientPort = gr.Slider(minimum=1000, maximum=9999, label="Websocket server port", interactive=True, randomize=False)
|
| 113 |
+
startClient = gr.Button("Start WebSocket client")
|
| 114 |
+
stopClient = gr.Button("Stop WebSocket client")
|
| 115 |
+
with gr.Row():
|
| 116 |
+
PortInUse = gr.Textbox()
|
| 117 |
+
ask_Qestion.click(get_response, inputs=userInput, outputs=[goalOutput, steps])
|
| 118 |
+
startWebsockets.click(start_websockets, inputs=websocketPort, outputs=port)
|
| 119 |
+
startClient.click(start_client, inputs=clientPort, outputs=None)
|
| 120 |
+
stopWebsockets.click(stop_websockets, inputs=None, outputs=port)
|
| 121 |
+
stopClient.click(stop_client, inputs=None, outputs=PortInUse)
|
| 122 |
+
|
| 123 |
+
demo.queue()
|
| 124 |
+
demo.launch(share=True, server_port=1112)
|