emergingtrajectories.factsrag2

This is an experimental approach to tracking information regardless of source type. It will also power more than recent updates. Here’s how it works…

  1. All “Content Sources” (a new class type that obtains content) will send content directly to the Facts DB.

  2. The “Facts DB” will then extract all relevant facts for a prediction or research theme. It will keep cache the original content, will track the sources, and will also input all the facts into a RAG database.

  3. We can then query the DB for relevant facts on an ad hoc basis, rather than only for new content.

Attributes

Classes

FactRAGFileCache

This is a RAG-based fact database. We build a database of facts available in JSON and via RAG and use this as a basic search engine for information. We use ChromaDB to index all facts, but also maintain a list of facts, sources, etc. in a JSON file. Finally, we keep a cache of all content and assume URLs do not get updated; we'll change this process in the future.

FactBot

The FactBot is like a ChatBot but enables you to ask questions that reference an underlying RAG database (KnowledgeBaseFileCache), which then enables the chatbot to cite sourcable facts.

Functions

uri_to_local(→ str)

Convert a URI to a local file name. In this case, we typically will use an MD5 sum.

clean_fact_citations(→ str)

Converts fact IDs referenced in a piece of text to relevant source links, appending sources as end notes in the document/text.

Module Contents

emergingtrajectories.factsrag2._DEFAULT_NUM_SEARCH_RESULTS = 10
emergingtrajectories.factsrag2.facts_base_system_prompt = Multiline-String
Show Value
"""You are a researcher tasked with helping forecast economic and social trends. The title of our research project is: {statement_title}.

The project description is as follows...
{statement_description}

We will provide you with content from reports and web pages that is meant to help with the above. We will ask you to review these documents, create a set of bullet points to inform your thinking. Rather than using bullet points, please list each as F1, F2, F3, etc... So that we can reference it.

The content we provided you contains source numbers in the format 'SOURCE: #'. When you extract facts, please include the citation in square brackets, with the #, like [#], but replace "#" with the actual Source # from the crawled content we are providing you.

For example, if you are referring to a fact that came under --- SOURCE: 3 ---, you would write something like: "Data is already trending to hotter temperatures [3]." Do not include the "#" in the brackets, just the number.

Thus, a bullet point would look like this:
F1: (information) [1]
F2: (information) [1]
F3: (information) [2]

... and so on, where F1, F2, F3, etc. are facts, and [1], [2] are the source documents you are extracting the facts from.
"""
emergingtrajectories.factsrag2.facts_base_user_prompt = Multiline-String
Show Value
"""Today's date is {the_date}. We will now provide you with all the content we've managed to collect.

----------------------
{scraped_content}
----------------------

Please think step-by-step by (a) extracting critical bullet points from the above, and (b) share any insights you might have based on the facts.

The content we provided you contains source numbers in the format 'SOURCE: #'. When you extract facts, please include the citation in square brackets, with the #, like [#], but replace "#" with the actual Source # from the crawled content we are providing you.

For example, if you are referring to a fact that came under --- SOURCE: 3 ---, you would write something like: "Data is already trending to hotter temperatures [3]." Do not include the "#" in the brackets, just the actual number.

DO NOT PROVIDE A FORECAST, BUT SIMPLY STATE AND SHARE THE FACTS AND INSIGHTS YOU HAVE GATHERED.
"""
emergingtrajectories.factsrag2.fact_system_prompt = Multiline-String
Show Value
"""You are a researcher helping extract facts about {topic}, trends, and related observations. We will give you a piece of content scraped on the web. Please extract facts from this. Each fact should stand on its own, and can be several sentences long if need be. You can have as many facts as needed. For each fact, please start it as a new line with "---" as the bullet point. For example:

--- Fact 1... This is the fact.
--- Here is a second fact.
--- And a third fact.

Please do not include new lines between bullet points. Make sure you write your facts in ENGLISH. Translate any foreign language content/facts/observations into ENGLISH.

We will simply provide you with content and you will just provide facts."""
emergingtrajectories.factsrag2.uri_to_local(uri: str) str

Convert a URI to a local file name. In this case, we typically will use an MD5 sum.

Parameters:

uri (str) – The URI to convert.

Returns:

The MD5 sum of the URI.

Return type:

str

class emergingtrajectories.factsrag2.FactRAGFileCache(folder_path: str, openai_api_key: str, cache_file: str = 'cache.json', rag_db_folder='cdb', crawler=None)

This is a RAG-based fact database. We build a database of facts available in JSON and via RAG and use this as a basic search engine for information. We use ChromaDB to index all facts, but also maintain a list of facts, sources, etc. in a JSON file. Finally, we keep a cache of all content and assume URLs do not get updated; we’ll change this process in the future.

