-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support fastchat http bindings
* feat: add support fastchat http bindings Signed-off-by: Lei Wen <wenlei03@qiyi.com>
- Loading branch information
Showing
4 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
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,93 @@ | ||
use async_trait::async_trait; | ||
use reqwest::header; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
use tabby_inference::{TextGeneration, TextGenerationOptions}; | ||
|
||
#[derive(Serialize)] | ||
struct Request { | ||
model: String, | ||
prompt: Vec<String>, | ||
max_tokens: usize, | ||
temperature: f32, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Response { | ||
choices: Vec<Prediction>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Prediction { | ||
text: Vec<String>, | ||
} | ||
|
||
pub struct FastChatEngine { | ||
client: reqwest::Client, | ||
api_endpoint: String, | ||
model_name: String, | ||
} | ||
|
||
impl FastChatEngine { | ||
pub fn create(api_endpoint: &str, model_name: &str, authorization: &str) -> Self { | ||
let mut headers = reqwest::header::HeaderMap::new(); | ||
if authorization.len() > 0 { | ||
headers.insert( | ||
"Authorization", | ||
header::HeaderValue::from_str(authorization) | ||
.expect("Failed to create authorization header"), | ||
); | ||
} | ||
let client = reqwest::Client::builder() | ||
.default_headers(headers) | ||
.build() | ||
.expect("Failed to construct HTTP client"); | ||
Self { | ||
api_endpoint: api_endpoint.to_owned(), | ||
model_name: model_name.to_owned(), | ||
client, | ||
} | ||
} | ||
|
||
pub fn prompt_template() -> String { | ||
"{prefix}<MID>{suffix}".to_owned() | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl TextGeneration for FastChatEngine { | ||
async fn generate(&self, prompt: &str, options: TextGenerationOptions) -> String { | ||
let _stop_sequences: Vec<String> = options | ||
.stop_words | ||
.iter() | ||
.map(|x| x.to_string()) | ||
.collect(); | ||
|
||
let tokens: Vec<&str> = prompt.split("<MID>").collect(); | ||
let request = Request { | ||
model: self.model_name.to_owned(), | ||
prompt: vec![tokens[0].to_owned()], | ||
max_tokens: options.max_decoding_length, | ||
temperature: options.sampling_temperature, | ||
}; | ||
|
||
// API Documentation: https://github.com/lm-sys/FastChat/blob/main/docs/openai_api.md | ||
let resp = self | ||
.client | ||
.post(&self.api_endpoint) | ||
.json(&request) | ||
.send() | ||
.await | ||
.expect("Failed to making completion request"); | ||
|
||
if resp.status() != 200 { | ||
let err: Value = resp.json().await.expect("Failed to parse response"); | ||
println!("Request failed: {}", err); | ||
std::process::exit(1); | ||
} | ||
|
||
let resp: Response = resp.json().await.expect("Failed to parse response"); | ||
|
||
resp.choices[0].text[0].clone() | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod vertex_ai; | ||
pub mod fastchat; |
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