精品欧美一区二区三区在线观看 _久久久久国色av免费观看性色_国产精品久久在线观看_亚洲第一综合网站_91精品又粗又猛又爽_小泽玛利亚一区二区免费_91亚洲精品国偷拍自产在线观看 _久久精品视频在线播放_美女精品久久久_欧美日韩国产成人在线

代理式AI的五級式綜合教程:從基礎快速響應到全自主代碼生成與執行 原創

發布于 2025-5-15 08:26
瀏覽
0收藏

在本教程中,我們將講解代理式架構的五個級別,從最簡單的語言模型調用到完全自主的代碼生成和執行系統。本教程專為在Google Colab上無縫運行而設計。從一個簡單的“處理器”開始(僅回顯模型輸出),你將逐步構建路由邏輯、集成外部工具、編排多步驟工作流,并最終使模型能夠規劃、驗證、優化并執行自己的Python代碼。在每個部分中,你都會找到詳細的解釋、自包含的演示函數以及清晰的提示,展示如何在實際AI應用中平衡人工控制與機器自治。

import os
import torch
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
import re
import json
import time
import random
from IPython.display import clear_output

我們導入核心Python和第三方庫,包括用于環境和執行控制的os和time,以及Hugging Face的Transformers(pipeline、AutoTokenizer、AutoModelForCausalLM)用于模型加載和推理。此外,我們使用re和json解析LLM輸出、隨機種子和模擬數據,同時利用clear_output保持整潔的Colab界面。

MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
def get_model_and_tokenizer():
 if not hasattr(get_model_and_tokenizer, "model"):
 print(f"Loading model {MODEL_NAME}...")
 tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 model = AutoModelForCausalLM.from_pretrained(
 MODEL_NAME,
 torch_dtype=torch.float16,
 device_map="auto",
 low_cpu_mem_usage=True
 )
 get_model_and_tokenizer.model = model
 get_model_and_tokenizer.tokenizer = tokenizer
 print("Model loaded successfully!")

return get_model_and_tokenizer.model, get_model_and_tokenizer.tokenizer

我們定義了一個變量MODEL_NAME指向TinyLlama 1.1B聊天模型,并實現了一個懶加載輔助函數get_model_and_tokenizer(),該函數僅在首次調用時下載并初始化分詞器和模型,將其緩存以最小化開銷,并在后續調用中返回緩存實例。

def get_model_and_tokenizer():
 if not hasattr(get_model_and_tokenizer, "model"):
 print(f"Loading model {MODEL_NAME}...")
 tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 model = AutoModelForCausalLM.from_pretrained(
 MODEL_NAME,
 torch_dtype=torch.float16,
 device_map="auto",
 low_cpu_mem_usage=True
 )
 get_model_and_tokenizer.model = model
 get_model_and_tokenizer.tokenizer = tokenizer
 print("Model loaded successfully!")

return get_model_and_tokenizer.model, get_model_and_tokenizer.tokenizer

這個輔助函數為TinyLlama模型及其標記器實現了懶加載模式。在第一次調用時,它下載并初始化半精度和自動設備排布,將它們作為函數對象的屬性緩存,在后續調用時,則直接返回已經加載的實例以避免冗余開銷。

def generate_text(prompt, max_length=512):
 model, tokenizer = get_model_and_tokenizer()

 messages = [{"role": "user", "content": prompt}]
 formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False)

 inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)

 with torch.no_grad():
 output = model.generate(
 **inputs,
 max_new_tokens=max_length,
 do_sample=True,
 temperature=0.7,
 top_p=0.9,
 )

 generated_text = tokenizer.decode(output[0], skip_special_tokens=True)

 response = generated_text.split("ASSISTANT: ")[-1].strip()
return response

generate_text函數封裝了TinyLlama推理流程:它獲取緩存的模型和分詞器,將用戶提示格式化為聊天模板,對輸入進行分詞并移動到模型設備,然后通過采樣參數生成響應。生成完成后,它解碼輸出并通過“ASSISTANT: ”標記提取助手的回答。

第一級:簡單處理器

在最簡單的級別,代碼定義了一個直接的文本生成管道,將模型純粹視為語言處理器。當用戶提供提示詞時,simple_processor函數調用generate_text輔助函數生成自由形式的響應,并直接顯示該響應。此級別展示了最基本的交互模式:接收輸入、生成輸出,程序流程完全由人工控制。

def simple_processor(prompt):
 """Level 1: Simple Processor - Model has no impact on program flow"""
 response = generate_text(prompt)
 return response


def demo_level1():
 print("\n" + "="*50)
 print("LEVEL 1: SIMPLE PROCESSOR DEMO")
 print("="*50)
 print("At this level, the AI has no control over program flow.")
 print("It simply takes input and produces output.\n")

 user_input = input("Enter your question or prompt: ") or "Write a short poem about artificial intelligence."
 print("\nProcessing your request...\n")

 output = simple_processor(user_input)
 print("OUTPUT:")
 print("-"*50)
 print(output)
print("-"*50)

simple_processor函數體現了智能體層次結構中的簡單處理器,將模型純粹作為文本生成器;它接受用戶提供的提示并委托給generate_text。它返回模型生成的內容,無任何分支或決策邏輯。伴隨的demo_level1例程提供了一個最小的交互循環,打印清晰的標題,請求用戶輸入(有合理的默認值),調用simple_processor,然后顯示原始輸出,展示最基本的提示到響應的工作流程,其中AI對程序流程沒有影響。

