Skip to content

Commit

Permalink
updating clippy example to use new endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
a5huynh committed Oct 2, 2023
1 parent f6e9591 commit a9a6651
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
30 changes: 27 additions & 3 deletions examples/clippy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,31 @@ enum Command {
pub struct Args {
#[arg(short, long, default_value = "resources/config.vicuna.toml")]
config: String,
#[arg(short, long, default_value = "http://127.0.0.1:8181")]
#[arg(short, long, default_value = "http://127.0.0.1:8181/api")]
memex_uri: String,
#[command(subcommand)]
command: Command,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ApiResponseStatus {
Ok,
Error,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiResponse<T> {
/// Execution time in seconds
pub time: f32,
pub status: ApiResponseStatus,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<T>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TaskResult {
task_id: i64,
collection: String,
Expand All @@ -48,6 +66,7 @@ pub struct TaskResult {
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchResults {
pub results: Vec<libclippy::Document>,
}
Expand Down Expand Up @@ -136,9 +155,12 @@ async fn main() -> ExitCode {
};

let resp = result
.json::<TaskResult>()
.json::<ApiResponse<TaskResult>>()
.await
.expect("Unable to parse response")
.result
.expect("Unable to parse response");

println!("✅ added document (task_id: {})", resp.task_id);
}
Command::Forget => {
Expand Down Expand Up @@ -188,9 +210,11 @@ async fn handle_ask_cmd(
.send()
.await
.expect("Unable to connect to memex")
.json::<SearchResults>()
.json::<ApiResponse<SearchResults>>()
.await
.expect("Unable to parse response")
.result
.expect("Unable to parse response")
.results
} else {
Vec::new()
Expand Down
1 change: 0 additions & 1 deletion lib/api/src/endpoints/collections/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ pub async fn handle_search_docs(
) -> Result<impl warp::Reply, warp::Rejection> {
let time = std::time::Instant::now();
let (_handle, embedder) = SentenceEmbedder::spawn(&ModelConfig::default());

let vector_uri = std::env::var("VECTOR_CONNECTION").expect("VECTOR_CONNECTION env var not set");
let client = match get_vector_storage(&vector_uri, &collection).await {
Ok(client) => client,
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> {
pub fn health_check() -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone
{
let version = dotenv!("GIT_HASH");
warp::path("health")
warp::path!("api" / "health")
.and(warp::get())
.map(move || warp::reply::json(&json!({ "version": version })))
}
Expand Down

0 comments on commit a9a6651

Please sign in to comment.