# BFCL Sample Synthesis using AWorld Runtime This example demonstrates how to use AWorld to construct a runtime environment and synthesize function call samples for model training. The BFCL (Basic Function Call Learning) example shows how to create a virtual file system with MCP (Model Context Protocol) tools and generate training data from agent interactions. ## πŸ“‹ Overview The BFCL example consists of: - **GorillaFileSystem**: A virtual file system with MCP tools - **Agent Runtime**: AWorld agent that interacts with the file system - **Function Call Synthesis**: Generation of training samples from agent trajectories ## πŸ—οΈ Architecture ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ AWorld Agent │───▢│ GorillaFileSystem│───▢│ MCP Tools β”‚ β”‚ β”‚ β”‚ (Virtual FS) β”‚ β”‚ (pwd, ls, cd, β”‚ β”‚ - LLM Provider β”‚ β”‚ β”‚ β”‚ touch, echo, β”‚ β”‚ - MCP Client β”‚ β”‚ - File/Directory β”‚ β”‚ cat, etc.) β”‚ β”‚ - Trajectory β”‚ β”‚ - State Managementβ”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Trajectory β”‚ β”‚ File System β”‚ β”‚ Function Call β”‚ β”‚ Collection β”‚ β”‚ Operations β”‚ β”‚ Samples β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ - Agent Actions β”‚ β”‚ - Create/Read/ β”‚ β”‚ - Tool Calls β”‚ β”‚ - Tool Calls β”‚ β”‚ Write Files β”‚ β”‚ - Parameters β”‚ β”‚ - Results β”‚ β”‚ - Directory β”‚ β”‚ - Results β”‚ β”‚ β”‚ β”‚ Navigation β”‚ β”‚ - Context β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` ## πŸš€ Quick Start ### 1. Environment Setup ```bash # Set your OpenRouter API key export OPENROUTER_API_KEY="your-api-key-here" ``` ### 2. Run the Example ```bash # Navigate to the BFCL example directory cd examples/BFCL # Run the BFCL agent example python run.py ``` ### 3. Expected Output The agent will: 1. Connect to the GorillaFileSystem MCP server 2. Perform file operations (create, read, write files) 3. Generate trajectory data with function calls 4. Display the results ## πŸ“ File Structure ``` examples/BFCL/ β”œβ”€β”€ README.md # This file β”œβ”€β”€ run.py # Main agent runner β”œβ”€β”€ mcp_tools/ β”‚ β”œβ”€β”€ __init__.py # Package initialization β”‚ β”œβ”€β”€ gorilla_file_system.py # Virtual file system β”‚ └── test_server.py # Function testing └── requirements.txt # Dependencies ``` ## πŸ”§ Core Components ### 1. Agent Configuration (`run.py`) ```python # Environment-based API key configuration api_key = os.getenv('OPENROUTER_API_KEY') agent_config = AgentConfig( llm_provider="openai", llm_model_name="openai/gpt-4o", llm_api_key=api_key, llm_base_url="https://openrouter.ai/api/v1" ) ``` ### 2. MCP Server Configuration ```python mcp_config = { "mcpServers": { "GorillaFileSystem": { "type": "stdio", "command": "python", "args": ["mcp_tools/gorilla_file_system.py"], } } } ``` ### 3. Agent Creation ```python file_sys_prompt = "You are a helpful agent to use the standard file system..." file_sys = Agent( conf=agent_config, name="file_sys_agent", system_prompt=file_sys_prompt, mcp_servers=mcp_config.get("mcpServers", []).keys(), mcp_config=mcp_config, ) ``` ### 4. Trajectory Collection ```python result = Runners.sync_run( input="use mcp tools to perform file operations...", agent=file_sys, ) print("=" * 100) print(f"result.answer: {result.answer}") print("=" * 100) print(f"result.trajectory: {json.dumps(result.trajectory[0], indent=4)}") ``` ## πŸ› οΈ MCP Tools (GorillaFileSystem) The virtual file system provides the following MCP tools: ### File Operations - `mcp_touch(file_name)`: Create a new file - `mcp_echo(content, file_name)`: Write content to file - `mcp_cat(file_name)`: Read file content - `mcp_rm(file_name)`: Remove file ### Directory Operations - `mcp_pwd()`: Get current directory - `mcp_ls(a=False)`: List directory contents - `mcp_cd(folder)`: Change directory - `mcp_mkdir(dir_name)`: Create directory - `mcp_rmdir(dir_name)`: Remove directory ### Advanced Operations - `mcp_find(path, name)`: Search for files - `mcp_wc(file_name, mode)`: Word count - `mcp_sort(file_name)`: Sort file content - `mcp_grep(file_name, pattern)`: Search in file - `mcp_mv(source, destination)`: Move/rename - `mcp_cp(source, destination)`: Copy files