第二級:路由機制

第二級引入基于模型分類的條件路由。router_agent函數首先要求模型將查詢分類為“技術性”、“創造性”或“事實性”,然后規范化該分類并將其分配到專門的處理函數(handle_technical_query、handle_creative_query或handle_factual_query)。此路由機制使模型能夠部分控制程序流程,指導后續交互路徑。

def router_agent(user_query):
 """Level 2: Router - Model determines basic program flow"""

 category_prompt = f"""Classify the following query into one of these categories:
 'technical', 'creative', or 'factual'.

 Query: {user_query}

 Return ONLY the category name and nothing else."""

 category_response = generate_text(category_prompt)

 category = category_response.lower()
 if "technical" in category:
 category = "technical"
 elif "creative" in category:
 category = "creative"
 else:
 category = "factual"

 print(f"Query classified as: {category}")

 if category == "technical":
 return handle_technical_query(user_query)
 elif category == "creative":
 return handle_creative_query(user_query)
 else: 
 return handle_factual_query(user_query)


def handle_technical_query(query):
 system_prompt = f"""You are a technical assistant. Provide detailed technical explanations.

 User query: {query}"""

 response = generate_text(system_prompt)
 return f"[Technical Response]\n{response}"


def handle_creative_query(query):
 system_prompt = f"""You are a creative assistant. Be imaginative and inspiring.

 User query: {query}"""

 response = generate_text(system_prompt)
 return f"[Creative Response]\n{response}"


def handle_factual_query(query):
 system_prompt = f"""You are a factual assistant. Provide accurate information concisely.

 User query: {query}"""

 response = generate_text(system_prompt)
 return f"[Factual Response]\n{response}"


def demo_level2():
 print("\n" + "="*50)
 print("LEVEL 2: ROUTER DEMO")
 print("="*50)
 print("At this level, the AI determines basic program flow.")
 print("It decides which processing path to take.\n")

 user_query = input("Enter your question or prompt: ") or "How do neural networks work?"
 print("\nProcessing your request...\n")

 result = router_agent(user_query)
 print("OUTPUT:")
 print("-"*50)
 print(result)
print("-"*50)

router_agent函數通過首先要求模型將用戶的查詢分類為“技術性”、“創造性”或“事實性”,然后規范化該分類并將其分配給相應的處理程序(handle_technical_query、handle_creative_query或handle_factual_query),每個處理程序都將原始查詢包裝在適當的系統風格提示中,然后調用generate_text來實現路由器行為。demo_level2例程提供了一個清晰的CLI風格界面,打印標題,接受輸入(有默認值),調用router_agent,并顯示分類后的響應,展示了模型如何通過對處理路徑的選擇來基本控制程序流程。

第三級:工具調用

第三級通過嵌入基于JSON的函數選擇協議,賦予模型決定調用哪些外部工具的能力。tool_calling_agent函數向用戶提供一個問題及一系列潛在工具選項(如天氣查詢、信息搜索、日期時間獲取或直接響應),并指示模型返回一個指定工具及其參數的有效JSON消息。通過正則表達式提取JSON對象后,代碼會安全地回退到直接響應以防解析失敗。最后,模型整合工具結果生成連貫的答案。

def tool_calling_agent(user_query):
 """Level 3: Tool Calling - Model determines how functions are executed"""

 tool_selection_prompt = f"""Based on the user query, select the most appropriate tool from the following list:
 1. get_weather: Get the current weather for a location
 2. search_information: Search for specific information on a topic
 3. get_date_time: Get current date and time
 4. direct_response: Provide a direct response without using tools

 USER QUERY: {user_query}

 INSTRUCTIONS:
 - Return your response in valid JSON format
 - Include the tool name and any required parameters
 - For get_weather, include location parameter
 - For search_information, include query and depth parameter (basic or detailed)
 - For get_date_time, include timezone parameter (optional)
 - For direct_response, no parameters needed

 Example output format: {{"tool": "get_weather", "parameters": {{"location": "New York"}}}}"""

 tool_selection_response = generate_text(tool_selection_prompt)

 try:
 json_match = re.search(r'({.*})', tool_selection_response, re.DOTALL)
 if json_match:
 tool_selection = json.loads(json_match.group(1))
 else:
 print("Could not parse tool selection. Defaulting to direct response.")
 tool_selection = {"tool": "direct_response", "parameters": {}}
 except json.JSONDecodeError:
 print("Invalid JSON in tool selection. Defaulting to direct response.")
 tool_selection = {"tool": "direct_response", "parameters": {}}

 tool_name = tool_selection.get("tool", "direct_response")
 parameters = tool_selection.get("parameters", {})

 print(f"Selected tool: {tool_name}")

 if tool_name == "get_weather":
 location = parameters.get("location", "Unknown")
 tool_result = get_weather(location)
 elif tool_name == "search_information":
 query = parameters.get("query", user_query)
 depth = parameters.get("depth", "basic")
 tool_result = search_information(query, depth)
 elif tool_name == "get_date_time":
 timezone = parameters.get("timezone", "UTC")
 tool_result = get_date_time(timezone)
 else:
 return generate_text(f"Please provide a helpful response to: {user_query}")

 final_prompt = f"""User Query: {user_query}
 Tool Used: {tool_name}
 Tool Result: {json.dumps(tool_result)}

 Based on the user's query and the tool result above, provide a helpful response."""

 final_response = generate_text(final_prompt)
 return final_response


