Spaces:
Configuration error
Configuration error
Fix: normalize the response in QA mode
Browse files
main.py
CHANGED
|
@@ -715,6 +715,31 @@ def ensure_async_embeddings(embedder):
|
|
| 715 |
return embedder
|
| 716 |
return AsyncEmbeddingAdapter(embedder)
|
| 717 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
@app.post("/set_api_key")
|
| 719 |
async def set_api_key(
|
| 720 |
api_key_input: Optional[str] = Form(None),
|
|
@@ -1813,7 +1838,7 @@ async def process_qa_async(
|
|
| 1813 |
response = await llm_QA.ainvoke(
|
| 1814 |
[HumanMessage(content=f"Summarise this in relation to '{question}':\n{doc.page_content}")]
|
| 1815 |
)
|
| 1816 |
-
summary = getattr(response, "content",
|
| 1817 |
# update progress *after* the chunk is done
|
| 1818 |
done = EMBEDDING_PROGRESS[progress_id]["processed_chunks"] + 1
|
| 1819 |
EMBEDDING_PROGRESS[progress_id]["processed_chunks"] = done
|
|
@@ -1830,7 +1855,7 @@ async def process_qa_async(
|
|
| 1830 |
final_response = await llm_QA.ainvoke(
|
| 1831 |
[HumanMessage(content="Combine the following partial answers into one:\n" + "\n".join(summaries))]
|
| 1832 |
)
|
| 1833 |
-
final = getattr(final_response, "content",
|
| 1834 |
EMBEDDING_PROGRESS[progress_id] = {
|
| 1835 |
"status": "completed",
|
| 1836 |
"progress": 100,
|
|
|
|
| 715 |
return embedder
|
| 716 |
return AsyncEmbeddingAdapter(embedder)
|
| 717 |
|
| 718 |
+
|
| 719 |
+
def llm_content_to_text(content: Any) -> str:
|
| 720 |
+
"""
|
| 721 |
+
Normalize LLM content payloads (string/list/dict) into plain text.
|
| 722 |
+
Some providers (e.g., Responses API) may return structured blocks.
|
| 723 |
+
"""
|
| 724 |
+
if content is None:
|
| 725 |
+
return ""
|
| 726 |
+
if isinstance(content, str):
|
| 727 |
+
return content
|
| 728 |
+
if isinstance(content, list):
|
| 729 |
+
parts: List[str] = []
|
| 730 |
+
for item in content:
|
| 731 |
+
text = llm_content_to_text(item)
|
| 732 |
+
if text:
|
| 733 |
+
parts.append(text)
|
| 734 |
+
return "\n".join(parts)
|
| 735 |
+
if isinstance(content, dict):
|
| 736 |
+
# Common structured message shapes from provider SDKs.
|
| 737 |
+
for key in ("text", "content", "output_text"):
|
| 738 |
+
if key in content:
|
| 739 |
+
return llm_content_to_text(content.get(key))
|
| 740 |
+
return json.dumps(content, ensure_ascii=False)
|
| 741 |
+
return str(content)
|
| 742 |
+
|
| 743 |
@app.post("/set_api_key")
|
| 744 |
async def set_api_key(
|
| 745 |
api_key_input: Optional[str] = Form(None),
|
|
|
|
| 1838 |
response = await llm_QA.ainvoke(
|
| 1839 |
[HumanMessage(content=f"Summarise this in relation to '{question}':\n{doc.page_content}")]
|
| 1840 |
)
|
| 1841 |
+
summary = llm_content_to_text(getattr(response, "content", response))
|
| 1842 |
# update progress *after* the chunk is done
|
| 1843 |
done = EMBEDDING_PROGRESS[progress_id]["processed_chunks"] + 1
|
| 1844 |
EMBEDDING_PROGRESS[progress_id]["processed_chunks"] = done
|
|
|
|
| 1855 |
final_response = await llm_QA.ainvoke(
|
| 1856 |
[HumanMessage(content="Combine the following partial answers into one:\n" + "\n".join(summaries))]
|
| 1857 |
)
|
| 1858 |
+
final = llm_content_to_text(getattr(final_response, "content", final_response))
|
| 1859 |
EMBEDDING_PROGRESS[progress_id] = {
|
| 1860 |
"status": "completed",
|
| 1861 |
"progress": 100,
|