Spaces:
Sleeping
Sleeping
convert result dict to markdown table.
Browse files- SNOMED-CT_Assistant.py +36 -9
SNOMED-CT_Assistant.py
CHANGED
@@ -105,7 +105,7 @@ def query_chroma_db(query_text, query_number):
|
|
105 |
|
106 |
# Func: chroma_db_result to dict
|
107 |
def get_dict_from_chroma_results(results):
|
108 |
-
result_dict = {'ids': results['ids'][0], 'concept_ids': [ str(sub['concept_id']) for sub in results['metadatas'][0] ], 'distances': results['distances'][0], '
|
109 |
return result_dict
|
110 |
|
111 |
|
@@ -129,16 +129,43 @@ def chat_input(prompt, med_text):
|
|
129 |
print("entity: ", entity)
|
130 |
results = query_chroma_db(entity, 10)
|
131 |
results_dict = get_dict_from_chroma_results(results)
|
132 |
-
|
133 |
-
st.session_state.messages.append({"role": "
|
134 |
-
|
135 |
-
|
136 |
-
mapping_msg = entity_mapping_response.choices[0].message.content
|
137 |
-
st.session_state.messages.append({"role": "assistant", "content": mapping_msg})
|
138 |
-
st.chat_message("assistant").write(mapping_msg)
|
139 |
-
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
if "messages" not in st.session_state:
|
144 |
st.session_state["messages"] = [{"role": "system", "content": system_prompt},
|
|
|
105 |
|
106 |
# Func: chroma_db_result to dict
|
107 |
def get_dict_from_chroma_results(results):
|
108 |
+
result_dict = {'ids': results['ids'][0], 'concept_ids': [ str(sub['concept_id']) for sub in results['metadatas'][0] ], 'distances': results['distances'][0], 'descriptions': results['documents'][0]}
|
109 |
return result_dict
|
110 |
|
111 |
|
|
|
129 |
print("entity: ", entity)
|
130 |
results = query_chroma_db(entity, 10)
|
131 |
results_dict = get_dict_from_chroma_results(results)
|
132 |
+
results_table = entity_mapping_result_to_table(entity, results_dict)
|
133 |
+
st.session_state.messages.append({"role": "assistant", "content": results_table})
|
134 |
+
st.chat_message("assistant").write(results_table)
|
135 |
+
|
|
|
|
|
|
|
|
|
136 |
|
137 |
+
# entity_mapping_prompt = generate_entity_mapping_prompt(entity, results_dict)
|
138 |
+
# st.session_state.messages.append({"role": "user", "content": entity_mapping_prompt})
|
139 |
+
# entity_mapping_response = client.chat.completions.create(
|
140 |
+
# model=model_tag, messages=st.session_state.messages, temperature=0.5)
|
141 |
+
# mapping_msg = entity_mapping_response.choices[0].message.content
|
142 |
+
# st.session_state.messages.append({"role": "assistant", "content": mapping_msg})
|
143 |
+
# st.chat_message("assistant").write(mapping_msg)
|
144 |
+
|
145 |
|
146 |
+
# Conver entity mapping result to markdown table
|
147 |
+
def entity_mapping_result_to_table(entity, results_dict):
|
148 |
+
|
149 |
+
ids = results_dict['ids']
|
150 |
+
concept_ids = results_dict['concept_ids']
|
151 |
+
distances = results_dict['distances']
|
152 |
+
descriptions = results_dict['descriptions']
|
153 |
+
|
154 |
+
# header
|
155 |
+
header = "| Identified Entity | Distance | IDs | SNOMED CT - Concept IDs | SNOMED CT - Descriptions |"
|
156 |
+
seperator = "| --- | --- | --- | --- | --- |"
|
157 |
+
|
158 |
+
# table
|
159 |
+
rows = []
|
160 |
+
for id, distance, concept_id, description in zip(ids, distances, concept_ids, descriptions):
|
161 |
+
row = f"| {entity} | {distance:.3f} | {id} | {concept_id} | {description} |"
|
162 |
+
rows.append(row)
|
163 |
+
|
164 |
+
# merge
|
165 |
+
markdown_table = "\n".join([header, seperator] + rows)
|
166 |
+
return markdown_table
|
167 |
+
|
168 |
+
|
169 |
|
170 |
if "messages" not in st.session_state:
|
171 |
st.session_state["messages"] = [{"role": "system", "content": system_prompt},
|