Skip to content

Commit

Permalink
Fetch public plugins from cache for triggers (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin authored Nov 17, 2024
2 parents da5a273 + 6132fa6 commit 0ec5de2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
8 changes: 7 additions & 1 deletion backend/utils/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
from database.redis_db import get_enabled_plugins, get_plugin_installs_count, get_plugin_reviews, get_generic_cache, \
set_generic_cache
from models.app import App
from utils.plugins import weighted_rating


def weighted_rating(plugin):
C = 3.0 # Assume 3.0 is the mean rating across all plugins
m = 5 # Minimum number of ratings required to be considered
R = plugin.rating_avg or 0
v = plugin.rating_count or 0
return (v / (v + m) * R) + (m / (v + m) * C)

def get_available_apps(uid: str, include_reviews: bool = False) -> List[App]:
private_data = []
public_approved_data = []
Expand Down
4 changes: 3 additions & 1 deletion backend/utils/memories/process_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import database.trends as trends_db
from database.plugins import record_plugin_usage
from database.vector_db import upsert_vector2, update_vector_metadata
from models.app import App
from models.facts import FactDB
from models.memory import *
from models.plugin import Plugin, UsageHistoryType
from models.task import Task, TaskStatus, TaskAction, TaskActionProvider
from models.trend import Trend
from utils.apps import get_available_apps
from utils.llm import obtain_emotional_message, retrieve_metadata_fields_from_transcript
from utils.llm import summarize_open_glass, get_transcript_structure, generate_embedding, \
get_plugin_result, should_discard_memory, summarize_experience_text, new_facts_extractor, \
Expand Down Expand Up @@ -102,7 +104,7 @@ def _get_memory_obj(uid: str, structured: Structured, memory: Union[Memory, Crea


def _trigger_plugins(uid: str, memory: Memory, is_reprocess: bool = False):
plugins: List[Plugin] = get_plugins_data_from_db(uid)
plugins: List[App] = get_available_apps(uid)
filtered_plugins = [plugin for plugin in plugins if plugin.works_with_memories() and plugin.enabled]
memory.plugins_results = []
threads = []
Expand Down
23 changes: 9 additions & 14 deletions backend/utils/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from database.plugins import record_plugin_usage
from database.redis_db import get_enabled_plugins, get_plugin_reviews, get_plugin_installs_count, get_generic_cache, \
set_generic_cache
from models.app import App
from models.memory import Memory, MemorySource
from models.notification_message import NotificationMessage
from models.plugin import Plugin, UsageHistoryType
from utils.apps import get_available_apps, weighted_rating
from utils.notifications import send_notification
from utils.other.endpoints import timeit
from utils.llm import get_metoring_message
Expand Down Expand Up @@ -55,7 +57,7 @@ def get_contents(path):
get_contents(item["path"])

get_contents(path)
set_generic_cache(f'get_github_docs_content_{repo}_{path}', docs_content, 60 * 24*7)
set_generic_cache(f'get_github_docs_content_{repo}_{path}', docs_content, 60 * 24 * 7)
return docs_content


Expand All @@ -71,14 +73,6 @@ def get_plugin_by_id(plugin_id: str) -> Optional[Plugin]:
return next((p for p in plugins if p.id == plugin_id), None)


def weighted_rating(plugin):
C = 3.0 # Assume 3.0 is the mean rating across all plugins
m = 5 # Minimum number of ratings required to be considered
R = plugin.rating_avg or 0
v = plugin.rating_count or 0
return (v / (v + m) * R) + (m / (v + m) * C)


def get_plugins_data_from_db(uid: str, include_reviews: bool = False) -> List[Plugin]:
private_data = []
public_data = []
Expand Down Expand Up @@ -157,7 +151,7 @@ def trigger_external_integrations(uid: str, memory: Memory) -> list:
if not memory or memory.discarded:
return []

plugins: List[Plugin] = get_plugins_data_from_db(uid)
plugins: List[App] = get_available_apps(uid)
filtered_plugins = [plugin for plugin in plugins if
plugin.triggers_on_memory_creation() and plugin.enabled and not plugin.deleted]
if not filtered_plugins:
Expand All @@ -166,7 +160,7 @@ def trigger_external_integrations(uid: str, memory: Memory) -> list:
threads = []
results = {}

def _single(plugin: Plugin):
def _single(plugin: App):
if not plugin.external_integration.webhook_url:
return

Expand Down Expand Up @@ -220,7 +214,7 @@ async def trigger_realtime_integrations(uid: str, segments: list[dict]):


def _trigger_realtime_integrations(uid: str, token: str, segments: List[dict]) -> dict:
plugins: List[Plugin] = get_plugins_data_from_db(uid, include_reviews=False)
plugins: List[App] = get_available_apps(uid)
filtered_plugins = [
plugin for plugin in plugins if
plugin.triggers_realtime() and plugin.enabled and not plugin.deleted
Expand All @@ -231,7 +225,7 @@ def _trigger_realtime_integrations(uid: str, token: str, segments: List[dict]) -
threads = []
results = {}

def _single(plugin: Plugin):
def _single(plugin: App):
if not plugin.external_integration.webhook_url:
return

Expand Down Expand Up @@ -271,7 +265,8 @@ def _single(plugin: Plugin):
send_plugin_notification(token, plugin.name, plugin.id, message)
results[plugin.id] = message
elif len(prompt) > 8000:
send_plugin_notification(token, plugin.name, plugin.id, f"Prompt too long: {len(prompt)}/8000 characters. Please shorten.")
send_plugin_notification(token, plugin.name, plugin.id,
f"Prompt too long: {len(prompt)}/8000 characters. Please shorten.")
print(f"Plugin {plugin.id} prompt too long, length: {len(prompt)}/8000")

except Exception as e:
Expand Down

0 comments on commit 0ec5de2

Please sign in to comment.