|
| 1 | +--- |
| 2 | +title: "teste" |
| 3 | +date: 2025-02-13 |
| 4 | +authors: |
| 5 | + - diego-init |
| 6 | +categories: |
| 7 | + - agent |
| 8 | +tags: |
| 9 | + - AI |
| 10 | + - agents |
| 11 | + - LLM |
| 12 | + - automation |
| 13 | +draft: true |
| 14 | + |
| 15 | +comments: true |
| 16 | +--- |
| 17 | + |
| 18 | +# Getting Started with AI Agents |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## What’s an AI Agent, Anyway? |
| 23 | + |
| 24 | +An **AI Agent** is just a smart system that uses AI to interact with its surroundings and get stuff done. It thinks, plans, and takes action (sometimes using extra tools) to complete tasks. |
| 25 | + |
| 26 | +AI Agents can do anything we set them up for using **Tools** to carry out **Actions**. |
| 27 | + |
| 28 | +### The Two Main Parts of an AI Agent |
| 29 | + |
| 30 | +1. **The Brain (AI Model)** - This is where all the decision-making happens. The AI figures out what to do next. Examples include Large Language Models (LLMs) like GPT-4. |
| 31 | + |
| 32 | +2. **The Body (Tools & Capabilities)** - This is what the agent actually *does*. Its abilities depend on the tools it has access to. |
| 33 | + |
| 34 | +## Why Use LLMs? |
| 35 | + |
| 36 | +LLMs (Large Language Models) are the go-to choice for AI Agents because they’re great at understanding and generating text. Popular ones include GPT-4, Llama, and Gemini. |
| 37 | + |
| 38 | +There are two ways you can use an LLM: |
| 39 | + |
| 40 | +- **Run Locally** (if your computer is powerful enough). |
| 41 | +- **Use a Cloud/API** (e.g., via Hugging Face’s API). |
| 42 | + |
| 43 | +## System Messages: Setting the Rules |
| 44 | + |
| 45 | +System messages (or prompts) tell the AI how it should behave. They act as guiding instructions. |
| 46 | + |
| 47 | +```python |
| 48 | +system_message = { |
| 49 | + "role": "system", |
| 50 | + "content": "You are a helpful customer service agent. Always be polite and clear." |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +These messages also define what tools the AI can use and how it should format its responses. |
| 55 | + |
| 56 | +## Conversations: How AI Talks to Users |
| 57 | + |
| 58 | +A conversation is just back-and-forth messages between a user and the AI. Chat templates help keep things organized and make sure the AI remembers what’s going on. |
| 59 | + |
| 60 | +Example: |
| 61 | + |
| 62 | +```python |
| 63 | +conversation = [ |
| 64 | + {"role": "user", "content": "I need help with my order"}, |
| 65 | + {"role": "assistant", "content": "Sure! What’s your order number?"}, |
| 66 | + {"role": "user", "content": "ORDER-123"}, |
| 67 | +] |
| 68 | +``` |
| 69 | + |
| 70 | +## Chat Templates: Keeping AI Conversations Structured |
| 71 | + |
| 72 | +Chat templates make sure LLMs correctly process messages. There are two main types of AI models: |
| 73 | + |
| 74 | +- **Base Models**: Trained on raw text to predict the next word. |
| 75 | +- **Instruct Models**: Fine-tuned to follow instructions and have conversations. |
| 76 | + |
| 77 | +We use **ChatML**, a structured format for messages. The transformers library takes care of this automatically: |
| 78 | + |
| 79 | +```python |
| 80 | +from transformers import AutoTokenizer |
| 81 | + |
| 82 | +tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-1.7B-Instruct") |
| 83 | +rendered_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
| 84 | +``` |
| 85 | + |
| 86 | +## What are Tools? |
| 87 | + |
| 88 | +Tools let AI Agents do more than just text generation. A Tool is basically a function the AI can call to get things done. |
| 89 | + |
| 90 | +| Tool | What It Does | |
| 91 | +| ---------------- | --------------------------------------------------- | |
| 92 | +| Web Search | Fetches up-to-date info from the internet. | |
| 93 | +| Image Generation | Creates images from text. | |
| 94 | +| Retrieval | Pulls in data from other sources. | |
| 95 | +| API Interface | Connects with external APIs like GitHub or Spotify. | |
| 96 | + |
| 97 | +### Why Do AI Agents Need Tools? |
| 98 | + |
| 99 | +LLMs have a limited knowledge base (they only know what they were trained on). Tools help by allowing: |
| 100 | + |
| 101 | +- **Real-time data fetching** (e.g., checking the weather). |
| 102 | +- **Specialized tasks** (e.g., doing math, calling APIs). |
| 103 | + |
| 104 | +## Building a Simple Tool: A Calculator |
| 105 | + |
| 106 | +Let’s create a basic calculator tool that multiplies two numbers: |
| 107 | + |
| 108 | +```python |
| 109 | +def calculator(a: int, b: int) -> int: |
| 110 | + """Multiply two numbers.""" |
| 111 | + return a * b |
| 112 | +``` |
| 113 | + |
| 114 | +This tool includes: |
| 115 | + |
| 116 | +- A clear name (`calculator`). |
| 117 | +- A description (via the docstring). |
| 118 | +- Input and output types. |
| 119 | + |
| 120 | +To define it as a tool, we describe it like this: |
| 121 | + |
| 122 | +```python |
| 123 | +Tool Name: calculator, Description: Multiplies two numbers., Arguments: a: int, b: int, Outputs: int |
| 124 | +``` |
| 125 | + |
| 126 | +### Automating Tool Descriptions |
| 127 | + |
| 128 | +Instead of writing descriptions manually, we can use Python introspection to extract details automatically. The `Tool` class helps manage this info. |
| 129 | + |
| 130 | +```python |
| 131 | +class Tool: |
| 132 | + def __init__(self, name: str, description: str, func: callable, arguments: list, outputs: str): |
| 133 | + self.name = name |
| 134 | + self.description = description |
| 135 | + self.func = func |
| 136 | + self.arguments = arguments |
| 137 | + self.outputs = outputs |
| 138 | + |
| 139 | + def to_string(self) -> str: |
| 140 | + args_str = ", ".join([f"{arg_name}: {arg_type}" for arg_name, arg_type in self.arguments]) |
| 141 | + return f"Tool Name: {self.name}, Description: {self.description}, Arguments: {args_str}, Outputs: {self.outputs}" |
| 142 | + |
| 143 | + def __call__(self, *args, **kwargs): |
| 144 | + return self.func(*args, **kwargs) |
| 145 | +``` |
| 146 | + |
| 147 | +Now, we can create a tool instance: |
| 148 | + |
| 149 | +```python |
| 150 | +calculator_tool = Tool( |
| 151 | + "calculator", "Multiplies two numbers.", calculator, [("a", "int"), ("b", "int")], "int" |
| 152 | +) |
| 153 | +``` |
| 154 | + |
| 155 | +### Using a Decorator to Define Tools |
| 156 | + |
| 157 | +A decorator makes tool creation easier: |
| 158 | + |
| 159 | +```python |
| 160 | +import inspect |
| 161 | + |
| 162 | +def tool(func): |
| 163 | + signature = inspect.signature(func) |
| 164 | + arguments = [(param.name, param.annotation.__name__) for param in signature.parameters.values()] |
| 165 | + return_annotation = signature.return_annotation.__name__ if signature.return_annotation else "No return annotation" |
| 166 | + return Tool(func.__name__, func.__doc__ or "No description provided.", func, arguments, return_annotation) |
| 167 | +``` |
| 168 | + |
| 169 | +Now, we can define tools like this: |
| 170 | + |
| 171 | +```python |
| 172 | +@tool |
| 173 | +def calculator(a: int, b: int) -> int: |
| 174 | + """Multiply two numbers.""" |
| 175 | + return a * b |
| 176 | +``` |
| 177 | + |
| 178 | +This makes it easy for AI Agents to recognize and use tools based on text input. |
| 179 | + |
| 180 | +## Recap |
| 181 | + |
| 182 | +- **AI Agents** use AI models to interact and make decisions. |
| 183 | +- **LLMs** handle language understanding and text generation. |
| 184 | +- **System Messages** define the agent’s behavior. |
| 185 | +- **Tools** extend an AI’s capabilities beyond text generation. |
| 186 | +- **Chat Templates** format conversations properly. |
| 187 | +- **Tools** help AI Agents fetch real-time data and execute tasks. |
| 188 | + |
| 189 | +By combining all these pieces, you can build smart AI Agents that think, act, and assist like pros! |
0 commit comments