Skip to content

Commit

Permalink
Merge branch 'v1.0' into jsibbison-20241212-fmt-ci-check
Browse files Browse the repository at this point in the history
* v1.0:
  turn on user prompting (#453)
  Fix typos and run cargo fmt (#447)
  fix: ready/notification prompts (#382)
  Raise error if viewing a file greater than 2MB (#449)
  • Loading branch information
michaelneale committed Dec 12, 2024
2 parents 4ae3f5c + b3f29b3 commit b6e9f91
Show file tree
Hide file tree
Showing 26 changed files with 489 additions and 288 deletions.
8 changes: 4 additions & 4 deletions crates/goose-cli/src/commands/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn handle_configure(
("openai", "OpenAI", "GPT-4o etc"),
("databricks", "Databricks", "Models on AI Gateway"),
("ollama", "Ollama", "Local open source models"),
("anthropic", "Anthropic", "Claude models"),
("anthropic", "Anthropic", "Claude models"),
])
.interact()?
.to_string()
Expand All @@ -68,7 +68,7 @@ pub async fn handle_configure(
// If the key is in the env, ask if we want to save to keyring
else if let Ok(value) = get_keyring_secret(key, KeyRetrievalStrategy::EnvironmentOnly) {
let _ = cliclack::log::info(format!("Detected {} in env, we can use this from your environment.\nIt will need to continue to be set in future goose usage.", key));
if cliclack::confirm("Would you like to save it to your kerying?").interact()? {
if cliclack::confirm("Would you like to save it to your keyring?").interact()? {
save_to_keyring(key, &value)?;
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ pub fn get_required_keys(provider_name: &str) -> Vec<&'static str> {
"openai" => vec!["OPENAI_API_KEY"],
"databricks" => vec!["DATABRICKS_HOST"],
"ollama" => vec!["OLLAMA_HOST"],
"anthropic" => vec!["ANTHROPIC_API_KEY"], // Removed ANTHROPIC_HOST since we use a fixed endpoint
"anthropic" => vec!["ANTHROPIC_API_KEY"], // Removed ANTHROPIC_HOST since we use a fixed endpoint
_ => panic!("Invalid provider name"),
}
}
}
8 changes: 4 additions & 4 deletions crates/goose-cli/src/profile.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Result;
use goose::key_manager::{get_keyring_secret, KeyRetrievalStrategy};
use goose::providers::configs::{
DatabricksAuth, DatabricksProviderConfig, OllamaProviderConfig, OpenAiProviderConfig,
ProviderConfig, AnthropicProviderConfig,
AnthropicProviderConfig, DatabricksAuth, DatabricksProviderConfig, OllamaProviderConfig,
OpenAiProviderConfig, ProviderConfig,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn get_provider_config(provider_name: &str, model: String) -> ProviderConfig
.expect("ANTHROPIC_API_KEY not available in env or the keychain\nSet an env var or rerun `goose configure`");

ProviderConfig::Anthropic(AnthropicProviderConfig {
host: "https://api.anthropic.com".to_string(), // Default Anthropic API endpoint
host: "https://api.anthropic.com".to_string(), // Default Anthropic API endpoint
api_key,
model,
temperature: None,
Expand All @@ -124,4 +124,4 @@ pub fn get_provider_config(provider_name: &str, model: String) -> ProviderConfig
}
_ => panic!("Invalid provider name"),
}
}
}
6 changes: 2 additions & 4 deletions crates/goose-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ async fn main() -> anyhow::Result<()> {
let settings = configuration::Settings::new()?;

// load secret key from GOOSE_SERVER__SECRET_KEY environment variable
let secret_key = std::env::var("GOOSE_SERVER__SECRET_KEY")
.unwrap_or_else(|_| {
"test".to_string()
});
let secret_key =
std::env::var("GOOSE_SERVER__SECRET_KEY").unwrap_or_else(|_| "test".to_string());

// Create app state
let state = state::AppState::new(settings.provider.into_config(), secret_key.clone())?;
Expand Down
2 changes: 1 addition & 1 deletion crates/goose-server/src/routes/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ mod tests {
api_key: "test-key".to_string(),
model: "test-model".to_string(),
temperature: None,
max_tokens: None
max_tokens: None,
}),
secret_key: "test-secret".to_string(),
};
Expand Down
2 changes: 1 addition & 1 deletion crates/goose-server/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ impl Clone for AppState {
secret_key: self.secret_key.clone(),
}
}
}
}
5 changes: 1 addition & 4 deletions crates/goose/benches/tokenization_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use goose::token_counter::TokenCounter;
fn benchmark_tokenization(c: &mut Criterion) {
let counter = TokenCounter::new();
let lengths = [1_000, 5_000, 10_000, 50_000, 100_000, 124_000, 200_000];
let models = [
"gpt-4o",
"claude-3.5-sonnet"
];
let models = ["gpt-4o", "claude-3.5-sonnet"];

for &length in &lengths {
for model_name in models {
Expand Down
80 changes: 42 additions & 38 deletions crates/goose/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::models::message::{Message, ToolRequest};
use crate::models::tool::{Tool, ToolCall};
use crate::prompt_template::load_prompt_file;
use crate::providers::base::Provider;
use crate::systems::{System, Resource};
use crate::systems::{Resource, System};
use crate::token_counter::TokenCounter;
use serde::Serialize;

Expand Down Expand Up @@ -133,8 +133,11 @@ impl Agent {
load_prompt_file("system.md", &context).map_err(|e| AgentError::Internal(e.to_string()))
}

async fn get_systems_resources(&self) -> AgentResult<HashMap<String, HashMap<String, (Resource,String)>>> {
let mut system_resource_content: HashMap<String, HashMap<String, (Resource, String)>> = HashMap::new();
async fn get_systems_resources(
&self,
) -> AgentResult<HashMap<String, HashMap<String, (Resource, String)>>> {
let mut system_resource_content: HashMap<String, HashMap<String, (Resource, String)>> =
HashMap::new();
for system in &self.systems {
let system_status = system
.status()
Expand All @@ -152,16 +155,15 @@ impl Agent {
Ok(system_resource_content)
}


/// Setup the next inference by budgeting the context window as well as we can
async fn prepare_inference(
&self,
system_prompt: &str,
tools: &Vec<Tool>,
messages: &Vec<Message>,
pending: &Vec<Message>,
target_limit: usize,
) -> AgentResult<Vec<Message>> {
&self,
system_prompt: &str,
tools: &Vec<Tool>,
messages: &Vec<Message>,
pending: &Vec<Message>,
target_limit: usize,
) -> AgentResult<Vec<Message>> {
// Prepares the inference by managing context window and token budget.
// This function:
// 1. Retrieves and formats system resources and status
Expand Down Expand Up @@ -222,12 +224,15 @@ impl Agent {
let mut all_resources: Vec<(String, String, Resource, u32)> = Vec::new();
for (system_name, resources) in &resource_content {
for (uri, (resource, _)) in resources {
if let Some(token_count) = system_token_counts.get(system_name).and_then(|counts| counts.get(uri)) {
if let Some(token_count) = system_token_counts
.get(system_name)
.and_then(|counts| counts.get(uri))
{
all_resources.push((
system_name.clone(),
uri.clone(),
resource.clone(),
*token_count
*token_count,
));
}
}
Expand Down Expand Up @@ -260,10 +265,9 @@ impl Agent {
if let Some((resource, content)) = system_resources.get(uri) {
status_content.push(format!("{}\n```\n{}\n```\n", resource.name, content));
}
}
}
}
}
else {
} else {
// Create status messages from all resources when no trimming needed
for (_system_name, resources) in &resource_content {
for (resource, content) in resources.values() {
Expand All @@ -282,7 +286,6 @@ impl Agent {
let status = load_prompt_file("status.md", &context)
.map_err(|e| AgentError::Internal(e.to_string()))?;


// Create a new messages vector with our changes
let mut new_messages = messages.to_vec();

Expand All @@ -298,7 +301,6 @@ impl Agent {
let message_result =
Message::user().with_tool_response("000", Ok(vec![Content::text(status)]));


new_messages.push(message_use);
new_messages.push(message_result);

Expand All @@ -312,9 +314,16 @@ impl Agent {
let tools = self.get_prefixed_tools();
let system_prompt = self.get_system_prompt()?;


// Update conversation history for the start of the reply
messages =self.prepare_inference(&system_prompt, &tools, &messages, &Vec::new(), ESTIMATED_TOKEN_LIMIT).await?;
messages = self
.prepare_inference(
&system_prompt,
&tools,
&messages,
&Vec::new(),
ESTIMATED_TOKEN_LIMIT,
)
.await?;

Ok(Box::pin(async_stream::try_stream! {
loop {
Expand Down Expand Up @@ -457,10 +466,9 @@ mod tests {
}

async fn read_resource(&self, uri: &str) -> AgentResult<String> {
self.resource_content
.get(uri)
.cloned()
.ok_or_else(|| AgentError::InvalidParameters(format!("Resource {} could not be found", uri)))
self.resource_content.get(uri).cloned().ok_or_else(|| {
AgentError::InvalidParameters(format!("Resource {} could not be found", uri))
})
}
}

Expand Down Expand Up @@ -594,16 +602,8 @@ mod tests {

// Add two resources with different priorities
let string_10toks = "hello ".repeat(10);
system.add_resource(
"high_priority",
&string_10toks,
4,
);
system.add_resource(
"low_priority",
&string_10toks,
1,
);
system.add_resource("high_priority", &string_10toks, 4);
system.add_resource("low_priority", &string_10toks, 1);

agent.add_system(Box::new(system));

Expand All @@ -624,7 +624,9 @@ mod tests {

// Get the last message which should be the tool response containing status
let status_message = result.last().unwrap();
let status_content = status_message.content.first()
let status_content = status_message
.content
.first()
.and_then(|content| content.as_tool_response_text())
.unwrap_or_default();

Expand All @@ -637,12 +639,14 @@ mod tests {

// Call prepare_inference
let result = agent
.prepare_inference(system_prompt, &tools, &messages, &pending, target_limit)
.await?;
.prepare_inference(system_prompt, &tools, &messages, &pending, target_limit)
.await?;

// Get the last message which should be the tool response containing status
let status_message = result.last().unwrap();
let status_content = status_message.content.first()
let status_content = status_message
.content
.first()
.and_then(|content| content.as_tool_response_text())
.unwrap_or_default();

Expand Down
Loading

0 comments on commit b6e9f91

Please sign in to comment.