Spaces:
Sleeping
Sleeping
File size: 6,865 Bytes
2c060c5 |
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
"""
Natural Language Project Completion Test
Tutorial: Finish article project using natural language with typos and casual conversation.
"""
import os
import sys
import asyncio
import json
from pathlib import Path
from datetime import datetime
from dotenv import load_dotenv
from phoenix.otel import register
import weave
from agents import Runner, Agent
sys.path.insert(0, str(Path(__file__).parent.parent))
from agent.todo_agent import create_agent
from agent.storage import JsonTodoStorage
def reset_test_data():
"""Reset todos and session data for clean test runs."""
os.makedirs("data", exist_ok=True)
with open("data/todos.json", "w") as f:
json.dump([], f)
with open("data/session_default.json", "w") as f:
json.dump({"history": []}, f)
print("🔄 Data reset - starting with clean slate")
def initialize_tracing(project_name: str):
"""Initialize tracing with graceful error handling."""
os.environ["OPENAI_TRACING_ENABLED"] = "1"
os.environ["WEAVE_PRINT_CALL_LINK"] = "false"
# Phoenix: Add minimal custom resource attributes via environment variable
os.environ["OTEL_RESOURCE_ATTRIBUTES"] = f"tutorial.name={project_name},tutorial.type=natural_language,environment=test,app.name=todo-agent"
try:
register(project_name=project_name, auto_instrument=True)
print(f"✅ Phoenix tracing initialized for: {project_name}")
except Exception as e:
print(f"⚠️ Phoenix tracing failed: {e}")
if not weave.get_client():
try:
weave.init(project_name)
print(f"✅ Weave tracing initialized for: {project_name}")
except Exception as e:
print(f"⚠️ Weave tracing failed (continuing without Weave): {e}")
async def run_natural_language_test():
"""Tutorial: Complete article project using casual, natural language."""
start_time = datetime.now()
test_details = {
"turns": 0,
"validation_results": {},
"errors": []
}
try:
reset_test_data()
load_dotenv()
initialize_tracing("finishing-article-project")
agent = create_agent(storage=JsonTodoStorage(), agent_name="To-Do Agent (Article Completion)")
print("🧪 Starting Natural Language Project Completion Tutorial")
print("=" * 50)
print("🎯 Learn: Natural conversation with typos and casual language")
print("📚 Goal: Finish observability article with editing and publishing tasks")
test_messages = [
# === Casual task additions with typos ===
"hey, add 'write conclusion section' and 'proofread everthing' to my Writing project - getting close to finishing this article",
# === Natural editing and context ===
"actually change that proofreading task to 'final review and editing' - sounds more professional",
# === Publishing tasks with informal language ===
"also add 'create code examples' and 'format for publication' to my Publishing project - gotta make sure the examples actually work",
# === Check final status ===
"lemme see what we have for the Writing project now"
]
history = []
# Weave: Add minimal context attributes for this tutorial session
with weave.attributes({'tutorial_type': 'natural_language', 'environment': 'test', 'app_name': 'todo-agent', 'tutorial_name': 'language-completion-tutorial'}):
for i, message in enumerate(test_messages, 1):
print(f"\n--- Completion Step {i} ---")
print(f"User: {message}")
history.append({"role": "user", "content": message})
result = await Runner.run(agent, input=history)
print(f"Agent: {result.final_output}")
history = result.to_input_list()
await asyncio.sleep(0.5)
test_details["turns"] = len(test_messages)
print("\n" + "=" * 50)
print("🎓 Natural Language Project Completion Tutorial Complete")
validation_success = True
try:
with open("data/todos.json", "r") as f:
todos = json.load(f)
total_todos = len(todos)
test_details["validation_results"]["total_todos"] = total_todos
projects = set(t.get('project') for t in todos if t.get('project'))
test_details["validation_results"]["projects"] = sorted(list(projects))
print(f"\n📊 Article Completion: {total_todos} finishing tasks across {len(projects)} projects")
project_groups = {}
for todo in todos:
project = todo.get('project') or 'No Project'
if project not in project_groups:
project_groups[project] = []
project_groups[project].append(todo)
for project, project_todos in sorted(project_groups.items()):
print(f"\n📂 {project}:")
for todo in project_todos:
print(f" • {todo['name']}")
except FileNotFoundError:
validation_success = False
error_msg = "No todos.json file found"
test_details["errors"].append(error_msg)
print(f"❌ {error_msg}")
overall_success = validation_success and len(test_details["errors"]) == 0
print(f"\n🎓 What You Learned:")
print("• Agent handles typos gracefully ('everthing' → 'everything')")
print("• Natural conversation flow with task modifications")
print("• Casual language processing: 'hey', 'lemme see', 'gotta make sure'")
print("• Context understanding: 'that proofreading task' references previous todo")
print("🎉 Tutorial Series Complete: You've mastered todo agent workflows!")
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
if overall_success:
print(f"\n✅ TUTORIAL PASSED: Natural language mastery achieved! ({duration:.1f}s)")
else:
print(f"\n❌ TUTORIAL FAILED: Language processing needs work ({duration:.1f}s)")
return overall_success
except Exception as e:
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
print(f"\n❌ TUTORIAL FAILED: {str(e)} ({duration:.1f}s)")
return False
if __name__ == "__main__":
success = asyncio.run(run_natural_language_test())
exit(0 if success else 1) |