forked from rustformers/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvicuna-chat.rs
149 lines (133 loc) · 5.21 KB
/
vicuna-chat.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use clap::Parser;
use rustyline::error::ReadlineError;
use std::{convert::Infallible, io::Write, path::PathBuf};
#[derive(Parser)]
struct Args {
architecture: String,
path: PathBuf,
#[arg(long, short = 'v')]
pub vocabulary_path: Option<PathBuf>,
#[arg(long, short = 'r')]
pub vocabulary_repository: Option<String>,
}
impl Args {
pub fn to_vocabulary_source(&self) -> llm::VocabularySource {
match (&self.vocabulary_path, &self.vocabulary_repository) {
(Some(_), Some(_)) => {
panic!("Cannot specify both --vocabulary-path and --vocabulary-repository");
}
(Some(path), None) => llm::VocabularySource::HuggingFaceTokenizerFile(path.to_owned()),
(None, Some(repo)) => llm::VocabularySource::HuggingFaceRemote(repo.to_owned()),
(None, None) => llm::VocabularySource::Model,
}
}
}
fn main() {
let args = Args::parse();
let vocabulary_source = args.to_vocabulary_source();
let architecture = args.architecture.parse().unwrap();
let path = args.path;
let model = llm::load_dynamic(
architecture,
&path,
vocabulary_source,
Default::default(),
llm::load_progress_callback_stdout,
)
.unwrap_or_else(|err| panic!("Failed to load {architecture} model from {path:?}: {err}"));
let mut session = model.start_session(Default::default());
let character_name = "### Assistant";
let user_name = "### Human";
let persona = "A chat between a human and an assistant.";
let history = format!(
"{character_name}: Hello - How may I help you today?\n\
{user_name}: What is the capital of France?\n\
{character_name}: Paris is the capital of France."
);
let inference_parameters = llm::InferenceParameters::default();
session
.feed_prompt(
model.as_ref(),
&inference_parameters,
format!("{persona}\n{history}").as_str(),
&mut Default::default(),
llm::feed_prompt_callback(|resp| match resp {
llm::InferenceResponse::PromptToken(t)
| llm::InferenceResponse::InferredToken(t) => print_token(t),
_ => Ok(llm::InferenceFeedback::Continue),
}),
)
.expect("Failed to ingest initial prompt.");
let mut rl = rustyline::DefaultEditor::new().expect("Failed to create input reader");
let mut rng = rand::thread_rng();
let mut res = llm::InferenceStats::default();
let mut buf = String::new();
loop {
println!();
let readline = rl.readline(format!("{user_name}: ").as_str());
print!("{character_name}:");
match readline {
Ok(line) => {
let stats = session
.infer(
model.as_ref(),
&mut rng,
&llm::InferenceRequest {
prompt: format!("{user_name}: {line}\n{character_name}:")
.as_str()
.into(),
parameters: &inference_parameters,
play_back_previous_tokens: false,
maximum_token_count: None,
},
&mut Default::default(),
inference_callback(String::from(user_name), &mut buf),
)
.unwrap_or_else(|e| panic!("{e}"));
res.feed_prompt_duration = res
.feed_prompt_duration
.saturating_add(stats.feed_prompt_duration);
res.prompt_tokens += stats.prompt_tokens;
res.predict_duration = res.predict_duration.saturating_add(stats.predict_duration);
res.predict_tokens += stats.predict_tokens;
}
Err(ReadlineError::Eof) | Err(ReadlineError::Interrupted) => {
break;
}
Err(err) => {
println!("{err}");
}
}
}
println!("\n\nInference stats:\n{res}");
}
fn inference_callback(
stop_sequence: String,
buf: &mut String,
) -> impl FnMut(llm::InferenceResponse) -> Result<llm::InferenceFeedback, Infallible> + '_ {
move |resp| match resp {
llm::InferenceResponse::InferredToken(t) => {
let mut reverse_buf = buf.clone();
reverse_buf.push_str(t.as_str());
if stop_sequence.as_str().eq(reverse_buf.as_str()) {
buf.clear();
return Ok(llm::InferenceFeedback::Halt);
} else if stop_sequence.as_str().starts_with(reverse_buf.as_str()) {
buf.push_str(t.as_str());
return Ok(llm::InferenceFeedback::Continue);
}
if buf.is_empty() {
print_token(t)
} else {
print_token(reverse_buf)
}
}
llm::InferenceResponse::EotToken => Ok(llm::InferenceFeedback::Halt),
_ => Ok(llm::InferenceFeedback::Continue),
}
}
fn print_token(t: String) -> Result<llm::InferenceFeedback, Infallible> {
print!("{t}");
std::io::stdout().flush().unwrap();
Ok(llm::InferenceFeedback::Continue)
}