-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(providers): add Perplexity as a supported model provider #8920
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
jliounis
wants to merge
1
commit into
aaif-goose:main
Choose a base branch
from
jliounis:feat/perplexity-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,151 @@ | ||
| use super::api_client::{ApiClient, AuthMethod}; | ||
| use super::base::{ConfigKey, ProviderDef, ProviderMetadata}; | ||
| use super::openai_compatible::OpenAiCompatibleProvider; | ||
| use crate::model::ModelConfig; | ||
| use anyhow::Result; | ||
| use futures::future::BoxFuture; | ||
|
|
||
| const PERPLEXITY_PROVIDER_NAME: &str = "perplexity"; | ||
| pub const PERPLEXITY_API_HOST: &str = "https://api.perplexity.ai"; | ||
| pub const PERPLEXITY_DEFAULT_MODEL: &str = "sonar-pro"; | ||
|
|
||
| /// Models exposed via Perplexity's OpenAI-compatible chat completions endpoint. | ||
| /// | ||
| /// Perplexity ships new and renames existing models on its own cadence; this list | ||
| /// is a curated default for setup wizards. Users can override | ||
| /// `GOOSE_MODEL` to point at any other model the API accepts. | ||
| pub const PERPLEXITY_KNOWN_MODELS: &[&str] = &[ | ||
| "sonar", | ||
| "sonar-pro", | ||
| "sonar-reasoning", | ||
| "sonar-reasoning-pro", | ||
| ]; | ||
|
|
||
| pub const PERPLEXITY_DOC_URL: &str = "https://docs.perplexity.ai/docs/getting-started"; | ||
|
|
||
| pub struct PerplexityProvider; | ||
|
|
||
| impl PerplexityProvider { | ||
| /// Resolves the API key, accepting either `PERPLEXITY_API_KEY` (the canonical | ||
| /// name) or `PPLX_API_KEY` (the abbreviated alias used by Perplexity's SDKs). | ||
| fn resolve_api_key() -> Result<String, crate::config::ConfigError> { | ||
| let config = crate::config::Config::global(); | ||
| match config.get_secret::<String>("PERPLEXITY_API_KEY") { | ||
| Ok(key) => Ok(key), | ||
| Err(primary_err) => match config.get_secret::<String>("PPLX_API_KEY") { | ||
| Ok(key) => Ok(key), | ||
| Err(_) => Err(primary_err), | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ProviderDef for PerplexityProvider { | ||
| type Provider = OpenAiCompatibleProvider; | ||
|
|
||
| fn metadata() -> ProviderMetadata { | ||
| ProviderMetadata::new( | ||
| PERPLEXITY_PROVIDER_NAME, | ||
| "Perplexity", | ||
| "Perplexity chat models with built-in real-time web search grounding", | ||
| PERPLEXITY_DEFAULT_MODEL, | ||
| PERPLEXITY_KNOWN_MODELS.to_vec(), | ||
| PERPLEXITY_DOC_URL, | ||
| vec![ | ||
| ConfigKey::new("PERPLEXITY_API_KEY", true, true, None, true), | ||
| ConfigKey::new( | ||
| "PERPLEXITY_HOST", | ||
| false, | ||
| false, | ||
| Some(PERPLEXITY_API_HOST), | ||
| false, | ||
| ), | ||
| ], | ||
| ) | ||
| .with_setup_steps(vec![ | ||
| "Go to https://www.perplexity.ai/account/api/keys", | ||
| "Create or copy an existing API key", | ||
| "Paste the key above as PERPLEXITY_API_KEY", | ||
| ]) | ||
| } | ||
|
|
||
| fn from_env( | ||
| model: ModelConfig, | ||
| _extensions: Vec<crate::config::ExtensionConfig>, | ||
| ) -> BoxFuture<'static, Result<OpenAiCompatibleProvider>> { | ||
| Box::pin(async move { | ||
| let api_key = Self::resolve_api_key()?; | ||
| let host: String = crate::config::Config::global() | ||
| .get_param("PERPLEXITY_HOST") | ||
| .unwrap_or_else(|_| PERPLEXITY_API_HOST.to_string()); | ||
|
|
||
| let api_client = ApiClient::new(host, AuthMethod::BearerToken(api_key))?; | ||
|
|
||
| Ok(OpenAiCompatibleProvider::new( | ||
| PERPLEXITY_PROVIDER_NAME.to_string(), | ||
| api_client, | ||
| model, | ||
| String::new(), | ||
| )) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_metadata_structure() { | ||
| let metadata = PerplexityProvider::metadata(); | ||
|
|
||
| assert_eq!(metadata.name, PERPLEXITY_PROVIDER_NAME); | ||
| assert_eq!(metadata.display_name, "Perplexity"); | ||
| assert_eq!(metadata.default_model, PERPLEXITY_DEFAULT_MODEL); | ||
| assert_eq!(metadata.model_doc_link, PERPLEXITY_DOC_URL); | ||
|
|
||
| assert_eq!(metadata.config_keys.len(), 2); | ||
|
|
||
| let api_key = &metadata.config_keys[0]; | ||
| assert_eq!(api_key.name, "PERPLEXITY_API_KEY"); | ||
| assert!(api_key.required); | ||
| assert!(api_key.secret); | ||
| assert!(api_key.primary); | ||
|
|
||
| let host = &metadata.config_keys[1]; | ||
| assert_eq!(host.name, "PERPLEXITY_HOST"); | ||
| assert!(!host.required); | ||
| assert!(!host.secret); | ||
| assert_eq!(host.default.as_deref(), Some(PERPLEXITY_API_HOST)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_known_models_non_empty() { | ||
| let metadata = PerplexityProvider::metadata(); | ||
| assert!(!metadata.known_models.is_empty()); | ||
| assert!(metadata | ||
| .known_models | ||
| .iter() | ||
| .any(|m| m.name == PERPLEXITY_DEFAULT_MODEL)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_setup_steps_present() { | ||
| let metadata = PerplexityProvider::metadata(); | ||
| assert!(!metadata.setup_steps.is_empty()); | ||
| assert!(metadata | ||
| .setup_steps | ||
| .iter() | ||
| .any(|step| step.contains("PERPLEXITY_API_KEY"))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_default_model_is_known() { | ||
| assert!(PERPLEXITY_KNOWN_MODELS.contains(&PERPLEXITY_DEFAULT_MODEL)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_doc_url_points_to_perplexity_docs() { | ||
| assert!(PERPLEXITY_DOC_URL.starts_with("https://docs.perplexity.ai")); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from_envexplicitly acceptsPPLX_API_KEYas a fallback, but metadata declares onlyPERPLEXITY_API_KEYas the required secret. Configuration status is computed from metadata viadefault_inventory_configured(crates/goose/src/providers/inventory/mod.rs), and exposed throughprovider_config_status(crates/goose/src/acp/server.rs), so users who set onlyPPLX_API_KEYwill still be reported as unconfigured (and can be filtered out in configured-provider flows) even though runtime auth would succeed. Please add alias-aware configured checks (or include the alias in required-key semantics) so status matches actual provider behavior.Useful? React with 👍 / 👎.