Formatting (#2750)
This commit is contained in:
@@ -20,19 +20,19 @@ agent = Agent(
|
||||
name="Fitness Agent",
|
||||
model=OpenAIChat(id="gpt-4o"),
|
||||
description="You are a helpful fitness assistant who remembers past logs and gives personalized suggestions for Anish's training and diet.",
|
||||
markdown=True
|
||||
markdown=True,
|
||||
)
|
||||
|
||||
|
||||
# Store user preferences as memory
|
||||
def store_user_preferences(conversation: list, user_id: str = USER_ID):
|
||||
"""Store user preferences from conversation history"""
|
||||
memory_client.add(conversation, user_id=user_id, output_format='v1.1')
|
||||
memory_client.add(conversation, user_id=user_id, output_format="v1.1")
|
||||
|
||||
|
||||
# Memory-aware assistant function
|
||||
def fitness_coach(user_input: str, user_id: str = USER_ID):
|
||||
memories = memory_client.search(user_input, user_id=user_id) # Search relevant memories bases on user query
|
||||
memories = memory_client.search(user_input, user_id=user_id) # Search relevant memories bases on user query
|
||||
memory_context = "\n".join(f"- {m['memory']}" for m in memories)
|
||||
|
||||
prompt = f"""You are a fitness assistant who helps Anish with his training, recovery, and diet. You have long-term memory of his health, routines, preferences, and past conversations.
|
||||
@@ -48,113 +48,66 @@ User query:
|
||||
memory_client.add(f"User: {user_input}\nAssistant: {response.content}", user_id=user_id)
|
||||
return response.content
|
||||
|
||||
|
||||
# --------------------------------------------------
|
||||
# Store user preferences and memories
|
||||
messages = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Hi, I’m Anish. I'm 26 years old, 5'10\", and weigh 72kg. I started working out 6 months ago with the goal of building lean muscle."
|
||||
"content": "Hi, I’m Anish. I'm 26 years old, 5'10\", and weigh 72kg. I started working out 6 months ago with the goal of building lean muscle.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Got it — you're 26, 5'10\", 72kg, and on a lean muscle journey. Started gym 6 months ago."
|
||||
"content": "Got it — you're 26, 5'10\", 72kg, and on a lean muscle journey. Started gym 6 months ago.",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I follow a push-pull-legs routine and train 5 times a week. My rest days are Wednesday and Sunday."
|
||||
"content": "I follow a push-pull-legs routine and train 5 times a week. My rest days are Wednesday and Sunday.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Understood — push-pull-legs split, training 5x/week with rest on Wednesdays and Sundays."
|
||||
"content": "Understood — push-pull-legs split, training 5x/week with rest on Wednesdays and Sundays.",
|
||||
},
|
||||
{"role": "user", "content": "After push days, I usually eat high-protein and moderate-carb meals to recover."},
|
||||
{"role": "assistant", "content": "Noted — high-protein, moderate-carb meals after push workouts."},
|
||||
{"role": "user", "content": "For pull days, I take whey protein and eat a banana after training."},
|
||||
{"role": "assistant", "content": "Logged — whey protein and banana post pull workouts."},
|
||||
{"role": "user", "content": "On leg days, I make sure to have complex carbs like rice or oats."},
|
||||
{"role": "assistant", "content": "Noted — complex carbs like rice and oats are part of your leg day meals."},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "After push days, I usually eat high-protein and moderate-carb meals to recover."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Noted — high-protein, moderate-carb meals after push workouts."
|
||||
"content": "I often feel sore after leg days, so I use turmeric milk and magnesium to help with recovery.",
|
||||
},
|
||||
{"role": "assistant", "content": "I'll remember turmeric milk and magnesium as part of your leg day recovery."},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "For pull days, I take whey protein and eat a banana after training."
|
||||
"content": "Last push day, I did 3x8 bench press at 60kg, 4x12 overhead press, and dips. Felt fatigued after.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Logged — whey protein and banana post pull workouts."
|
||||
"content": "Push day logged — 60kg bench, overhead press, dips. You felt fatigued afterward.",
|
||||
},
|
||||
{"role": "user", "content": "I prefer light dinners post-workout like tofu, soup, and vegetables."},
|
||||
{"role": "assistant", "content": "Got it — light dinners post-workout: tofu, soup, and veggies."},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "On leg days, I make sure to have complex carbs like rice or oats."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Noted — complex carbs like rice and oats are part of your leg day meals."
|
||||
"content": "I have mild lactose intolerance, so I avoid dairy. I use almond milk or lactose-free whey.",
|
||||
},
|
||||
{"role": "assistant", "content": "Understood — avoiding regular dairy, using almond milk and lactose-free whey."},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I often feel sore after leg days, so I use turmeric milk and magnesium to help with recovery."
|
||||
"content": "I get occasional knee pain, so I avoid deep squats and do more hamstring curls and glute bridges on leg days.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "I'll remember turmeric milk and magnesium as part of your leg day recovery."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Last push day, I did 3x8 bench press at 60kg, 4x12 overhead press, and dips. Felt fatigued after."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Push day logged — 60kg bench, overhead press, dips. You felt fatigued afterward."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I prefer light dinners post-workout like tofu, soup, and vegetables."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Got it — light dinners post-workout: tofu, soup, and veggies."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I have mild lactose intolerance, so I avoid dairy. I use almond milk or lactose-free whey."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Understood — avoiding regular dairy, using almond milk and lactose-free whey."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I get occasional knee pain, so I avoid deep squats and do more hamstring curls and glute bridges on leg days."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Noted — due to knee discomfort, you substitute deep squats with curls and glute bridges."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I track sleep and notice poor performance when I sleep less than 6 hours."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Logged — performance drops when you get under 6 hours of sleep."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I take magnesium supplements to help with muscle recovery and sleep quality."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Remembered — magnesium helps you with recovery and sleep."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I avoid caffeine after 4 PM because it affects my sleep."
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Got it — you avoid caffeine post-4 PM to protect your sleep."
|
||||
"content": "Noted — due to knee discomfort, you substitute deep squats with curls and glute bridges.",
|
||||
},
|
||||
{"role": "user", "content": "I track sleep and notice poor performance when I sleep less than 6 hours."},
|
||||
{"role": "assistant", "content": "Logged — performance drops when you get under 6 hours of sleep."},
|
||||
{"role": "user", "content": "I take magnesium supplements to help with muscle recovery and sleep quality."},
|
||||
{"role": "assistant", "content": "Remembered — magnesium helps you with recovery and sleep."},
|
||||
{"role": "user", "content": "I avoid caffeine after 4 PM because it affects my sleep."},
|
||||
{"role": "assistant", "content": "Got it — you avoid caffeine post-4 PM to protect your sleep."},
|
||||
]
|
||||
store_user_preferences(messages)
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import asyncio
|
||||
import warnings
|
||||
|
||||
from google.adk.agents import Agent
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.adk.runners import Runner
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
from google.genai import types
|
||||
|
||||
from mem0 import MemoryClient
|
||||
|
||||
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||||
@@ -19,14 +21,14 @@ def save_patient_info(information: str) -> dict:
|
||||
print(f"Storing patient information: {information[:30]}...")
|
||||
|
||||
# Get user_id from session state or use default
|
||||
user_id = getattr(save_patient_info, 'user_id', 'default_user')
|
||||
user_id = getattr(save_patient_info, "user_id", "default_user")
|
||||
|
||||
# Store in Mem0
|
||||
response = mem0_client.add(
|
||||
mem0_client.add(
|
||||
[{"role": "user", "content": information}],
|
||||
user_id=user_id,
|
||||
run_id="healthcare_session",
|
||||
metadata={"type": "patient_information"}
|
||||
metadata={"type": "patient_information"},
|
||||
)
|
||||
|
||||
return {"status": "success", "message": "Information saved"}
|
||||
@@ -37,7 +39,7 @@ def retrieve_patient_info(query: str) -> str:
|
||||
print(f"Searching for patient information: {query}")
|
||||
|
||||
# Get user_id from session state or use default
|
||||
user_id = getattr(retrieve_patient_info, 'user_id', 'default_user')
|
||||
user_id = getattr(retrieve_patient_info, "user_id", "default_user")
|
||||
|
||||
# Search Mem0
|
||||
results = mem0_client.search(
|
||||
@@ -45,7 +47,7 @@ def retrieve_patient_info(query: str) -> str:
|
||||
user_id=user_id,
|
||||
run_id="healthcare_session",
|
||||
limit=5,
|
||||
threshold=0.7 # Higher threshold for more relevant results
|
||||
threshold=0.7, # Higher threshold for more relevant results
|
||||
)
|
||||
|
||||
if not results:
|
||||
@@ -65,7 +67,7 @@ def schedule_appointment(date: str, time: str, reason: str) -> dict:
|
||||
"status": "success",
|
||||
"appointment_id": appointment_id,
|
||||
"confirmation": f"Appointment scheduled for {date} at {time} for {reason}",
|
||||
"message": "Please arrive 15 minutes early to complete paperwork."
|
||||
"message": "Please arrive 15 minutes early to complete paperwork.",
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +91,7 @@ IMPORTANT GUIDELINES:
|
||||
- For serious symptoms, always recommend consulting a healthcare professional.
|
||||
- Keep all patient information confidential.
|
||||
""",
|
||||
tools=[save_patient_info, retrieve_patient_info, schedule_appointment]
|
||||
tools=[save_patient_info, retrieve_patient_info, schedule_appointment],
|
||||
)
|
||||
|
||||
# Set Up Session and Runner
|
||||
@@ -101,18 +103,10 @@ USER_ID = "Alex"
|
||||
SESSION_ID = "session_001"
|
||||
|
||||
# Create a session
|
||||
session = session_service.create_session(
|
||||
app_name=APP_NAME,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
)
|
||||
session = session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID)
|
||||
|
||||
# Create the runner
|
||||
runner = Runner(
|
||||
agent=healthcare_agent,
|
||||
app_name=APP_NAME,
|
||||
session_service=session_service
|
||||
)
|
||||
runner = Runner(agent=healthcare_agent, app_name=APP_NAME, session_service=session_service)
|
||||
|
||||
|
||||
# Interact with the Healthcare Assistant
|
||||
@@ -121,21 +115,14 @@ async def call_agent_async(query, runner, user_id, session_id):
|
||||
print(f"\n>>> Patient: {query}")
|
||||
|
||||
# Format the user's message
|
||||
content = types.Content(
|
||||
role='user',
|
||||
parts=[types.Part(text=query)]
|
||||
)
|
||||
content = types.Content(role="user", parts=[types.Part(text=query)])
|
||||
|
||||
# Set user_id for tools to access
|
||||
save_patient_info.user_id = user_id
|
||||
retrieve_patient_info.user_id = user_id
|
||||
|
||||
# Run the agent
|
||||
async for event in runner.run_async(
|
||||
user_id=user_id,
|
||||
session_id=session_id,
|
||||
new_message=content
|
||||
):
|
||||
async for event in runner.run_async(user_id=user_id, session_id=session_id, new_message=content):
|
||||
if event.is_final_response():
|
||||
if event.content and event.content.parts:
|
||||
response = event.content.parts[0].text
|
||||
@@ -152,7 +139,7 @@ async def run_conversation():
|
||||
"Hi, I'm Alex. I've been having headaches for the past week, and I have a penicillin allergy.",
|
||||
runner=runner,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
session_id=SESSION_ID,
|
||||
)
|
||||
|
||||
# Request for health information
|
||||
@@ -160,7 +147,7 @@ async def run_conversation():
|
||||
"Can you tell me more about what might be causing my headaches?",
|
||||
runner=runner,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
session_id=SESSION_ID,
|
||||
)
|
||||
|
||||
# Schedule an appointment
|
||||
@@ -168,15 +155,12 @@ async def run_conversation():
|
||||
"I think I should see a doctor. Can you help me schedule an appointment for next Monday at 2pm?",
|
||||
runner=runner,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
session_id=SESSION_ID,
|
||||
)
|
||||
|
||||
# Test memory - should remember patient name, symptoms, and allergy
|
||||
await call_agent_async(
|
||||
"What medications should I avoid for my headaches?",
|
||||
runner=runner,
|
||||
user_id=USER_ID,
|
||||
session_id=SESSION_ID
|
||||
"What medications should I avoid for my headaches?", runner=runner, user_id=USER_ID, session_id=SESSION_ID
|
||||
)
|
||||
|
||||
|
||||
@@ -191,37 +175,28 @@ async def interactive_mode():
|
||||
session_id = f"session_{hash(patient_id) % 1000:03d}"
|
||||
|
||||
# Create session for this user
|
||||
session = session_service.create_session(
|
||||
app_name=APP_NAME,
|
||||
user_id=patient_id,
|
||||
session_id=session_id
|
||||
)
|
||||
session_service.create_session(app_name=APP_NAME, user_id=patient_id, session_id=session_id)
|
||||
|
||||
print(f"\nStarting conversation with patient ID: {patient_id}")
|
||||
print("Type your message and press Enter.")
|
||||
|
||||
while True:
|
||||
user_input = input("\n>>> Patient: ").strip()
|
||||
if user_input.lower() in ['exit', 'quit', 'bye']:
|
||||
if user_input.lower() in ["exit", "quit", "bye"]:
|
||||
print("Ending conversation. Thank you!")
|
||||
break
|
||||
|
||||
await call_agent_async(
|
||||
user_input,
|
||||
runner=runner,
|
||||
user_id=patient_id,
|
||||
session_id=session_id
|
||||
)
|
||||
await call_agent_async(user_input, runner=runner, user_id=patient_id, session_id=session_id)
|
||||
|
||||
|
||||
# Main execution
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='Healthcare Assistant with Memory')
|
||||
parser.add_argument('--demo', action='store_true', help='Run the demo conversation')
|
||||
parser.add_argument('--interactive', action='store_true', help='Run in interactive mode')
|
||||
parser.add_argument('--patient-id', type=str, default=USER_ID, help='Patient ID for the conversation')
|
||||
parser = argparse.ArgumentParser(description="Healthcare Assistant with Memory")
|
||||
parser.add_argument("--demo", action="store_true", help="Run the demo conversation")
|
||||
parser.add_argument("--interactive", action="store_true", help="Run in interactive mode")
|
||||
parser.add_argument("--patient-id", type=str, default=USER_ID, help="Patient ID for the conversation")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.demo:
|
||||
@@ -231,5 +206,3 @@ if __name__ == "__main__":
|
||||
else:
|
||||
# Default to demo mode if no arguments provided
|
||||
asyncio.run(run_conversation())
|
||||
|
||||
|
||||
|
||||
@@ -16,26 +16,21 @@ from mem0 import Memory
|
||||
|
||||
# Configure Mem0 with Grok 3 and Qdrant
|
||||
config = {
|
||||
"vector_store": {
|
||||
"provider": "qdrant",
|
||||
"config": {
|
||||
"embedding_model_dims": 384
|
||||
}
|
||||
},
|
||||
"vector_store": {"provider": "qdrant", "config": {"embedding_model_dims": 384}},
|
||||
"llm": {
|
||||
"provider": "xai",
|
||||
"config": {
|
||||
"model": "grok-3-beta",
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 2000,
|
||||
}
|
||||
},
|
||||
},
|
||||
"embedder": {
|
||||
"provider": "huggingface",
|
||||
"config": {
|
||||
"model": "all-MiniLM-L6-v2" # open embedding model
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# Instantiate memory layer
|
||||
@@ -57,20 +52,14 @@ def recommend_movie_with_memory(user_id: str, user_query: str):
|
||||
prompt += f"\nPreviously, the user mentioned: {past_memories}"
|
||||
|
||||
# Generate movie recommendation using Grok 3
|
||||
response = grok_client.chat.completions.create(
|
||||
model="grok-3-beta",
|
||||
messages=[
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
)
|
||||
response = grok_client.chat.completions.create(model="grok-3-beta", messages=[{"role": "user", "content": prompt}])
|
||||
recommendation = response.choices[0].message.content
|
||||
|
||||
# Store conversation in memory
|
||||
memory.add(
|
||||
[{"role": "user", "content": user_query},
|
||||
{"role": "assistant", "content": recommendation}],
|
||||
[{"role": "user", "content": user_query}, {"role": "assistant", "content": recommendation}],
|
||||
user_id=user_id,
|
||||
metadata={"category": "movie"}
|
||||
metadata={"category": "movie"},
|
||||
)
|
||||
|
||||
return recommendation
|
||||
@@ -81,10 +70,11 @@ if __name__ == "__main__":
|
||||
user_id = "arshi"
|
||||
recommend_movie_with_memory(user_id, "I'm looking for a movie to watch tonight. Any suggestions?")
|
||||
# OUTPUT: You have watched Intersteller last weekend and you don't like horror movies, maybe you can watch "Purple Hearts" today.
|
||||
recommend_movie_with_memory(user_id, "Can we skip the tearjerkers? I really enjoyed Notting Hill and Crazy Rich Asians.")
|
||||
recommend_movie_with_memory(
|
||||
user_id, "Can we skip the tearjerkers? I really enjoyed Notting Hill and Crazy Rich Asians."
|
||||
)
|
||||
# OUTPUT: Got it — no sad endings! You might enjoy "The Proposal" or "Love, Rosie". They’re both light-hearted romcoms with happy vibes.
|
||||
recommend_movie_with_memory(user_id, "Any light-hearted movie I can watch after work today?")
|
||||
# OUTPUT: Since you liked Crazy Rich Asians and The Proposal, how about "The Intern" or "Isn’t It Romantic"? Both are upbeat, funny, and perfect for relaxing.
|
||||
recommend_movie_with_memory(user_id, "I’ve already watched The Intern. Something new maybe?")
|
||||
# OUTPUT: No problem! Try "Your Place or Mine" - romcoms that match your taste and are tear-free!
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@ agent = Agent(
|
||||
name="Personal Agent",
|
||||
model=OpenAIChat(id="gpt-4o"),
|
||||
description="You are a helpful personal agent that helps me with day to day activities."
|
||||
"You can process both text and images.",
|
||||
markdown=True
|
||||
"You can process both text and images.",
|
||||
markdown=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -35,24 +35,16 @@ def chat_user(user_input: str = None, user_id: str = "user_123", image_path: str
|
||||
base64_image = base64.b64encode(image_file.read()).decode("utf-8")
|
||||
|
||||
# First: the text message
|
||||
text_msg = {
|
||||
"role": "user",
|
||||
"content": user_input
|
||||
}
|
||||
text_msg = {"role": "user", "content": user_input}
|
||||
|
||||
# Second: the image message
|
||||
image_msg = {
|
||||
"role": "user",
|
||||
"content": {
|
||||
"type": "image_url",
|
||||
"image_url": {
|
||||
"url": f"data:image/jpeg;base64,{base64_image}"
|
||||
}
|
||||
}
|
||||
"content": {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}},
|
||||
}
|
||||
|
||||
# Send both as separate message objects
|
||||
client.add([text_msg, image_msg], user_id=user_id, output_format='v1.1')
|
||||
client.add([text_msg, image_msg], user_id=user_id, output_format="v1.1")
|
||||
print("✅ Image uploaded and stored in memory.")
|
||||
|
||||
if user_input:
|
||||
@@ -92,10 +84,13 @@ print(chat_user("When is my test?", user_id=user_id))
|
||||
# OUTPUT: Your pilot's test is on your birthday, which is in five days. You're turning 25!
|
||||
# Good luck with your preparations, and remember to take some time to relax amidst the studying.
|
||||
|
||||
print(chat_user("This is the picture of what I brought with me in the trip to Bahamas",
|
||||
image_path="travel_items.jpeg", # this will be added to Mem0 memory
|
||||
user_id=user_id))
|
||||
print(chat_user("hey can you quickly tell me if brought my sunglasses to my trip, not able to find",
|
||||
user_id=user_id))
|
||||
print(
|
||||
chat_user(
|
||||
"This is the picture of what I brought with me in the trip to Bahamas",
|
||||
image_path="travel_items.jpeg", # this will be added to Mem0 memory
|
||||
user_id=user_id,
|
||||
)
|
||||
)
|
||||
print(chat_user("hey can you quickly tell me if brought my sunglasses to my trip, not able to find", user_id=user_id))
|
||||
# OUTPUT: Yes, you did bring your sunglasses on your trip to the Bahamas along with your laptop, face masks and other items..
|
||||
# Since you can't find them now, perhaps check the pockets of jackets you wore or in your luggage compartments.
|
||||
|
||||
@@ -7,6 +7,7 @@ In order to run this file, you need to set up your Mem0 API at Mem0 platform and
|
||||
export OPENAI_API_KEY="your_openai_api_key"
|
||||
export MEM0_API_KEY="your_mem0_api_key"
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from agents import Agent, Runner
|
||||
@@ -23,25 +24,19 @@ study_agent = Agent(
|
||||
- Identify topics the user has struggled with (e.g., "I'm confused", "this is hard")
|
||||
- Help with spaced repetition by suggesting topics to revisit based on last review time
|
||||
- Personalize answers using stored memories
|
||||
- Summarize PDFs or notes the user uploads""")
|
||||
- Summarize PDFs or notes the user uploads""",
|
||||
)
|
||||
|
||||
|
||||
# Upload and store PDF to Mem0
|
||||
def upload_pdf(pdf_url: str, user_id: str):
|
||||
pdf_message = {
|
||||
"role": "user",
|
||||
"content": {
|
||||
"type": "pdf_url",
|
||||
"pdf_url": {"url": pdf_url}
|
||||
}
|
||||
}
|
||||
pdf_message = {"role": "user", "content": {"type": "pdf_url", "pdf_url": {"url": pdf_url}}}
|
||||
client.add([pdf_message], user_id=user_id)
|
||||
print("✅ PDF uploaded and processed into memory.")
|
||||
|
||||
|
||||
# Main interaction loop with your personal study buddy
|
||||
async def study_buddy(user_id: str, topic: str, user_input: str):
|
||||
|
||||
memories = client.search(f"{topic}", user_id=user_id)
|
||||
memory_context = "n".join(f"- {m['memory']}" for m in memories)
|
||||
|
||||
@@ -56,9 +51,11 @@ Now respond to the user's new question or comment:
|
||||
result = await Runner.run(study_agent, prompt)
|
||||
response = result.final_output
|
||||
|
||||
client.add([
|
||||
{"role": "user", "content": f'''Topic: {topic}nUser: {user_input}nnStudy Assistant: {response}'''}
|
||||
], user_id=user_id, metadata={"topic": topic})
|
||||
client.add(
|
||||
[{"role": "user", "content": f"""Topic: {topic}nUser: {user_input}nnStudy Assistant: {response}"""}],
|
||||
user_id=user_id,
|
||||
metadata={"topic": topic},
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
@@ -78,7 +75,12 @@ async def main():
|
||||
|
||||
# Demonstrate spaced repetition prompting
|
||||
topic = "Momentum Conservation"
|
||||
print(await study_buddy(user_id, topic, "I think we covered this last week. Is it time to review momentum conservation again?"))
|
||||
print(
|
||||
await study_buddy(
|
||||
user_id, topic, "I think we covered this last week. Is it time to review momentum conservation again?"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
@@ -57,7 +57,7 @@ def initialize_memory():
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I prefer brief and concise responses without unnecessary explanations. I get frustrated when assistants are too wordy or repeat information I already know."
|
||||
"content": "I prefer brief and concise responses without unnecessary explanations. I get frustrated when assistants are too wordy or repeat information I already know.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
@@ -65,7 +65,7 @@ def initialize_memory():
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I like to listen to jazz music when I'm working, especially artists like Miles Davis and John Coltrane. I find it helps me focus and be more productive."
|
||||
"content": "I like to listen to jazz music when I'm working, especially artists like Miles Davis and John Coltrane. I find it helps me focus and be more productive.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
@@ -73,7 +73,7 @@ def initialize_memory():
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "I usually wake up at 7 AM and prefer reminders for meetings 30 minutes in advance. My most productive hours are between 9 AM and noon, so I try to schedule important tasks during that time."
|
||||
"content": "I usually wake up at 7 AM and prefer reminders for meetings 30 minutes in advance. My most productive hours are between 9 AM and noon, so I try to schedule important tasks during that time.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
@@ -81,7 +81,7 @@ def initialize_memory():
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "My favorite color is navy blue, and I prefer dark mode in all my apps. I'm allergic to peanuts, so please remind me to check ingredients when I ask about recipes or restaurants."
|
||||
"content": "My favorite color is navy blue, and I prefer dark mode in all my apps. I'm allergic to peanuts, so please remind me to check ingredients when I ask about recipes or restaurants.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
@@ -89,7 +89,7 @@ def initialize_memory():
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "My partner's name is Jamie, and we have a golden retriever named Max who is 3 years old. My parents live in Chicago, and I try to visit them once every two months."
|
||||
"content": "My partner's name is Jamie, and we have a golden retriever named Max who is 3 years old. My parents live in Chicago, and I try to visit them once every two months.",
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
@@ -135,11 +135,11 @@ def record_audio(filename="input.wav", record_seconds=5):
|
||||
stream.close()
|
||||
p.terminate()
|
||||
|
||||
with wave.open(filename, 'wb') as wf:
|
||||
with wave.open(filename, "wb") as wf:
|
||||
wf.setnchannels(channels)
|
||||
wf.setsampwidth(p.get_sample_size(fmt))
|
||||
wf.setframerate(rate)
|
||||
wf.writeframes(b''.join(frames))
|
||||
wf.writeframes(b"".join(frames))
|
||||
|
||||
|
||||
# ------------------ STT USING WHISPER ------------------
|
||||
@@ -147,10 +147,7 @@ def transcribe_whisper(audio_path):
|
||||
print("🔎 Transcribing with Whisper...")
|
||||
try:
|
||||
with open(audio_path, "rb") as audio_file:
|
||||
transcript = openai_client.audio.transcriptions.create(
|
||||
model="whisper-1",
|
||||
file=audio_file
|
||||
)
|
||||
transcript = openai_client.audio.transcriptions.create(model="whisper-1", file=audio_file)
|
||||
print(f"🗣️ You said: {transcript.text}")
|
||||
return transcript.text
|
||||
except Exception as e:
|
||||
@@ -165,9 +162,7 @@ def get_agent_response(user_input):
|
||||
|
||||
try:
|
||||
task = Task(
|
||||
description=f"Respond to: {user_input}",
|
||||
expected_output="A short and relevant reply.",
|
||||
agent=voice_agent
|
||||
description=f"Respond to: {user_input}", expected_output="A short and relevant reply.", agent=voice_agent
|
||||
)
|
||||
crew = Crew(
|
||||
agents=[voice_agent],
|
||||
@@ -175,22 +170,19 @@ def get_agent_response(user_input):
|
||||
process=Process.sequential,
|
||||
verbose=True,
|
||||
memory=True,
|
||||
memory_config={
|
||||
"provider": "mem0",
|
||||
"config": {"user_id": USER_ID}
|
||||
}
|
||||
memory_config={"provider": "mem0", "config": {"user_id": USER_ID}},
|
||||
)
|
||||
result = crew.kickoff()
|
||||
|
||||
# Extract the text response from the complex result object
|
||||
if hasattr(result, 'raw'):
|
||||
if hasattr(result, "raw"):
|
||||
return result.raw
|
||||
elif isinstance(result, dict) and 'raw' in result:
|
||||
return result['raw']
|
||||
elif isinstance(result, dict) and 'tasks_output' in result:
|
||||
outputs = result['tasks_output']
|
||||
elif isinstance(result, dict) and "raw" in result:
|
||||
return result["raw"]
|
||||
elif isinstance(result, dict) and "tasks_output" in result:
|
||||
outputs = result["tasks_output"]
|
||||
if outputs and isinstance(outputs, list) and len(outputs) > 0:
|
||||
return outputs[0].get('raw', str(result))
|
||||
return outputs[0].get("raw", str(result))
|
||||
|
||||
# Fallback to string representation if we can't extract the raw response
|
||||
return str(result)
|
||||
@@ -204,10 +196,7 @@ def get_agent_response(user_input):
|
||||
def speak_response(text):
|
||||
print(f"🤖 Agent: {text}")
|
||||
audio = tts_client.text_to_speech.convert(
|
||||
text=text,
|
||||
voice_id="JBFqnCBsd6RMkjVDRZzb",
|
||||
model_id="eleven_multilingual_v2",
|
||||
output_format="mp3_44100_128"
|
||||
text=text, voice_id="JBFqnCBsd6RMkjVDRZzb", model_id="eleven_multilingual_v2", output_format="mp3_44100_128"
|
||||
)
|
||||
play(audio)
|
||||
|
||||
@@ -220,7 +209,7 @@ def run_voice_agent():
|
||||
record_audio(tmp_audio.name)
|
||||
try:
|
||||
user_text = transcribe_whisper(tmp_audio.name)
|
||||
if user_text.lower() in ['exit', 'quit', 'stop']:
|
||||
if user_text.lower() in ["exit", "quit", "stop"]:
|
||||
print("👋 Exiting.")
|
||||
break
|
||||
response = get_agent_response(user_text)
|
||||
|
||||
Reference in New Issue
Block a user