erniebot_rs/chat/
message.rs

1use super::FunctionCall;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
5#[serde(rename_all(serialize = "snake_case", deserialize = "snake_case"))]
6pub enum Role {
7    #[default]
8    User,
9    Assistant,
10    Function,
11}
12/**  Definition of the message structure
13
14as metioned in <https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11>,
15The "messages" member must not be empty. One member represents a single round of conversation, while multiple members represent multiple rounds of conversation. For example:
16(1) Example with one member: "messages": [ {"role": "user", "content": "Hello"}]
17```
18    use erniebot_rs::chat::{Message, Role};
19    let message1 = Message {
20        role: Role::User,
21        content: "hello, I'm a user".to_string(),
22        ..Default::default()
23    };
24    let messages = vec![message1];
25```
26(2) Example with three members:
27```use erniebot_rs::chat::{Message, Role};
28    let message1 = Message {
29        role: Role::User,
30        content: "hello, I'm a user".to_string(),
31        ..Default::default()
32    };
33    let message2 = Message {
34        role: Role::Assistant,
35        content: "hello, I'm a AI LLM model".to_string(),
36        ..Default::default()
37    };
38    let message3 = Message {
39        role: Role::User,
40        content: "hello, I want you to help me".to_string(),
41        ..Default::default()
42    };
43    let messages = vec![message1, message2, message3];
44```
45The last message is the current request information, while the previous messages are historical conversation information.
46
47The number of members must be odd. The role values of the messages in the members are explained as follows: The role value of the message at odd positions must be either "user" or "function", while the role value of the message at even positions must be "assistant". The role of the first message cannot be "function". For example:
48
49In the example, the role values of the messages are "user", "assistant", "user", "assistant", and "user" respectively. The role values of the messages at odd positions are "user", which means the role values of the 1st, 3rd, and 5th messages are "user". The role values at even positions are "assistant", which means the role values of the 2nd and 4th messages are "assistant".
50*/
51#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
52pub struct Message {
53    /// The role of the message. The value can be "user", "assistant", or "function"(for some specific model).
54    pub role: Role,
55    /// The content of the message. The value is a string.
56    pub content: String,
57    /// The "author" of the message. the This member is required when the role value is "function", and in this case is should be the name in the function_call in the response content
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub name: Option<String>,
60    /// this is function calling result of last round of function call, serving as chat history.
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub function_call: Option<FunctionCall>,
63}
64
65#[cfg(test)]
66mod tests {
67    use super::{Message, Role};
68    use serde_json::{self, to_string};
69    #[test]
70    fn test_message() {
71        let message1 = Message {
72            role: Role::User,
73            content: "hello, I'm a user".to_string(),
74            ..Default::default()
75        };
76        let message2 = Message {
77            role: Role::Assistant,
78            content: "hello, I'm a AI LLM model".to_string(),
79            ..Default::default()
80        };
81        let message3 = Message {
82            role: Role::User,
83            content: "hello, I want you to help me".to_string(),
84            ..Default::default()
85        };
86        let messages = vec![message1, message2, message3];
87        let messages_str = to_string(&messages).unwrap();
88        println!("{}", messages_str);
89    }
90}