Spaces:
Running
Running
File size: 2,092 Bytes
ba2ada2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | """
Knowledge Forge — synthesizes and retrieves knowledge from the agent's
knowledge web. Combines past solutions, extracted concepts, and
LLM reasoning to produce informed responses.
"""
from typing import Optional
class KnowledgeForge:
"""
Knowledge synthesis tool.
Queries the agent's knowledge web for past solutions,
combines relevant information, and produces synthesized responses.
"""
def __init__(self, knowledge_web=None, llm_call_fn=None):
self._knowledge = knowledge_web
self._llm = llm_call_fn
async def synthesize(self, query: str, context: str = "") -> str:
"""
Synthesize knowledge from past solutions and LLM reasoning.
"""
# Check knowledge web first
if self._knowledge:
past = await self._knowledge.query_solution(query)
if past:
return f"[Knowledge Web] {past['summary']}\n\n{past['result']}"
# Use LLM for novel queries
if self._llm:
prompt = f"""Provide a well-informed response based on available knowledge.
Query: {query}
Context: {context[:500]}
Be accurate and cite any sources you reference."""
return await self._llm(prompt, model_hint="balanced", max_tokens=1000)
return f"Knowledge synthesis unavailable for: {query}"
async def extract_concepts(self, text: str) -> list[str]:
"""Extract key concepts from text for knowledge web indexing."""
if not text:
return []
prompt = f"""Extract the 3-5 most important concepts from this text.
Return as a comma-separated list.
Text: {text[:500]}
Concepts:"""
if self._llm:
try:
raw = await self._llm(prompt, model_hint="fast", max_tokens=100)
return [c.strip() for c in raw.split(",") if c.strip()]
except Exception:
pass
# Fallback: simple keyword extraction
words = text.lower().split()
important = [w for w in words if len(w) > 4 and w.isalpha()]
return list(set(important[:5]))
|