def get_weather(location):
 weather_conditions = ["Sunny", "Partly cloudy", "Overcast", "Light rain", "Heavy rain", "Thunderstorms", "Snowy", "Foggy"]
 temperatures = {
 "cold": list(range(-10, 10)),
 "mild": list(range(10, 25)),
 "hot": list(range(25, 40))
 }

 location_hash = sum(ord(c) for c in location)
 condition_index = location_hash % len(weather_conditions)
 season = ["winter", "spring", "summer", "fall"][location_hash % 4]

 temp_range = temperatures["cold"] if season in ["winter", "fall"] else temperatures["hot"] if season == "summer" else temperatures["mild"]
 temperature = random.choice(temp_range)

 return {
 "location": location,
 "temperature": f"{temperature}°C",
 "conditions": weather_conditions[condition_index],
 "humidity": f"{random.randint(30, 90)}%"
 }


def search_information(query, depth="basic"):
 mock_results = [
 f"First result about {query}",
 f"Second result discussing {query}",
 f"Third result analyzing {query}"
 ]

 if depth == "detailed":
 mock_results.extend([
 f"Fourth detailed analysis of {query}",
 f"Fifth comprehensive overview of {query}",
 f"Sixth academic paper on {query}"
 ])

 return {
 "query": query,
 "results": mock_results,
 "depth": depth,
 "sources": [f"source{i}.com" for i in range(1, len(mock_results) + 1)]
 }


def get_date_time(timezone="UTC"):
 current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
 return {
 "current_datetime": current_time,
 "timezone": timezone
 }


def demo_level3():
 print("\n" + "="*50)
 print("LEVEL 3: TOOL CALLING DEMO")
 print("="*50)
 print("At this level, the AI selects which tools to use and with what parameters.")
 print("It can process the results from tools to create a final response.\n")

 user_query = input("Enter your question or prompt: ") or "What's the weather like in San Francisco?"
 print("\nProcessing your request...\n")

 result = tool_calling_agent(user_query)
 print("OUTPUT:")
 print("-"*50)
 print(result)
 print("-"*50)

在第三級實現中,tool_calling_agent函數提示模型從預定義的實用程序集中選擇,例如天氣查詢、模擬網絡搜索或日期/時間檢索,通過返回一個包含選定工具名稱及其參數的JSON對象。然后,它安全地解析該JSON,調用相應的Python函數以獲得結構化數據,并進行后續模型調用,將工具的輸出整合到一個連貫的面向用戶的響應中。

第四級:多步智能體

第四級擴展了工具調用模式,形成一個多步智能體,管理其工作流和狀態。MultiStepAgent類維護用戶輸入、工具輸出和智能體操作的內部記憶。每次迭代生成一個規劃提示,總結整個記憶,要求模型選擇幾個工具之一,如網絡搜索模擬、信息提取、文本摘要或報告創建,或完成任務并生成最終輸出。執行所選工具并將結果附加回記憶后,重復該過程,直到模型發出“完成”動作或達到最大步驟數。最后,智能體將記憶整理成一個連貫的最終響應。這種結構展示了LLM如何協調復雜的多階段過程,同時咨詢外部函數并根據先前結果細化其計劃。

