Direct interface to Gemma (for LTEngine)

If you need functionality that exceeds the normal for translator APIs, it gets difficult unless you have direct access to the AI.

So I added this code to main.rs:

#[derive(Debug, Deserialize, Serialize)]
struct LLMRequest {
    role: Option<String>,
    qry: Option<String>,
    api_key: Option<String>,
}

#[derive(MultipartForm)]
struct MPLLMRequest {
    role: Option<MPText<String>>,
    qry: Option<MPText<String>>,
    api_key: Option<MPText<String>>,
}
impl MPLLMRequest {
    fn into_translate_request(self) -> LLMRequest {
        LLMRequest {
            role: self.role.map(|v| v.into_inner()),
            qry: self.qry.map(|v| v.into_inner()),
            api_key: self.api_key.map(|v| v.into_inner()),
        }
    }
}

async fn parse_payload(req: HttpRequest, payload: web::Payload) -> Result<LLMRequest, ErrorResponse
>{
    let content_type = req.headers().get(header::CONTENT_TYPE).map(|h| h.to_str().unwrap_or("")).un
wrap_or("");
    let body: LLMRequest;

    //if content_type.starts_with("application/json") {
     //   let json = actix_web::web::Json::<LLMRequest>::from_request(&req, &mut payload.into_inner
()).await?;
      //  body = json.into_inner()
//    } else if content_type.starts_with("application/x-www-form-urlencoded") 
    if content_type.starts_with("application/x-www-form-urlencoded") {
        let form = actix_web::web::Form::<LLMRequest>::from_request(&req, &mut payload.into_inner()
).await?;
        body = form.into_inner().into_translate_request();
    } else {
        return Err(ErrorResponse{ error: "Unsupported content-type".to_string(), status: 400 });
    }

    return Ok(body);
}

You see, it is a very simple but effective quick+dirty hack. I don’t know Rust, but it is pretty obvious how to add such simple thing.
If done by a skilled Rust programmer, it would surely be less ugly.

1 Like