Parameters:
  • folder_path (str) – The folder where everything will be stored.

  • openai_api_key (str) – The OpenAI API key. Used for RAG embeddings.

  • cache_file (str, optional) – The name of the cache file. Defaults to “cache.json”.

  • rag_db_folder (str, optional) – The folder where the ChromaDB database will be stored. Defaults to “cdb”.

  • crawler (optional) – The crawler to use. Defaults to None, in which case a Playwright crawler will be used.

root_path
root_parsed
root_original
cache_file
rag_db_folder
openai_api_key
chromadb_client
facts_rag_collection
cache
get_facts_as_dict(n_results=-1, min_date: datetime.datetime = None) list

Get all facts as a list.

Parameters:

n_results (int, optional) – The number of results to return. Defaults to -1, in which case all results are returned.

Returns:

A list of fact dictionaries containing content, source, and added (the date string for when the fact was added).

Return type:

list

get_facts_as_list() list

Get all facts as a list.

Parameters:

None

Returns:

A list of facts (as strings).

Return type:

list

count_facts() int

Returns the number of facts in the knowledge database.

Returns:

The number of facts in the knowledge database.

Return type:

int

get_fact_details(fact_id: str) dict

Returns similar structure as query_to_fact_list() but for a specific fact ID. Returns NONE otherwise.

Parameters:

fact_id (str) – The fact ID to get.

Returns:

A dictionary with the content, source, added date, and added timestamp.

Return type:

dict

query_to_fact_list(query: str, n_results: int = 10, since_date: datetime.datetime = None) dict

Takes a query and finds the closest semantic matches to the query in the knowledge base.

Parameters:
  • query (str) – The query to search for.

  • n_results (int, optional) – The number of results to return. Defaults to 10.

  • since_date (datetime, optional) – The date to search from. Defaults to None, in which case all dates are searched.

Returns:

A list of the facts found, with the key being the fact ID and each fact having its source, add date, and content info.

Return type:

dict

query_to_fact_content(query: str, n_results: int = 10, since_date=None, skip_separator=False) str

Takes a query and finds the closest semantic matches to the query in the knowledge base.

Parameters:
  • query (str) – The query to search for.

  • n_results (int, optional) – The number of results to return. Defaults to 10.

  • since_date ([type], optional) – The date to search from. Defaults to None, in which case all dates are searched.

  • skip_separator (bool, optional) – Whether to prepend and append a note horizontal line and title to the string being returned. Defaults to False.

Returns:

The content of the facts found, along with the fact IDs.

Return type:

str

get_all_recent_facts(days: float = 1, skip_separator=False) str

Returns a list of all facts and sources added in the last n days.

Parameters:
  • days (float, optional) – The number of days to search back. Defaults to 1. Can be fractional as well.

  • skip_separator (bool, optional) – Whether to prepend and append a note horizontal line and title to the string being returned. Defaults to False.

Returns:

The content of the facts found, along with the fact IDs.

Return type:

str

get_fact_source(fact_id: str) str

Returns the source of a fact given its ID.

Parameters:

fact_id – The fact ID to get.

Returns:

The source of the fact.

Return type:

str

get_fact_content(fact_id: str) str

Returns the content of a fact given its ID.

Parameters:

fact_id – The fact ID to get.

Returns:

The content of the fact.

Return type:

str

add_fact(fact: str, url: str) bool

Adds a fact to the knowledge base.

Parameters:
  • fact (str) – The fact to add.

  • url (str) – The URL source of the fact.

Returns:

True if the fact was added, False otherwise.

Return type:

bool

add_facts(facts: list, sources: list) bool

Adds a facts to the knowledge base.

Parameters:
  • facts (list) – List of strings. Each string is a fact.

  • sources (list) – List of sources (e.g., URLs) for the facts.

Returns:

True if the facts were added, False otherwise.

Return type:

bool

facts_from_url(url: str, topic: str) None

Given a URL, extract facts from it and save them to ChromaDB and the facts dictionary. Also returns the facts in an array, in case one wants to analyze new facts.

Parameters:
  • url (str) – Location of the content.

  • topic (str) – a brief description of the research you are undertaking.

Crawls an RSS feed and its posts.

Parameters:
  • rss_url (str) – The URL of the RSS feed.

  • topic (str) – a brief description of the research you are undertaking.

new_get_new_info_news(newsapi_api_key, topic, queries, top_headlines=False) None

Uses the News API to find new information and extract facts from it.

Parameters:
  • newsapi_api_key (str) – The News API key.

  • topic (str) – a brief description of the research you are undertaking.

  • queries (list[str]) – A list of queries to search for.

  • top_headlines (bool, optional) – Whether to search for top headlines. Defaults to False.

get_ft_news(ft_user, ft_pass, topic) None

Uses the Financial Times Agent to find new information and extract facts from it.

Parameters:
  • ft_user (str) – The Financial Times username.

  • ft_pass (str) – The Financial Times password.

  • topic (str) – a brief description of the research you are undertaking.

