forked from bionic-gpt/bionic-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: API to create prompts based on dataset. (bionic-gpt#40)
- Loading branch information
Showing
72 changed files
with
400 additions
and
4,407 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
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
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
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,47 +1,117 @@ | ||
//! Run with | ||
//! | ||
//! ```not_rust | ||
//! $ cargo run -p example-http-proxy | ||
//! ``` | ||
//! | ||
//! In another terminal: | ||
//! | ||
//! ```not_rust | ||
//! $ curl -v -x "127.0.0.1:3000" https://tokio.rs | ||
//! ``` | ||
//! | ||
//! Example is based on <https://github.com/hyperium/hyper/blob/master/examples/http_proxy.rs> | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::State, | ||
http::Request, | ||
http::StatusCode, | ||
response::{IntoResponse, Response}, | ||
Extension, RequestExt, | ||
}; | ||
use http::Uri; | ||
use hyper::client::HttpConnector; | ||
|
||
use db::{queries, Pool}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
type Client = hyper::client::Client<HttpConnector, Body>; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct Message { | ||
role: String, | ||
content: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct Completion { | ||
model: String, | ||
streaming: Option<bool>, | ||
messages: Vec<Message>, | ||
temperature: Option<f32>, | ||
} | ||
|
||
pub async fn handler( | ||
Extension(pool): Extension<Pool>, | ||
State(client): State<Client>, | ||
mut req: Request<hyper::Body>, | ||
mut req: Request<Body>, | ||
) -> Result<Response, StatusCode> { | ||
let path = req.uri().path(); | ||
let path_query = req | ||
.uri() | ||
.path_and_query() | ||
.map(|v| v.as_str()) | ||
.unwrap_or(path); | ||
|
||
let uri = format!("http://llm-api:8080{path_query}"); | ||
|
||
*req.uri_mut() = Uri::try_from(uri).unwrap(); | ||
|
||
Ok(client | ||
.request(req) | ||
.await | ||
.map_err(|_| StatusCode::BAD_REQUEST)? | ||
.into_response()) | ||
if let Some(api_key) = req.headers().get("Authorization") { | ||
let mut db_client = pool.get().await.unwrap(); | ||
let transaction = db_client.transaction().await.unwrap(); | ||
|
||
let prompt = queries::prompts::prompt_by_api_key() | ||
.bind(&transaction, &api_key.to_str().unwrap()) | ||
.one() | ||
.await | ||
.map_err(|_| StatusCode::BAD_REQUEST)?; | ||
|
||
let api_key = queries::api_keys::find_api_key() | ||
.bind(&transaction, &api_key.to_str().unwrap()) | ||
.one() | ||
.await | ||
.map_err(|_| StatusCode::UNAUTHORIZED)?; | ||
|
||
let path = req.uri().path(); | ||
let path_query = req | ||
.uri() | ||
.path_and_query() | ||
.map(|v| v.as_str()) | ||
.unwrap_or(path); | ||
|
||
let base_url = prompt.base_url; | ||
let uri = format!("{base_url}{path_query}"); | ||
|
||
// If we are completions we need to add the prompt to the request | ||
if path_query.ends_with("/completions") { | ||
super::rls::set_row_level_security_user_id(&transaction, api_key.user_id) | ||
.await | ||
.map_err(|_| StatusCode::BAD_REQUEST)?; | ||
|
||
let body: String = req.extract().await.map_err(|_| StatusCode::BAD_REQUEST)?; | ||
let completion: Completion = | ||
serde_json::from_str(&body).map_err(|_| StatusCode::BAD_REQUEST)?; | ||
|
||
let generated_prompt = crate::prompt::execute_prompt( | ||
&transaction, | ||
prompt.id, | ||
prompt.organisation_id, | ||
"message.message", | ||
) | ||
.await | ||
.map_err(|_| StatusCode::BAD_REQUEST)?; | ||
|
||
let completion = Completion { | ||
messages: vec![Message { | ||
role: "user".to_string(), | ||
content: generated_prompt, | ||
}], | ||
..completion | ||
}; | ||
|
||
let completion_json = | ||
serde_json::to_string(&completion).map_err(|_| StatusCode::BAD_REQUEST)?; | ||
|
||
// Create a new request | ||
let req = Request::post(uri) | ||
.header("content-type", "application/json") | ||
.body(Body::from(completion_json)) | ||
.map_err(|_| StatusCode::BAD_REQUEST)?; | ||
|
||
Ok(client | ||
.request(req) | ||
.await | ||
.map_err(|_| StatusCode::BAD_REQUEST)? | ||
.into_response()) | ||
} else { | ||
// Anything that is not completions gets passed direct to the LLM API | ||
|
||
*req.uri_mut() = Uri::try_from(uri).unwrap(); | ||
|
||
Ok(client | ||
.request(req) | ||
.await | ||
.map_err(|_| StatusCode::BAD_REQUEST)? | ||
.into_response()) | ||
} | ||
} else { | ||
Err(StatusCode::UNAUTHORIZED) | ||
} | ||
} |
Oops, something went wrong.