forked from julep-ai/julep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-User_Management_and_Personalization.py
188 lines (158 loc) · 5.5 KB
/
09-User_Management_and_Personalization.py
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
178
179
180
181
182
183
184
185
186
187
188
# User Management and Personalization Cookbook
#
# Plan:
# 1. Import necessary libraries and set up the Julep client
# 2. Create an agent for handling user management and personalization
# 3. Define a task for user registration and profile creation
# 4. Define a task for personalized content recommendation
# 5. Create sample users with different preferences
# 6. Demonstrate user registration and profile creation
# 7. Show personalized content recommendations for different users
# 8. Implement a function to update user preferences
# 9. Display updated personalized recommendations after preference changes
import uuid
import yaml
from julep import Client
# Global UUIDs for agent and tasks
AGENT_UUID = uuid.uuid4()
REGISTRATION_TASK_UUID = uuid.uuid4()
RECOMMENDATION_TASK_UUID = uuid.uuid4()
# Creating Julep Client with the API Key
api_key = "" # Your API key here
client = Client(api_key=api_key, environment="dev")
# Creating an agent for user management and personalization
agent = client.agents.create_or_update(
agent_id=AGENT_UUID,
name="Personalization Assistant",
about="An AI agent specialized in user management and personalized content recommendations.",
model="gpt-4-turbo",
)
# Defining a task for user registration and profile creation
registration_task_def = yaml.safe_load("""
name: User Registration and Profile Creation
input_schema:
type: object
properties:
username:
type: string
interests:
type: array
items:
type: string
main:
- prompt:
role: system
content: >-
You are a user registration assistant. Create a user profile based on the following information:
Username: {{inputs[0].username}}
Interests: {{inputs[0].interests}}
Generate a brief bio and suggest some initial content preferences based on the user's interests.
unwrap: true
- evaluate:
user_profile: >-
{
"username": inputs[0].username,
"interests": inputs[0].interests,
"bio": _.split('\n\n')[0],
"content_preferences": _.split('\n\n')[1]
}
- return: outputs[1].user_profile
""")
# Creating the registration task
registration_task = client.tasks.create_or_update(
task_id=REGISTRATION_TASK_UUID,
agent_id=AGENT_UUID,
**registration_task_def
)
# Defining a task for personalized content recommendation
recommendation_task_def = yaml.safe_load("""
name: Personalized Content Recommendation
input_schema:
type: object
properties:
user_profile:
type: object
tools:
- name: content_database
type: integration
integration:
provider: mock
setup:
data: [
{"id": 1, "title": "Introduction to AI", "category": "Technology"},
{"id": 2, "title": "Healthy Eating Habits", "category": "Health"},
{"id": 3, "title": "Financial Planning 101", "category": "Finance"},
{"id": 4, "title": "The Art of Photography", "category": "Art"},
{"id": 5, "title": "Beginner's Guide to Yoga", "category": "Fitness"}
]
main:
- tool: content_database
arguments: {}
- prompt:
role: system
content: >-
You are a content recommendation system. Based on the user's profile and the available content,
recommend 3 pieces of content that best match the user's interests and preferences.
User Profile:
{{inputs[0].user_profile}}
Available Content:
{{outputs[0]}}
Provide your recommendations in the following format:
1. [Content ID] - [Content Title] - Reason for recommendation
2. [Content ID] - [Content Title] - Reason for recommendation
3. [Content ID] - [Content Title] - Reason for recommendation
unwrap: true
- return: _
""")
# Creating the recommendation task
recommendation_task = client.tasks.create_or_update(
task_id=RECOMMENDATION_TASK_UUID,
agent_id=AGENT_UUID,
**recommendation_task_def
)
# Function to register a user and create their profile
def register_user(username, interests):
execution = client.executions.create(
task_id=REGISTRATION_TASK_UUID,
input={
"username": username,
"interests": interests
}
)
result = client.executions.get(execution.id)
return result.output
# Function to get personalized content recommendations
def get_recommendations(user_profile):
execution = client.executions.create(
task_id=RECOMMENDATION_TASK_UUID,
input={
"user_profile": user_profile
}
)
result = client.executions.get(execution.id)
return result.output
# Function to update user preferences
def update_user_preferences(user_profile, new_interests):
user_profile["interests"] = list(set(user_profile["interests"] + new_interests))
return user_profile
# Demonstrate user registration and personalization
print("Demonstrating User Management and Personalization:")
# Register users
user1 = register_user("alice", ["technology", "finance"])
user2 = register_user("bob", ["health", "fitness"])
print("\nUser Profiles:")
print(f"Alice: {user1}")
print(f"Bob: {user2}")
# Get personalized recommendations
print("\nPersonalized Recommendations:")
print("Alice's Recommendations:")
print(get_recommendations(user1))
print("\nBob's Recommendations:")
print(get_recommendations(user2))
# Update user preferences
print("\nUpdating User Preferences:")
updated_alice = update_user_preferences(user1, ["art"])
print(f"Alice's Updated Profile: {updated_alice}")
# Get updated recommendations
print("\nUpdated Personalized Recommendations for Alice:")
print(get_recommendations(updated_alice))