Skip to contents

add_context lets you add context info (in the shape of a context dataframe containing embeddings and text info) to supplement or narrow down the answers from the LLM.

Usage

add_context(workflow_obj, context_df, context_usage_mandatory = FALSE)

Arguments

workflow_obj

A workflow object containing all parameters describing the workflow required

context_df

a dataframe containing the text embeddings and text information that can be used to retrieve relevant context

context_usage_mandatory

a boolean value (defaults to FALSE) to let the LLM know if it should ONLY use the context to answer, or if it can use the context optionally to answer a question.

Value

a workflow object with the new added context parameter.

Details

For this to work you need to add a context_df that was build using the generate_document_embeddings function.

Examples


conn <- get_ollama_connection()

document <- "Standing proudly on the Île de la Cité in the heart of Paris, 
France's capital city, lies one of the world's most beloved and historic 
landmarks: the magnificent Notre Dame Cathedral. This Gothic masterpiece 
has been welcoming pilgrims and tourists alike for over 850 years, since its 
construction began in 1163 under King Louis VII. With its towering spires, 
stunning stained glass windows, and intricate stone carvings, this beautiful 
church is a testament to medieval architecture and engineering skill. 
Unfortunately, a devastating fire ravaged the cathedral on April 15, 2019, 
but thanks to swift action from firefighters and restoration efforts 
underway, Notre Dame continues to inspire awe in those who visit her."

writeLines(document, con = "doc1.txt")

write_vectors_to_feather_file(file_name = "doc1.feather",
vector_data = generate_document_embeddings(conn, 
document_path = "doc1.txt", splitter = "paragraph"))

notre_dame_embeddings <- load_context_embeddings_from_feather_files(filenames = "doc1.feather")

my_workflow <- ai_workflow() |> 
set_system_prompt(system_prompt="You are a helpful AI assistant. 
Answer to the best of your knowledge.") |>
add_context(context_df = notre_dame_embeddings, use_context_mandatory=TRUE)
#> Error in add_context(set_system_prompt(ai_workflow(), system_prompt = "You are a helpful AI assistant. \nAnswer to the best of your knowledge."),     context_df = notre_dame_embeddings, use_context_mandatory = TRUE): unused argument (use_context_mandatory = TRUE)