-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00d6633
commit 16085e2
Showing
9 changed files
with
116 additions
and
162 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::models::chat::Message; | ||
|
||
/// Chat history. | ||
pub struct History(Vec<Message>); | ||
|
||
// Adapted from https://github.com/meta-llama/llama3/blob/main/llama/tokenizer.py#L202 | ||
impl History { | ||
fn encode_header(message: &Message) -> String { | ||
format!("<|start_header_id|>{}<|end_header_id|>\n\n", message.role) | ||
} | ||
|
||
fn encode_message(message: &Message) -> String { | ||
Self::encode_header(message) + message.content.trim() + "<|eot_id|>" | ||
} | ||
|
||
/// Create a new instance of this object. | ||
pub fn new() -> Self { | ||
Self(vec![]) | ||
} | ||
|
||
/// Encode the dialog to llama3 prompt format. | ||
pub fn encode_dialog_to_prompt(&self) -> String { | ||
let mut encoded = "<|begin_of_text|>".to_string(); | ||
|
||
for message in self.iter() { | ||
encoded += &Self::encode_message(message); | ||
} | ||
|
||
// Add the start of an assistant message for the model to complete. | ||
encoded += &Self::encode_header(&Message::assistant("".to_string())); | ||
|
||
encoded | ||
} | ||
} | ||
|
||
impl std::ops::Deref for History { | ||
type Target = Vec<Message>; | ||
fn deref(&self) -> &Vec<Message> { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl std::ops::DerefMut for History { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.