From f0b06142bf09812f9209d6e001fa4cf57e88d3c9 Mon Sep 17 00:00:00 2001 From: Believethehype Date: Tue, 27 Feb 2024 13:53:01 +0100 Subject: [PATCH] collect multiple events when list is given (speed up) --- nostr_dvm/tasks/summarization_huggingchat.py | 20 ++++++++---- nostr_dvm/utils/nostr_utils.py | 31 +++++++++++++++++++ .../src/components/SearchResultTable.vue | 11 ------- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/nostr_dvm/tasks/summarization_huggingchat.py b/nostr_dvm/tasks/summarization_huggingchat.py index 8e38951..73208dd 100644 --- a/nostr_dvm/tasks/summarization_huggingchat.py +++ b/nostr_dvm/tasks/summarization_huggingchat.py @@ -6,7 +6,7 @@ from nostr_dvm.utils.admin_utils import AdminConfig from nostr_dvm.utils.definitions import EventDefinitions from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag -from nostr_dvm.utils.nostr_utils import get_referenced_event_by_id, get_event_by_id +from nostr_dvm.utils.nostr_utils import get_referenced_event_by_id, get_event_by_id, get_events_by_ids from nostr_sdk import Tag """ @@ -42,15 +42,17 @@ class TextSummarizationHuggingChat(DVMTaskInterface): def create_request_from_nostr_event(self, event, client=None, dvm_config=None): request_form = {"jobID": event.id().to_hex() + "_" + self.NAME.replace(" ", "")} prompt = "" + collect_events = [] for tag in event.tags(): if tag.as_vec()[0] == 'i': input_type = tag.as_vec()[2] if input_type == "text": - prompt = tag.as_vec()[1] + prompt += tag.as_vec()[1] + "\n" elif input_type == "event": - evt = get_event_by_id(tag.as_vec()[1], client=client, config=dvm_config) - prompt = evt.content() + collect_events.append(tag.as_vec()[1]) + #evt = get_event_by_id(tag.as_vec()[1], client=client, config=dvm_config) + #prompt += evt.content() + "\n" elif input_type == "job": evt = get_referenced_event_by_id(event_id=tag.as_vec()[1], client=client, kinds=[EventDefinitions.KIND_NIP90_RESULT_EXTRACT_TEXT, @@ -72,6 +74,12 @@ class TextSummarizationHuggingChat(DVMTaskInterface): else: prompt = evt.content() + + evts = get_events_by_ids(collect_events, client=client, config=dvm_config) + if evts is not None: + for evt in evts: + prompt += evt.content() + "\n" + options = { "prompt": prompt, } @@ -96,7 +104,7 @@ class TextSummarizationHuggingChat(DVMTaskInterface): try: chatbot = hugchat.ChatBot(cookies=cookies.get_dict()) # or cookie_path="usercookies/.json" - query_result = chatbot.query("Summarize the following text in maximum 5 sentences: " + options["prompt"]) + query_result = chatbot.query("Summarize the following notes: " + options["prompt"]) print(query_result["text"]) # or query_result.text or query_result["text"] return str(query_result["text"]).lstrip() @@ -116,7 +124,7 @@ def build_example(name, identifier, admin_config): nip89info = { "name": name, "image": "https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg", - "about": "I use a LLM connected via Huggingchat", + "about": "I use a LLM connected via Huggingchat to summarize Inputs", "encryptionSupported": True, "cashuAccepted": True, "nip90Params": {} diff --git a/nostr_dvm/utils/nostr_utils.py b/nostr_dvm/utils/nostr_utils.py index 767106b..b3304b4 100644 --- a/nostr_dvm/utils/nostr_utils.py +++ b/nostr_dvm/utils/nostr_utils.py @@ -37,6 +37,37 @@ def get_event_by_id(event_id: str, client: Client, config=None) -> Event | None: return None +def get_events_by_ids(event_ids, client: Client, config=None) -> List | None: + search_ids = [] + for event_id in event_ids: + split = event_id.split(":") + if len(split) == 3: + pk = PublicKey.from_hex(split[1]) + id_filter = Filter().author(pk).custom_tag(SingleLetterTag.lowercase(Alphabet.D), [split[2]]) + events = client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) + else: + if str(event_id).startswith('note'): + event_id = EventId.from_bech32(event_id) + elif str(event_id).startswith("nevent"): + event_id = Nip19Event.from_bech32(event_id).event_id() + elif str(event_id).startswith('nostr:note'): + event_id = EventId.from_nostr_uri(event_id) + elif str(event_id).startswith("nostr:nevent"): + event_id = Nip19Event.from_nostr_uri(event_id).event_id() + + else: + event_id = EventId.from_hex(event_id) + search_ids.append(event_id) + + id_filter = Filter().ids(search_ids) + events = client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) + if len(events) > 0: + + return events + else: + return None + + def get_events_by_id(event_ids: list, client: Client, config=None) -> list[Event] | None: id_filter = Filter().ids(event_ids) events = client.get_events_of([id_filter], timedelta(seconds=config.RELAY_TIMEOUT)) diff --git a/ui/noogle/src/components/SearchResultTable.vue b/ui/noogle/src/components/SearchResultTable.vue index de2501d..8da3ef4 100644 --- a/ui/noogle/src/components/SearchResultTable.vue +++ b/ui/noogle/src/components/SearchResultTable.vue @@ -23,17 +23,7 @@ Highlighter Nostrudel - - - @@ -51,7 +41,6 @@ const sortType: SortType = "desc"; const headers: Header[] = [ { text: "Results:", value: "content", fixed:true}, - // { text: "Time", value: "indicator.time", sortable: true, }, ];