-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathpixelagent_basics_tutorial.py
More file actions
97 lines (74 loc) · 3.47 KB
/
pixelagent_basics_tutorial.py
File metadata and controls
97 lines (74 loc) · 3.47 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
# ============================================================================
# Pixelagent Basics Tutorial
# ============================================================================
# This tutorial demonstrates the fundamental capabilities of the Pixelagent
# framework, including agent creation, memory persistence, and tool integration.
# pip install pixelagent openai
import pixeltable as pxt
from pixelagent.openai import Agent
# ============================================================================
# SECTION 1: BASIC AGENT CREATION
# ============================================================================
# Create a simple conversational agent with a system prompt
# Initialize the agent with a name and personality
agent = Agent(
name="openai_agent", # Unique identifier for this agent
system_prompt="You're my assistant.", # Defines agent personality and capabilities
reset=True, # Start with fresh conversation history
)
# ============================================================================
# SECTION 2: CONVERSATIONAL MEMORY
# ============================================================================
# Demonstrate how agents maintain conversation context and history
# Basic conversation with persistent memory
print("Initial greeting:")
print(agent.chat("Hi, how are you?"))
# Agent remembers previous messages in the conversation
print("\nMemory test:")
print(agent.chat("What was my last question?"))
# ============================================================================
# SECTION 3: ACCESSING AGENT MEMORY
# ============================================================================
# How to retrieve and analyze the agent's conversation history
# Access the agent's memory table using the name
memory = pxt.get_table("openai_agent.memory")
# Retrieve all conversation history
print("\nFull conversation memory:")
print(memory.collect())
# ============================================================================
# SECTION 4: AGENT WITH TOOLS
# ============================================================================
# Extend agent capabilities with custom Python functions as tools
# Define a simple weather tool as a user-defined function (UDF)
@pxt.udf
def weather(city: str) -> str:
"""
Get the current weather for a specified city.
Args:
city (str): The name of the city to check weather for
Returns:
str: Weather description for the requested city
"""
return f"The weather in {city} is sunny."
# Add tool to our exisitng agent with custom system prompt instructions
agent = Agent(
name="openai_agent",
system_prompt="Use your tools to answer the users questions.",
tools=pxt.tools(weather),
)
# ============================================================================
# SECTION 6: TOOL CALLING
# ============================================================================
# Demonstrate how agents can use tools to access external functionality
# Execute a tool call with a specific query
print("\nTool call test:")
print(agent.tool_call("Get weather in San Francisco"))
# ============================================================================
# SECTION 7: TOOL CALL OBSERVABILITY
# ============================================================================
# How to monitor and analyze the agent's tool usage
# Access the agent's tool call history
tools = pxt.get_table("openai_agent.tools")
# Retrieve all tool usage records
print("\nTool call history:")
print(tools.collect())