-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the create facts action on external integration (#1967)
## deploy plan - [ ] deploy docs - [ ] clean the create_memory integration - [ ] deploy backend
- Loading branch information
Showing
13 changed files
with
444 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import List, Tuple, Optional | ||
|
||
import database.facts as facts_db | ||
from database.auth import get_user_name | ||
from models.facts import Fact | ||
|
||
def get_prompt_facts(uid: str) -> str: | ||
user_name, user_made_facts, generated_facts = get_prompt_data(uid) | ||
facts_str = f'you already know the following facts about {user_name}: \n{Fact.get_facts_as_str(generated_facts)}.' | ||
if user_made_facts: | ||
facts_str += f'\n\n{user_name} also shared the following about self: \n{Fact.get_facts_as_str(user_made_facts)}' | ||
return user_name, facts_str + '\n' | ||
|
||
|
||
def get_prompt_data(uid: str) -> Tuple[str, List[Fact], List[Fact]]: | ||
# TODO: cache this | ||
existing_facts = facts_db.get_facts(uid, limit=100) | ||
user_made = [Fact(**fact) for fact in existing_facts if fact['manually_added']] | ||
# TODO: filter only reviewed True | ||
generated = [Fact(**fact) for fact in existing_facts if not fact['manually_added']] | ||
user_name = get_user_name(uid) | ||
# print('get_prompt_data', user_name, len(user_made), len(generated)) | ||
return user_name, user_made, generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
from typing import List, Tuple | ||
from typing import List, Tuple, Optional | ||
|
||
import database.facts as facts_db | ||
from database.auth import get_user_name | ||
from models.facts import Fact | ||
from models.facts import FactDB | ||
from models.integrations import ExternalIntegrationCreateFact | ||
from utils.llm import extract_facts_from_text | ||
|
||
def process_external_integration_fact(uid: str, fact_data: ExternalIntegrationCreateFact, app_id: str) -> List[FactDB]: | ||
fact_data.app_id = app_id | ||
|
||
def get_prompt_facts(uid: str) -> str: | ||
user_name, user_made_facts, generated_facts = get_prompt_data(uid) | ||
facts_str = f'you already know the following facts about {user_name}: \n{Fact.get_facts_as_str(generated_facts)}.' | ||
if user_made_facts: | ||
facts_str += f'\n\n{user_name} also shared the following about self: \n{Fact.get_facts_as_str(user_made_facts)}' | ||
return user_name, facts_str + '\n' | ||
# Extract facts from text | ||
extracted_facts = extract_facts_from_text( | ||
uid, | ||
fact_data.text, | ||
fact_data.text_source_spec if fact_data.text_source_spec else fact_data.text_source.value | ||
) | ||
if not extracted_facts or len(extracted_facts) == 0: | ||
return [] | ||
|
||
saved_facts = [] | ||
|
||
def get_prompt_data(uid: str) -> Tuple[str, List[Fact], List[Fact]]: | ||
# TODO: cache this | ||
existing_facts = facts_db.get_facts(uid, limit=100) | ||
user_made = [Fact(**fact) for fact in existing_facts if fact['manually_added']] | ||
# TODO: filter only reviewed True | ||
generated = [Fact(**fact) for fact in existing_facts if not fact['manually_added']] | ||
user_name = get_user_name(uid) | ||
# print('get_prompt_data', user_name, len(user_made), len(generated)) | ||
return user_name, user_made, generated | ||
# Save each extracted fact | ||
for fact in extracted_facts: | ||
fact_db = FactDB.from_fact(fact, uid, None, None) | ||
fact_db.manually_added = False | ||
fact_db.app_id = app_id | ||
saved_facts.append(fact_db) | ||
facts_db.save_facts(uid, [fact_db.dict() for fact_db in saved_facts]) | ||
|
||
return saved_facts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.