Skip to content

Commit e8df33b

Browse files
committed
♻️ Refactor(streamlit_report.py): modify the _generate_chatbot_content to handle a JSON file retrieved from the BKGR API
1 parent 8b8e62a commit e8df33b

File tree

1 file changed

+90
-1
lines changed

1 file changed

+90
-1
lines changed

src/vuegen/streamlit_reportview.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,97 @@ def _generate_apicall_content(self, apicall) -> List[str]:
854854
f"Successfully generated content for APICall '{apicall.title}' using method '{apicall.method}'"
855855
)
856856
return apicall_content
857-
857+
858858
def _generate_chatbot_content(self, chatbot) -> List[str]:
859+
"""
860+
Generate content for a ChatBot component with exact API requirements.
861+
"""
862+
chatbot_content = []
863+
864+
# Add title
865+
chatbot_content.append(
866+
self._format_text(
867+
text=chatbot.title, type="header", level=4, color="#2b8cbe"
868+
)
869+
)
870+
871+
# Chatbot logic with exact API requirements
872+
chatbot_content.append(
873+
f"""
874+
def generate_query(prompt):
875+
try:
876+
response = requests.post(
877+
"{chatbot.api_call.api_url}",
878+
json={{"prompt": prompt}}, # Only send the prompt as required
879+
headers={chatbot.api_call.headers}
880+
)
881+
response.raise_for_status()
882+
return response.json()
883+
except requests.exceptions.RequestException as e:
884+
st.error(f"API request failed: {{str(e)}}")
885+
if hasattr(e, 'response') and e.response:
886+
try:
887+
error_details = e.response.json()
888+
st.error(f"Error details: {{error_details}}")
889+
except ValueError:
890+
st.error(f"Response text: {{e.response.text}}")
891+
return None
892+
893+
# Chatbot interaction
894+
if 'messages' not in st.session_state:
895+
st.session_state['messages'] = []
896+
897+
# Display chat history
898+
for message in st.session_state['messages']:
899+
with st.chat_message(message['role']):
900+
if isinstance(message['content'], dict):
901+
st.markdown(message['content'].get('text', ''), unsafe_allow_html=True)
902+
if 'links' in message['content']:
903+
st.markdown("**Sources:**")
904+
for link in message['content']['links']:
905+
st.markdown(f"- [{{link}}]({{link}})")
906+
if 'subgraph_pyvis' in message['content']:
907+
st.components.v1.html(message['content']['subgraph_pyvis'], height=600)
908+
else:
909+
st.write(message['content'])
910+
911+
# Handle user input
912+
if prompt := st.chat_input("Enter your prompt here:"):
913+
st.session_state.messages.append({{"role": "user", "content": prompt}})
914+
with st.chat_message("user"):
915+
st.write(prompt)
916+
917+
with st.spinner('Generating answer...'):
918+
response = generate_query(prompt)
919+
920+
if response:
921+
st.session_state.messages.append({{
922+
"role": "assistant",
923+
"content": response
924+
}})
925+
with st.chat_message("assistant"):
926+
st.markdown(response.get('text', ''), unsafe_allow_html=True)
927+
if 'links' in response:
928+
st.markdown("**Sources:**")
929+
for link in response['links']:
930+
st.markdown(f"- [{{link}}]({{link}})")
931+
if 'subgraph_pyvis' in response:
932+
st.components.v1.html(response['subgraph_pyvis'], height=600)
933+
else:
934+
st.error("Failed to get response from API")
935+
"""
936+
)
937+
938+
if chatbot.caption:
939+
chatbot_content.append(
940+
self._format_text(
941+
text=chatbot.caption, type="caption", text_align="left"
942+
)
943+
)
944+
945+
return chatbot_content
946+
947+
def _generate_ollama_chatbot_content(self, chatbot) -> List[str]:
859948
"""
860949
Generate content for a ChatBot component.
861950

0 commit comments

Comments
 (0)