class MultiStepAgent:
 """Level 4: Multi-Step Agent - Model controls iteration and program continuation"""

 def __init__(self):
 self.tools = {
 "search_web": self.search_web,
 "extract_info": self.extract_info,
 "summarize_text": self.summarize_text,
 "create_report": self.create_report
 }
 self.memory = []
 self.max_steps = 5

 def run(self, user_task):
 self.memory.append({"role": "user", "content": user_task})

 steps_taken = 0
 while steps_taken < self.max_steps:
 next_action = self.determine_next_action()

 if next_action["action"] == "complete":
 return next_action["output"]

 tool_name = next_action["tool"]
 tool_args = next_action["args"]

 print(f"\n Step {steps_taken + 1}: Using tool '{tool_name}' with arguments: {tool_args}")

 tool_result = self.tools[tool_name](**tool_args)

 self.memory.append({
 "role": "tool",
 "content": json.dumps(tool_result)
 })

 steps_taken += 1

 return self.generate_final_response("Maximum steps reached. Here's what I've found so far.")

 def determine_next_action(self):
 context = "Current memory state:\n"
 for item in self.memory:
 if item["role"] == "user":
 context += f"USER INPUT: {item['content']}\n\n"
 elif item["role"] == "tool":
 context += f"TOOL RESULT: {item['content']}\n\n"

 prompt = f"""{context}

 Based on the above information, determine the next action to take.
 Choose one of the following options:
 1. search_web: Search for information (args: query)
 2. extract_info: Extract specific information from a text (args: text, target_info)
 3. summarize_text: Create a summary of text (args: text)
 4. create_report: Create a structured report (args: title, content)
 5. complete: Task is complete (include final output)

 Respond with a JSON object with the following structure:
 For tools: {{"action": "tool", "tool": "tool_name", "args": {{tool-specific arguments}}}}
 For completion: {{"action": "complete", "output": "final output text"}}

 Only return the JSON object and nothing else."""

 next_action_response = generate_text(prompt)

 try:
 json_match = re.search(r'({.*})', next_action_response, re.DOTALL)
 if json_match:
 next_action = json.loads(json_match.group(1))
 else:
 return {"action": "complete", "output": "I encountered an error in planning. Here's what I know so far: " + self.generate_final_response("Error in planning")}
 except json.JSONDecodeError:
 return {"action": "complete", "output": "I encountered an error in planning. Here's what I know so far: " + self.generate_final_response("Error in planning")}

 self.memory.append({"role": "assistant", "content": next_action_response})
 return next_action

 def generate_final_response(self, prefix=""):
 context = "Task history:\n"
 for item in self.memory:
 if item["role"] == "user":
 context += f"USER INPUT: {item['content']}\n\n"
 elif item["role"] == "tool":
 context += f"TOOL RESULT: {item['content']}\n\n"
 elif item["role"] == "assistant":
 context += f"AGENT ACTION: {item['content']}\n\n"

 prompt = f"""{context}

 {prefix} Generate a comprehensive final response that addresses the original user task."""

 final_response = generate_text(prompt)
 return final_response

 def search_web(self, query):
 time.sleep(1) 

 query_hash = sum(ord(c) for c in query)
 num_results = (query_hash % 3) + 2

 results = []
 for i in range(num_results):
 results.append(f"Result {i+1}: Information about '{query}' related to aspect {chr(97 + i)}.")

 return {
 "query": query,
 "results": results
 }

 def extract_info(self, text, target_info):
 time.sleep(0.5) 

 return {
 "extracted_info": f"Extracted information about '{target_info}' from the text: The text indicates that {target_info} is related to several key aspects mentioned in the content.",
 "confidence": round(random.uniform(0.7, 0.95), 2)
 }

 def summarize_text(self, text):
 time.sleep(0.5)

 word_count = len(text.split())

 return {
 "summary": f"Summary of the provided text ({word_count} words): The text discusses key points related to the subject matter, highlighting important aspects and providing context.",
 "original_length": word_count,
 "summary_length": round(word_count * 0.3)
 }

 def create_report(self, title, content):
 time.sleep(0.7)

 report_sections = [
 "## Introduction",
 f"This report provides an overview of {title}.",
 "",
 "## Key Findings",
 content,
 "",
 "## Conclusion",
 f"This analysis of {title} highlights several important aspects that warrant consideration."
 ]

 return {
 "report": "\n".join(report_sections),
 "word_count": len(content.split()),
 "section_count": 3
 }


def demo_level4():
 print("\n" + "="*50)
 print("LEVEL 4: MULTI-STEP AGENT DEMO")
 print("="*50)
 print("At this level, the AI manages the entire workflow, deciding which tools")
 print("to use, when to use them, and determining when the task is complete.\n")

 user_task = input("Enter a research or analysis task: ") or "Research quantum computing recent developments and create a brief report"
 print("\nProcessing your request... (this may take a minute)\n")

 agent = MultiStepAgent()
 result = agent.run(user_task)
 print("\nFINAL OUTPUT:")
 print("-"*50)
 print(result)
print("-"*50)

MultiStepAgent類維護用戶輸入和工具輸出的演變記憶,然后反復提示LLM決定其下一步行動,無論是搜索網絡、提取信息、匯總文本、創建報告還是完成任務,執行所選工具并將結果附加到記憶中,直到任務完成或達到步驟上限。在此過程中,它展示了第四級智能體如何通過讓模型控制迭代和程序繼續來協調多步驟工作流。

第五級:完全自主智能體

在最高層次上,AutonomousAgent類展示了一個閉環系統,其中模型不僅負責規劃和執行,還能生成、驗證、優化并運行新的Python代碼。記錄用戶任務后,智能體要求模型生成詳細計劃,然后提示其生成獨立的解決方案代碼,自動清除Markdown格式。隨后的驗證步驟查詢模型是否存在語法或邏輯問題;如果發現問題,則要求模型優化代碼。驗證后的代碼被包裝了沙箱實用程序,例如安全打印、捕獲輸出緩沖區和結果捕獲邏輯,并在受限制的本地環境中執行。最后,智能體合成了一份專業報告,解釋了所做的工作、完成方式和最終結果。這一級別展示了真正自主的AI系統,可以通過動態代碼創建和執行擴展其能力。

