Spaces:
Paused
Paused
Update controllers/project_controller.py
Browse files- controllers/project_controller.py +70 -107
controllers/project_controller.py
CHANGED
@@ -1,107 +1,70 @@
|
|
1 |
-
from fastapi import APIRouter, Request
|
2 |
-
from
|
3 |
-
from
|
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 |
-
return {"response": f"Please provide: {', '.join(missing)}"}
|
72 |
-
session.state = "validation"
|
73 |
-
|
74 |
-
if session.state == "validation":
|
75 |
-
intent_def = next((i for i in service_config.get_project_intents(project_name) if i["name"] == session.last_intent), None)
|
76 |
-
if not intent_def:
|
77 |
-
return {"error": f"Intent definition not found: {session.last_intent}"}
|
78 |
-
is_valid, errors = validation_engine.validate_parameters(intent_def, session.variables)
|
79 |
-
if not is_valid:
|
80 |
-
return {"response": " ".join(errors)}
|
81 |
-
session.state = "api_call"
|
82 |
-
|
83 |
-
if session.state == "api_call":
|
84 |
-
intent_def = next((i for i in service_config.get_project_intents(project_name) if i["name"] == session.last_intent), None)
|
85 |
-
api_response = api_connector.call_api(intent_def, session)
|
86 |
-
if "fallback" in api_response:
|
87 |
-
return {"response": api_response["fallback"]}
|
88 |
-
session.state = "humanization"
|
89 |
-
session.variables["api_result"] = api_response
|
90 |
-
|
91 |
-
if session.state == "humanization":
|
92 |
-
prompt = prompt_engine.build_humanization_prompt(project_name, session.last_intent)
|
93 |
-
chat_history = [{"role": "system", "content": str(session.variables["api_result"])}]
|
94 |
-
humanized_response = llm_connector.call_spark(project_name, prompt, chat_history)
|
95 |
-
if humanized_response is None:
|
96 |
-
return {"error": "Failed to humanize response."}
|
97 |
-
session.chat_history.append({"role": "assistant", "content": humanized_response.get("answer")})
|
98 |
-
session.state = "intent_detection"
|
99 |
-
session.last_intent = None
|
100 |
-
session.variables = {}
|
101 |
-
session.awaiting_parameters = []
|
102 |
-
return {"response": humanized_response.get("answer")}
|
103 |
-
|
104 |
-
except Exception as e:
|
105 |
-
log(f"❌ Error in chat: {e}")
|
106 |
-
traceback.print_exc()
|
107 |
-
return {"error": str(e)}
|
|
|
1 |
+
from fastapi import APIRouter, Request
|
2 |
+
from service_config import ServiceConfig
|
3 |
+
from log import log
|
4 |
+
import json
|
5 |
+
|
6 |
+
router = APIRouter()
|
7 |
+
service_config = ServiceConfig()
|
8 |
+
service_config.load()
|
9 |
+
|
10 |
+
@router.get("/list_projects")
|
11 |
+
def list_projects():
|
12 |
+
projects = list(service_config.projects.keys())
|
13 |
+
log("📋 Listing projects.")
|
14 |
+
return {"projects": projects}
|
15 |
+
|
16 |
+
@router.post("/add_project")
|
17 |
+
async def add_project(request: Request):
|
18 |
+
data = await request.json()
|
19 |
+
project_name = data.get("project_name")
|
20 |
+
if not project_name:
|
21 |
+
return {"error": "Project name required."}
|
22 |
+
|
23 |
+
if project_name in service_config.projects:
|
24 |
+
return {"error": "Project already exists."}
|
25 |
+
|
26 |
+
service_config.projects[project_name] = {
|
27 |
+
"project_name": project_name,
|
28 |
+
"project_version": 1,
|
29 |
+
"llm": {},
|
30 |
+
"intents": []
|
31 |
+
}
|
32 |
+
|
33 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
34 |
+
json.dump({
|
35 |
+
"config": {
|
36 |
+
"work_mode": service_config.work_mode,
|
37 |
+
"cloud_token": service_config.cloud_token,
|
38 |
+
"system_prompt": service_config.system_prompt,
|
39 |
+
"llm_inference_service_url": service_config.llm_inference_service_url
|
40 |
+
},
|
41 |
+
"projects": service_config.projects,
|
42 |
+
"apis": service_config.apis
|
43 |
+
}, f, indent=2)
|
44 |
+
|
45 |
+
log(f"🆕 Added project: {project_name}")
|
46 |
+
return {"message": f"Project {project_name} added."}
|
47 |
+
|
48 |
+
@router.post("/delete_project")
|
49 |
+
async def delete_project(request: Request):
|
50 |
+
data = await request.json()
|
51 |
+
project_name = data.get("project_name")
|
52 |
+
if project_name not in service_config.projects:
|
53 |
+
return {"error": "Project not found."}
|
54 |
+
|
55 |
+
del service_config.projects[project_name]
|
56 |
+
|
57 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
58 |
+
json.dump({
|
59 |
+
"config": {
|
60 |
+
"work_mode": service_config.work_mode,
|
61 |
+
"cloud_token": service_config.cloud_token,
|
62 |
+
"system_prompt": service_config.system_prompt,
|
63 |
+
"llm_inference_service_url": service_config.llm_inference_service_url
|
64 |
+
},
|
65 |
+
"projects": service_config.projects,
|
66 |
+
"apis": service_config.apis
|
67 |
+
}, f, indent=2)
|
68 |
+
|
69 |
+
log(f"🗑️ Deleted project: {project_name}")
|
70 |
+
return {"message": f"Project {project_name} deleted."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|