dolphinium
commited on
Commit
·
c13e67b
1
Parent(s):
a811652
parse intentions only on search_list returned by ex. api
Browse files- data_processing.py +13 -7
- extract_results.py +11 -4
- ui.py +8 -4
data_processing.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
"""
|
3 |
Core data processing and analysis logic for the PharmaCircle AI Data Analyst.
|
4 |
|
@@ -44,11 +43,18 @@ def llm_generate_analysis_plan_with_history(llm_model, natural_language_query, c
|
|
44 |
and dynamic field suggestions from an external API.
|
45 |
"""
|
46 |
search_fields, search_name, field_mappings = [], "", {}
|
|
|
47 |
try:
|
48 |
-
search_fields, search_name, field_mappings = get_search_list_params(natural_language_query)
|
49 |
-
print(f"API returned core: '{search_name}' with {len(search_fields)} fields and {len(field_mappings)} mappings.")
|
|
|
|
|
|
|
|
|
|
|
50 |
except Exception as e:
|
51 |
print(f"Warning: Could not retrieve dynamic search fields. Proceeding without them. Error: {e}")
|
|
|
52 |
|
53 |
core_name = search_name if search_name else 'news'
|
54 |
|
@@ -70,15 +76,15 @@ def llm_generate_analysis_plan_with_history(llm_model, natural_language_query, c
|
|
70 |
response = llm_model.generate_content(prompt)
|
71 |
cleaned_text = re.sub(r'```json\s*|\s*```', '', response.text, flags=re.MULTILINE | re.DOTALL).strip()
|
72 |
plan = json.loads(cleaned_text)
|
73 |
-
return plan, mapped_search_fields, core_name
|
74 |
except json.JSONDecodeError as e:
|
75 |
raw_response_text = response.text if 'response' in locals() else 'N/A'
|
76 |
print(f"Error decoding JSON from LLM response: {e}\nRaw Response:\n{raw_response_text}")
|
77 |
-
return None, mapped_search_fields, core_name
|
78 |
except Exception as e:
|
79 |
raw_response_text = response.text if 'response' in locals() else 'N/A'
|
80 |
print(f"Error in llm_generate_analysis_plan_with_history: {e}\nRaw Response:\n{raw_response_text}")
|
81 |
-
return None, mapped_search_fields, core_name
|
82 |
|
83 |
def execute_quantitative_query(solr_client, plan):
|
84 |
"""Executes the facet query to get aggregate data."""
|
@@ -194,4 +200,4 @@ def execute_viz_code_and_get_path(viz_code, facet_data):
|
|
194 |
print(f"Error: {e}")
|
195 |
print(f"--- Code---\n{viz_code}")
|
196 |
print("-----------------------------------------")
|
197 |
-
return None
|
|
|
|
|
1 |
"""
|
2 |
Core data processing and analysis logic for the PharmaCircle AI Data Analyst.
|
3 |
|
|
|
43 |
and dynamic field suggestions from an external API.
|
44 |
"""
|
45 |
search_fields, search_name, field_mappings = [], "", {}
|
46 |
+
intent = None
|
47 |
try:
|
48 |
+
intent, search_fields, search_name, field_mappings = get_search_list_params(natural_language_query)
|
49 |
+
print(f"API returned intent: '{intent}', core: '{search_name}' with {len(search_fields)} fields and {len(field_mappings)} mappings.")
|
50 |
+
|
51 |
+
if intent != 'search_list':
|
52 |
+
print(f"API returned intent '{intent}' which is not 'search_list'. Aborting analysis.")
|
53 |
+
return None, None, None, intent
|
54 |
+
|
55 |
except Exception as e:
|
56 |
print(f"Warning: Could not retrieve dynamic search fields. Proceeding without them. Error: {e}")
|
57 |
+
return None, [], None, 'api_error'
|
58 |
|
59 |
core_name = search_name if search_name else 'news'
|
60 |
|
|
|
76 |
response = llm_model.generate_content(prompt)
|
77 |
cleaned_text = re.sub(r'```json\s*|\s*```', '', response.text, flags=re.MULTILINE | re.DOTALL).strip()
|
78 |
plan = json.loads(cleaned_text)
|
79 |
+
return plan, mapped_search_fields, core_name, intent
|
80 |
except json.JSONDecodeError as e:
|
81 |
raw_response_text = response.text if 'response' in locals() else 'N/A'
|
82 |
print(f"Error decoding JSON from LLM response: {e}\nRaw Response:\n{raw_response_text}")
|
83 |
+
return None, mapped_search_fields, core_name, intent
|
84 |
except Exception as e:
|
85 |
raw_response_text = response.text if 'response' in locals() else 'N/A'
|
86 |
print(f"Error in llm_generate_analysis_plan_with_history: {e}\nRaw Response:\n{raw_response_text}")
|
87 |
+
return None, mapped_search_fields, core_name, intent
|
88 |
|
89 |
def execute_quantitative_query(solr_client, plan):
|
90 |
"""Executes the facet query to get aggregate data."""
|
|
|
200 |
print(f"Error: {e}")
|
201 |
print(f"--- Code---\n{viz_code}")
|
202 |
print("-----------------------------------------")
|
203 |
+
return None
|
extract_results.py
CHANGED
@@ -19,10 +19,10 @@ def _parse_mappings(mapping_str: str) -> dict:
|
|
19 |
|
20 |
def get_search_list_params(query, k=20):
|
21 |
"""
|
22 |
-
Connects to the external API, parses the stream, and returns the core name,
|
23 |
search fields, and field mappings.
|
24 |
|
25 |
-
Returns tuple: (search_fields, search_name, field_mappings)
|
26 |
"""
|
27 |
url = "https://aitest.ebalina.com/stream"
|
28 |
|
@@ -39,6 +39,7 @@ def get_search_list_params(query, k=20):
|
|
39 |
search_fields = []
|
40 |
search_name = ""
|
41 |
field_mappings_str = ""
|
|
|
42 |
|
43 |
for line in response.iter_lines():
|
44 |
if line and line.startswith(b'data: '):
|
@@ -50,6 +51,12 @@ def get_search_list_params(query, k=20):
|
|
50 |
data = json.loads(line_str)
|
51 |
log_title = data.get('log_title')
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
if log_title == 'Search List Result':
|
54 |
content = data.get('content', '')
|
55 |
if content:
|
@@ -68,8 +75,8 @@ def get_search_list_params(query, k=20):
|
|
68 |
|
69 |
field_mappings = _parse_mappings(field_mappings_str)
|
70 |
|
71 |
-
return search_fields, search_name, field_mappings
|
72 |
|
73 |
except requests.exceptions.RequestException as e:
|
74 |
print(f"Error connecting to the external API: {e}")
|
75 |
-
return [], "", {}
|
|
|
19 |
|
20 |
def get_search_list_params(query, k=20):
|
21 |
"""
|
22 |
+
Connects to the external API, parses the stream, and returns the intent, core name,
|
23 |
search fields, and field mappings.
|
24 |
|
25 |
+
Returns tuple: (intent, search_fields, search_name, field_mappings)
|
26 |
"""
|
27 |
url = "https://aitest.ebalina.com/stream"
|
28 |
|
|
|
39 |
search_fields = []
|
40 |
search_name = ""
|
41 |
field_mappings_str = ""
|
42 |
+
intent = None
|
43 |
|
44 |
for line in response.iter_lines():
|
45 |
if line and line.startswith(b'data: '):
|
|
|
51 |
data = json.loads(line_str)
|
52 |
log_title = data.get('log_title')
|
53 |
|
54 |
+
if log_title == 'NER Succeded':
|
55 |
+
content = data.get('content', {})
|
56 |
+
if 'intent' in content and content['intent']:
|
57 |
+
intent = content['intent'][0]
|
58 |
+
print(f"DEBUG: Intent detected: {intent}")
|
59 |
+
|
60 |
if log_title == 'Search List Result':
|
61 |
content = data.get('content', '')
|
62 |
if content:
|
|
|
75 |
|
76 |
field_mappings = _parse_mappings(field_mappings_str)
|
77 |
|
78 |
+
return intent, search_fields, search_name, field_mappings
|
79 |
|
80 |
except requests.exceptions.RequestException as e:
|
81 |
print(f"Error connecting to the external API: {e}")
|
82 |
+
return None, [], "", {}
|
ui.py
CHANGED
@@ -96,8 +96,8 @@ def create_ui(llm_model, solr_client):
|
|
96 |
history.append((user_input, f"Analyzing: '{query_context}'\n\n*Generating analysis plan...*"))
|
97 |
yield (history, state, None, None, None, None, None, None, None, None)
|
98 |
|
99 |
-
# Generate plan
|
100 |
-
analysis_plan, mapped_search_fields, core_name = llm_generate_analysis_plan_with_history(llm_model, query_context, history)
|
101 |
|
102 |
# Update and display search field suggestions in its own accordion
|
103 |
if mapped_search_fields:
|
@@ -107,7 +107,11 @@ def create_ui(llm_model, solr_client):
|
|
107 |
suggestions_display_update = gr.update(value="No suggestions were returned from the external API.", visible=True)
|
108 |
|
109 |
if not analysis_plan:
|
110 |
-
|
|
|
|
|
|
|
|
|
111 |
yield (history, state, None, None, None, None, None, None, None, suggestions_display_update)
|
112 |
return
|
113 |
|
@@ -225,4 +229,4 @@ def create_ui(llm_model, solr_client):
|
|
225 |
queue=False
|
226 |
)
|
227 |
|
228 |
-
return demo
|
|
|
96 |
history.append((user_input, f"Analyzing: '{query_context}'\n\n*Generating analysis plan...*"))
|
97 |
yield (history, state, None, None, None, None, None, None, None, None)
|
98 |
|
99 |
+
# Generate plan, get search field suggestions, and intent.
|
100 |
+
analysis_plan, mapped_search_fields, core_name, intent = llm_generate_analysis_plan_with_history(llm_model, query_context, history)
|
101 |
|
102 |
# Update and display search field suggestions in its own accordion
|
103 |
if mapped_search_fields:
|
|
|
107 |
suggestions_display_update = gr.update(value="No suggestions were returned from the external API.", visible=True)
|
108 |
|
109 |
if not analysis_plan:
|
110 |
+
if intent and intent != 'search_list':
|
111 |
+
message = f"I am sorry, I can only perform analysis for 'search_list' type queries. Your query was identified as a '{intent}', which is not supported."
|
112 |
+
else:
|
113 |
+
message = "I'm sorry, I couldn't generate a valid analysis plan. Please try rephrasing your question."
|
114 |
+
history.append((None, message))
|
115 |
yield (history, state, None, None, None, None, None, None, None, suggestions_display_update)
|
116 |
return
|
117 |
|
|
|
229 |
queue=False
|
230 |
)
|
231 |
|
232 |
+
return demo
|