-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (45 loc) · 1.96 KB
/
main.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
# main.py
import logging
from flask import Flask, request, jsonify
from dotenv import load_dotenv
from gpt_client import interpret_query_with_gpt
from k8s_client import handle_k8s_query
from utils import ensure_string
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(levelname)s - %(message)s',
handlers=[logging.FileHandler("agent.log"), logging.StreamHandler()]
)
app = Flask(__name__)
@app.route('/query', methods=['POST'])
def create_query():
"""
Handles POST requests to the /query endpoint.
Extracts the user's query, interprets it, and returns the output of the kubectl command.
"""
try:
# Extract the query from the request data
request_data = request.json
if not request_data or 'query' not in request_data:
logging.warning("Invalid request: 'query' field is missing.")
return jsonify({"error": "Invalid request. 'query' field is required."}), 400
query = request_data.get('query').strip()
if not query:
logging.warning("Empty query received.")
return jsonify({"error": "Query cannot be empty."}), 400
logging.info(f"Received query: {query}")
# Get structured response from GPT with kubectl command
structured_query = interpret_query_with_gpt(query)
if not structured_query or "error" in structured_query:
return jsonify(structured_query), 500
# Execute kubectl command based on GPT's response, passing `query` as the second argument
answer = handle_k8s_query(structured_query, query)
logging.info(f"Generated answer: {answer}")
return jsonify({"query": query, "answer": ensure_string(answer)})
except Exception as e:
logging.error(f"Error processing query: {str(e)}", exc_info=True)
return jsonify({"error": "Internal server error."}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)