Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/routers/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from utils.other import endpoints as auth
from models.app import App, ActionType
from utils.other.storage import upload_plugin_logo, delete_plugin_logo, upload_app_thumbnail, get_app_thumbnail_url
from utils.social import get_twitter_profile, get_twitter_timeline, verify_latest_tweet, \
from utils.social import get_twitter_profile, verify_latest_tweet, \
upsert_persona_from_twitter_profile, add_twitter_to_persona

router = APIRouter()
Expand Down
10 changes: 6 additions & 4 deletions backend/utils/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def get_available_apps(uid: str, include_reviews: bool = False) -> List[App]:
if cachedApps := get_generic_cache('get_public_approved_apps_data'):
print('get_public_approved_plugins_data from cache----------------------------')
public_approved_data = cachedApps
public_approved_data = get_public_approved_apps_db()
public_unapproved_data = get_public_unapproved_apps(uid)
private_data = get_private_apps(uid)
pass
Expand Down Expand Up @@ -394,7 +393,7 @@ async def generate_persona_prompt(uid: str, persona: dict):

tweets = None
if "twitter" in persona['connected_accounts']:
print("twitter in connected accounts---------------------------")
print("twitter is in connected accounts")
# Get latest tweets
tweets = await get_twitter_timeline(persona['twitter']['username'])
tweets = [{'tweet': tweet['text'], 'posted_at': tweet['created_at']} for tweet in tweets['timeline']]
Expand Down Expand Up @@ -474,13 +473,15 @@ def sync_update_persona_prompt(persona: dict):
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(update_persona_prompt(persona))
except Exception as e:
print(f"Error in update_persona_prompt for persona {persona.get('id', 'unknown')}: {str(e)}")
return None
finally:
loop.close()


async def update_persona_prompt(persona: dict):
"""Update a persona's chat prompt with latest facts and memories."""

# Get latest facts and user info
facts = get_facts(persona['uid'], limit=250)
user_name = get_user_name(persona['uid'])
Expand Down Expand Up @@ -533,7 +534,7 @@ async def update_persona_prompt(persona: dict):

# Add a guideline about tweets if they exist
if condensed_tweets:
persona_prompt += "7. Utilize condensed tweets to enhance authenticity, incorporating common expressions, opinions, and phrasing from {user_name}s social media presence.\n"
persona_prompt += "7. Utilize condensed tweets to enhance authenticity, incorporating common expressions, opinions, and phrasing from {user_name}'s social media presence.\n"

persona_prompt += f"""
**Rules:**
Expand All @@ -558,6 +559,7 @@ async def update_persona_prompt(persona: dict):

persona['persona_prompt'] = persona_prompt
persona['updated_at'] = datetime.now(timezone.utc)

update_persona_in_db(persona)
delete_app_cache_by_id(persona['id'])

Expand Down
27 changes: 17 additions & 10 deletions backend/utils/memories/process_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from models.task import Task, TaskStatus, TaskAction, TaskActionProvider
from models.trend import Trend
from models.notification_message import NotificationMessage
from utils.apps import get_available_apps
from utils.apps import get_available_apps, sync_update_persona_prompt
from utils.llm import obtain_emotional_message, retrieve_metadata_fields_from_transcript, \
summarize_open_glass, get_transcript_structure, generate_embedding, \
get_plugin_result, should_discard_memory, summarize_experience_text, new_facts_extractor, \
Expand Down Expand Up @@ -208,6 +208,19 @@ def save_structured_vector(uid: str, memory: Memory, update_only: bool = False):
update_vector_metadata(uid, memory.id, metadata)


def _update_personas_async(uid: str):
print(f"[PERSONAS] Starting persona updates in background thread for uid={uid}")
personas = get_omi_personas_by_uid_db(uid)
if personas:
threads = []
for persona in personas:
threads.append(threading.Thread(target=sync_update_persona_prompt, args=(persona,)))

[t.start() for t in threads]
[t.join() for t in threads]
print(f"[PERSONAS] Finished persona updates in background thread for uid={uid}")


def process_memory(
uid: str, language_code: str, memory: Union[Memory, CreateMemory, ExternalIntegrationCreateMemory],
force_process: bool = False, is_reprocess: bool = False
Expand All @@ -227,15 +240,9 @@ def process_memory(
threading.Thread(target=memory_created_webhook, args=(uid, memory,)).start()
# TODO: Bad code, cause the websocket was drop, need to check it carefully before enabling.
# Update persona prompts with new memory
# personas = get_omi_personas_by_uid_db(uid)
# if personas:
# threads = []
# print('updating personas after memory creation')
# for persona in personas:
# threads.append(threading.Thread(target=sync_update_persona_prompt, args=(persona,)))
#
# [t.start() for t in threads]
# [t.join() for t in threads]
print("before creating the thread for _update_personas_async")
threading.Thread(target=_update_personas_async, args=(uid,)).start()
print("after calling start for _update_personas_async")

# TODO: trigger external integrations here too

Expand Down
1 change: 1 addition & 0 deletions backend/utils/social.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async def get_twitter_profile(handle: str) -> Dict[str, Any]:
"X-RapidAPI-Host": rapid_api_host
}


def fetch_profile():
response = httpx.get(url, headers=headers, timeout=defaultTimeoutSec)
if response.status_code == 200:
Expand Down