feat: support calling single LLM config
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::config::AppSettings;
|
||||
use crate::config::{AppSettings, LLMConfig};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -23,7 +23,7 @@ pub struct ChatCompletionResponse {
|
||||
pub choices: Vec<ChatChoice>,
|
||||
}
|
||||
|
||||
pub fn call_llm(settings: &AppSettings, user_input: String, selected_prompt: Option<String>) -> Result<String, String> {
|
||||
pub fn call_single_llm(config: &LLMConfig, user_input: String, selected_prompt: Option<String>, header_configs: &[crate::config::HeaderConfig]) -> Result<String, String> {
|
||||
let full_content = if let Some(prompt) = selected_prompt {
|
||||
if !prompt.is_empty() {
|
||||
format!("{}{}", prompt, user_input)
|
||||
@@ -40,7 +40,7 @@ pub fn call_llm(settings: &AppSettings, user_input: String, selected_prompt: Opt
|
||||
}];
|
||||
|
||||
let request = ChatCompletionRequest {
|
||||
model: settings.llm_config.model.clone(),
|
||||
model: config.model.clone(),
|
||||
messages,
|
||||
};
|
||||
|
||||
@@ -50,14 +50,14 @@ pub fn call_llm(settings: &AppSettings, user_input: String, selected_prompt: Opt
|
||||
.map_err(|e| format!("Failed to create HTTP client: {}", e))?;
|
||||
|
||||
let mut req_builder = client
|
||||
.post(format!("{}/chat/completions", settings.llm_config.base_url))
|
||||
.post(format!("{}/chat/completions", config.base_url))
|
||||
.header("Content-Type", "application/json");
|
||||
|
||||
if !settings.llm_config.api_key.is_empty() {
|
||||
req_builder = req_builder.header("Authorization", format!("Bearer {}", settings.llm_config.api_key));
|
||||
if !config.api_key.is_empty() {
|
||||
req_builder = req_builder.header("Authorization", format!("Bearer {}", config.api_key));
|
||||
}
|
||||
|
||||
for header in &settings.header_configs {
|
||||
for header in header_configs {
|
||||
if !header.key.is_empty() {
|
||||
req_builder = req_builder.header(&header.key, &header.value);
|
||||
}
|
||||
@@ -90,3 +90,11 @@ pub fn call_llm(settings: &AppSettings, user_input: String, selected_prompt: Opt
|
||||
|
||||
Ok(completion.choices[0].message.content.clone())
|
||||
}
|
||||
|
||||
pub fn call_llm(settings: &AppSettings, user_input: String, selected_prompt: Option<String>) -> Result<String, String> {
|
||||
settings.llm_configs
|
||||
.iter()
|
||||
.find(|c| c.enabled && !c.api_key.is_empty() && !c.base_url.is_empty())
|
||||
.ok_or_else(|| "没有可用的模型配置".to_string())
|
||||
.and_then(|c| call_single_llm(c, user_input, selected_prompt, &settings.header_configs))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user