new_get_new_info_google(google_api_key, google_search_id, google_search_queries, topic) None

Uses Google search to find new information and extract facts from it.

Parameters:
  • google_api_key (str) – The Google API key.

  • google_search_id (str) – The Google search ID.

  • google_search_queries (list[str]) – A list of queries to search for.

  • topic (str) – a brief description of the research you are undertaking.

save_state() None

Saves the in-memory changes to the knowledge base to the JSON cache file.

load_cache() None

Loads the cache from the cache file, or creates the relevant files and folders if one does not exist.

in_cache(uri: str) bool

Checks if a URI is in the cache already.

Parameters:

uri (str) – The URI to check.

Returns:

True if the URI is in the cache, False otherwise.

Return type:

bool

update_cache(uri: str, obtained_on: datetime.datetime, last_accessed: datetime.datetime) None

Updates the cache file for a given URI, specifically when it was obtained and last accessed.

Parameters:
  • uri (str) – The URI to update.

  • obtained_on (datetime) – The date and time when the content was obtained.

  • last_accessed (datetime) – The date and time when the content was last accessed.

log_access(uri: str) None

Saves the last accessed time and updates the accessed tracker for a given URI.

Parameters:

uri (str) – The URI to update.

get_unaccessed_content() list[str]

Returns a list of URIs that have not been accessed by the agent.

Returns:

A list of URIs that have not been accessed by the agent.

Return type:

list[str]

force_content(uri: str, content: str, check_exists: bool = True) bool

Forces a specific URI to have specific content (both HTML and text content). Used to fill old links that we don’t actually want to crawl.

Parameters:
  • uri (str) – The URI to force content for.

  • content (str) – The content to force.

  • check_exists (bool) – checks if content has already been included in the cache before forcing the new content.

Returns:

True if the content was forced, False otherwise.

Return type:

bool

get(uri: str) str

Returns the content for a given URI. If the content is not in the cache, it will be scraped and added to the cache.

Parameters:

uri (str) – The URI to get the content for.

Returns:

The content for the given URI.

Return type:

str

add_content(content: str, uri: str = None) None

Adds content to cache.

Parameters:
  • content (str) – The content to add to the cache.

  • uri (str, optional) – The URI to use for the content. Defaults to None, in which case an MD5 sum of the content will be used.

add_content_from_file(filepath: str, uri: str = None) None

Adds content from a text file to the cache.

Parameters:
  • filepath (str) – The path to the file to add to the cache.

  • uri (str, optional) – The URI to use for the content. Defaults to None, in which case an MD5 sum of the content will be used.

class emergingtrajectories.factsrag2.FactBot(knowledge_db: FactRAGFileCache, openai_api_key: str = None, chatbot: phasellm.llms.ChatBot = None)

The FactBot is like a ChatBot but enables you to ask questions that reference an underlying RAG database (KnowledgeBaseFileCache), which then enables the chatbot to cite sourcable facts.

Parameters:
  • knowledge_db (FactRAGFileCache) – The knowledge database to use.

  • openai_api_key (str, optional) – The OpenAI API key. Defaults to None.

  • chatbot (ChatBot, optional) – The PhaseLLB chatbot to use. Defaults to None, in which case an OpenAI chatbot is used (and the OpenAI API key must be provided).

knowledge_db
ask(question: str, clean_sources: bool = True) str

Ask a question to the FactBot. This will query the underlying knowledge database and use the returned facts to answer the question.

Parameters:
  • question (str) – The question to ask.

  • clean_sources (bool, optional) – Whether to clean the sources in the response. Defaults to True; in this case, it will replace fact IDs with relevant source links at the end of the response.

Returns:

The response to the question.

Return type:

str

source(fact_id: str) str

Returns the URL source for a given fact ID.

Parameters:

fact_id (str) – The fact ID to get the source for.

Returns:

The URL source for the given fact ID.

Return type:

str

clean_and_source_to_html(text_to_clean: str, start_count: int = 0) list

Returns a formatted response with sourced HTML. This is used for emergingtrajectories.com and acts as a base for anyone else wanting to build similar features.

Parameters:
  • text_to_clean – The text to clean/cite/source.

  • start_count – The starting count for the sources.

Returns:

two strings – the actual response in the first case, and the sources in the second case, and an integer representing the new source count.

Return type:

list

emergingtrajectories.factsrag2.clean_fact_citations(knowledge_db: FactRAGFileCache, text_to_clean: str) str

Converts fact IDs referenced in a piece of text to relevant source links, appending sources as end notes in the document/text.

Parameters:
  • knowledge_db (FactRAGFileCache) – The knowledge database to use for fact lookups.

  • text_to_clean (str) – The text to clean.

Returns:

The cleaned text.

Return type:

str