Skip to content

Commit

Permalink
add some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwanqq committed Feb 27, 2024
1 parent 324b852 commit 7a63dd3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/chat/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use url::Url;

static CHAT_API_URL: &str = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/";

//TODO: finish this
/** ChatEndpoint is a struct that represents the chat endpoint of erniebot API
*/
pub struct ChatEndpoint {
url: Url,
access_token: String,
Expand Down Expand Up @@ -60,6 +61,7 @@ impl ChatEndpoint {
Ok(body)
}

/// invoke method is used to send a request to erniebot chat endpoint. This is a blocking method that will return a full response from the chat endpoint
pub fn invoke(
&self,
messages: Vec<Message>,
Expand All @@ -83,6 +85,7 @@ impl ChatEndpoint {
}
Ok(Response::new(response))
}
/// stream method is used to send a request to erniebot chat endpoint. This is a blocking method that will return response in multiple chunks from the chat endpoint
pub fn stream(
&self,
messages: Vec<Message>,
Expand All @@ -103,6 +106,7 @@ impl ChatEndpoint {
Ok(response)
}

/// ainvoke method is used to send a request to erniebot chat endpoint. This is an async method that will return a full response from the chat endpoint
pub async fn ainvoke(
&self,
messages: Vec<Message>,
Expand All @@ -129,6 +133,7 @@ impl ChatEndpoint {
Ok(Response::new(response))
}

/// astream method is used to send a request to erniebot chat endpoint. This is an async method that will return response in multiple chunks from the chat endpoint
pub async fn astream(
&self,
messages: Vec<Message>,
Expand Down
36 changes: 36 additions & 0 deletions src/chat/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,43 @@ pub enum Role {
Assistant,
Function,
}
/** as metioned in <https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11>,
The "messages" member must not be empty. One member represents a single round of conversation, while multiple members represent multiple rounds of conversation. For example:
(1) Example with one member: "messages": [ {"role": "user", "content": "Hello"}]
```
use erniebot_rs::chat::{Message, Role};
let message1 = Message {
role: Role::User,
content: "hello, I'm a user".to_string(),
name: None,
};
let messages = vec![message1];
```
(2) Example with three members:
```use erniebot_rs::chat::{Message, Role};
let message1 = Message {
role: Role::User,
content: "hello, I'm a user".to_string(),
name: None,
};
let message2 = Message {
role: Role::Assistant,
content: "hello, I'm a AI LLM model".to_string(),
name: None,
};
let message3 = Message {
role: Role::User,
content: "hello, I want you to help me".to_string(),
name: None,
};
let messages = vec![message1, message2, message3];
```
The last message is the current request information, while the previous messages are historical conversation information.
The 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:
In 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".
*/
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Message {
pub role: Role,
Expand Down

0 comments on commit 7a63dd3

Please sign in to comment.