forked from YasirAhmadX/BlueQuery-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain4.py
More file actions
232 lines (213 loc) · 11 KB
/
main4.py
File metadata and controls
232 lines (213 loc) · 11 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
## this code adapts devsan-mcp to fastapi endpoint for frontend integration
import os
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
# crewai imports (keep the same as your original script)
from crewai import LLM, Agent, Task, Crew, Process
from crewai_tools import MCPServerAdapter
from mcp import StdioServerParameters # For stdio-based MCP servers
app = FastAPI(title="Oceanographic Data Assistant API")
# Request model
class QueryRequest(BaseModel):
query: str
# Make DB path configurable via env var (fallback to your original path)
ARGO_DB_PATH = os.environ.get(
"ARGO_DB_PATH",
"C:/Users/YasirAhmd/Downloads/agro_db_5_floats.db"
)
# Pre-create LLM instance (shared)
llm = LLM(
model="gemini/gemini-2.5-flash-lite",
temperature=0.7,
)
server_params = StdioServerParameters(
command="npx",
args=[
"-y",
"@executeautomation/database-server",
"C:/Users/YasirAhmd/Downloads/agro_db_5_floats.db"
],
env={**os.environ},
)
@app.post("/query")
async def process_query(request: QueryRequest):
"""
Endpoint that:
- runs the prompt guard agent,
- runs the query processor (with MCP tools),
- runs the output formatter,
- returns the final formatted result.
"""
user_query = request.query
# --- MCP Setup (make sure `npx @executeautomation/database-server` is available) ---
try:
with MCPServerAdapter(server_params, connect_timeout=60) as mcp_tools:
# --- Agents ---
prompt_guard = Agent(
role="Prompt Guard Agent",
goal="Check if the user input is safe and relevant to oceanographic queries.",
backstory="Strict filter that blocks unsafe prompts.",
llm=llm,
verbose=True,
memory=True,
)
query_processor = Agent(
role="Query Processor Agent",
goal="Interpret safe user queries and answer those queries.FOr normal queries you have to answer without tool call, If the queries are related to data then only fetch/analyze ARGO float data using the SQLite MCP tools.",
backstory=(
"You are an ocean data assistant who queries the ARGO database "
"via MCP tools, analyzes the results, and produces summaries."
),
llm=llm,
verbose=True,
memory=True,
tools=mcp_tools, # attach MCP tools
)
output_formatter = Agent(
role="Output Formatter Agent",
goal="Format the final response into clean, structured text.",
backstory="Ensures safe, user-friendly, and dashboard-ready responses.",
llm=llm,
verbose=True,
memory=True,
)
# --- Tasks ---
guard_task = Task(
description=(
"Check the input: {user_query}. "
"If unsafe or irrelevant to ocean and argo projects, respond ONLY with 'UNSAFE PROMPT'. "
"If safe, respond with 'SAFE PROMPT'."
),
name="guardrails",
expected_output="Either 'SAFE PROMPT' or 'UNSAFE PROMPT'.",
agent=prompt_guard,
verbose=True,
)
process_task = Task(
description=(
"If guard output was 'SAFE PROMPT', process the query: {user_query}. "
"If the prompt is a normal question, answer it directly without using any tool. "
"Use the SQLite MCP tools to run SQL queries against the ARGO DB. "
"Return a scientific summary (salinity profile, trajectory, etc.). "
"If guard output was 'UNSAFE PROMPT', return 'BLOCKED'."
"""the tables and their columns in the database are as follows:
{
"meta_rel": {
"columns": [
{"name": "PLATFORM_NUMBER", "type": "INTEGER"},
{"name": "FLOAT_SERIAL_NO", "type": "INTEGER"},
{"name": "PLATFORM_TYPE", "type": "TEXT"},
{"name": "PLATFORM_FAMILY", "type": "TEXT"},
{"name": "PLATFORM_MAKER", "type": "TEXT"},
{"name": "DATA_CENTRE", "type": "TEXT"},
{"name": "PROJECT_NAME", "type": "TEXT"},
{"name": "PI_NAME", "type": "TEXT"},
{"name": "DEPLOYMENT_PLATFORM", "type": "TEXT"},
{"name": "LAUNCH_DATE", "type": "TEXT"},
{"name": "LAUNCH_LATITUDE", "type": "REAL"},
{"name": "LAUNCH_LONGITUDE", "type": "REAL"},
{"name": "SENSOR", "type": "TEXT"},
{"name": "PARAMETER", "type": "TEXT"},
{"name": "file_name", "type": "TEXT"}
]
},
"traj_rel": {
"columns": [
{"name": "PLATFORM_NUMBER", "type": "INTEGER"},
{"name": "FLOAT_SERIAL_NO", "type": "REAL"},
{"name": "PLATFORM_TYPE", "type": "TEXT"},
{"name": "DATA_CENTRE", "type": "TEXT"},
{"name": "PROJECT_NAME", "type": "TEXT"},
{"name": "PI_NAME", "type": "TEXT"},
{"name": "POSITIONING_SYSTEM", "type": "TEXT"},
{"name": "DATA_STATE_INDICATOR", "type": "TEXT"},
{"name": "JULD", "type": "TEXT"},
{"name": "LATITUDE", "type": "REAL"},
{"name": "LONGITUDE", "type": "REAL"},
{"name": "POSITION_QC", "type": "REAL"},
{"name": "POSITION_ACCURACY", "type": "REAL"},
{"name": "CYCLE_NUMBER", "type": "REAL"},
{"name": "DATA_MODE", "type": "TEXT"},
{"name": "file_name", "type": "TEXT"}
]
},
"prof_rel": {
"columns": [
{"name": "float_id", "type": "INTEGER"},
{"name": "file_name", "type": "TEXT"},
{"name": "PLATFORM_NUMBER", "type": "INTEGER"},
{"name": "CYCLE_NUMBER", "type": "REAL"},
{"name": "JULD", "type": "TEXT"},
{"name": "LATITUDE", "type": "REAL"},
{"name": "LONGITUDE", "type": "REAL"},
{"name": "PRES", "type": "REAL"},
{"name": "TEMP", "type": "REAL"},
{"name": "PSAL", "type": "REAL"},
{"name": "PRES_QC", "type": "INTEGER"},
{"name": "TEMP_QC", "type": "INTEGER"},
{"name": "PSAL_QC", "type": "INTEGER"},
{"name": "PRES_ADJUSTED", "type": "REAL"},
{"name": "TEMP_ADJUSTED", "type": "REAL"},
{"name": "PSAL_ADJUSTED", "type": "REAL"},
{"name": "PRES_ADJUSTED_QC", "type": "REAL"},
{"name": "TEMP_ADJUSTED_QC", "type": "REAL"},
{"name": "PSAL_ADJUSTED_QC", "type": "REAL"},
{"name": "DATA_MODE", "type": "TEXT"},
{"name": "PLATFORM_TYPE", "type": "TEXT"}
]
},
"tech_rel": {
"columns": [
{"name": "N_TECH_PARAM", "type": "INTEGER"},
{"name": "DATE_CREATION", "type": "TEXT"},
{"name": "DATE_UPDATE", "type": "TEXT"},
{"name": "PLATFORM_NUMBER", "type": "INTEGER"},
{"name": "DATA_CENTRE", "type": "TEXT"},
{"name": "DATA_TYPE", "type": "TEXT"},
{"name": "FORMAT_VERSION", "type": "REAL"},
{"name": "HANDBOOK_VERSION", "type": "REAL"},
{"name": "TECHNICAL_PARAMETER_NAME", "type": "TEXT"},
{"name": "TECHNICAL_PARAMETER_VALUE", "type": "TEXT"},
{"name": "CYCLE_NUMBER", "type": "REAL"},
{"name": "file_name", "type": "TEXT"}
]
}
}
use this schema to directly query the tables"""
),
name="processor",
expected_output="A scientific summary with tables formatted in markdown format or 'BLOCKED'. return only answer, user-friendly Markdown formatted tabular answer.",
agent=query_processor,
tools=mcp_tools,
verbose=True,
)
format_task = Task(
description=(
"Take the processor output and return a clean formatted message. format the tabular data in clean markdown tables"
"If 'BLOCKED', say: '🚫 The input was unsafe and cannot be processed.' "
"Otherwise, return the response as Markdown with sections."
),
name="formatter",
expected_output="return only answer, user-friendly Markdown formatted answer.",
agent=output_formatter,
verbose=True,
)
# --- Crew ---
crew = Crew(
name="OceanCrew-turtle",
#agents=[prompt_guard, query_processor, output_formatter],
#tasks=[guard_task, process_task, format_task],
agents=[prompt_guard, query_processor],
tasks=[guard_task, process_task],
process=Process.sequential,
verbose=True,
tracing=True,
)
# --- Run crew with the user's query ---
result = crew.kickoff(inputs={"user_query": user_query})
# Return the crew output
# result may be a dict-like or string depending on Crew API; adapt if needed
return {"query": user_query, "result": result}
except Exception as e:
# Provide helpful HTTP error
raise HTTPException(status_code=500, detail=f"Processing failed: {e}")