improve context and several fixes

This commit is contained in:
LUIS NOVO 2024-11-18 22:01:49 -03:00
parent 4a5d47d934
commit 7f79f8224f
5 changed files with 25 additions and 30 deletions

View file

@ -180,11 +180,15 @@ class ObjectModel(BaseModel):
f"Failed to delete {self.__class__.table_name}"
)
def relate(self, relationship: str, target_id: str) -> Any:
def relate(
self, relationship: str, target_id: str, data: Optional[Dict] = {}
) -> Any:
if not relationship or not target_id or not self.id:
raise InvalidInputError("Relationship and target ID must be provided")
try:
return repo_relate(self.id, relationship, target_id)
return repo_relate(
source=self.id, relationship=relationship, target=target_id, data=data
)
except Exception as e:
logger.error(f"Error creating relationship: {str(e)}")
logger.exception(e)

View file

@ -33,16 +33,12 @@ class Notebook(ObjectModel):
def sources(self) -> List["Source"]:
try:
srcs = repo_query(f"""
select * OMIT full_text from (
select
<- source as source
from reference
where out={self.id}
fetch source
)
order by source.updated desc
select * omit source.full_text from (
select in as source from reference where out={self.id}
fetch source
) order by source.updated desc
""")
return [Source(**src["source"][0]) for src in srcs] if srcs else []
return [Source(**src["source"]) for src in srcs] if srcs else []
except Exception as e:
logger.error(f"Error fetching sources for notebook {self.id}: {str(e)}")
logger.exception(e)
@ -52,16 +48,12 @@ class Notebook(ObjectModel):
def notes(self) -> List["Note"]:
try:
srcs = repo_query(f"""
select * OMIT content from (
select
<- note as note
from artifact
where out={self.id}
fetch note
)
order by updated desc
select * omit note.content, note.embedding from (
select in as note from artifact where out={self.id}
fetch note
) order by note.updated desc
""")
return [Note(**src["note"][0]) for src in srcs] if srcs else []
return [Note(**src["note"]) for src in srcs] if srcs else []
except Exception as e:
logger.error(f"Error fetching notes for notebook {self.id}: {str(e)}")
logger.exception(e)

View file

@ -32,11 +32,12 @@ def build_context(notebook_id):
if "not in" in status:
continue
item: Union[Note, Source] = ObjectModel.get(id)
if not item:
try:
item: Union[Note, Source] = ObjectModel.get(id)
except Exception:
continue
if "summary" in status:
if "insights" in status:
st.session_state[notebook_id]["context"][item_type] += [
item.get_context(context_size="short")
]

View file

@ -1,6 +1,6 @@
source_context_icons = [
"⛔ not in context",
"🟡 summary",
"🟡 insights",
"🟢 full content",
]

View file

@ -5,7 +5,7 @@ from humanize import naturaltime
from open_notebook.domain.models import model_manager
from open_notebook.domain.notebook import Note
from open_notebook.graphs.multipattern import graph as pattern_graph
from open_notebook.graphs.prompt import graph as prompt_graph
from open_notebook.utils import surreal_clean
from pages.components import note_panel
@ -34,10 +34,8 @@ def note_panel_dialog(note: Optional[Note] = None, notebook_id=None):
def make_note_from_chat(content, notebook_id=None):
# todo: make this more efficient
patterns = [
"Based on the Note below, please provide a Title for this content, with max 15 words"
]
output = pattern_graph.invoke(dict(content_stack=[content], patterns=patterns))
prompt = "Based on the Note below, please provide a Title for this content, with max 15 words"
output = prompt_graph.invoke(dict(input_text=content, prompt=prompt))
title = surreal_clean(output["output"])
note = Note(