๐ง LangGraph + Pyodide + WebLLM
Model:
Qwen 2.5 0.5B
Gemma 2 2B
Load Model
โถ Run
โน Stop
Ready to load model
# LangGraph-style Agent Loop Example # Demonstrates an agent that iteratively refines its understanding from js import ask_model, should_stop class AgentState: def __init__(self, topic): self.topic = topic self.iteration = 0 self.max_iterations = 3 self.knowledge = [] self.next_question = f"What is {topic}?" def should_continue(self): return self.iteration < self.max_iterations and not should_stop() # Agent nodes async def research_node(state): """Research node: ask a question and gather knowledge""" print(f"\n{'='*60}") print(f"๐ Iteration {state.iteration + 1}/{state.max_iterations}") print(f"Question: {state.next_question}") print(f"{'='*60}") # Call the LLM (will throw error if stopped) answer = await ask_model(state.next_question) print(f"Answer: {answer}\n") # Update state state.knowledge.append({ "question": state.next_question, "answer": answer }) state.iteration += 1 return state async def planning_node(state): """Planning node: decide what to ask next""" if state.should_continue(): # Generate next question based on current knowledge prompt = f"Based on this answer about {state.topic}: '{state.knowledge[-1]['answer']}', ask ONE brief follow-up question to learn more. Only output the question, nothing else." state.next_question = await ask_model(prompt) print(f"๐ญ Next question planned: {state.next_question}\n") return state async def summary_node(state): """Summary node: synthesize all knowledge""" print(f"\n{'='*60}") print(f"๐ FINAL SUMMARY - {state.topic}") print(f"{'='*60}") print(f"Gathered {len(state.knowledge)} pieces of knowledge:\n") for i, item in enumerate(state.knowledge, 1): print(f"{i}. Q: {item['question']}") print(f" A: {item['answer'][:100]}...\n") return state # Main agent loop (LangGraph-style) async def run_agent(topic): state = AgentState(topic) print(f"๐ค Starting LangGraph Agent for topic: '{topic}'") # Agent loop while state.should_continue(): state = await research_node(state) state = await planning_node(state) # Final summary state = await summary_node(state) return state # Run the agent result = await run_agent("Python programming") print(f"\nโ Agent completed {result.iteration} iterations")