搜索

PythonReactSEOPDF
GitHub Skill

ai-agent-patterns

Design patterns for building autonomous AI agents that can reason, plan, and execute tasks.

AmnadTaowsoam☆ 0 仓库 Stars0 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

AI Agent Patterns

Overview

AI agents are autonomous systems that use Large Language Models (LLMs) to reason, plan, and execute tasks. Unlike simple chatbots, agents can break down complex problems into steps, use tools to interact with external systems, and adapt their behavior based on feedback. This skill covers agent architectures (ReAct, Plan-and-Execute, Tree of Thoughts, Reflexion), core components (memory, planning, tool use), frameworks (LangChain, AutoGPT, CrewAI, Semantic Kernel), tool design, multi-agent systems, error recovery, guardrails, evaluation, and production deployment.

Prerequisites

  • Understanding of LLMs and their capabilities
  • Knowledge of prompt engineering and system prompts
  • Familiarity with function calling and tool APIs
  • Understanding of agent architectures and design patterns
  • Basic knowledge of vector databases and embeddings
  • Understanding of safety and guardrails for AI systems

Key Concepts

Agent Characteristics
  • Autonomy: Can make decisions without human intervention
  • Reasoning: Can break down complex tasks into subtasks
  • Planning: Can create and execute multi-step plans
  • Tool Use: Can interact with external systems via function calling
  • Memory: Can maintain context across interactions
  • Adaptation: Can learn from feedback and adjust behavior
Simple vs Complex Agents
  • Simple Agent: Single-step execution (e.g., weather query → result)
  • Complex Agent: Multi-step planning (e.g., trip planning → search → book → confirm)
Agent Architectures
  • ReAct: Reasoning + Acting in a loop
  • Plan-and-Execute: Create plan first, then execute step by step
  • Tree of Thoughts: Explore multiple reasoning paths before deciding
  • Reflexion: Reflect on actions and learn from mistakes
Core Components
  • Memory: Short-term (conversation history), Long-term (knowledge base), Episodic (specific experiences)
  • Planning: Break down goals into actionable steps
  • Tool Use: Register, validate, and execute tools safely
  • Reflection: Analyze actions and improve behavior
Deployment Patterns
  • FastAPI Server: REST API for agent inference
  • Docker Deployment: Containerized agent service
  • Kubernetes: Scalable deployment with GPU support
  • Multi-Agent Systems: Agent collaboration and delegation

Implementation Guide

Agent Architectures
ReAct (Reasoning + Acting)

The ReAct pattern combines reasoning and acting in a loop.

from openai import OpenAI

class ReActAgent:
    def __init__(self, llm_client, tools):
        self.client = llm_client
        self.tools = tools
        self.max_iterations = 10

    def run(self, query):
        thoughts = []

        for i in range(self.max_iterations):
            # Thought: What should I do?
            thought_response = self.client.chat.completions.create(
                model="gpt-4",
                messages=[
                    {"role": "system", "content": self.get_system_prompt()},
                    *thoughts,
                    {"role": "user", "content": query}
                ],
                tools=self.tools
            )

            thought = thought_response.choices[0].message.content
            thoughts.append({"role": "assistant", "content": thought})

            # Check if agent wants to use a tool
            if thought_response.choices[0].finish_reason == "function_calls":
                fc = thought_response.choices[0].message.function_calls[0]

                # Action: Execute tool
                tool_result = self.execute_tool(fc.name, fc.arguments)

                # Observation: What did I observe?
                observation = f"Tool {fc.name} returned: {json.dumps(tool_result)}"
                thoughts.append({"role": "assistant", "content": observation})
            else:
                # No more actions needed
                break

        return thoughts

    def get_system_prompt(self):
        return """You are a helpful assistant with access to tools.
Use the following format:

Thought: your thought about what to do
Action: action to take (tool call or final answer)
Observation: result of the action
... (repeat Thought/Action/Observation as needed)"""

    def execute_tool(self, tool_name, arguments):
        # Execute tool
        for tool in self.tools:
            if tool["name"] == tool_name:
                handler = tool["handler"]
                return handler(**arguments)
        return None

# Usage
tools = [
    {
        "type": "function",
        "function": {
            "name": "search",
            "description": "Search for information",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string"}
                },
                "re

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-01-20
索引更新时间
2026-01-26
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!