chore: make proxy in simple-chat configure (#134)

and remove some prints and some useless codes

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
This commit is contained in:
jokemanfire 2025-04-20 11:00:47 +08:00 • committed by GitHub
commit b6fabc8836
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 14 additions and 35 deletions

View file

@ -23,7 +23,7 @@ async fn main() -> Result<()> {
.unwrap_or_else(|| std::env::var("OPENAI_API_KEY").expect("need set api key")); .unwrap_or_else(|| std::env::var("OPENAI_API_KEY").expect("need set api key"));
let url = config.chat_url.clone(); let url = config.chat_url.clone();
println!("url is {:?}", url); println!("url is {:?}", url);
let openai_client = Arc::new(OpenAIClient::new(api_key, url)); let openai_client = Arc::new(OpenAIClient::new(api_key, url, config.proxy));
// create tool set // create tool set
let mut tool_set = ToolSet::default(); let mut tool_set = ToolSet::default();

View file

@ -4,11 +4,10 @@ use std::{
}; };
use anyhow::Result; use anyhow::Result;
use serde_json::Value;
use crate::{ use crate::{
client::ChatClient, client::ChatClient,
model::{CompletionRequest, Message, Tool as ModelTool}, model::{CompletionRequest, Message},
tool::{Tool as ToolTrait, ToolSet}, tool::{Tool as ToolTrait, ToolSet},
}; };
@ -149,22 +148,3 @@ impl ChatSession {
Ok(()) Ok(())
} }
} }
#[async_trait::async_trait]
impl ToolTrait for ModelTool {
fn name(&self) -> String {
self.name.clone()
}
fn description(&self) -> String {
self.description.clone()
}
fn parameters(&self) -> Value {
self.parameters.clone()
}
async fn call(&self, _args: Value) -> Result<String> {
unimplemented!("ModelTool can't be called directly, only for tool definition")
}
}

View file

@ -16,14 +16,17 @@ pub struct OpenAIClient {
} }
impl OpenAIClient { impl OpenAIClient {
pub fn new(api_key: String, url: Option<String>) -> Self { pub fn new(api_key: String, url: Option<String>, proxy: Option<bool>) -> Self {
let base_url = url.unwrap_or("https://api.openai.com/v1/chat/completions".to_string()); let base_url = url.unwrap_or("https://api.openai.com/v1/chat/completions".to_string());
let proxy = proxy.unwrap_or(false);
// create http client without proxy let client = if proxy {
let client = HttpClient::builder() HttpClient::new()
.no_proxy() } else {
.build() HttpClient::builder()
.unwrap_or_else(|_| HttpClient::new()); .no_proxy()
.build()
.unwrap_or_else(|_| HttpClient::new())
};
Self { Self {
api_key, api_key,
@ -41,12 +44,6 @@ impl OpenAIClient {
#[async_trait] #[async_trait]
impl ChatClient for OpenAIClient { impl ChatClient for OpenAIClient {
async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse> { async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse> {
println!("sending request to {}", self.base_url);
println!("using api key: {}", self.api_key);
let request_json = serde_json::to_string(&request)?;
println!("request content: {}", request_json);
// no proxy
let response = self let response = self
.client .client
.post(&self.base_url) .post(&self.base_url)

View file

@ -10,6 +10,7 @@ pub struct Config {
pub chat_url: Option<String>, pub chat_url: Option<String>,
pub mcp: Option<McpConfig>, pub mcp: Option<McpConfig>,
pub model_name: Option<String>, pub model_name: Option<String>,
pub proxy: Option<bool>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]

View file

@ -1,6 +1,7 @@
openai_key = "key" openai_key = "key"
chat_url = "url" chat_url = "url"
model_name = "model_name" model_name = "model_name"
proxy = false
[mcp] [mcp]
[[mcp.server]] [[mcp.server]]