### LLMExtractionStrategy Example Implementation in Python Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Crawl4AI/06_extractionstrategy.md Provides a simplified example implementation for an LLMExtractionStrategy, likely intended for use within the Pocketflow tutorial. This snippet serves as a placeholder or starting point for more complex LLM-based extraction logic. ```python # Simplified from crawl4ai/extraction_strategy.py ``` -------------------------------- ### Python: Create and Start A2AServer Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Google A2A/04_a2a_server_implementation.md This Python code snippet demonstrates how to instantiate and start an A2AServer. It requires an agent card and a task manager, and specifies the host and port for the server. The server is then started to listen for incoming requests. ```python server = A2AServer( agent_card=echo_agent_card, task_manager=EchoTaskManager(), # Pass our task handler host="localhost", port=5000, ) logger.info("Starting Echo Agent server on http://localhost:5000") server.start() ``` -------------------------------- ### Create DSPy Example with Keyword Arguments Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/DSPy/03_example.md Demonstrates creating a dspy.Example object using keyword arguments that match the Signature fields. It shows how to access the stored values. ```python import dspy # Create an example for our translation task example1 = dspy.Example( english_sentence="Hello, world!", french_sentence="Bonjour le monde!" ) # You can access the values like attributes print(f"English: {example1.english_sentence}") print(f"French: {example1.french_sentence}") ``` -------------------------------- ### Setup and Run Agent Runtime (Python) Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/AutoGen Core/03_agentruntime.md Configures and executes a `SingleThreadedAgentRuntime`. It registers agent factories, adds a subscription for message routing, starts the runtime, sends an initial message to trigger agent processing, and waits for the runtime to become idle before stopping. ```python # 3. Setup and Run the Runtime async def main(): # Create the runtime (the office manager) runtime = SingleThreadedAgentRuntime() # Register the factories (tell the manager how to hire) await runtime.register_factory("researcher", researcher_factory) await runtime.register_factory("writer", writer_factory) print("Registered agent factories.") # Add the subscription (tell manager who listens to which announcements) # Rule: Messages to topics of type "research.facts.available" # should go to a "writer" agent whose key matches the topic source. writer_sub = TypeSubscription(topic_type="research.facts.available", agent_type="writer") await runtime.add_subscription(writer_sub) print(f"Added subscription: {writer_sub.id}") # Start the runtime (open the office) runtime.start() print("Runtime started.") # Send the initial message to kick things off research_task_topic = "AutoGen Agents" researcher_instance_id = AgentId(type="researcher", key=research_task_topic) print(f"Sending initial topic '{research_task_topic}' to {researcher_instance_id}") await runtime.send_message( message=ResearchTopic(topic=research_task_topic), recipient=researcher_instance_id, ) # Wait until all messages are processed (wait for work day to end) print("Waiting for runtime to become idle...") await runtime.stop_when_idle() print("Runtime stopped.") # Run the main function asyncio.run(main()) ``` -------------------------------- ### Python: Initialize SmolAgents MultiStepAgent with Tools and Model Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/SmolaAgents/01_multistepagent.md This Python code snippet shows how to import and set up a MultiStepAgent. It includes defining example tools (SearchTool, WeatherTool) and a language model (LiteLLMModel), then instantiating the agent with these components. Ensure 'litellm' is installed and API keys are configured for the chosen model. ```python from smolagents import MultiStepAgent from smolagents.models import LiteLLMModel from smolagents.tools import SearchTool, WeatherTool # Define the tools the agent can use search_tool = SearchTool() weather_tool = WeatherTool() # Choose a language model # Make sure you have 'litellm' installed: pip install litellm llm = LiteLLMModel(model_id="gpt-3.5-turbo") # Needs an API key set up # Create the MultiStepAgent instance agent = MultiStepAgent( model=llm, tools=[search_tool, weather_tool] # By default, a 'final_answer' tool is always added. ) print("Agent created!") # Give the agent a task! task = "What is the capital of France, and what is its current weather?" print(f"Running agent with task: '{task}'") # Run the agent final_answer = agent.run(task) print("-" * 20) print(f"Final Answer received: {final_answer}") ``` -------------------------------- ### Create DSPy Example from Dictionary Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/DSPy/03_example.md Illustrates creating a dspy.Example object from a Python dictionary. The dictionary keys should correspond to the Signature field names. ```python import dspy data_dict = { "english_sentence": "How are you?", "french_sentence": "Comment ça va?" } example2 = dspy.Example(data_dict) print(f"Example 2 English: {example2.english_sentence}") ``` -------------------------------- ### TOML Configuration File Example Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/OpenManus/07_configuration__config_.md An example of a TOML (Tom's Obvious, Minimal Language) file used for application configuration in OpenManus. It shows how to define sections and key-value pairs for various settings. ```toml # config/config.toml (Simplified Example) [llm] # Settings for the Large Language Model model = "gpt-4o" api_key = "YOUR_OPENAI_API_KEY_HERE" # Replace with your actual key base_url = "https://api.openai.com/v1" api_type = "openai" [sandbox] # Settings for the code execution sandbox use_sandbox = true image = "python:3.12-slim" memory_limit = "256m" [search_config] # Settings for web search engine = "DuckDuckGo" [browser_config] # Settings for the browser tool headless = false ``` -------------------------------- ### DSPy Example and Summarizer Call Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/DSPy/09_adapter.md Demonstrates creating a DSPy Example and calling a summarizer module with the example. This showcases how user-defined data is prepared for DSPy processing. ```python import dspy # Assume Summarize is a pre-defined DSPy Signature # class Summarize(dspy.Signature): # text: str # summary: str # Demo example demo = dspy.Example( text="Long article about cats.", summary="Cats are popular pets." ).with_inputs("text") # Conceptual summarizer module # summarizer = dspy.Predict(Summarize) # Placeholder for actual long text # long_text = "..." # Call the summarizer with the demo (conceptual) # result = summarizer(text=long_text, demos=[demo]) ``` -------------------------------- ### Docker Sandboxing Environment Setup Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Codex/06_command_execution___sandboxing.md Defines a restricted environment using a Dockerfile, suitable for Linux or as a fallback. It starts from a minimal Node.js image, installs only essential tools, copies the application, sets up a non-root user, and configures a network firewall via iptables to limit outbound connections. ```dockerfile # File: codex-cli/Dockerfile (Simplified Snippets) # Start from a basic Node.js image FROM node:20 # Install only necessary tools (git, jq, rg, maybe python/bash, etc.) # Avoid installing powerful tools unless absolutely needed. RUN apt update && apt install -y \ git jq ripgrep sudo iproute2 iptables ipset \ # ... other minimal tools ... && apt-get clean && rm -rf /var/lib/apt/lists/* # Copy codex itself into the container COPY dist/codex.tgz codex.tgz RUN npm install -g codex.tgz # Setup non-root user USER node WORKDIR /home/node/workspace # Work happens here # Copy and set up firewall script (runs via sudo) # This script uses iptables/ipset to block network access by default, # potentially allowing only specific domains if configured. COPY scripts/init_firewall.sh /usr/local/bin/ USER root RUN chmod +x /usr/local/bin/init_firewall.sh && \ # Allow 'node' user to run firewall script via sudo without password echo "node ALL=(root) NOPASSWD: /usr/local/bin/init_firewall.sh" > /etc/sudoers.d/node-firewall USER node # Default command when container starts (might be codex or just a shell) # ENTRYPOINT ["codex"] ``` -------------------------------- ### CodeAgent Initialization and System Prompt Setup (Python) Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/SmolaAgents/05_prompttemplates.md Demonstrates the `__init__` and `initialize_system_prompt` methods of the `CodeAgent` class. It shows how default prompt templates are loaded from a YAML file using `importlib.resources` if not provided, and how the system prompt is constructed by populating a Jinja2 template with relevant agent data. ```python import yaml import importlib.resources from .tools import Tool # From Chapter 3 from .agents import MultiStepAgent, populate_template, PromptTemplates # Helper function class CodeAgent(MultiStepAgent): def __init__( self, tools: list[Tool], model: callable, prompt_templates: PromptTemplates | None = None, # Allow custom templates # ... other parameters ... ): # 1. Load default templates if none provided if prompt_templates is None: # Find the default 'code_agent.yaml' file default_yaml_path = importlib.resources.files("smolagents.prompts").joinpath("code_agent.yaml") # Load the templates from the YAML file prompt_templates = yaml.safe_load(default_yaml_path.read_text()) # 2. Call the parent class init, passing the templates along super().__init__( tools=tools, model=model, prompt_templates=prompt_templates, # Use loaded or provided templates # ... other parameters ... ) # ... rest of CodeAgent setup ... # self.system_prompt is set later using initialize_system_prompt def initialize_system_prompt(self) -> str: """Creates the final system prompt string by filling the template.""" # 3. Get necessary data (tools, managed agents, authorized imports) formatted_tools = # ... format self.tools for the template ... formatted_managed_agents = # ... format self.managed_agents ... authorized_imports = # ... get list of allowed imports for CodeAgent ... # 4. Use the populate_template helper to fill in the blanks system_prompt_string = populate_template( template=self.prompt_templates["system_prompt"], # Get the template string variables= { "tools": formatted_tools, "managed_agents": formatted_managed_agents, "authorized_imports": authorized_imports, # ... other potential variables ... } ) return system_prompt_string # ... other CodeAgent methods ... ``` -------------------------------- ### Get Individual Scores from DSPy Evaluation Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/DSPy/07_evaluate.md Retrieves individual scores for each example in the evaluation dataset. This is useful for pinpointing specific examples where the program fails. It requires a DSPy program and the `return_all_scores=True` flag. ```python avg_score, individual_scores = evaluator_detailed(qa_program, return_all_scores=True) print(f"Individual Scores: {individual_scores}") ``` -------------------------------- ### AsyncWebCrawler Initialization and Context Management (Python) Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Crawl4AI/02_asyncwebcrawler.md Demonstrates the initialization of the AsyncWebCrawler, including dependency injection for crawler strategies and browser configuration. It also shows how to use the asynchronous context manager (__aenter__ and __aexit__) for setup and cleanup. ```python class AsyncWebCrawler: def __init__( self, crawler_strategy: AsyncCrawlerStrategy = None, # You can provide a strategy... config: BrowserConfig = None, # Configuration for the browser # ... other parameters like logger, base_directory ... ): # If no strategy is given, it defaults to Playwright (the 'truck') self.crawler_strategy = crawler_strategy or AsyncPlaywrightCrawlerStrategy(...) self.browser_config = config or BrowserConfig() # ... setup logger, directories, etc. ... self.ready = False # Flag to track if setup is complete async def __aenter__(self): # This is called when you use 'async with'. It starts the strategy. await self.crawler_strategy.__aenter__() await self.awarmup() # Perform internal setup self.ready = True return self async def __aexit__(self, exc_type, exc_val, exc_tb): # This is called when exiting 'async with'. It cleans up. await self.crawler_strategy.__aexit__(exc_type, exc_val, exc_tb) self.ready = False ``` -------------------------------- ### Example Client Request for Prompt Rendering Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/MCP Python SDK/05_fastmcp_prompts___prompt____promptmanager__.md A simplified example of a client request in MCP format to get a rendered prompt. It specifies the method 'getPrompt', the prompt name 'summarize_text', and provides the necessary arguments. ```json // Example Client Request (Simplified MCP format) { "method": "getPrompt", "params": { "name": "summarize_text", "arguments": { "text_to_summarize": "The quick brown fox jumps over the lazy dog." } } } ``` -------------------------------- ### Project Tutorial File Structure (Example) Source: https://context7.com/the-pocket/pocketflow-tutorial-codebase-knowledge/llms.txt Illustrates a typical output directory structure for generated tutorials. Includes an index file and individual markdown files for concepts. ```markdown output/ └── ProjectName/ ├── index.md # Main index with: │ # - Project summary │ # - Mermaid relationship diagram │ # - Chapter listing with links │ ├── 01_first_concept.md # Chapter 1: Core concept ├── 02_second_concept.md # Chapter 2: Building on chapter 1 ├── 03_third_concept.md # Chapter 3: More advanced topic └── ... # Each chapter includes: # - Beginner-friendly explanations with analogies # - Code examples (simplified, <10 lines each) # - Mermaid sequence diagrams # - Links to related chapters # - Navigation to previous/next chapters ``` -------------------------------- ### StateGraph Example with Nodes Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/LangGraph/03_channels.md Illustrates a basic StateGraph setup with a simple state dictionary and two nodes, 'adder' and 'multiplier', demonstrating the flow of data through the graph. ```python # State: {'value': int} # Node 1: adder (reads 'value', returns {'value': value + 1}) # Node 2: multiplier (reads 'value', returns {'value': value * 2}) # Flow: START -> adder -> multiplier -> END ``` -------------------------------- ### Generate Tutorials from CLI (Bash) Source: https://context7.com/the-pocket/pocketflow-tutorial-codebase-knowledge/llms.txt The main entry point for generating tutorials. It accepts arguments for GitHub repositories or local directories, including options for file inclusion/exclusion, size limits, and language. ```bash # Generate a tutorial from a GitHub repository python main.py --repo https://github.com/username/repo --include "*.py" "*.js" --exclude "tests/*" --max-size 50000 # Generate a tutorial from a local directory python main.py --dir /path/to/your/codebase --include "*.py" --exclude "*test*" # Generate a tutorial in Chinese language python main.py --repo https://github.com/username/repo --language "Chinese" # Full example with all options python main.py \ --repo https://github.com/pallets/flask \ --name "Flask Tutorial" \ --token $GITHUB_TOKEN \ --output ./my-tutorials \ --include "*.py" "*.md" \ --exclude "tests/*" "docs/*" \ --max-size 100000 \ --language "english" \ --max-abstractions 8 \ --no-cache ``` -------------------------------- ### Make a GET Request using Requests Functional API Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Requests/01_functional_api.md Fetches data from a specified URL using the `requests.get()` function. It imports the requests library, defines a URL, makes the request, and prints the status code and the beginning of the response text. Ensure the 'requests' library is installed (`pip install requests`). ```python import requests # The URL we want to get data from url = 'https://httpbin.org/get' # A handy website for testing requests # Use the functional API 'get' function print(f"Fetching data from: {url}") response = requests.get(url) # Check if the request was successful (Status Code 200 means OK) print(f"Status Code: {response.status_code}") # Print the first 200 characters of the content we received print("Response Content (first 200 chars):") print(response.text[:200]) ``` -------------------------------- ### LLM Tool Call Response Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/SmolaAgents/03_tool.md Example JSON output from an LLM indicating a tool to use and its input arguments. This format guides the agent's next action. ```json { "thought": "The user wants me to greet Bob. I should use the 'greet_person' tool.", "action": "greet_person", "action_input": {"name": "Bob"} } ``` -------------------------------- ### Initialize Agent Components in Python Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Browser Use/01_agent.md This Python snippet demonstrates the basic setup for using the Agent. It involves importing necessary classes, defining the task, configuring browser settings, and initializing the Browser component. This sets the stage for the Agent to manage browser automation tasks. ```python from browser_use import Agent, Browser, Controller, BrowserConfig, BrowserContextConfig from my_llm_setup import my_llm # Placeholder for your specific LLM setup my_task = "Go to google.com, search for 'cute cat pictures', and click the first image result." browser_config = BrowserConfig() context_config = BrowserContextConfig() browser = Browser(config=browser_config) ``` -------------------------------- ### Get Full Outputs from DSPy Evaluation Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/DSPy/07_evaluate.md Retrieves the full output for each example, including the input, prediction, and correctness. This provides granular insights into the program's behavior for each data point. Use the `return_outputs=True` flag. ```python avg_score, outputs_list = evaluator_detailed(qa_program, return_outputs=True) print(f"Number of outputs returned: {len(outputs_list)}") ``` -------------------------------- ### Combine Tutorial Files (Python) Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/design.md Assembles the final tutorial files, including a Mermaid diagram and an index file. Generates Markdown content for chapters and an index, applying an English attribution footer. ```python def prep(self, shared): project_name = shared['project_name'] relationships = shared['relationships'] chapter_order = shared['chapter_order'] abstractions = shared['abstractions'] chapters = shared['chapters'] repo_url = shared['repo_url'] output_dir = shared['output_dir'] mermaid_diagram = self.generate_mermaid_diagram(relationships['details'], abstractions) index_md_content = self.construct_index_md(project_name, mermaid_diagram, chapter_order, abstractions, chapters, repo_url) output_path = f'./{output_dir}/{project_name}' chapter_files = [] for i, chapter_content in enumerate(chapters): chapter_filename = f'{i+1:02d}_{abstractions[chapter_order[i]].get("translated_name", abstractions[chapter_order[i]]["name"]).lower().replace(" ", "_")}.md' full_chapter_content = chapter_content + "\n\n---\n*This tutorial was generated by Pocketflow.*" chapter_files.append({"filename": chapter_filename, "content": full_chapter_content}) index_md_content += "\n\n---\n*This tutorial was generated by Pocketflow.*" return output_path, chapter_files, index_md_content ``` ```python def exec(self, output_path, chapter_files, index_md_content): import os os.makedirs(output_path, exist_ok=True) with open(os.path.join(output_path, 'index.md'), 'w', encoding='utf-8') as f: f.write(index_md_content) for file_data in chapter_files: with open(os.path.join(output_path, file_data['filename']), 'w', encoding='utf-8') as f: f.write(file_data['content']) ``` ```python def post(self, shared, prep_res): output_path, _, _ = prep_res shared['final_output_dir'] = output_path print(f"Tutorial successfully generated at: {output_path}") ``` -------------------------------- ### MultiStepAgent Execution Start (run) Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/SmolaAgents/01_multistepagent.md Sets up the agent's task, resets memory, records the initial task step, and initiates the internal execution loop (_run). It returns the final answer from the loop's output. ```python # --- File: agents.py (Simplified run) --- class MultiStepAgent: def run(self, task: str, ...): self.task = task # ... maybe handle additional arguments ... # Reset memory if needed self.memory.reset() self.memory.steps.append(TaskStep(task=self.task)) # Record the task # Start the internal execution loop # The deque gets the *last* item yielded, which is the final answer return deque(self._run(task=self.task, max_steps=self.max_steps), maxlen=1)[0].final_answer ``` -------------------------------- ### Create Echo Agent Server in Python Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Google A2A/04_a2a_server_implementation.md Implements a simple Echo Agent using A2AServer in Python. It defines a TaskManager to handle incoming tasks, an agent card, and assumes a simplified server setup. This example focuses on a non-streaming response. ```python # File: simple_agent/main.py (Conceptual Example) from common.server import A2AServer, TaskManager # Simplified import from common.types import ( AgentCard, AgentCapabilities, AgentSkill, Task, TaskSendParams, TaskStatus, TaskState, Message, TextPart, SendTaskResponse ) import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # 1. Define the Agent's Logic Handler (Task Manager) # This class bridges the server and the agent's actual logic. class EchoTaskManager(TaskManager): # Inherit from the base TaskManager async def on_send_task(self, params: TaskSendParams) -> SendTaskResponse: # Simulate processing the task input_text = params.message.parts[0].text if params.message.parts else "No text" logger.info(f"Echo Agent received: {input_text}") # Create the final Task object (simplified for non-streaming) final_task = Task( id=params.id, status=TaskStatus( state=TaskState.COMPLETED, message=Message(role="agent", parts=[TextPart(text=f"You said: {input_text}")]) ), # ... other Task fields ... ) # In a real scenario, you'd store/update the task state # self.tasks[params.id] = final_task # Example storage return SendTaskResponse(id=params.id, result=final_task) # Implement other abstract methods from TaskManager (get, cancel, etc.) # (Skipped for brevity in this example) async def on_get_task(self, request): raise NotImplementedError() async def on_cancel_task(self, request): raise NotImplementedError() # ... and so on for streaming, push notifications etc. # 2. Define the Agent Card echo_agent_card = AgentCard( name="Echo Agent", description="Replies with the text it receives.", url="http://localhost:5000/", # Where this server will run version="1.0", capabilities=AgentCapabilities(streaming=False), # Simplified non-streaming Python example skills=[AgentSkill(id="echo", name="Echo Text")], # ... other card details ) ``` -------------------------------- ### Project Setup and Guidelines (TypeScript) Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Codex/07_configuration_management.md This section outlines project-specific guidelines for the 'my-cool-project'. It specifies the use of TypeScript, adherence to Prettier, the requirement for Jest unit tests, and a workflow for pull requests instead of direct pushes. ```markdown - This project uses TypeScript and adheres to the Prettier style guide. - When adding new features, always include unit tests using Jest. - Do not run `git push` directly; always suggest creating a pull request. ``` -------------------------------- ### Define Fake DB Session Dependency in Python Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/FastAPI/05_dependency_injection.md Defines an asynchronous dependency function `get_db_session` that simulates getting a database session. It prints a message and returns a fake session string. This is a foundational example for dependency injection in FastAPI. ```python async def get_db_session(): print("Getting DB Session") # In reality, this would connect to a DB and yield/return a session object session = "fake_db_session_123" # You might use 'yield' here for setup/teardown (see FastAPI docs) return session ``` -------------------------------- ### Initialize and Run Pocketflow Tutorial Generation Source: https://context7.com/the-pocket/pocketflow-tutorial-codebase-knowledge/llms.txt Sets up the shared state dictionary with repository details, patterns, and output configurations, then executes the main tutorial flow. The results, including the final output directory and identified abstractions, are printed. ```python import os # Prepare shared state with all configuration shared = { "repo_url": "https://github.com/pallets/click", "local_dir": None, # Or use local_dir instead of repo_url "project_name": "Click", "github_token": os.environ.get("GITHUB_TOKEN"), "output_dir": "./output", "include_patterns": {"*.py"}, "exclude_patterns": {"tests/*", "docs/*"}, "max_file_size": 100000, "language": "english", "use_cache": True, "max_abstraction_num": 10, # Outputs populated by nodes "files": [], "abstractions": [], "relationships": {}, "chapter_order": [], "chapters": [], "final_output_dir": None } # Run the flow tutorial_flow.run(shared) # Results available in shared dict print(f"Tutorial generated at: {shared['final_output_dir']}") print(f"Identified {len(shared['abstractions'])} abstractions") ``` -------------------------------- ### Implicit Dispatcher Usage with arun_many in Python Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Crawl4AI/10_basedispatcher.md Demonstrates how to use `crawler.arun_many` without explicitly specifying a dispatcher. The `MemoryAdaptiveDispatcher` is used by default to manage concurrency and collect all crawl results at the end. This example shows basic setup, calling `arun_many`, and processing the returned results. ```python # chapter10_example_1.py import asyncio from crawl4ai import AsyncWebCrawler, CrawlerRunConfig async def main(): urls_to_crawl = [ "https://httpbin.org/html", "https://httpbin.org/links/5/0", # Page with 5 links "https://httpbin.org/robots.txt", "https://httpbin.org/status/200", ] # We DON'T specify a dispatcher here. # arun_many will use the default MemoryAdaptiveDispatcher. async with AsyncWebCrawler() as crawler: print(f"Crawling {len(urls_to_crawl)} URLs using the default dispatcher...") config = CrawlerRunConfig(stream=False) # Get results as a list at the end # The MemoryAdaptiveDispatcher manages concurrency behind the scenes. results = await crawler.arun_many(urls=urls_to_crawl, config=config) print(f"\nFinished! Got {len(results)} results.") for result in results: status = "✅" if result.success else "❌" url_short = result.url.split('/')[-1] print(f" {status} {url_short:<15} | Title: {result.metadata.get('title', 'N/A')}") if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Create and Run a Pocketflow Agent Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/PocketFlow/03_actions___transitions_.md This code illustrates the creation of a Pocketflow agent by defining transitions and then running the flow. It explains how the Flow object interprets the transitions based on the return values of node's post methods, guiding the execution path. ```python # Example of how transitions are interpreted by the Flow object # agent_flow.run(shared_data) # If decide returns "search", go to search node # decide - "search" >> search # If decide returns "answer", go to answer node # decide - "answer" >> answer # If search returns "decide", go back to decide node (creates a loop) # search - "decide" >> decide ``` -------------------------------- ### Start Celery Worker Command Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Celery/05_worker.md This command initiates a Celery worker process. It requires specifying the Celery application instance and optionally sets the logging level. Ensure Celery and the necessary broker driver (e.g., redis) are installed in your environment. ```bash celery -A celery_app worker --loglevel=info ``` -------------------------------- ### Orchestrate AsyncNodes with AsyncFlow Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/PocketFlow/05_asynchronous_processing___asyncnode____asyncflow___.md This Python code demonstrates how to set up and run an asynchronous workflow using PocketFlow's AsyncFlow. It instantiates an AsyncNode, creates an AsyncFlow starting with that node, and then runs the flow asynchronously using await weather_flow.run_async(), passing shared data that gets updated by the node. ```python from pocketflow import AsyncFlow # Assume WeatherFetcherNode is defined as above weather_node = WeatherFetcherNode() weather_flow = AsyncFlow(start=weather_node) # main.py import asyncio async def main(): shared_data = {"city_name": "London"} print("Starting weather flow...") await weather_flow.run_async(shared_data) # Use run_async() print("Weather flow finished.") print(f"Final shared data: {shared_data}") if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### BrowserContext Initialization and Session Setup (Python) Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Browser Use/03_browsercontext.md Demonstrates the initialization of the BrowserContext class, including its configuration and the lazy initialization of the underlying Playwright session. It highlights the use of dataclasses for configuration and session state, and the asynchronous context management (`__aenter__`) for setting up the browser session. ```python # --- File: browser/context.py (Simplified __init__) --- import uuid # ... other imports ... from typing import TYPE_CHECKING, Optional from dataclasses import dataclass # Assuming Playwright types are available from playwright.sync_api import BrowserContext as PlaywrightBrowserContext # Placeholder for BrowserState and logger class BrowserState: pass class DomService: pass class Logger: def debug(self, message): print(message) logger = Logger() if TYPE_CHECKING: from browser_use.browser.browser import Browser # Link to the Browser class @dataclass class BrowserContextConfig: # Configuration settings # ... various settings like user_agent, cookies_file, window_size ... pass @dataclass class BrowserSession: # Holds the actual Playwright context context: PlaywrightBrowserContext # The underlying Playwright object cached_state: Optional[BrowserState] = None # Stores the last known state class BrowserContext: def __init__( self, browser: 'Browser', # Reference to the main Browser instance config: BrowserContextConfig = BrowserContextConfig(), # ... other optional state ... ): self.context_id = str(uuid.uuid4()) # Unique ID for this session self.config = config # Store the configuration self.browser = browser # Store the reference to the parent Browser # The actual Playwright session is created later, when needed self.session: BrowserSession | None = None logger.debug(f"BrowserContext object created (ID: {self.context_id}). Session not yet initialized.") # The 'async with' statement calls __aenter__ which initializes the session async def __aenter__(self): await self._initialize_session() # Creates the actual browser window/tab return self async def _initialize_session(self): # ... (complex setup code happens here) ... # Gets the main Playwright browser from self.browser # playwright_browser = await self.browser.get_playwright_browser() # Creates the isolated Playwright context (like the incognito window) # context = await self._create_context(playwright_browser) # Creates the BrowserSession to hold the context and state # self.session = BrowserSession(context=context, cached_state=None) logger.debug(f"BrowserContext session initialized (ID: {self.context_id}).") # ... (sets up the initial page) ... return self.session # ... other methods like navigate_to, close, etc. ... ``` -------------------------------- ### Create Echo Agent Server in JavaScript Source: https://github.com/the-pocket/pocketflow-tutorial-codebase-knowledge/blob/main/docs/Google A2A/04_a2a_server_implementation.md Implements a simple Echo Agent using A2AServer in JavaScript. It defines agent logic to echo user messages, an agent card for its profile, and starts a server on port 4000. This example uses TypeScript and assumes simplified imports from the 'google-a2a' library. ```typescript // File: simple-agent/index.ts (Conceptual Example) import { A2AServer, TaskContext, TaskYieldUpdate } from "google-a2a/server"; // Simplified import import * as schema from "google-a2a/schema"; // 1. Define the Agent's Logic (The "Expert") // This function handles a single task. async function* echoAgentLogic( context: TaskContext ): AsyncGenerator { const inputText = context.userMessage.parts[0].text ?? "No text found"; // Yield a status update: "working" yield { state: "working", message: { role: "agent", parts: [{ text: "Echoing..." }] } }; // Yield the final result: "completed" yield { state: "completed", message: { role: "agent", parts: [{ text: `You said: ${inputText}` }] } }; // (Artifacts could also be yielded here if needed) } // 2. Define the Agent Card const echoAgentCard: schema.AgentCard = { name: "Echo Agent", description: "Replies with the text it receives.", url: "http://localhost:4000", // Where this server will run version: "1.0", capabilities: { streaming: true }, // It yields updates skills: [{ id: "echo", name: "Echo Text" }], // ... other card details }; // 3. Create and Start the Server const server = new A2AServer(echoAgentLogic, { card: echoAgentCard }); server.start(4000); // Start listening on port 4000 console.log("Echo Agent server running on http://localhost:4000"); ```