-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathch04_v1_agent.py
More file actions
117 lines (87 loc) · 2.95 KB
/
ch04_v1_agent.py
File metadata and controls
117 lines (87 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
PicoAgents Code Along - Chapter 4.1: The Core Agent
Minimal Agent with the SAME API as picoagents. Swap the import to use full library.
What this implements:
- Agent class with name, instructions, model
- run() method that calls LLM and returns response
What's omitted (see later versions or full library):
- Tools, Memory, Streaming, Middleware
Run: python ch04_v1_agent.py
Model Client: Uses Azure OpenAI. For other clients:
from openai import AsyncOpenAI
client = AsyncOpenAI() # Uses OPENAI_API_KEY
"""
import asyncio
from dataclasses import dataclass, field
from typing import Any, List, Optional
from openai import AsyncAzureOpenAI
@dataclass
class Message:
content: str
source: str = "assistant"
@dataclass
class UserMessage(Message):
source: str = "user"
@dataclass
class AssistantMessage(Message):
source: str = "assistant"
@dataclass
class AgentResponse:
messages: List[Message] = field(default_factory=list)
source: str = ""
@property
def final_content(self) -> str:
return self.messages[-1].content if self.messages else ""
class Agent:
"""
Minimal Agent - same interface as picoagents.Agent.
Usage:
agent = Agent(name="assistant", instructions="You are helpful.")
response = await agent.run("What is 2+2?")
print(response.final_content)
"""
def __init__(
self,
name: str,
instructions: str = "You are a helpful assistant.",
model: str = "gpt-4.1-mini",
tools: Optional[List] = None, # API compat - not used in v1
memory=None, # API compat - not used in v1
description: str = "",
):
self.name = name
self.instructions = instructions
self.model = model
self.description = description or f"Agent: {name}"
self._tools = tools
self._memory = memory
self._client = AsyncAzureOpenAI(api_version="2024-12-01-preview")
async def run(self, task: str) -> AgentResponse:
"""Execute agent on a task."""
messages: List[Any] = [
{"role": "system", "content": self.instructions},
{"role": "user", "content": task}
]
response = await self._client.chat.completions.create(
model=self.model,
messages=messages
)
content = response.choices[0].message.content or ""
return AgentResponse(
messages=[
Message(content=task, source="user"),
Message(content=content, source="assistant")
],
source=self.name
)
async def main():
print("=== Code Along v1: Core Agent ===\n")
agent = Agent(
name="assistant",
instructions="You are a helpful assistant. Be concise.",
model="gpt-4.1-mini"
)
response = await agent.run("What is the capital of France?")
print(f"Agent: {response.final_content}")
if __name__ == "__main__":
asyncio.run(main())