Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,162 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
|
|
2 |
from log import log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import uvicorn
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
# FastAPI app'i başlat
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
@app.get("/")
|
13 |
-
def
|
14 |
return {"status": "ok"}
|
15 |
|
16 |
-
|
17 |
-
app.
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
if __name__ == "__main__":
|
23 |
-
log("🌐 Starting Flare Service...")
|
24 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from fastapi import FastAPI, Request, Header
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import threading
|
4 |
from log import log
|
5 |
+
from service_config import ServiceConfig
|
6 |
+
from session import SessionStore
|
7 |
+
from prompt_engine import PromptEngine
|
8 |
+
from llm_connector import LLMConnector
|
9 |
+
from api_connector import APIConnector
|
10 |
+
from validation_engine import ValidationEngine
|
11 |
+
import traceback
|
12 |
import uvicorn
|
13 |
|
14 |
+
# Initialize core components
|
15 |
+
service_config = ServiceConfig()
|
16 |
+
service_config.load()
|
17 |
+
session_store = SessionStore()
|
18 |
+
prompt_engine = PromptEngine(service_config)
|
19 |
+
llm_connector = LLMConnector(service_config)
|
20 |
+
api_connector = APIConnector(service_config)
|
21 |
+
validation_engine = ValidationEngine()
|
22 |
|
|
|
23 |
app = FastAPI()
|
24 |
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
@app.get("/")
|
31 |
+
def health_check():
|
32 |
return {"status": "ok"}
|
33 |
|
34 |
+
|
35 |
+
@app.post("/start_chat")
|
36 |
+
def start_chat(request: Request):
|
37 |
+
project_name = request.query_params.get("project_name")
|
38 |
+
if not project_name:
|
39 |
+
return {"error": "Missing project_name parameter."}
|
40 |
+
|
41 |
+
session = session_store.create_session(project_name)
|
42 |
+
return {"session_id": session.session_id}
|
43 |
+
|
44 |
+
|
45 |
+
@app.post("/chat")
|
46 |
+
async def chat(request: Request, x_session_id: str = Header(None)):
|
47 |
+
if not x_session_id:
|
48 |
+
return {"error": "Missing X-Session-ID header."}
|
49 |
+
|
50 |
+
session = session_store.get_session(x_session_id)
|
51 |
+
if not session:
|
52 |
+
return {"error": "Invalid or expired session."}
|
53 |
+
|
54 |
+
try:
|
55 |
+
body = await request.json()
|
56 |
+
user_input = body.get("user_input", "").strip()
|
57 |
+
if not user_input:
|
58 |
+
return {"error": "Empty user input."}
|
59 |
+
|
60 |
+
session.chat_history.append({"role": "user", "content": user_input})
|
61 |
+
project_name = session.project_name
|
62 |
+
|
63 |
+
if session.state == "intent_detection":
|
64 |
+
prompt = prompt_engine.build_intent_prompt(project_name)
|
65 |
+
llm_response = llm_connector.call_spark(project_name, prompt, session.chat_history)
|
66 |
+
if llm_response is None:
|
67 |
+
return {"error": "Failed to get intent detection result."}
|
68 |
+
|
69 |
+
intent = llm_response.get("intent")
|
70 |
+
params = llm_response.get("params", {})
|
71 |
+
missing = llm_response.get("missing", [])
|
72 |
+
|
73 |
+
session.last_intent = intent
|
74 |
+
session.variables.update(params)
|
75 |
+
session.awaiting_parameters = missing
|
76 |
+
|
77 |
+
if missing:
|
78 |
+
session.state = "parameter_extraction"
|
79 |
+
return {"response": f"Please provide: {', '.join(missing)}"}
|
80 |
+
|
81 |
+
session.state = "validation"
|
82 |
+
|
83 |
+
if session.state == "parameter_extraction":
|
84 |
+
prompt = prompt_engine.build_parameter_prompt(project_name, session.last_intent, session.awaiting_parameters)
|
85 |
+
llm_response = llm_connector.call_spark(project_name, prompt, session.chat_history)
|
86 |
+
if llm_response is None:
|
87 |
+
return {"error": "Failed to extract parameters."}
|
88 |
+
|
89 |
+
params = llm_response.get("params", {})
|
90 |
+
missing = llm_response.get("missing", [])
|
91 |
+
|
92 |
+
session.variables.update(params)
|
93 |
+
session.awaiting_parameters = missing
|
94 |
+
|
95 |
+
if missing:
|
96 |
+
return {"response": f"Please provide: {', '.join(missing)}"}
|
97 |
+
|
98 |
+
session.state = "validation"
|
99 |
+
|
100 |
+
if session.state == "validation":
|
101 |
+
intent_def = next((i for i in service_config.get_project_intents(project_name) if i["name"] == session.last_intent), None)
|
102 |
+
if not intent_def:
|
103 |
+
return {"error": f"Intent definition not found: {session.last_intent}"}
|
104 |
+
|
105 |
+
is_valid, errors = validation_engine.validate_parameters(intent_def, session.variables)
|
106 |
+
if not is_valid:
|
107 |
+
return {"response": " ".join(errors)}
|
108 |
+
|
109 |
+
session.state = "api_call"
|
110 |
+
|
111 |
+
if session.state == "api_call":
|
112 |
+
intent_def = next((i for i in service_config.get_project_intents(project_name) if i["name"] == session.last_intent), None)
|
113 |
+
api_response = api_connector.call_api(intent_def, session)
|
114 |
+
if "fallback" in api_response:
|
115 |
+
return {"response": api_response["fallback"]}
|
116 |
+
|
117 |
+
session.state = "humanization"
|
118 |
+
session.variables["api_result"] = api_response
|
119 |
+
|
120 |
+
if session.state == "humanization":
|
121 |
+
prompt = prompt_engine.build_humanization_prompt(project_name, session.last_intent)
|
122 |
+
chat_history = [{"role": "system", "content": str(session.variables["api_result"])}]
|
123 |
+
humanized_response = llm_connector.call_spark(project_name, prompt, chat_history)
|
124 |
+
if humanized_response is None:
|
125 |
+
return {"error": "Failed to humanize response."}
|
126 |
+
|
127 |
+
session.chat_history.append({"role": "assistant", "content": humanized_response.get("answer")})
|
128 |
+
session.state = "intent_detection" # reset state
|
129 |
+
session.last_intent = None
|
130 |
+
session.variables = {}
|
131 |
+
session.awaiting_parameters = []
|
132 |
+
|
133 |
+
return {"response": humanized_response.get("answer")}
|
134 |
+
|
135 |
+
except Exception as e:
|
136 |
+
log(f"❌ Error in chat: {e}")
|
137 |
+
traceback.print_exc()
|
138 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
139 |
+
|
140 |
+
|
141 |
+
@app.post("/reload_config")
|
142 |
+
def reload_config():
|
143 |
+
def background_reload():
|
144 |
+
try:
|
145 |
+
service_config.load()
|
146 |
+
log("✅ Service config reloaded successfully.")
|
147 |
+
except Exception as e:
|
148 |
+
log(f"❌ Error reloading config: {e}")
|
149 |
+
|
150 |
+
threading.Thread(target=background_reload, daemon=True).start()
|
151 |
+
return {"status": "accepted", "message": "Config reload started in background."}
|
152 |
+
|
153 |
+
|
154 |
+
@app.post("/run_tests")
|
155 |
+
def run_tests():
|
156 |
+
log("🚦 /run_tests endpoint called. (Test runner needs to be implemented.)")
|
157 |
+
return {"status": "not_implemented", "message": "Test runner is not yet implemented."}
|
158 |
+
|
159 |
|
160 |
if __name__ == "__main__":
|
161 |
+
log("🌐 Starting Flare Intent Service...")
|
162 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|