Spaces:
Sleeping
Sleeping
Commit ·
6211544
1
Parent(s): 9bb6d8e
feat: backup, save image in state
Browse files- src/FinancialAgentApp.py +25 -34
src/FinancialAgentApp.py
CHANGED
|
@@ -26,7 +26,9 @@ class FinancialAgentApp (ABC):
|
|
| 26 |
def render_messages(self):
|
| 27 |
"""Render previous chat messages."""
|
| 28 |
for message in self.st.session_state.messages:
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
self.st.markdown(message["content"])
|
| 31 |
|
| 32 |
@abstractmethod
|
|
@@ -71,7 +73,26 @@ class FinancialAgentApp (ABC):
|
|
| 71 |
@abstractmethod
|
| 72 |
def __handle_context__(self, response_state: ResponseState) -> str:
|
| 73 |
"""Handle context if need to add context from data/pdf"""
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def generate_final_answer(self, context_prompt: str):
|
| 77 |
"""Generate and stream the final answer with context."""
|
|
@@ -116,20 +137,7 @@ class HFFinancialRAG(FinancialAgentApp):
|
|
| 116 |
|
| 117 |
def __handle_context__(self, response_state: ResponseState) -> str:
|
| 118 |
"""Handle additional context (data, PDF, etc.)."""
|
| 119 |
-
context_prompt =
|
| 120 |
-
if response_state.contextType in ("data", "both"):
|
| 121 |
-
local_scope = {"df": self.df, "np": np, "pd": pd, "plt": plt, "savefig": self.__safe_savefig__}
|
| 122 |
-
exec(response_state.code, {}, local_scope)
|
| 123 |
-
|
| 124 |
-
fig = plt.gcf()
|
| 125 |
-
if fig.get_axes(): # if a chart was generated
|
| 126 |
-
with self.st.chat_message("assistant"):
|
| 127 |
-
self.st.pyplot(fig)
|
| 128 |
-
plt.close(fig)
|
| 129 |
-
|
| 130 |
-
context_prompt = "## CONTEXT DATAFRAME.\n"
|
| 131 |
-
context_prompt += str(local_scope.get("result", ""))
|
| 132 |
-
|
| 133 |
if response_state.contextType in ("pdf", "both"):
|
| 134 |
context_prompt += "## CONTEXT PDF.\n"
|
| 135 |
results = self.vector_db.similarity_search(response_state.retriverKey, k=5)
|
|
@@ -171,22 +179,5 @@ class OpenAIFinancialRAG(FinancialAgentApp):
|
|
| 171 |
|
| 172 |
def __handle_context__(self, response_state: ResponseState):
|
| 173 |
"""Handle additional context (data, PDF, etc.)."""
|
| 174 |
-
context_prompt =
|
| 175 |
-
if response_state.contextType in ("data", "both"):
|
| 176 |
-
local_scope = {"df": self.df, "np": np, "pd": pd, "plt": plt, "savefig": self.__safe_savefig__}
|
| 177 |
-
exec(response_state.code, {}, local_scope)
|
| 178 |
-
|
| 179 |
-
fig = plt.gcf()
|
| 180 |
-
if fig.get_axes(): # if a chart was generated
|
| 181 |
-
with self.st.chat_message("assistant"):
|
| 182 |
-
self.st.pyplot(fig)
|
| 183 |
-
plt.close(fig)
|
| 184 |
-
|
| 185 |
-
context_prompt = "## CONTEXT DATAFRAME.\n"
|
| 186 |
-
context_prompt += str(local_scope.get("result", ""))
|
| 187 |
-
|
| 188 |
-
# Placeholder for PDF or other context handling
|
| 189 |
-
# elif response_state.contextType in ("pdf", "both"):
|
| 190 |
-
# context_prompt = "Provide the relevant information from the PDF documents."
|
| 191 |
-
|
| 192 |
return context_prompt
|
|
|
|
| 26 |
def render_messages(self):
|
| 27 |
"""Render previous chat messages."""
|
| 28 |
for message in self.st.session_state.messages:
|
| 29 |
+
if message.get("type") == "plot":
|
| 30 |
+
self.st.image(message["content"])
|
| 31 |
+
else:
|
| 32 |
self.st.markdown(message["content"])
|
| 33 |
|
| 34 |
@abstractmethod
|
|
|
|
| 73 |
@abstractmethod
|
| 74 |
def __handle_context__(self, response_state: ResponseState) -> str:
|
| 75 |
"""Handle context if need to add context from data/pdf"""
|
| 76 |
+
context_prompt = ""
|
| 77 |
+
if response_state.contextType in ("data", "both"):
|
| 78 |
+
local_scope = {"df": self.df, "np": np, "pd": pd, "plt": plt, "savefig": self.__safe_savefig__}
|
| 79 |
+
exec(response_state.code, {}, local_scope)
|
| 80 |
+
|
| 81 |
+
fig = plt.gcf()
|
| 82 |
+
if fig.get_axes(): # if a chart was generated
|
| 83 |
+
with self.st.chat_message("assistant"):
|
| 84 |
+
self.st.pyplot(fig)
|
| 85 |
+
plt.close(fig)
|
| 86 |
+
self.st.session_state.messages.append({
|
| 87 |
+
"role": "assistant",
|
| 88 |
+
"type": "plot",
|
| 89 |
+
"content": self.__safe_savefig__()
|
| 90 |
+
})
|
| 91 |
+
|
| 92 |
+
context_prompt = "## CONTEXT DATAFRAME.\n"
|
| 93 |
+
context_prompt += str(local_scope.get("result", ""))
|
| 94 |
+
|
| 95 |
+
return context_prompt
|
| 96 |
|
| 97 |
def generate_final_answer(self, context_prompt: str):
|
| 98 |
"""Generate and stream the final answer with context."""
|
|
|
|
| 137 |
|
| 138 |
def __handle_context__(self, response_state: ResponseState) -> str:
|
| 139 |
"""Handle additional context (data, PDF, etc.)."""
|
| 140 |
+
context_prompt = super().__handle_context(response_state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
if response_state.contextType in ("pdf", "both"):
|
| 142 |
context_prompt += "## CONTEXT PDF.\n"
|
| 143 |
results = self.vector_db.similarity_search(response_state.retriverKey, k=5)
|
|
|
|
| 179 |
|
| 180 |
def __handle_context__(self, response_state: ResponseState):
|
| 181 |
"""Handle additional context (data, PDF, etc.)."""
|
| 182 |
+
context_prompt = super().__handle_context(response_state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
return context_prompt
|