pub struct OpenAIClient {
pub client: Client,
pub end_point: String,
pub api_key: Option<String>,
pub tools: HashMap<String, (Arc<dyn Tool + Send + Sync>, bool)>,
pub model_config: Option<ModelConfig>,
}Expand description
Main client structure for interacting with the OpenAI API.
Fields§
§client: ClientHTTP client
end_point: StringAPI endpoint
api_key: Option<String>Optional API key
tools: HashMap<String, (Arc<dyn Tool + Send + Sync>, bool)>Registered tools: key is the tool name, value is a tuple (tool, is_enabled)
model_config: Option<ModelConfig>Configuration for the model request.
Implementations§
Source§impl OpenAIClient
impl OpenAIClient
Sourcepub fn new(end_point: &str, api_key: Option<&str>) -> Self
pub fn new(end_point: &str, api_key: Option<&str>) -> Self
Create a new OpenAIClient.
§Arguments
end_point- The endpoint of the OpenAI API.api_key- Optional API key.
Sourcepub fn set_model_config(&mut self, model_config: &ModelConfig)
pub fn set_model_config(&mut self, model_config: &ModelConfig)
Sourcepub fn def_tool<T: Tool + Send + Sync + 'static>(&mut self, tool: Arc<T>)
pub fn def_tool<T: Tool + Send + Sync + 'static>(&mut self, tool: Arc<T>)
Register a tool.
If a tool with the same name already exists, it will be overwritten.
§Arguments
tool- Reference-counted tool implementing the Tool trait.
Sourcepub fn list_tools(&self) -> Vec<(String, String, bool)>
pub fn list_tools(&self) -> Vec<(String, String, bool)>
List all registered tools.
§Returns
A list of tuples containing (tool name, tool description, enabled flag).
Sourcepub fn switch_tool(&mut self, tool_name: &str, t_enable: bool)
pub fn switch_tool(&mut self, tool_name: &str, t_enable: bool)
Switch the enable/disable state of a tool.
§Arguments
tool_name- The name of the tool.t_enable- True to enable, false to disable.
Sourcepub fn export_tool_def(&self) -> Result<Vec<ToolDef>, ClientError>
pub fn export_tool_def(&self) -> Result<Vec<ToolDef>, ClientError>
Sourcepub async fn send(
&self,
prompt: &VecDeque<Message>,
model: Option<&ModelConfig>,
) -> Result<APIResult, ClientError>
pub async fn send( &self, prompt: &VecDeque<Message>, model: Option<&ModelConfig>, ) -> Result<APIResult, ClientError>
Sourcepub async fn send_can_use_tool(
&self,
prompt: &VecDeque<Message>,
model: Option<&ModelConfig>,
) -> Result<APIResult, ClientError>
pub async fn send_can_use_tool( &self, prompt: &VecDeque<Message>, model: Option<&ModelConfig>, ) -> Result<APIResult, ClientError>
Sourcepub async fn send_use_tool(
&self,
prompt: &VecDeque<Message>,
model: Option<&ModelConfig>,
) -> Result<APIResult, ClientError>
pub async fn send_use_tool( &self, prompt: &VecDeque<Message>, model: Option<&ModelConfig>, ) -> Result<APIResult, ClientError>
Sourcepub async fn send_with_tool(
&self,
prompt: &VecDeque<Message>,
tool_name: &str,
model: Option<&ModelConfig>,
) -> Result<APIResult, ClientError>
pub async fn send_with_tool( &self, prompt: &VecDeque<Message>, tool_name: &str, model: Option<&ModelConfig>, ) -> Result<APIResult, ClientError>
Sourcepub async fn call_api(
&self,
prompt: &VecDeque<Message>,
tool_choice: Option<&Value>,
model_config: Option<&ModelConfig>,
) -> Result<APIResult, ClientError>
pub async fn call_api( &self, prompt: &VecDeque<Message>, tool_choice: Option<&Value>, model_config: Option<&ModelConfig>, ) -> Result<APIResult, ClientError>
Calls the OpenAI chat completions API.
§Arguments
model- The model name; e.g. “GPT-4o”.prompt- The list of messages.function_call- Indicates function call mode:- “auto”
- “none”
- { “name”: “get_weather” }
temp- Temperature parameter.max_token- Maximum tokens parameter.top_p- Top-p sampling parameter.
§Returns
An APIResult on success or a ClientError on failure.
pub async fn request_api( &self, end_point: &str, api_key: Option<&str>, model_config: &ModelConfig, message: &VecDeque<Message>, tools: &Vec<ToolDef>, tool_choice: &Value, ) -> Result<Response, ClientError>
Sourcepub fn create_prompt(&self) -> OpenAIClientState
pub fn create_prompt(&self) -> OpenAIClientState
Trait Implementations§
Source§impl Clone for OpenAIClient
impl Clone for OpenAIClient
Source§fn clone(&self) -> OpenAIClient
fn clone(&self) -> OpenAIClient
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for OpenAIClient
impl !RefUnwindSafe for OpenAIClient
impl Send for OpenAIClient
impl Sync for OpenAIClient
impl Unpin for OpenAIClient
impl !UnwindSafe for OpenAIClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more