rust-sdk/examples/simple-chat-client/src/model.rs
jokemanfire 6de444c62f
feat(examples): Add func call support (#168)
Some works about simple-chat.
1. fix some bugs.
2. add func call optional.

Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
2025-05-12 13:52:57 +08:00

102 lines
2.3 KiB
Rust

use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Message {
pub role: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
}
impl Message {
pub fn system(content: impl ToString) -> Self {
Self {
role: "system".to_string(),
content: content.to_string(),
tool_calls: None,
}
}
pub fn user(content: impl ToString) -> Self {
Self {
role: "user".to_string(),
content: content.to_string(),
tool_calls: None,
}
}
pub fn assistant(content: impl ToString) -> Self {
Self {
role: "assistant".to_string(),
content: content.to_string(),
tool_calls: None,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CompletionRequest {
pub model: String,
pub messages: Vec<Message>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tools: Option<Vec<Tool>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Tool {
pub name: String,
pub description: String,
pub parameters: serde_json::Value,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CompletionResponse {
pub id: String,
pub object: String,
pub created: u64,
pub model: String,
pub choices: Vec<Choice>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Choice {
pub index: u32,
pub message: Message,
pub finish_reason: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ToolCall {
pub id: String,
#[serde(rename = "type")]
pub _type: String,
pub function: ToolFunction,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ToolFunction {
pub name: String,
pub arguments: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ToolResult {
pub success: bool,
pub contents: Vec<Content>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Content {
pub content_type: String,
pub body: String,
}
impl Content {
pub fn text(content: impl ToString) -> Self {
Self {
content_type: "text/plain".to_string(),
body: content.to_string(),
}
}
}