class AutonomousAgent:
 """Level 5: Fully Autonomous Agent - Model creates & executes new code"""

 def __init__(self):
 self.memory = []

 def run(self, user_task):
 self.memory.append({"role": "user", "content": user_task})

 print(" Planning solution approach...")
 planning_message = self.plan_solution(user_task)
 self.memory.append({"role": "assistant", "content": planning_message})

 print(" Generating solution code...")
 generated_code = self.generate_solution_code()
 self.memory.append({"role": "assistant", "content": f"Generated code: ```python\n{generated_code}\n```"})

 print(" Validating code...")
 validation_result = self.validate_code(generated_code)
 if not validation_result["valid"]:
 print(" Code validation found issues - refining...")
 refined_code = self.refine_code(generated_code, validation_result["issues"])
 self.memory.append({"role": "assistant", "content": f"Refined code: ```python\n{refined_code}\n```"})
 generated_code = refined_code
 else:
 print(" Code validation passed")

 try:
 print(" Executing solution...")
 execution_result = self.safe_execute_code(generated_code, user_task)
 self.memory.append({"role": "system", "content": f"Execution result: {execution_result}"})

 # Generate a final report
 print(" Creating final report...")
 final_report = self.create_final_report(execution_result)
 return final_report

 except Exception as e:
 return f"Error executing the solution: {str(e)}\n\nGenerated code was:\n```python\n{generated_code}\n```"

 def plan_solution(self, task):
 prompt = f"""Task: {task}


 You are an autonomous problem-solving agent. Create a detailed plan to solve this task.
 Include:
 1. Breaking down the task into subtasks
 2. What algorithms or approaches you'll use
 3. What data structures are needed
 4. Any external resources or libraries required
 5. Expected challenges and how to address them

 Provide a step-by-step plan.
 """

 return generate_text(prompt)

 def generate_solution_code(self):
 context = "Task and planning information:\n"
 for item in self.memory:
 if item["role"] == "user":
 context += f"USER TASK: {item['content']}\n\n"
 elif item["role"] == "assistant":
 context += f"PLANNING: {item['content']}\n\n"

 prompt = f"""{context}

 Generate clean, efficient Python code that solves this task. Include comments to explain the code.
 The code should be self-contained and able to run inside a Python script or notebook.
 Only include the Python code itself without any markdown formatting.
 """

 code = generate_text(prompt)

 code = re.sub(r'^```python\n|```$', '', code, flags=re.MULTILINE)

 return code

 def validate_code(self, code):
 prompt = f"""Code to validate:
 ```python
 {code}
 ```

 Examine the code for the following issues:
 1. Syntax errors
 2. Logic errors
 3. Inefficient implementations
 4. Security concerns
 5. Missing error handling
 6. Import statements for unavailable libraries

 If the code has any issues, describe them in detail. If the code looks good, state "No issues found."
 """

 validation_response = generate_text(prompt)

 if "no issues" in validation_response.lower() or "code looks good" in validation_response.lower():
 return {"valid": True, "issues": None}
 else:
 return {"valid": False, "issues": validation_response}

 def refine_code(self, original_code, issues):
 prompt = f"""Original code:
 ```python
 {original_code}
 ```

 Issues identified:
 {issues}

 Please provide a corrected version of the code that addresses these issues.
 Only include the Python code itself without any markdown formatting.
 """

 refined_code = generate_text(prompt)

 refined_code = re.sub(r'^```python\n|```$', '', refined_code, flags=re.MULTILINE)

 return refined_code

 def safe_execute_code(self, code, user_task):

 safe_imports = """
 # Standard library imports
 import math
 import random
 import re
 import time
 import json
 from datetime import datetime

 # Define a function to capture printed output
 captured_output = []
 original_print = print

 def safe_print(*args, **kwargs):
 output = " ".join(str(arg) for arg in args)
 captured_output.append(output)
 original_print(output)

 print = safe_print

 # Define a result variable to store the final output
 result = None

 # Function to store the final result
 def store_result(value):
 global result
 result = value
 return value
 """

 result_capture = """
 # Store the final result if not already done
 if 'result' not in locals() or result is None:
 try:
 # Look for variables that might contain the final result
 potential_results = [var for var in locals() if not var.startswith('_') and var not in
 ['math', 'random', 're', 'time', 'json', 'datetime',
 'captured_output', 'original_print', 'safe_print',
 'result', 'store_result']]
 if potential_results:
 # Use the last defined variable as the result
 store_result(locals()[potential_results[-1]])
 except:
 pass
 """

 full_code = safe_imports + "\n# User code starts here\n" + code + "\n\n" + result_capture

 code_lines = code.split('\n')
 first_lines = code_lines[:3]
 print(f"\nExecuting (first 3 lines):\n{first_lines}")

 local_env = {}

 try:
 exec(full_code, {}, local_env)

 return {
 "output": local_env.get('captured_output', []),
 "result": local_env.get('result', "No explicit result returned")
 }
 except Exception as e:
 return {"error": str(e)}

 def create_final_report(self, execution_result):
 if isinstance(execution_result.get('output'), list):
 output_text = "\n".join(execution_result.get('output', []))
 else:
 output_text = str(execution_result.get('output', ''))

 result_text = str(execution_result.get('result', ''))
 error_text = execution_result.get('error', '')

 context = "Task history:\n"
 for item in self.memory:
 if item["role"] == "user":
 context += f"USER TASK: {item['content']}\n\n"

 prompt = f"""{context}

 EXECUTION OUTPUT:
 {output_text}

 EXECUTION RESULT:
 {result_text}

 {f"ERROR: {error_text}" if error_text else ""}

 Create a final report that explains the solution to the original task. Include:
 1. What was done
 2. How it was accomplished
 3. The final results
 4. Any insights or conclusions drawn from the analysis

 Format the report in a professional, easy to read manner.
 """

 return generate_text(prompt)

def demo_level5():
 print("\n" + "="*50)
 print("LEVEL 5: FULLY AUTONOMOUS AGENT DEMO")
 print("="*50)
 print("At this level, the AI generates and executes code to solve complex problems.")
 print("It can create, validate, refine, and run custom code solutions.\n")

 user_task = input("Enter a data analysis or computational task: ") or "Analyze a dataset of numbers [10, 45, 65, 23, 76, 12, 89, 32, 50] and create visualizations of the distribution"
 print("\nProcessing your request... (this may take a minute or two)\n")

 agent = AutonomousAgent()
 result = agent.run(user_task)
 print("\nFINAL REPORT:")
 print("-"*50)
 print(result)
