Skip to content

100+ AI Agents, Agent Skills and RAG Apps - Free and Open Source.

fix(ai-knowledge-explorer): handle code-fence-only model replies

Kshitijacd9a84314e1
diff --git a/generative_ui_agents/ai-knowledge-explorer/agent/src/tools.py b/generative_ui_agents/ai-knowledge-explorer/agent/src/tools.py
index 88257604..1dc3bae7 100644
--- a/generative_ui_agents/ai-knowledge-explorer/agent/src/tools.py
+++ b/generative_ui_agents/ai-knowledge-explorer/agent/src/tools.py
@@ -14,6 +14,15 @@ def _get_llm():
return ChatOpenAI(model=model_name, temperature=0.3)
+def _strip_code_fence(content: str) -> str:
+ """Remove a leading ```/```json fence and trailing ``` if present."""
+ content = content.strip()
+ if content.startswith("```"):
+ _, _, rest = content.partition("\n")
+ content = rest.rsplit("```", 1)[0]
+ return content.strip()
+
+
@tool
def extract_knowledge(runtime: ToolRuntime[Any]) -> Command:
"""Extract entities and relationships from uploaded documents or source code files. ALWAYS call this tool when the user adds files. Do not extract knowledge yourself."""
@@ -144,8 +153,7 @@ RULES:
response = llm.invoke(extraction_prompt)
content = response.content.strip()
- if content.startswith("```"):
- content = content.split("\n", 1)[1].rsplit("```", 1)[0].strip()
+ content = _strip_code_fence(content)
try:
data = json.loads(content)
@@ -265,8 +273,7 @@ Focus on non-obvious connections. Prefer "contradicts", "extends", "causes" over
response = llm.invoke(prompt)
content = response.content.strip()
- if content.startswith("```"):
- content = content.split("\n", 1)[1].rsplit("```", 1)[0].strip()
+ content = _strip_code_fence(content)
try:
data = json.loads(content)
@@ -391,8 +398,7 @@ Existing node labels: {json.dumps([n['label'] for n in nodes])}"""
response = llm.invoke(prompt)
content = response.content.strip()
- if content.startswith("```"):
- content = content.split("\n", 1)[1].rsplit("```", 1)[0].strip()
+ content = _strip_code_fence(content)
try:
data = json.loads(content)
diff --git a/generative_ui_agents/ai-knowledge-explorer/agent/tests/test_tool_code_fence.py b/generative_ui_agents/ai-knowledge-explorer/agent/tests/test_tool_code_fence.py
new file mode 100644
index 00000000..22f398bf
--- /dev/null
+++ b/generative_ui_agents/ai-knowledge-explorer/agent/tests/test_tool_code_fence.py
@@ -0,0 +1,16 @@
+import pytest
+
+from src.tools import _strip_code_fence
+
+
+ "raw, expected",
+ [
+ ("```", ""),
+ ("```json", ""),
+ ('```json\n{"a": 1}\n```', '{"a": 1}'),
+ ('{"a": 1}', '{"a": 1}'),
+ ],
+)
+def test_strip_code_fence(raw, expected):
+ assert _strip_code_fence(raw) == expected
\ No newline at end of file