""" 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]))