use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
pub struct PerplexityProvider {
#[allow(dead_code)]
api_key: String,
}
impl PerplexityProvider {
pub fn new(api_key: &str) -> Self {
Self {
api_key: api_key.to_string(),
}
}
pub fn from_env() -> Result<Self> {
let api_key = std::env::var("PERPLEXITY_API_KEY").map_err(|_| {
Error::Configuration("PERPLEXITY_API_KEY environment variable not set".to_string())
})?;
Ok(Self::new(&api_key))
}
pub async fn list_models(&self) -> Result<Vec<String>> {
Ok(vec![
"pplx-7b-online".to_string(),
"pplx-70b-online".to_string(),
"pplx-8x7b-online".to_string(),
])
}
pub fn get_model_info(model: &str) -> Option<PerplexityModelInfo> {
match model {
"pplx-7b-online" => Some(PerplexityModelInfo {
name: "pplx-7b-online".to_string(),
context_window: 8000,
supports_search: true,
max_output_tokens: 2000,
}),
"pplx-70b-online" => Some(PerplexityModelInfo {
name: "pplx-70b-online".to_string(),
context_window: 8000,
supports_search: true,
max_output_tokens: 4000,
}),
"pplx-8x7b-online" => Some(PerplexityModelInfo {
name: "pplx-8x7b-online".to_string(),
context_window: 8000,
supports_search: true,
max_output_tokens: 4000,
}),
_ => None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerplexityModelInfo {
pub name: String,
pub context_window: u32,
pub supports_search: bool,
pub max_output_tokens: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub enum PerplexitySearchMode {
#[default]
Default,
Recent,
Academic,
News,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Citation {
pub url: String,
pub title: String,
pub relevance: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchAugmentedResponse {
pub answer: String,
pub citations: Vec<Citation>,
pub search_mode: PerplexitySearchMode,
pub num_sources: u32,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_perplexity_provider_creation() {
let provider = PerplexityProvider::new("test-key");
assert_eq!(provider.api_key, "test-key");
}
#[tokio::test]
async fn test_list_models() {
let provider = PerplexityProvider::new("test-key");
let models = provider.list_models().await.unwrap();
assert!(!models.is_empty());
assert!(models.contains(&"pplx-7b-online".to_string()));
}
#[test]
fn test_get_model_info() {
let info = PerplexityProvider::get_model_info("pplx-70b-online").unwrap();
assert_eq!(info.name, "pplx-70b-online");
assert!(info.supports_search);
assert_eq!(info.context_window, 8000);
}
#[test]
fn test_model_info_invalid() {
let info = PerplexityProvider::get_model_info("invalid-model");
assert!(info.is_none());
}
#[test]
fn test_search_mode_default() {
assert_eq!(
PerplexitySearchMode::default(),
PerplexitySearchMode::Default
);
}
#[test]
fn test_citation_relevance() {
let citation = Citation {
url: "https://example.com".to_string(),
title: "Example Article".to_string(),
relevance: 0.95,
};
assert!(citation.relevance > 0.9);
}
#[test]
fn test_search_augmented_response() {
let response = SearchAugmentedResponse {
answer: "The answer is 42".to_string(),
citations: vec![
Citation {
url: "https://example1.com".to_string(),
title: "Source 1".to_string(),
relevance: 0.9,
},
Citation {
url: "https://example2.com".to_string(),
title: "Source 2".to_string(),
relevance: 0.85,
},
],
search_mode: PerplexitySearchMode::Recent,
num_sources: 15,
};
assert_eq!(response.citations.len(), 2);
assert_eq!(response.num_sources, 15);
}
}