print("-"*50)

AutonomousAgent類體現了完全自主智能體的自主性,包括維護用戶任務的連續記憶,并系統協調五大核心階段:規劃、代碼生成、驗證、安全執行和報告。啟動運行時,智能體會提示模型生成解決任務的詳細計劃,并將該計劃存儲在記憶中。接下來,它要求模型基于該計劃創建獨立的Python代碼,去掉任何Markdown格式,并通過查詢模型語法、邏輯、性能和安全問題來驗證代碼。如果驗證發現任何問題,智能體還會指示模型優化代碼,直至通過檢查。最終代碼被包裝在一個沙箱執行框架中,包含捕獲的輸出緩沖區和自動結果提取,并在隔離的本地環境中執行。最后,智能體通過將執行結果反饋給模型,生成一份精心制作的專業報告,具體解釋做了什么、如何完成以及獲得了哪些見解。隨附的demo_level5函數則提供一個簡單的交互循環,即接受用戶任務、運行智能體并呈現全面的最終報告。

Main函數:囊括以上各步驟

def main():
 while True:
 clear_output(wait=True)
 print("\n" + "="*50)
 print("AI AGENT LEVELS DEMO")
 print("="*50)
 print("\nThis notebook demonstrates the 5 levels of AI agents:")
 print("1. Simple Processor - Model has no impact on program flow")
 print("2. Router - Model determines basic program flow")
 print("3. Tool Calling - Model determines how functions are executed")
 print("4. Multi-Step Agent - Model controls iteration and program continuation")
 print("5. Fully Autonomous Agent - Model creates & executes new code")
 print("6. Quit")

 choice = input("\nSelect a level to demo (1-6): ")

 if choice == "1":
 demo_level1()
 elif choice == "2":
 demo_level2()
 elif choice == "3":
 demo_level3()
 elif choice == "4":
 demo_level4()
 elif choice == "5":
 demo_level5()
 elif choice == "6":
 print("\nThank you for exploring the AI Agent levels!")
 break
 else:
 print("\nInvalid choice. Please select 1-6.")

 input("\nPress Enter to return to the main menu...")


if __name__ == "__main__":
main()

最后,main函數會呈現一個簡單的交互菜單循環,清除Colab輸出以提高可讀性,顯示所有五個智能體級別以及退出選項,然后將用戶的選擇分發到相應的演示函數,再等待輸入返回到菜單。這種結構提供了一個集中的CLI風格界面,使你可以按順序探索每個智能體級別,而無需手動介入各具體環節。

通過這五個級別的實踐,我們深入了解了代理式AI的原則以及控制、靈活性和自治之間的權衡。我們看到系統如何從簡單的提示詞響應行為演變為復雜的決策管道,甚至自我修改代碼執行。無論你是希望開發智能助手、構建數據管道還是試驗新興AI能力,這一漸進框架都為設計強大且可擴展的智能體提供了堅實的路線指導。

原文標題:??A Comprehensive Tutorial on the Five Levels of Agentic AI Architectures: From Basic Prompt Responses to Fully Autonomous Code Generation and Execution?,作者:Asif Razzaq

