Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
@@ -52,7 +52,7 @@ class AgentProcessor:
|
|
52 |
# Update API base to use environment variable with fallback
|
53 |
self.api_base = os.getenv(
|
54 |
"API_BASE_URL",
|
55 |
-
"
|
56 |
)
|
57 |
self.formatter = ResponseFormatter()
|
58 |
self.client = httpx.AsyncClient(timeout=60.0)
|
@@ -120,6 +120,7 @@ class AgentProcessor:
|
|
120 |
citations = []
|
121 |
metadata = {}
|
122 |
tool_outputs = []
|
|
|
123 |
|
124 |
try:
|
125 |
async with self.client.stream(
|
@@ -134,12 +135,12 @@ class AgentProcessor:
|
|
134 |
f"Headers: {dict(response.headers)}"
|
135 |
)
|
136 |
|
137 |
-
buffer = ""
|
138 |
async for line in response.aiter_lines():
|
139 |
if not line.strip():
|
140 |
continue
|
141 |
|
142 |
self.logger.debug(f"Raw SSE line: {line}")
|
|
|
143 |
|
144 |
if "data:" in line:
|
145 |
try:
|
@@ -205,30 +206,29 @@ class AgentProcessor:
|
|
205 |
e,
|
206 |
{"line": line, "event": "parse_data"}
|
207 |
)
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
buffer = ""
|
232 |
|
233 |
except Exception as e:
|
234 |
self.logger.error(f"Stream processing error: {str(e)}")
|
|
|
52 |
# Update API base to use environment variable with fallback
|
53 |
self.api_base = os.getenv(
|
54 |
"API_BASE_URL",
|
55 |
+
"https://ai-engine.yamamotoqa.com/v1"
|
56 |
)
|
57 |
self.formatter = ResponseFormatter()
|
58 |
self.client = httpx.AsyncClient(timeout=60.0)
|
|
|
120 |
citations = []
|
121 |
metadata = {}
|
122 |
tool_outputs = []
|
123 |
+
buffer = ""
|
124 |
|
125 |
try:
|
126 |
async with self.client.stream(
|
|
|
135 |
f"Headers: {dict(response.headers)}"
|
136 |
)
|
137 |
|
|
|
138 |
async for line in response.aiter_lines():
|
139 |
if not line.strip():
|
140 |
continue
|
141 |
|
142 |
self.logger.debug(f"Raw SSE line: {line}")
|
143 |
+
buffer += line + "\n"
|
144 |
|
145 |
if "data:" in line:
|
146 |
try:
|
|
|
206 |
e,
|
207 |
{"line": line, "event": "parse_data"}
|
208 |
)
|
209 |
+
|
210 |
+
# Process buffer when we have a complete message
|
211 |
+
if buffer.strip().endswith("}"):
|
212 |
+
try:
|
213 |
+
processed_response = parser.parse_sse_event(buffer)
|
214 |
+
if processed_response and isinstance(processed_response, dict):
|
215 |
+
cleaned_response = self.clean_response(processed_response)
|
216 |
+
if cleaned_response:
|
217 |
+
xml_content = cleaned_response.get("content", "")
|
218 |
+
yield f"data: {xml_content}\n\n"
|
219 |
+
except Exception as parse_error:
|
220 |
+
await self.log_error(
|
221 |
+
parse_error,
|
222 |
+
{"buffer": buffer, "event": "process_buffer"}
|
223 |
+
)
|
224 |
+
error_xml = (
|
225 |
+
f"<agent_response>"
|
226 |
+
f"<error>{str(parse_error)}</error>"
|
227 |
+
f"</agent_response>"
|
228 |
+
)
|
229 |
+
yield f"data: {error_xml}\n\n"
|
230 |
+
finally:
|
231 |
+
buffer = ""
|
|
|
232 |
|
233 |
except Exception as e:
|
234 |
self.logger.error(f"Stream processing error: {str(e)}")
|