### Complete Setup with Logging Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-utilities.md A comprehensive example demonstrating the setup of an AgentFlow agent with logging, rewards, and training. ```python import logging from agentflow import ( configure_logger, LitAgent, Trainer, DevTaskLoader, LLM, PromptTemplate, ResourcesUpdate, reward ) # Configure logging logger = configure_logger(level=logging.INFO) class MyAgent(LitAgent): def __init__(self): super().__init__() @reward def compute_reward(self, response, expected): return 1.0 if response == expected else 0.5 def training_rollout(self, task, rollout_id, resources): logger.info(f"Processing task {rollout_id}") # Your logic here response = f"Answer to {task}" r = self.compute_reward(response, task) logger.debug(f"Reward: {r}") return r # Setup resources resources = ResourcesUpdate( resources_id="v1_0", resources={ 'main_llm': LLM( endpoint="http://localhost:8000", model="gpt-4o", sampling_parameters={'temperature': 0.7} ) } ) # Create trainer trainer = Trainer(n_workers=2) agent = MyAgent() # Create dev loader loader = DevTaskLoader( tasks=["task1", "task2"], resources=resources ) # Train logger.info("Starting training") trainer.fit(agent, backend=loader) # Collect results for rollout in loader.rollouts: logger.info(f"Rollout {rollout.rollout_id}: {rollout.final_reward}") ``` -------------------------------- ### Multi-Engine Setup Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example demonstrating how to set up and use multiple LLM engines within an agent. ```python from agentflow.agentflow.engine.factory import create_llm_engine # Fast, cheap model fast_model = create_llm_engine("gpt-3.5-turbo") # Powerful model strong_model = create_llm_engine( "gpt-4o", temperature=0.5, use_cache=True ) # Local model for privacy local_model = create_llm_engine( "ollama-llama2", base_url="http://localhost:11434" ) # Use in agent class MultiEngineAgent(LitAgent): def training_rollout(self, task, rollout_id, resources): # Use fast model first quick_answer = fast_model(f"Quick answer: {task}") # If needed, use powerful model if needs_refinement(quick_answer): refined = strong_model(f"Refine: {quick_answer}") return 0.9 return 0.5 ``` -------------------------------- ### Example Benchmark Configuration Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/benchmark.md Example configuration within a task's run.sh script, showing parameters like task name, threads, data file, and model definitions. ```bash #!/bin/bash # Configuration TASK="bamboogle" THREADS=20 DATA_FILE_NAME="data.json" MODELS=( "8000:vllm-AgentFlow/agentflow-planner-7b,AgentFlow-7B,\\nBase_Generator_Tool|Python_Coder_Tool|Google_Search_Tool|Wikipedia_Search_Tool,\\nเถฑเทŠเถฏgpt-4o-mini|gpt-4o-mini|Default|Default,\\ntrainable|gpt-4o|gpt-4o|gpt-4o" ) ``` -------------------------------- ### Installation and Setup Source: https://github.com/lupantech/agentflow/blob/main/README.md Commands to install AgentFlow, activate the virtual environment, and optionally install the 'parallel' utility. ```bash bash setup.sh source .venv/bin/activate # (Optional) Install `parallel` for running benchmark experiments in parallel: sudo apt-get update sudo apt-get install parallel ``` -------------------------------- ### Basic AgentFlow Setup Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Demonstrates how to initialize Planner, Executor, Verifier, and Memory components, then create and use a Solver to address a problem. ```python planner = Planner( llm_engine_name="gpt-4o", available_tools=["Web_Search_Tool", "Python_Coder_Tool"], toolbox_metadata={...} ) executor = Executor( llm_engine_name="gpt-4o", max_time=120 ) verifier = Verifier(llm_engine_name="gpt-4o") memory = Memory() # Create solver solver = Solver( planner=planner, executor=executor, verifier=verifier, memory=memory, max_steps=10, output_types="base,final,direct" ) # Solve a problem result = solver.solve("Complex question with image", image_path=None) print(f"Base response: {result['base_response']}") print(f"Final response: {result['final_output']}") ``` -------------------------------- ### Task Management with Server Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Provides an example of how to add tasks to the server asynchronously, including specifying sample, mode, resources, and metadata. ```python # Create tasks on server import asyncio async def add_tasks(server): tasks = [ ("Q1", "train", "v1_0"), ("Q2", "train", "v1_0"), ("Q3", "val", "v1_0"), ] rollout_ids = [] for question, mode, resources_id in tasks: rollout_id = await server.add_task( sample=question, mode=mode, resources_id=resources_id, metadata={"source": "benchmark"} ) rollout_ids.append(rollout_id) return rollout_ids # asyncio.run(add_tasks(server)) ``` -------------------------------- ### Handle async Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Example of handling asynchronous operations. ```python import asyncio asyncio.run(async_function()) ``` -------------------------------- ### Distributed Training Setup Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Shows how to configure a Trainer for distributed training with multiple workers and connect to an AgentFlow client. ```python from agentflow import Trainer, AgentFlowClient # Start with multiple workers trainer = Trainer( n_workers=8, # 8 parallel workers max_tasks=5000, # Total tasks to process daemon=True, # Clean shutdown tracer={ "type": "agentflow.tracer.agentops.AgentOpsTracer", "daemon": True } ) # Connect to server client = AgentFlowClient( endpoint="http://agentflow.example.com:8000", poll_interval=2.0, timeout=30.0 ) # Run training trainer.fit(MyAgent(), backend=client) ``` -------------------------------- ### Installation & Imports Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Essential imports for AgentFlow, including LitAgent, Trainer, LLM, PromptTemplate, and logging configuration. ```python # After: pip install agentflow from agentflow import ( LitAgent, Trainer, DevTaskLoader, LLM, PromptTemplate, ResourcesUpdate, reward, configure_logger ) import logging # Configure logging logger = configure_logger(level=logging.INFO) ``` -------------------------------- ### Reference Cheat Sheet: Resources Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Shows an example of how to structure resources, which can include LLMs, prompt templates, and backup LLMs. ```python # Create resources { 'main_llm': LLM(...), 'system_prompt': PromptTemplate(...), 'backup_llm': LLM(...) } ``` -------------------------------- ### Local Development (DevTaskLoader) Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Demonstrates local development setup using DevTaskLoader for training an agent with predefined tasks and resources. ```python # Create tasks tasks = [ "What is Python?", "Explain machine learning", "What is AI?" ] # Set up trainer trainer = Trainer( n_workers=1, max_tasks=None, dev=False ) # Create agent agent = MyAgent() # Create local loader loader = DevTaskLoader( tasks=tasks, resources=resources, poll_interval=1.0 ) # Train trainer.fit(agent, backend=loader) # Inspect results for rollout in loader.rollouts: print(f"Task {rollout.rollout_id}: reward={rollout.final_reward}") ``` -------------------------------- ### LLM Configuration Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Examples of configuring and creating LLM engines for different providers like OpenAI, Claude, Ollama, and custom endpoints, including caching. ```python from agentflow.agentflow.engine.factory import create_llm_engine # OpenAI gpt4 = create_llm_engine("gpt-4o", temperature=0.7) # Claude (requires ANTHROPIC_API_KEY) claude = create_llm_engine("claude-3-opus", temperature=0.5) # Local Ollama ollama = create_llm_engine("ollama-llama2", temperature=0.7) # Custom endpoint custom = create_llm_engine( "vllm-llama2-70b", base_url="http://localhost:8000/v1" ) # With caching cached = create_llm_engine("gpt-4o", use_cache=True) ``` -------------------------------- ### AgentFlowServer Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-trainer-server.md Example of how to initialize and run an AgentFlowServer. ```python from agentflow import AgentFlowServer server = AgentFlowServer( host="0.0.0.0", port=8000, max_retries=3 ) server.run() ``` -------------------------------- ### Monitoring and Logging Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Configures AgentFlow logger for detailed monitoring and logging, including an example of a verbose agent. ```python from agentflow import configure_logger import logging # Set logging level logger = configure_logger(level=logging.DEBUG, name="agentflow") class VerboseAgent(LitAgent): def training_rollout(self, task, rollout_id, resources): logger.info(f"Starting rollout {rollout_id}") try: result = self.process_task(task) logger.debug(f"Result: {result}") return 0.9 except Exception as e: logger.exception(f"Error in {rollout_id}") return 0.0 ``` -------------------------------- ### Planner Initialization Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-models.md Example of how to initialize the Planner. ```python from agentflow.agentflow.models.planner import Planner planner = Planner( llm_engine_name="gpt-4o", llm_engine_fixed_name="gpt-4o", available_tools=[ "Base_Generator_Tool", "Google_Search_Tool", "Python_Coder_Tool" ], toolbox_metadata={ "Base_Generator_Tool": { "description": "General purpose question answering" } } ) ``` -------------------------------- ### Add telemetry Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Example of adding telemetry to a reward function. ```python import logging @reward def my_reward(...): ... # Configure logger logger = configure_logger(level=logging.DEBUG) ``` -------------------------------- ### Quick Start Inference Source: https://github.com/lupantech/agentflow/blob/main/README.md Command to run AgentFlow inference and example output. ```python python quick_start.py ``` ```text ==> Initializing agentflow... ==> Setting up tools... ==> ๐ŸŽฏ Reasoning Steps from AgentFlow (Deep Thinking...) ==> ๐Ÿ” Step 0: Query Analysis ==> ๐ŸŽฏ Step 1: Action Prediction (Google_Search_Tool) ==> ๐Ÿ› ๏ธ Step 1: Command Execution (Google_Search_Tool) ... **Answer:** The capital of France is Paris. ==> โœ… Query Solved! **Process Summary:** 1. **Query Analysis:** Identified as a factual question about the capital of France. 2. **Tool Selection:** Used Google Search for accurate information. 3. **Execution:** Confirmed Paris as the capital. 4. **Verification:** Cross-referenced sources for reliability. **Answer:** The capital of France is Paris. ``` -------------------------------- ### DevTaskLoader Initialization Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-client.md Example of creating a DevTaskLoader with tasks and resources. ```python from agentflow import DevTaskLoader, LLM, PromptTemplate # Define resources resources = { 'main_llm': LLM( endpoint="http://localhost:8080", model="llama3", sampling_parameters={'temperature': 0.7, 'max_tokens': 100} ), 'system_prompt': PromptTemplate( template="You are a helpful assistant.", engine='f-string' ) } # Create task loader loader = DevTaskLoader( tasks=[ "What is the capital of France?", "Explain quantum computing" ], resources=resources ) ``` -------------------------------- ### PromptTemplate Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/types.md An example of how to instantiate a PromptTemplate resource. ```python from agentflow import PromptTemplate prompt = PromptTemplate( template="You are a helpful assistant. Answer the following question: {question}", engine="f-string" ) ``` -------------------------------- ### training_rollout Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-litagent.md Example implementation of the training_rollout method. ```python def training_rollout(self, task, rollout_id, resources): # Access resources main_llm = resources['main_llm'] prompt_template = resources['system_prompt'] # Process task question = task.get('question', '') # Generate response using LLM response = self._generate_response(question, main_llm) # Compute reward reward = self._evaluate_response(response, task.get('expected')) return reward ``` -------------------------------- ### Executor Initialization Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-models.md Example of how to initialize an Executor instance. ```python from agentflow.agentflow.models.executor import Executor executor = Executor( llm_engine_name="gpt-4o", max_time=120, root_cache_dir="/tmp/cache" ) ``` -------------------------------- ### Production Deployment (Server) Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Code for starting the AgentFlow server and running an agent fleet using AgentFlowClient and Trainer. ```python from agentflow import AgentFlowServer server = AgentFlowServer( host="0.0.0.0", port=8000, log_level="info", task_timeout=600 ) # In separate process or container server.run() # Or add tasks programmatically: # import asyncio # task_id = asyncio.run(server.add_task("my_question", mode="train")) ``` ```python from agentflow import AgentFlowClient, Trainer # Connect to server client = AgentFlowClient( endpoint="http://localhost:8000", poll_interval=2.0 ) # Create trainer with multiple workers trainer = Trainer( n_workers=4, max_tasks=1000, daemon=True ) # Run agents trainer.fit(MyAgent(), backend=client) ``` -------------------------------- ### VLLM Serving Script Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/benchmark.md Automated script to serve models with VLLM. ```bash bash scripts/serve_vllm.sh ``` -------------------------------- ### Planner analyze_query Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-models.md Example of analyzing a query. ```python analysis = planner.analyze_query( question="Search for information about climate change and summarize it", image=None ) ``` -------------------------------- ### DashScope Alibaba Qwen Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of creating a DashScope (Alibaba Qwen) engine. ```python qwen = create_llm_engine( model_string="dashscope-qwen", temperature=0.7 ) ``` -------------------------------- ### rollouts Property Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-client.md Example of iterating through rollouts after processing tasks. ```python # After processing tasks... for rollout in loader.rollouts: print(f"Rollout {rollout.rollout_id}: reward={rollout.final_reward}") ``` -------------------------------- ### LLM Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/types.md An example of how to instantiate an LLM resource. ```python from agentflow import LLM llm = LLM( endpoint="http://localhost:8000/v1", model="gpt-4o", sampling_parameters={ "temperature": 0.7, "max_tokens": 2000, "top_p": 0.9 } ) ``` -------------------------------- ### Trainer Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-trainer-server.md Example of how to use the Trainer class with a custom LitAgent. ```python from agentflow import Trainer, LitAgent class MyAgent(LitAgent): def training_rollout(self, task, rollout_id, resources): # Implement your agent logic return 0.9 # return reward trainer = Trainer( n_workers=4, max_tasks=100, daemon=True ) agent = MyAgent() trainer.fit(agent, backend="http://localhost:8000") ``` -------------------------------- ### Post Rollout Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-client.md Posts a completed rollout to the server synchronously. ```python from agentflow import Rollout rollout = Rollout( rollout_id="task_123", final_reward=0.85, logs=["Step 1 completed", "Step 2 completed"] ) response = client.post_rollout(rollout) ``` -------------------------------- ### execute_tool_command Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-models.md Example of calling the execute_tool_command method. ```python result = executor.execute_tool_command( tool_name="Web_Search_Tool", command="execution = tool.execute(query='Python loops')" ) ``` -------------------------------- ### Define Models for Benchmarking Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/benchmark.md Specifying models and their configurations for benchmarking. ```bash MODELS=( "8000:vllm-AgentFlow/agentflow-planner-7b,AgentFlow-7B,Base_Generator_Tool|Python_Coder_Tool|Google_Search_Tool|Wikipedia_Search_Tool,dashscope-qwen2.5-7b-instruct|dashscope-qwen2.5-7b-instruct|Default|Default" ) ``` -------------------------------- ### generate_tool_command Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-models.md Example of calling the generate_tool_command method. ```python tool_cmd = executor.generate_tool_command( question="Find Python documentation", image=None, context="", sub_goal="Search for Python loops", tool_name="Web_Search_Tool", tool_metadata={"description": "Search the web"}, step_count=1 ) ``` -------------------------------- ### DeepSeek Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of creating a DeepSeek engine. ```python deepseek = create_llm_engine(model_string="deepseek-chat") ``` -------------------------------- ### AgentFlowServer add_task Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-trainer-server.md Example of adding a task to the server queue. ```python rollout_id = await server.add_task( sample="What is 2+2?", mode="train", resources_id="v1_0", metadata={"source": "benchmark"} ) print(f"Task added: {rollout_id}") ``` -------------------------------- ### Constructor Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-litagent.md Example of subclassing LitAgent and initializing the base class. ```python from agentflow import LitAgent class MyCustomAgent(LitAgent): def __init__(self): super().__init__(trained_agents="base_agent_v1") def training_rollout(self, task, rollout_id, resources): # Your training logic return reward_value def validation_rollout(self, task, rollout_id, resources): # Your validation logic return reward_value ``` -------------------------------- ### Engine Call Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Demonstrates the equivalence between calling generate() and the instance directly. ```python engine = create_llm_engine("gpt-4o") # Both are equivalent response1 = engine.generate("What is AI?") response2 = engine("What is AI?") ``` -------------------------------- ### Rollout Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/types.md Example usage of the Rollout class. ```python from agentflow import Rollout, Triplet rollout = Rollout( rollout_id="rollout-12345", final_reward=0.85, triplets=[ Triplet(prompt="Q1", response="A1", reward=0.9), Triplet(prompt="Q2", response="A2", reward=0.8), ], logs=["Step 1 completed", "Step 2 completed"], metadata={"duration_seconds": 2.5} ) ``` -------------------------------- ### Resource Versioning Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/INDEX.md An example demonstrating how resources (like LLM and PromptTemplate) are versioned in the AgentFlow server. ```python resources_v1 = { 'main_llm': LLM(...), 'system_prompt': PromptTemplate(...) } resources_v2 = { # Different model/prompt 'main_llm': LLM(...), # Updated 'system_prompt': PromptTemplate(...) # Different } ``` -------------------------------- ### Planner generate_next_step Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-models.md Example of generating the next step and extracting context, sub-goal, and tool. ```python from agentflow.agentflow.models.memory import Memory memory = Memory() next_step = planner.generate_next_step( question="What is AI?", image=None, query_analysis="Need to explain AI concept", memory=memory, step_count=1, max_step_count=5 ) context, sub_goal, tool = planner.extract_context_subgoal_and_tool(next_step) ``` -------------------------------- ### OpenAI Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of creating an OpenAI GPT engine with various parameters. ```python gpt = create_llm_engine( model_string="gpt-4o", temperature=0.7, top_p=0.9, frequency_penalty=0.5, presence_penalty=0.5 ) ``` -------------------------------- ### Google Gemini Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of creating a Google Gemini engine. ```python gemini = create_llm_engine( model_string="gemini-2.0-flash", temperature=0.7, top_p=0.9 ) ``` -------------------------------- ### Azure OpenAI Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of creating an Azure OpenAI engine. ```python azure = create_llm_engine( model_string="azure-gpt-4o", temperature=0.7, frequency_penalty=0.5 ) ``` -------------------------------- ### run Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-trainer-server.md Start the FastAPI server. This is a blocking call. ```python def run(self) -> None: pass ``` ```python server = AgentFlowServer(port=8000) server.run() # Blocks until KeyboardInterrupt ``` -------------------------------- ### Complete Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-litagent.md A complete example demonstrating how to define a simple question-answering agent using LitAgent, configure resources with an LLM and prompt template, and run training using the Trainer and DevTaskLoader. ```python from agentflow import \ LitAgent, Trainer, DevTaskLoader, \ LLM, PromptTemplate, ResourcesUpdate class SimpleQAAgent(LitAgent): def __init__(self): super().__init__(trained_agents="simple_qa_v1") def training_rollout(self, task, rollout_id, resources): # Extract question from task question = task.get('question') or task # Get LLM resource llm_config = resources['main_llm'] # Simple response generation response = f"Answer to: {question}" # Evaluate against expected answer expected = task.get('expected_answer', '') reward = 1.0 if expected.lower() in response.lower() else 0.5 return reward def validation_rollout(self, task, rollout_id, resources): return self.training_rollout(task, rollout_id, resources) def test_rollout(self, task, rollout_id, resources): return self.training_rollout(task, rollout_id, resources) # Create agent and trainer agent = SimpleQAAgent() trainer = Trainer(n_workers=2, max_tasks=10) # Create local task loader for development resources = ResourcesUpdate( resources_id="dev_v1", resources={ 'main_llm': LLM( endpoint="http://localhost:8000", model="llama2", sampling_parameters={'temperature': 0.7} ), 'system_prompt': PromptTemplate( template="Answer the following question: {query}", engine='f-string' ) } ) tasks = [ "What is Python?", "Explain machine learning", "What is AI?" ] loader = DevTaskLoader(tasks=tasks, resources=resources) # Run training trainer.fit(agent, backend=loader) # Collect results for rollout in loader.rollouts: print(f"Rollout {rollout.rollout_id}: reward={rollout.final_reward}") ``` -------------------------------- ### Constructor Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-client.md Initializes an AgentFlowClient with a server endpoint. ```python from agentflow import AgentFlowClient client = AgentFlowClient( endpoint="http://localhost:8000", poll_interval=5.0, timeout=20.0 ) ``` -------------------------------- ### Planner generate_base_response Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-models.md Example of generating a base response. ```python response = planner.generate_base_response( question="What is machine learning?", image=None ) ``` -------------------------------- ### NamedResources Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/types.md An example demonstrating the creation of a NamedResources dictionary with different resource types. ```python from agentflow import NamedResources, LLM, PromptTemplate resources: NamedResources = { 'main_llm': LLM( endpoint="http://localhost:8080", model="llama3", sampling_parameters={'temperature': 0.7} ), 'system_prompt': PromptTemplate( template="You are a helpful assistant.", engine='f-string' ), 'backup_llm': LLM( endpoint="http://api.openai.com/v1", model="gpt-4o", sampling_parameters={'temperature': 0.5} ) } ``` -------------------------------- ### Logging Configuration Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-utilities.md Example of configuring Python logging for AgentFlow with different levels. ```python import logging from agentflow import configure_logger # Configure for debug logging logger = configure_logger(level=logging.DEBUG) logger.info("Starting AgentFlow") logger.debug("Detailed debug info") # Configure for production logger = configure_logger(level=logging.WARNING) ``` -------------------------------- ### Planner Initialization Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/configuration.md Example of initializing a Planner object. ```python from agentflow.agentflow.models.planner import Planner planner = Planner( llm_engine_name: str, llm_engine_fixed_name: str = "gpt-4o", toolbox_metadata: dict = None, available_tools: List = None, verbose: bool = False, base_url: str = None, is_multimodal: bool = False, check_model: bool = True, temperature: float = 0.0 ) ``` -------------------------------- ### Caching Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of enabling and using disk-based caching for an engine. ```python engine = create_llm_engine( "gpt-4o", use_cache=True ) # First call generates and caches response1 = engine("What is Python?") # Second call retrieves from cache response2 = engine("What is Python?") # Instant ``` -------------------------------- ### tracer Property Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-litagent.md Example of accessing the tracer instance to get spans. ```python spans = self.tracer.get_spans() ``` -------------------------------- ### Launch the vLLM Server Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Command to run the serving script and start the vLLM server. ```bash bash scripts/serve_vllm_qwen.sh ``` -------------------------------- ### Error Handling Example - Missing API Key Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example demonstrating error handling for a missing API key when creating an engine. ```python from agentflow.agentflow.engine.factory import create_llm_engine try: # Missing required API key engine = create_llm_engine("claude-3-opus") except ValueError as e: print(f"Setup error: {e}") # Error: Please set the ANTHROPIC_API_KEY environment variable ``` -------------------------------- ### Set Up Resources Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Configures resources for the agent, including an LLM engine and a system prompt template. ```python from agentflow import LLM, PromptTemplate, ResourcesUpdate resources = ResourcesUpdate( resources_id="v1_0", resources={ 'main_llm': LLM( endpoint="http://localhost:8000", model="gpt-4o", sampling_parameters={'temperature': 0.7, 'max_tokens': 2000} ), 'system_prompt': PromptTemplate( template="You are a helpful assistant. Answer: {question}", engine='f-string' ) } ) ``` -------------------------------- ### test_rollout Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-litagent.md Example implementation of the test_rollout method, typically using greedy decoding. ```python def test_rollout(self, task, rollout_id, resources): # Use greedy decoding for best performance return self.training_rollout(task, rollout_id, resources) ``` -------------------------------- ### Together AI Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of creating a Together AI engine, including setting the API key. ```python import os os.environ["TOGETHER_API_KEY"] = "..." together = create_llm_engine( model_string="together-llama2-70b" ) ``` -------------------------------- ### Engine Factory Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example usage of the `create_llm_engine` factory function for different LLM providers. ```python from agentflow.agentflow.engine.factory import create_llm_engine # OpenAI gpt4 = create_llm_engine("gpt-4o", temperature=0.7) # Claude claude = create_llm_engine("claude-3-opus", temperature=0.5) # Local Ollama ollama = create_llm_engine("ollama-llama2", temperature=0.7) # Specify custom base URL custom = create_llm_engine( "gpt-4o", base_url="http://localhost:8000/v1", temperature=0.7 ) ``` -------------------------------- ### Make Serving Script Executable Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Command to make the serving script executable. ```bash chmod +x scripts/serve_vllm_qwen.sh ``` -------------------------------- ### Get Latest Resources Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-client.md Fetches the latest available resources from the server synchronously. ```python latest_resources = client.get_latest_resources() if latest_resources: print(f"Resources version: {latest_resources.resources_id}") ``` -------------------------------- ### Python_Coder_Tool Constructor Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-tools.md Initialize the Python coder tool. ```python from agentflow.agentflow.tools.python_coder.tool import Python_Coder_Tool coder = Python_Coder_Tool(model_string="gpt-4o") ``` -------------------------------- ### retrieve_completed_rollouts (ServerDataStore) Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-trainer-server.md Get all completed rollouts and clear store. ```python async def retrieve_completed_rollouts(self) -> List[Rollout]: pass ``` -------------------------------- ### Get Resources By ID Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-client.md Fetches a specific version of resources by its ID synchronously. Results are cached to avoid redundant requests. ```python resources = client.get_resources_by_id("resource_v2_123") if resources: llm = resources.resources.get('main_llm') prompt = resources.resources.get('system_prompt') ``` -------------------------------- ### DashScope (Qwen) Engine Creation Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/configuration.md Example of creating an LLM engine for DashScope (Qwen) models. ```python engine = create_llm_engine( model_string="dashscope-qwen", temperature=0.7 ) ``` -------------------------------- ### Custom Tool Implementation Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Shows how to create a custom tool by inheriting from BaseTool and implementing the execute method, and how to use it. ```python from agentflow.agentflow.tools.base import BaseTool class MyCustomTool(BaseTool): require_llm_engine = False def __init__(self): super().__init__( tool_name="My_Custom_Tool", tool_description="Does something specific", tool_version="1.0.0", input_types={"query": "str - Input query"}, output_type="str - Output result", demo_commands=[ { "command": 'execution = tool.execute(query="example")', "description": "Example usage" } ] ) def execute(self, query: str) -> str: """Execute the tool.""" return f"Processed: {query}" # Use in agent tool = MyCustomTool() result = tool.execute(query="test") ``` -------------------------------- ### Select Tasks for Benchmarking Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/benchmark.md Enabling or disabling specific benchmarks by commenting/uncommenting them in the TASKS array. ```bash TASKS=( "aime24" "gameof24" "bamboogle" # "gpqa" ) ``` -------------------------------- ### Production Deployment Workflow Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/README.md Example of setting up AgentFlowServer for production and connecting with AgentFlowClient. ```python server = AgentFlowServer(port=8000) server.run() # In separate process client = AgentFlowClient("http://localhost:8000") trainer = Trainer(n_workers=4) trainer.fit(MyAgent(), backend=client) ``` -------------------------------- ### Quick Test with curl Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Example curl command to test the vLLM server for chat completions. ```bash curl http://localhost:8001/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "Qwen/Qwen2.5-7B-Instruct", "messages": [ {"role": "user", "content": "What is the capital of France?"} ], "temperature": 0.7 }' ``` -------------------------------- ### LiteLLM (Proxy) Engine Creation Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/configuration.md Example of creating an LLM engine using LiteLLM as a proxy. ```python engine = create_llm_engine( model_string="litellm-gpt-4o", temperature=0.7 ) ``` -------------------------------- ### Server Setup with Logging Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-utilities.md Shows how to configure logging for the AgentFlow server and set up a basic server instance. ```python import logging from agentflow import AgentFlowServer, configure_logger # Configure logging for server logger = configure_logger(level=logging.INFO, name="agentflow.server") # Create and run server server = AgentFlowServer( host="0.0.0.0", port=8000, log_level="info", task_timeout=600 ) logger.info("Starting AgentFlow server on http://0.0.0.0:8000") server.run() # Blocks ``` -------------------------------- ### poll_next_task Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-client.md Example of polling the next task. ```python task = loader.poll_next_task() print(f"Task ID: {task.rollout_id}") print(f"Task input: {task.input}") ``` -------------------------------- ### Setup Environment Variables Source: https://github.com/lupantech/agentflow/blob/main/README.md Commands to copy the environment template and instructions on which variables to set. ```bash cp agentflow/.env.template agentflow/.env # Then edit agentflow/.env with your API keys ``` -------------------------------- ### GenericResponse Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/types.md An example of creating a GenericResponse object. ```python from agentflow import GenericResponse response = GenericResponse( status="success", message="Task completed successfully", data={"rollout_id": "rollout-123", "reward": 0.95} ) ``` -------------------------------- ### ResourcesUpdate Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/types.md An example of how to create a ResourcesUpdate object. ```python from agentflow import ResourcesUpdate, LLM, PromptTemplate resources_update = ResourcesUpdate( resources_id="v2_0_20240101", resources={ 'main_llm': LLM( endpoint="http://localhost:8000", model="llama3-70b", sampling_parameters={'temperature': 0.5} ), 'system_prompt': PromptTemplate( template="You are an expert...", engine='jinja' ) } ) ``` -------------------------------- ### Serving Script for Qwen Model Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md This script launches Qwen-2.5-7B-Instruct using vLLM in a tmux window, specifying GPU, tensor-parallel-size, and port. ```bash #!/bin/bash # =========================================================================== # Script: serve_vllm_qwen.sh # Description: # Launch Qwen-2.5-7B-Instruct using vLLM in a tmux window # - Uses GPU 0 # - tensor-parallel-size=1 # - Port 8001 (different from planner model on port 8000) # =========================================================================== MODEL="Qwen/Qwen2.5-7B-Instruct" GPU="0" PORT=8001 TMUX_SESSION="vllm_qwen" TP=1 VENV_ACTIVATE="source .venv/bin/activate" echo "Launching model: $MODEL" echo " Port: $PORT" echo " GPU: $GPU" echo " Tensor Parallel Size: $TP" # Create tmux session and run vLLM tmux new-session -d -s "$TMUX_SESSION" CMD_START=" $VENV_ACTIVATE; export CUDA_VISIBLE_DEVICES=$GPU; echo '--- Starting $MODEL on port $PORT with TP=$TP --- '; echo 'CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES'; echo 'Current virtual env: $(python -c "import sys; print(sys.prefix)")'; vllm serve \"$MODEL\" \ --host 0.0.0.0 \ --port $PORT \ --tensor-parallel-size $TP " tmux send-keys -t "${TMUX_SESSION}:0" "$CMD_START" C-m echo "" echo "=== Model launched in tmux session: '$TMUX_SESSION'" echo "=== View logs: tmux attach-session -t $TMUX_SESSION" echo "=== Detach: Ctrl+B, then D" echo "=== Kill session: tmux kill-session -t $TMUX_SESSION" ``` -------------------------------- ### Monitoring Task Results with Rollouts and Triplets Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Demonstrates how an agent can return rich data, including Triplets, for monitoring and how a server retrieves completed rollouts. ```python from agentflow import Rollout class MonitoringAgent(LitAgent): def training_rollout(self, task, rollout_id, resources): result = self.process(task) reward = 1.0 if result else 0.5 # Return rich data from agentflow import Triplet triplets = [ Triplet( prompt=task, response=result, reward=reward, metadata={"step": 1} ) ] return triplets # Framework converts to Rollout # Server retrieves results rollouts = asyncio.run(server.retrieve_completed_rollouts()) for rollout in rollouts: print(f"Rollout {rollout.rollout_id}:") print(f" Reward: {rollout.final_reward}") print(f" Triplets: {len(rollout.triplets or [])}") print(f" Logs: {rollout.logs}") ``` -------------------------------- ### Using Multiple GPUs with Tensor Parallelism Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Configuration for using multiple GPUs with tensor parallelism in the serve_vllm_qwen.sh script. ```bash # In serve_vllm_qwen.sh GPU="0,1" # Use GPU 0 and 1 TP=2 # Tensor parallel size ``` -------------------------------- ### Model Download Fails Solution Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Environment variables to set for HuggingFace cache and token when model download fails. ```bash export HF_HOME=/path/to/cache export HF_TOKEN=your_huggingface_token # If accessing gated models ``` -------------------------------- ### Reference Cheat Sheet: Common Patterns Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Illustrates a common pattern for checking if resources are available and accessing them, such as an LLM. ```python # Check if task is available if resources: llm = resources['main_llm'] ``` -------------------------------- ### Task Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/types.md Example usage of the Task class. ```python from agentflow import Task task = Task( rollout_id="rollout-abc123", input={"question": "What is Python?", "expected": "A programming language"}, mode="train", resources_id="v1_0", create_time=1234567890.0, metadata={"source": "dataset_v2"} ) ``` -------------------------------- ### trainer Property Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-litagent.md Example of accessing the trainer instance. ```python resources = self.trainer.client().get_latest_resources() ``` -------------------------------- ### Triplet Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/types.md Example usage of the Triplet class. ```python from agentflow import Triplet triplet = Triplet( prompt="What is AI?", response="AI is artificial intelligence...", reward=0.95, metadata={"step": 1, "model": "gpt-4o"} ) ``` -------------------------------- ### Configure AgentFlow to Use Local vLLM (Code Method) Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Example of modifying agentflow/agentflow/models/planner.py to use the local vLLM server. ```python self.llm_engine_fixed = create_llm_engine( model_string="vllm-Qwen/Qwen2.5-7B-Instruct", base_url="http://localhost:8001/v1/", is_multimodal=False, temperature=temperature ) ``` -------------------------------- ### Import Examples Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/00-MANIFEST.txt Demonstrates common import statements for various AgentFlow components. ```python from agentflow import LitAgent from agentflow import Trainer from agentflow import AgentFlowServer, AgentFlowClient, DevTaskLoader from agentflow import Planner, Executor, Verifier, Solver, Memory from agentflow import Task, Rollout, Triplet, LLM, PromptTemplate from agentflow import reward, configure_logger from agentflow.agentflow.engine.factory import create_llm_engine from agentflow.agentflow.tools.base_generator.tool import Base_Generator_Tool ``` -------------------------------- ### Troubleshooting: Port Already in Use Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Solution for the 'Address already in use' error by changing the port in the serving script. ```bash PORT=8002 # Use a different port ``` -------------------------------- ### Load Model Checkpoint with Transformers Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/logs.md Example of how to load a model checkpoint directly using the transformers library. ```python transformers.from_pretrained("checkpoints/{PROJECT}/{EXPERIMENT}/global_step_X/actor/huggingface/") ``` -------------------------------- ### Create Your Agent Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/QUICK_START.md Defines a custom agent 'MyAgent' inheriting from LitAgent, including methods for reward computation and different rollout modes (training, validation, test). ```python class MyAgent(LitAgent): def __init__(self): super().__init__(trained_agents="my_agent_v1") @reward def compute_reward(self, prediction, expected): """Simple accuracy-based reward.""" return 1.0 if prediction == expected else 0.0 def training_rollout(self, task, rollout_id, resources): """Training mode: may use sampling.""" # Extract task components question = task.get('question') or str(task) expected = task.get('expected', '') # Get LLM from resources llm_config = resources.get('main_llm') # Generate response (implement your logic) response = f"Answer to: {question}" # Compute reward reward = self.compute_reward(response, expected) return reward def validation_rollout(self, task, rollout_id, resources): """Validation mode: typically same as training.""" return self.training_rollout(task, rollout_id, resources) def test_rollout(self, task, rollout_id, resources): """Test mode: use best settings (greedy).""" return self.training_rollout(task, rollout_id, resources) ``` -------------------------------- ### Configure AgentFlow Test Script Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Modifications needed in agentflow/scripts/test_vllm.py to point to the local vLLM server. ```python # In agentflow/scripts/test_vllm.py, modify lines 7-8: llm = ChatVLLM( model_string="Qwen/Qwen2.5-7B-Instruct", # Replace YOUR_LOCAL_MODEL_NAME base_url="http://localhost:8001/v1/", # Replace YOUR_PORT with 8001 use_cache=False, system_prompt="You are a helpful AI assistant." ) ``` -------------------------------- ### Trainer init Method Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-trainer-server.md Initializes the trainer and its internal client. ```python def init(self, backend: Union[str, AgentFlowClient]) -> None: ``` -------------------------------- ### validation_rollout Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-litagent.md Example implementation of the validation_rollout method, often similar to training_rollout. ```python def validation_rollout(self, task, rollout_id, resources): # Typically similar to training but may use different sampling return self.training_rollout(task, rollout_id, resources) ``` -------------------------------- ### Run AgentFlow Test Script Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/serve_vllm_local.md Command to run the test script after configuring it for the local vLLM server. ```bash cd agentflow python scripts/test_vllm.py ``` -------------------------------- ### Check LLM engine setup Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/INDEX.md Creates an LLM engine and tests a basic response. ```python from agentflow.agentflow.engine.factory import create_llm_engine engine = create_llm_engine("gpt-4o") test_response = engine("Hello") ``` -------------------------------- ### Reward Decorator Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-utilities.md Example usage of the @reward decorator for synchronous reward computation. ```python from agentflow import reward @reward def compute_reward(prediction, expected): """Compute task reward.""" if prediction == expected: return 1.0 elif similarity(prediction, expected) > 0.8: return 0.7 else: return 0.0 # Usage in agent reward_value = compute_reward(prediction="answer", expected="answer") print(reward_value) # 1.0 ``` -------------------------------- ### LiteLLM Engine Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of creating an LLM engine using the LiteLLM model string. ```python litellm = create_llm_engine( model_string="litellm-gpt-4o", temperature=0.7 ) ``` -------------------------------- ### Rollout Data Directory Structure Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/logs.md Illustrates the directory structure for saving rollout trajectories during training. ```bash rollout_data/ โ””โ”€โ”€ {PUBLIC_IP}/ โ””โ”€โ”€ {EXPERIMENT_NAME}_{TIMESTAMP}/ # e.g., rollout_all_7B_{time_stamp} โ”œโ”€โ”€ .init.lock โ”œโ”€โ”€ .run_info โ””โ”€โ”€ {MODEL_NAME}_{TIMESTAMP}/ # e.g., Qwen2.5-7B-Instruct_{time_stamp} โ”œโ”€โ”€ train/ # Training rollouts (usually empty to save space) โ””โ”€โ”€ validation/ โ”œโ”€โ”€ .val.lock โ””โ”€โ”€ step_0/ # Validation at global step 0 โ”œโ”€โ”€ idx_0/ # Individual validation samples โ”‚ โ””โ”€โ”€ rollout_{uuid}.json โ”œโ”€โ”€ idx_1/ โ””โ”€โ”€ ... ``` -------------------------------- ### Run a Specific Benchmark Source: https://github.com/lupantech/agentflow/blob/main/assets/doc/benchmark.md Command to navigate to the test directory and run a specific benchmark, e.g., Bamboogle. ```bash cd test bash bamboogle/run.sh ``` -------------------------------- ### Async Reward Decorator Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/agentflow-utilities.md Example usage of the @reward decorator for asynchronous reward computation. ```python @reward async def compute_reward_async(prediction, expected): """Async reward computation.""" score = await evaluate(prediction, expected) return float(score) # Usage reward_value = await compute_reward_async(pred, exp) ``` -------------------------------- ### Anthropic Claude Example Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example of creating an Anthropic Claude engine, including setting the API key. ```python import os os.environ["ANTHROPIC_API_KEY"] = "sk-ant-" claude = create_llm_engine( model_string="claude-3-opus", temperature=0.7, top_k=50 ) ``` -------------------------------- ### Error Handling Example - Unsupported Model Source: https://github.com/lupantech/agentflow/blob/main/_autodocs/engines.md Example demonstrating error handling for an unsupported model when creating an engine. ```python from agentflow.agentflow.engine.factory import create_llm_engine try: # Unsupported model engine = create_llm_engine("unknown-model-xyz") except ValueError as e: print(f"Unsupported model: {e}") # Error: Engine unknown-model-xyz not supported ```