?著作權歸作者所有,如需轉載,請注明出處,否則將追究法律責任
收藏
回復
舉報
回復
相關推薦
欧美三区美女| 成人免费一区| 91欧美一区二区| 国产精品免费在线免费| 午夜国产福利视频| 亚洲国产精品免费视频| 欧美视频一二三| 老司机av福利| 亚洲av片一区二区三区| 毛片一区二区三区| 69久久夜色精品国产69| 中文字幕乱码av| 啪啪激情综合网| 欧美精品久久久久久久多人混战 | 亚洲婷婷在线观看| 91成人在线| 五月婷婷综合网| 伊人天天久久大香线蕉av色| 天堂av手机版| 国产伦精品一区二区三区视频青涩 | 激情亚洲影院在线观看| 一区二区三区四区中文字幕| 日韩久久精品一区二区三区| 国产91久久久| 国产在线一区二区综合免费视频| 欧美最顶级的aⅴ艳星| 一区二区在线观看免费视频| 不卡视频在线| 亚洲精品在线91| 野战少妇38p| 国产精品一区二区三区av| 色综合天天天天做夜夜夜夜做| 永久免费网站视频在线观看| gogogo高清在线观看免费完整版| 99久久国产综合精品女不卡| 91影院未满十八岁禁止入内| 亚洲在线精品视频| 日韩1区2区日韩1区2区| 琪琪亚洲精品午夜在线| 日本一区二区网站| 黑人一区二区| 欧美激情在线观看| 美女福利视频在线观看| 亚洲成av人片一区二区密柚| 尤物九九久久国产精品的特点 | 日韩久久一区二区| 性高潮久久久久久久久| 国内精品一区视频| 国产亚洲精品超碰| 日本精品一区二区三区高清 久久 日本精品一区二区三区不卡无字幕 | 欧美一区二区三区免费视| 久草免费在线观看视频| 欧美激情亚洲| 欧美激情按摩在线| 国产亚洲精品女人久久久久久| 欧美一区影院| 欧美日韩999| 久草国产在线视频| 国产日韩欧美一区| 欧美一区第一页| 午夜一级黄色片| 秋霞电影网一区二区| 国产精品亚洲精品| 一二三区中文字幕| 国产一区二区三区久久久 | 4438全国成人免费| 国产美女激情视频| 奇米精品一区二区三区在线观看一| 国产精品第七十二页| 最新中文字幕在线观看视频| 欧美96一区二区免费视频| 国产精品无av码在线观看| 91国产免费视频| 国产制服丝袜一区| 精品国产一区二区三区麻豆小说| 五月婷婷在线播放| 国产三级精品视频| 在线观看三级网站| 男人久久天堂| 欧美日韩综合在线| 久久久久无码精品| 人人香蕉久久| 色偷偷av一区二区三区| 欧美黑人一级片| 午夜在线精品| 国产欧美中文字幕| 蜜桃视频久久一区免费观看入口| 91蝌蚪porny| 影音先锋欧美在线| 182在线视频观看| 欧美在线免费观看亚洲| 91丨porny丨九色| 亚洲精品国产动漫| 欧美日韩国产成人在线| 黄色片视频免费| 国产精品一品二品| 国产精品久久7| av影片在线看| 欧美日韩精品在线视频| 亚洲欧美偷拍另类| 国产精品自在| 久久精品视频导航| 丁香六月婷婷综合| 国产成人在线视频网站| 视频在线一区二区三区| 日本乱理伦在线| 欧美性生活大片视频| 亚洲成年人av| 欧美xxxx中国| 国产精品av在线| 日韩在线视频免费| 亚洲欧洲制服丝袜| 丰满少妇在线观看| 久久精品国产亚洲blacked| 久久九九亚洲综合| 91视频在线视频| 99精品国产91久久久久久 | 精品日韩在线视频| 亚洲尤物精选| 国产精品乱码| 18+视频在线观看| 欧美日韩精品二区第二页| 中文字幕在线播放一区| 欧美色图首页| 亚洲a在线观看| 欧美三级黄网| 91黄色免费看| 波多野结衣 在线| 亚洲第一黄网| 国产视频在线观看一区| 日本在线观看大片免费视频| 欧美精品粉嫩高潮一区二区| 国产精品20p| 久久国产精品久久久久久电车 | 精品成人久久av| 91精品人妻一区二区三区四区| 欧美激情偷拍自拍| 国产精品亚洲欧美导航| 成人性爱视频在线观看| 欧洲一区在线观看| 亚洲黄色免费视频| 日本三级亚洲精品| 神马影院午夜我不卡影院| 向日葵视频成人app网址| 国产视频精品一区二区三区| 日本一区二区网站| 91亚洲精品久久久蜜桃| 久久国产亚洲精品无码| 网曝91综合精品门事件在线 | 色综合视频一区二区三区44| 中文字幕日韩精品有码视频| 中文天堂在线视频| 国产精品人成在线观看免费| 69久久久久久| 婷婷成人基地| 91丨九色丨国产| 欧美6一10sex性hd| 亚洲国产精品电影| 国产视频1区2区| 国产精品免费av| 久久久久亚洲av无码麻豆| 在线欧美视频| 久久精品一区二区三区不卡免费视频| 校园春色亚洲| 伊人久久综合97精品| 97人妻人人澡人人爽人人精品| 中文字幕在线免费不卡| 国产成人av片| 亚洲一区不卡| 亚洲欧洲国产日韩精品| 国产一区二区三区精品在线观看| 欧美巨猛xxxx猛交黑人97人| 日本免费网站在线观看| 一本大道久久a久久精品综合| 懂色av粉嫩av浪潮av| 狠狠色伊人亚洲综合成人| 四虎4hu永久免费入口| 丝袜久久网站| 国产欧美日韩综合精品| 欧美一卡二卡| 亚洲午夜未满十八勿入免费观看全集| 亚洲一二区视频| 亚洲在线视频网站| 性欧美精品男男| 国产精品一区专区| 日韩精品一区二区三区不卡| 国产韩日影视精品| 激情视频一区二区| 久久久久黄色| 97欧美精品一区二区三区| 成人在线观看网站| 精品少妇一区二区三区日产乱码| 久久久免费高清视频| 亚洲同性gay激情无套| free性中国hd国语露脸| 韩国视频一区二区| 免费看的黄色大片| 欧美一区91| 日韩欧美三级一区二区| 91综合精品国产丝袜长腿久久| 日韩av高清不卡| 婷婷色在线播放| 视频直播国产精品| 日韩私人影院| 精品乱人伦小说| 国产尤物视频在线观看| 日韩欧美亚洲范冰冰与中字| 国产一区二区视频在线观看免费| 91免费视频网| 亚洲911精品成人18网站| 青草av.久久免费一区| 缅甸午夜性猛交xxxx| 伊人久久大香线| 日韩免费av电影| 偷拍一区二区| 久久99精品久久久久久秒播放器| 国产高清亚洲| 国产欧美日韩精品在线观看| jk漫画禁漫成人入口| 欧美精品国产精品日韩精品| 黄网站免费在线播放| 亚洲一区二区久久久| 亚洲人视频在线观看| 欧美v日韩v国产v| 国产女人18毛片水18精| 欧美日韩综合色| 国产裸体美女永久免费无遮挡| 香蕉影视欧美成人| 精品无码人妻一区二区三区| 国产精品久久久久一区二区三区共 | 老熟妇一区二区三区| 欧美日韩性生活视频| 日本免费在线播放| 亚洲高清视频中文字幕| 劲爆欧美第一页| 一区二区欧美在线观看| www.超碰在线观看| 亚洲免费观看高清完整版在线观看| 国产三级黄色片| 国产精品久久久久久久久搜平片 | 超碰国产在线| 中文字幕亚洲二区| 午夜在线视频| 日韩中文综合网| 快射av在线播放一区| 久久中文精品视频| 日本高清成人vr专区| 欧美精品国产精品日韩精品| 1区2区3区在线| 欧美一级在线亚洲天堂| 欧美91看片特黄aaaa| 国产精品av在线播放| 国产在视频一区二区三区吞精| 国产精品香蕉国产| 成人在线视频www| 亚洲综合日韩在线| 国产精品久久久久av蜜臀| 国产亚洲欧美一区二区| 久久99国产成人小视频| 天堂资源在线亚洲视频| 亚洲最新av| 欧美一区二区激情| 久久国产精品99国产| 亚洲国产日韩欧美在线观看| 国产在线精品一区二区| 中文字幕在线观看91| 91免费视频网| 你懂得视频在线观看| 亚洲精品乱码久久久久久| 日本三级免费看| 色婷婷av一区二区三区gif | 亚洲精品久久7777777| 你懂得网站在线| 精品久久久av| 黑森林国产精品av| 国产精品视频自拍| 一区二区三区在线资源| 精品国产中文字幕| 日韩亚洲一区在线| 国产欧美精品aaaaaa片| 久久精品欧洲| 宇都宫紫苑在线播放| 99精品国产一区二区三区不卡| 丰满的亚洲女人毛茸茸| 亚洲综合激情小说| 青青国产在线视频| 日韩精品一区二区在线观看| 男女视频在线观看免费| 久久久999国产| a日韩av网址| 99精品99久久久久久宅男| 亚洲国产精品嫩草影院久久av| 在线一区高清| 久久经典综合| 伊人av在线播放| 国产精品视频你懂的| 国产无遮挡又黄又爽| 欧美三级日韩三级国产三级| 日本国产在线观看| 久久精品免费电影| 国产精品极品美女在线观看| 国产精品推荐精品| 99国产**精品****| 国产一区视频免费观看| 成人av在线一区二区三区| 日韩在线一卡二卡| 色欧美片视频在线观看 | 伊人久久精品视频| 岛国av在线播放| 91观看网站| 97精品在线| 91视频免费版污| 91亚洲男人天堂| 日本五十路女优| 日韩一区二区在线看| √天堂资源地址在线官网| 琪琪亚洲精品午夜在线| 黄色欧美在线| 久操手机在线视频| 国产一区二区三区在线看麻豆| 一级黄色片网址| 91久久精品一区二区三| 视频一区二区免费| 午夜精品久久久久久99热软件| 欧美国产中文高清| www亚洲国产| 久久狠狠亚洲综合| 少妇愉情理伦三级| 欧美色涩在线第一页| 国产三级视频在线| 国产91色在线|免| 欧美人与牛zoz0性行为| 色综合av综合无码综合网站| 91在线视频网址| 日韩 欧美 综合| 亚洲高清不卡av| 天堂√中文最新版在线| 久久精品五月婷婷| 亚洲一区区二区| 毛茸茸多毛bbb毛多视频| 欧美日韩另类在线| 你懂的在线观看视频网站| 欧美制服第一页| 国产精品中文字幕亚洲欧美| 人妻内射一区二区在线视频 | 国产美女在线观看一区| 欧美丰满艳妇bbwbbw| 日韩欧美一区中文| 黑人另类精品××××性爽| 国产日韩精品推荐| 国产精品婷婷| 真实乱视频国产免费观看| 在线观看欧美黄色| 国产大片在线免费观看| 国产精品中文久久久久久久| 91久久久精品国产| 久久久久久国产精品日本| 亚洲国产裸拍裸体视频在线观看乱了| 免费av一级片| 欧洲成人午夜免费大片| 成人在线免费观看视频| 国产欧美精品一二三| 一区二区视频在线看| 天堂在线一二区| 国产精品视频精品| 欧美淫片网站| 好吊日免费视频| 欧美日韩激情一区二区三区| 99福利在线| 精品一卡二卡三卡四卡日本乱码| 日韩在线a电影| 91传媒免费观看| 亚洲电影成人av99爱色| 深夜成人福利| 日韩人妻精品一区二区三区| av影院午夜一区| 中文字幕在线视频免费| 九九久久国产精品| 免费视频国产一区| 亚洲免费在线播放视频| 精品国产精品自拍| 男人和女人做事情在线视频网站免费观看 | 日韩国产成人无码av毛片| 91蜜桃网址入口| 国产美女明星三级做爰| 97在线观看视频| 羞羞答答成人影院www| 精品视频站长推荐| 欧美日精品一区视频| 爱啪啪综合导航| 日韩性感在线| gogogo免费视频观看亚洲一| 中文字幕在线播放av| 孩xxxx性bbbb欧美| 四虎成人精品永久免费av九九| 亚洲熟女一区二区三区| 欧美性三三影院| 欧美男人天堂| 日韩精品手机在线观看| 国产午夜精品理论片a级大结局|