Skip to content

Knowledge beta improvements #2545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
550 changes: 312 additions & 238 deletions Cargo.lock

Large diffs are not rendered by default.

305 changes: 244 additions & 61 deletions crates/chat-cli/src/cli/chat/cli/knowledge.rs

Large diffs are not rendered by default.

30 changes: 21 additions & 9 deletions crates/chat-cli/src/cli/chat/tools/knowledge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ impl Knowledge {
Knowledge::Update(update) => {
// Require at least one identifier (context_id or name)
if update.context_id.is_empty() && update.name.is_empty() && update.path.is_empty() {
eyre::bail!("Please provide either context_id or name or path to identify the context to update");
eyre::bail!(
"Please provide either context_id, name, or path to identify the knowledge base entry to update"
);
}

// Validate the path exists
Expand Down Expand Up @@ -310,8 +312,10 @@ impl Knowledge {
}

pub async fn invoke(&self, os: &Os, _updates: &mut impl Write) -> Result<InvokeOutput> {
// Get the async knowledge store singleton
let async_knowledge_store = KnowledgeStore::get_async_instance().await;
// Get the async knowledge store singleton with OS-aware directory
let async_knowledge_store = KnowledgeStore::get_async_instance_with_os(os)
.await
.map_err(|e| eyre::eyre!("Failed to access knowledge base: {}", e))?;
let mut store = async_knowledge_store.lock().await;

let result = match self {
Expand All @@ -325,7 +329,14 @@ impl Knowledge {
add.value.clone()
};

match store.add(&add.name, &value_to_use).await {
match store
.add(
&add.name,
&value_to_use,
crate::util::knowledge_store::AddOptions::with_db_defaults(os),
)
.await
{
Ok(context_id) => format!(
"Added '{}' to knowledge base with ID: {}. Track active jobs in '/knowledge status' with provided id.",
add.name, context_id
Expand Down Expand Up @@ -412,23 +423,24 @@ impl Knowledge {
.await
.unwrap_or_else(|e| format!("Failed to clear knowledge base: {}", e)),
Knowledge::Search(search) => {
// Only use a spinner for search, not a full progress bar
let results = store.search(&search.query, search.context_id.as_deref()).await;
match results {
Ok(results) => {
if results.is_empty() {
"No matching entries found in knowledge base".to_string()
format!("No matching entries found for query: \"{}\"", search.query)
} else {
let mut output = String::from("Search results:\n");
let mut output = format!("Search results for \"{}\":\n\n", search.query);
for result in results {
if let Some(text) = result.text() {
output.push_str(&format!("- {}\n", text));
output.push_str(&format!("{}\n\n", text));
}
}
output
}
},
Err(e) => format!("Search failed: {}", e),
Err(e) => {
format!("Search failed: {}", e)
},
}
},
Knowledge::Show => {
Expand Down
19 changes: 19 additions & 0 deletions crates/chat-cli/src/database/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ pub enum Setting {
ShareCodeWhispererContent,
EnabledThinking,
EnabledKnowledge,
KnowledgeDefaultIncludePatterns,
KnowledgeDefaultExcludePatterns,
KnowledgeMaxFiles,
KnowledgeChunkSize,
KnowledgeChunkOverlap,
SkimCommandKey,
ChatGreetingEnabled,
ApiTimeout,
Expand All @@ -47,6 +52,11 @@ impl AsRef<str> for Setting {
Self::ShareCodeWhispererContent => "codeWhisperer.shareCodeWhispererContentWithAWS",
Self::EnabledThinking => "chat.enableThinking",
Self::EnabledKnowledge => "chat.enableKnowledge",
Self::KnowledgeDefaultIncludePatterns => "knowledge.defaultIncludePatterns",
Self::KnowledgeDefaultExcludePatterns => "knowledge.defaultExcludePatterns",
Self::KnowledgeMaxFiles => "knowledge.maxFiles",
Self::KnowledgeChunkSize => "knowledge.chunkSize",
Self::KnowledgeChunkOverlap => "knowledge.chunkOverlap",
Self::SkimCommandKey => "chat.skimCommandKey",
Self::ChatGreetingEnabled => "chat.greeting.enabled",
Self::ApiTimeout => "api.timeout",
Expand Down Expand Up @@ -82,6 +92,11 @@ impl TryFrom<&str> for Setting {
"codeWhisperer.shareCodeWhispererContentWithAWS" => Ok(Self::ShareCodeWhispererContent),
"chat.enableThinking" => Ok(Self::EnabledThinking),
"chat.enableKnowledge" => Ok(Self::EnabledKnowledge),
"knowledge.defaultIncludePatterns" => Ok(Self::KnowledgeDefaultIncludePatterns),
"knowledge.defaultExcludePatterns" => Ok(Self::KnowledgeDefaultExcludePatterns),
"knowledge.maxFiles" => Ok(Self::KnowledgeMaxFiles),
"knowledge.chunkSize" => Ok(Self::KnowledgeChunkSize),
"knowledge.chunkOverlap" => Ok(Self::KnowledgeChunkOverlap),
"chat.skimCommandKey" => Ok(Self::SkimCommandKey),
"chat.greeting.enabled" => Ok(Self::ChatGreetingEnabled),
"api.timeout" => Ok(Self::ApiTimeout),
Expand Down Expand Up @@ -166,6 +181,10 @@ impl Settings {
self.get(key).and_then(|value| value.as_i64())
}

pub fn get_int_or(&self, key: Setting, default: usize) -> usize {
self.get_int(key).map_or(default, |v| v as usize)
}

pub async fn save_to_file(&self) -> Result<(), DatabaseError> {
if cfg!(test) {
return Ok(());
Expand Down
5 changes: 5 additions & 0 deletions crates/chat-cli/src/util/directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ pub fn chat_profiles_dir(os: &Os) -> Result<PathBuf> {
Ok(home_dir(os)?.join(".aws").join("amazonq").join("profiles"))
}

/// The directory for knowledge base storage
pub fn knowledge_bases_dir(os: &Os) -> Result<PathBuf> {
Ok(home_dir(os)?.join(".aws").join("amazonq").join("knowledge_bases"))
}

/// The path to the fig settings file
pub fn settings_path() -> Result<PathBuf> {
Ok(fig_data_dir()?.join("settings.json"))
Expand Down
Loading