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:
parent
3a97917cd7
commit
b6fabc8836
5 changed files with 14 additions and 35 deletions
|
|
@ -23,7 +23,7 @@ async fn main() -> Result<()> {
|
|||
.unwrap_or_else(|| std::env::var("OPENAI_API_KEY").expect("need set api key"));
|
||||
let url = config.chat_url.clone();
|
||||
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
|
||||
let mut tool_set = ToolSet::default();
|
||||
|
|
|
|||
|
|
@ -4,11 +4,10 @@ use std::{
|
|||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
client::ChatClient,
|
||||
model::{CompletionRequest, Message, Tool as ModelTool},
|
||||
model::{CompletionRequest, Message},
|
||||
tool::{Tool as ToolTrait, ToolSet},
|
||||
};
|
||||
|
||||
|
|
@ -149,22 +148,3 @@ impl ChatSession {
|
|||
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")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,14 +16,17 @@ pub struct 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());
|
||||
|
||||
// create http client without proxy
|
||||
let client = HttpClient::builder()
|
||||
.no_proxy()
|
||||
.build()
|
||||
.unwrap_or_else(|_| HttpClient::new());
|
||||
let proxy = proxy.unwrap_or(false);
|
||||
let client = if proxy {
|
||||
HttpClient::new()
|
||||
} else {
|
||||
HttpClient::builder()
|
||||
.no_proxy()
|
||||
.build()
|
||||
.unwrap_or_else(|_| HttpClient::new())
|
||||
};
|
||||
|
||||
Self {
|
||||
api_key,
|
||||
|
|
@ -41,12 +44,6 @@ impl OpenAIClient {
|
|||
#[async_trait]
|
||||
impl ChatClient for OpenAIClient {
|
||||
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
|
||||
.client
|
||||
.post(&self.base_url)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ pub struct Config {
|
|||
pub chat_url: Option<String>,
|
||||
pub mcp: Option<McpConfig>,
|
||||
pub model_name: Option<String>,
|
||||
pub proxy: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
openai_key = "key"
|
||||
chat_url = "url"
|
||||
model_name = "model_name"
|
||||
proxy = false
|
||||
|
||||
[mcp]
|
||||
[[mcp.server]]
|
||||
|
|
|
|||
Loading…
Reference in a new issue