Skip to content

Commit b6fabc8

Browse files
authored
chore: make proxy in simple-chat configure (#134)
and remove some prints and some useless codes Signed-off-by: jokemanfire <[email protected]>
1 parent 3a97917 commit b6fabc8

File tree

5 files changed

+14
-35
lines changed

5 files changed

+14
-35
lines changed

examples/simple-chat-client/src/bin/simple_chat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn main() -> Result<()> {
2323
.unwrap_or_else(|| std::env::var("OPENAI_API_KEY").expect("need set api key"));
2424
let url = config.chat_url.clone();
2525
println!("url is {:?}", url);
26-
let openai_client = Arc::new(OpenAIClient::new(api_key, url));
26+
let openai_client = Arc::new(OpenAIClient::new(api_key, url, config.proxy));
2727

2828
// create tool set
2929
let mut tool_set = ToolSet::default();

examples/simple-chat-client/src/chat.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ use std::{
44
};
55

66
use anyhow::Result;
7-
use serde_json::Value;
87

98
use crate::{
109
client::ChatClient,
11-
model::{CompletionRequest, Message, Tool as ModelTool},
10+
model::{CompletionRequest, Message},
1211
tool::{Tool as ToolTrait, ToolSet},
1312
};
1413

@@ -149,22 +148,3 @@ impl ChatSession {
149148
Ok(())
150149
}
151150
}
152-
153-
#[async_trait::async_trait]
154-
impl ToolTrait for ModelTool {
155-
fn name(&self) -> String {
156-
self.name.clone()
157-
}
158-
159-
fn description(&self) -> String {
160-
self.description.clone()
161-
}
162-
163-
fn parameters(&self) -> Value {
164-
self.parameters.clone()
165-
}
166-
167-
async fn call(&self, _args: Value) -> Result<String> {
168-
unimplemented!("ModelTool can't be called directly, only for tool definition")
169-
}
170-
}

examples/simple-chat-client/src/client.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ pub struct OpenAIClient {
1616
}
1717

1818
impl OpenAIClient {
19-
pub fn new(api_key: String, url: Option<String>) -> Self {
19+
pub fn new(api_key: String, url: Option<String>, proxy: Option<bool>) -> Self {
2020
let base_url = url.unwrap_or("https://api.openai.com/v1/chat/completions".to_string());
21-
22-
// create http client without proxy
23-
let client = HttpClient::builder()
24-
.no_proxy()
25-
.build()
26-
.unwrap_or_else(|_| HttpClient::new());
21+
let proxy = proxy.unwrap_or(false);
22+
let client = if proxy {
23+
HttpClient::new()
24+
} else {
25+
HttpClient::builder()
26+
.no_proxy()
27+
.build()
28+
.unwrap_or_else(|_| HttpClient::new())
29+
};
2730

2831
Self {
2932
api_key,
@@ -41,12 +44,6 @@ impl OpenAIClient {
4144
#[async_trait]
4245
impl ChatClient for OpenAIClient {
4346
async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse> {
44-
println!("sending request to {}", self.base_url);
45-
println!("using api key: {}", self.api_key);
46-
let request_json = serde_json::to_string(&request)?;
47-
println!("request content: {}", request_json);
48-
// no proxy
49-
5047
let response = self
5148
.client
5249
.post(&self.base_url)

examples/simple-chat-client/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct Config {
1010
pub chat_url: Option<String>,
1111
pub mcp: Option<McpConfig>,
1212
pub model_name: Option<String>,
13+
pub proxy: Option<bool>,
1314
}
1415

1516
#[derive(Debug, Serialize, Deserialize)]

examples/simple-chat-client/src/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
openai_key = "key"
22
chat_url = "url"
33
model_name = "model_name"
4+
proxy = false
45

56
[mcp]
67
[[mcp.server]]

0 commit comments

Comments
 (0)