use std::borrow::Cow;
use hyper::{Body, Request};
use serde::Serialize;
use crate::endpoints::request::Endpoint;
#[derive(Debug, Clone, Serialize)]
pub struct Search<'a> {
pub query: Cow<'a, str>,
pub documents: Vec<Cow<'a, str>>,
pub file: Option<Cow<'a, str>>,
pub max_rerank: u32,
pub return_metadata: bool,
pub user: Cow<'a, str>
}
impl Default for Search<'_> {
fn default() -> Self {
Self {
query: Cow::Borrowed(""),
documents: Vec::new(),
file: None,
max_rerank: 200,
return_metadata: false,
user: Cow::Borrowed("")
}
}
}
impl Endpoint for Search<'_> {
const ENDPOINT: &'static str = "https://api.openai.com/v1/engines/{}/search";
fn request(&self, auth_token: &str, engine_id: Option<&str>) -> Request<Body> {
let endpoint = Self::ENDPOINT.replace("{}", engine_id.unwrap());
let serialized = serde_json::to_string(&self)
.expect("Failed to serialize Search");
trace!("endpoint={}, serialized={}", endpoint, serialized);
super::request::post!(endpoint, auth_token, serialized)
}
}