-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.py
67 lines (58 loc) · 2.11 KB
/
index.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
from flask import Flask, request, Response, stream_with_context, jsonify
from flask_cors import CORS
from dotenv import load_dotenv
from agent.initialize_agent import initialize_agent
from agent.run_agent import run_agent
from db.setup import setup
from db.tokens import get_tokens
from db.nfts import get_nfts
load_dotenv()
app = Flask(__name__)
CORS(app)
# Setup SQLite tables
setup()
# Initialize the agent
agent_executor = initialize_agent()
app.agent_executor = agent_executor
# Interact with the agent
@app.route("/api/chat", methods=['POST'])
def chat():
try:
data = request.get_json()
# Parse the user input from the request
input = data['input']
# Use the conversation_id passed in the request for conversation memory
config = {"configurable": {"thread_id": data['conversation_id']}}
return Response(
stream_with_context(run_agent(input, app.agent_executor, config)),
mimetype='text/event-stream',
headers={
'Cache-Control': 'no-cache',
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'X-Accel-Buffering': 'no'
}
)
except Exception as e:
app.logger.error(f"Unexpected error in chat endpoint: {str(e)}")
return jsonify({'error': 'An unexpected error occurred'}), 500
# Retrieve a list of tokens the agent has deployed
@app.route("/tokens", methods=['GET'])
def tokens():
try:
tokens = get_tokens()
return jsonify({'tokens': tokens}), 200
except Exception as e:
app.logger.error(f"Unexpected error in tokens endpoint: {str(e)}")
return jsonify({'error': 'An unexpected error occurred'}), 500
# Retrieve a list of tokens the agent has deployed
@app.route("/nfts", methods=['GET'])
def nfts():
try:
nfts = get_nfts()
return jsonify({'nfts': nfts}), 200
except Exception as e:
app.logger.error(f"Unexpected error in nfts endpoint: {str(e)}")
return jsonify({'error': 'An unexpected error occurred'}), 500
if __name__ == "__main__":
app.run()