Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/simple-chat-client/src/bin/simple_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 1 addition & 21 deletions examples/simple-chat-client/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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")
}
}
23 changes: 10 additions & 13 deletions examples/simple-chat-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/simple-chat-client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions examples/simple-chat-client/src/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
openai_key = "key"
chat_url = "url"
model_name = "model_name"
proxy = false

[mcp]
[[mcp.server]]
Expand Down
Loading