-
Notifications
You must be signed in to change notification settings - Fork 10
/
FireworksAgentsGPT.py
112 lines (90 loc) · 3.86 KB
/
FireworksAgentsGPT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import gradio as gr
import os
import sqlite3
import websockets
import json
import asyncio
import time
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms.fireworks import Fireworks
os.chdir("E:/repos/")
async def connect_websocket():
async with websockets.connect('ws://localhost:5000') as websocket:
while True:
message = await websocket.recv()
# Process the received message
response = process_message(message)
await websocket.send(response)
def process_message(message):
# Perform message processing logic here
return 'Processed response'
def run_agent1(input, google_cse_id, google_api_key, fireworks_api_key):
os.environ["GOOGLE_CSE_ID"] = google_cse_id
os.environ["GOOGLE_API_KEY"] = google_api_key
os.environ["FIREWORKS_API_KEY"] = fireworks_api_key
llm = Fireworks(model="accounts/fireworks/models/llama-v2-13b")
tools = load_tools(["google-search", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)
response = agent({"input": input})
return response["output"], response["intermediate_steps"]
# Create a connection to the database
conn = sqlite3.connect('E:/repos/chatcenter/chat-hub.sql')
# Create a cursor object to execute SQL commands
cursor = conn.cursor()
# Create a table to store user inputs and chatbot responses
try:
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='chat_history';")
result = cursor.fetchone()
if not result:
cursor.execute('''CREATE TABLE chat_history
(id INTEGER PRIMARY KEY AUTOINCREMENT,
user_input TEXT,
chatbot_response TEXT,
chatbot_name TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
except Exception as e:
st.error(f"Error: {e}")
# Define start_client function with a variable port
async def start_client(websocketPort):
async with websockets.connect(f'ws://localhost:{websocketPort}') as ws:
print("Connected to server at:", websocketPort)
used_ports.append(websocketPort)
print(f"Starting WebSocket server on port {websocketPort}...")
return "Used ports:\n" + '\n'.join(map(str, used_ports))
while True:
server_message = await ws.recv()
print(server_message)
client_message = await askQuestion(server_message)
print(client_message)
await ws.send(client_message)
input_text = input("Your message: ")
message = f'{{"text": "userB: {input_text}"}}'
await ws.send(message)
def on_submit(inputs):
goal_output, intermediate_steps = get_response(*inputs)
iface.set_output([goal_output, intermediate_steps])
send_response_to_server(goal_output),
iface = gr.Interface(
fn=get_response,
inputs=[
gr.Textbox(label="Goal definition:"),
gr.Textbox(label="Google CSE - ID:", type="text"),
gr.Textbox(label="Google API KEY:", type="password"),
gr.Textbox(label="Fireworks API KEY:", placeholder="Fireworks API", type="password")
],
outputs=[
gr.Textbox(label="Goal output:"),
gr.Json(label="Intermediate Steps")
],
title="GPT Agents Demo",
description="Demo application of gpt-based agents including two tools (google-search and llm-math). The result and intermediate steps are included.",
on_submit=on_submit
)
# Commit the changes to the database
conn.commit()
# Close the database connection
conn.close()
# error capturing in integration as a component
error_message = ""