Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +25 -5
src/streamlit_app.py
CHANGED
@@ -8,6 +8,12 @@ from datetime import datetime
|
|
8 |
from typing import Generator, Dict, Any
|
9 |
from requests.exceptions import RequestException
|
10 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def parse_and_render_streamed_response(streamed):
|
13 |
"""
|
@@ -96,15 +102,20 @@ def call_perplexity_api(user_query: str) -> str:
|
|
96 |
"messages": [{"role": "user", "content": user_query}],
|
97 |
"stream": False,
|
98 |
"search_mode": search_mode,
|
99 |
-
"search_after_date_filter": search_after_date_filter
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
}
|
101 |
-
|
102 |
retries = 3
|
103 |
for attempt in range(retries):
|
104 |
try:
|
105 |
response = requests.post(url, headers=headers, json=payload, timeout=120)
|
106 |
response.raise_for_status()
|
107 |
-
return response
|
108 |
except RequestException as err:
|
109 |
if attempt < retries - 1:
|
110 |
wait_time = 2 ** attempt
|
@@ -126,8 +137,17 @@ if user_input:
|
|
126 |
with st.spinner("Thinking..."):
|
127 |
try:
|
128 |
full_response = call_perplexity_api(user_input)
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
132 |
except requests.RequestException as err:
|
133 |
st.error(f"Error: {err}")
|
|
|
8 |
from typing import Generator, Dict, Any
|
9 |
from requests.exceptions import RequestException
|
10 |
import re
|
11 |
+
import json
|
12 |
+
from pydantic import BaseModel
|
13 |
+
|
14 |
+
class AnswerFormat(BaseModel):
|
15 |
+
think: str
|
16 |
+
answer: str
|
17 |
|
18 |
def parse_and_render_streamed_response(streamed):
|
19 |
"""
|
|
|
102 |
"messages": [{"role": "user", "content": user_query}],
|
103 |
"stream": False,
|
104 |
"search_mode": search_mode,
|
105 |
+
"search_after_date_filter": search_after_date_filter,
|
106 |
+
"response_format": {
|
107 |
+
"type": "json_schema",
|
108 |
+
"json_schema": {
|
109 |
+
"schema": AnswerFormat.model_json_schema()
|
110 |
+
}
|
111 |
+
}
|
112 |
}
|
|
|
113 |
retries = 3
|
114 |
for attempt in range(retries):
|
115 |
try:
|
116 |
response = requests.post(url, headers=headers, json=payload, timeout=120)
|
117 |
response.raise_for_status()
|
118 |
+
return response
|
119 |
except RequestException as err:
|
120 |
if attempt < retries - 1:
|
121 |
wait_time = 2 ** attempt
|
|
|
137 |
with st.spinner("Thinking..."):
|
138 |
try:
|
139 |
full_response = call_perplexity_api(user_input)
|
140 |
+
full_response_json = json.loads(full_response["choices"][0]["message"]["content"])
|
141 |
+
st.subheader("🤔 Reasoning")
|
142 |
+
if full_response_json.think:
|
143 |
+
st.markdown(full_response_json.think)
|
144 |
+
else:
|
145 |
+
st.markdown("... Reasoning not found ...")
|
146 |
+
st.subheader("💡 Answer")
|
147 |
+
if full_response_json.answer:
|
148 |
+
st.markdown(full_response_json.answer)
|
149 |
+
else:
|
150 |
+
st.markdown("... Answer not found ...")
|
151 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
152 |
except requests.RequestException as err:
|
153 |
st.error(f"Error: {err}")
|