-
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.
Streamline api, Introduce item re-indexing, and introduce intelligenc…
…e module
- Loading branch information
Showing
19 changed files
with
380 additions
and
116 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::env; | ||
|
||
use ollama_rs::Ollama; | ||
use tracing::info; | ||
|
||
pub struct Intelligence { | ||
pub ollama: Ollama, | ||
} | ||
|
||
impl Intelligence { | ||
pub async fn new(url: String, port: u16) -> Result<Self, anyhow::Error> { | ||
let ollama = Ollama::new(url, port); | ||
|
||
let models: Vec<String> = ollama | ||
.list_local_models() | ||
.await? | ||
.iter() | ||
.map(|m| m.name.clone()) | ||
.collect(); | ||
|
||
info!("Ollama models detected: {:?}", models); | ||
|
||
Ok(Self { | ||
ollama, | ||
}) | ||
} | ||
|
||
pub async fn guess() -> Result<Self, anyhow::Error> { | ||
let url = env::var("OLLAMA_URL") | ||
.map_err(|_| anyhow::anyhow!("OLLAMA_URL is not set"))?; | ||
let port = env::var("OLLAMA_PORT") | ||
.map_err(|_| anyhow::anyhow!("OLLAMA_PORT is not set"))? | ||
.parse::<u16>() | ||
.map_err(|_| anyhow::anyhow!("OLLAMA_PORT is not a valid u16"))?; | ||
|
||
Self::new(url, port) | ||
.await | ||
.map_err(anyhow::Error::from) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ mod models; | |
mod routes; | ||
mod state; | ||
mod search; | ||
mod intelligence; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use std::sync::Arc; | ||
|
||
use poem::{ | ||
web::{Data, Path}, | ||
Result, | ||
}; | ||
use poem_openapi::{payload::Json, Object, OpenApi}; | ||
use reqwest::StatusCode; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
auth::middleware::AuthToken, | ||
models::media::Media, | ||
state::AppState, | ||
}; | ||
|
||
pub struct MediaApi; | ||
|
||
#[derive(Deserialize, Debug, Serialize, Object)] | ||
pub struct MediaIdResponse { | ||
media_id: String, | ||
} | ||
|
||
#[derive(Deserialize, Debug, Serialize, Object)] | ||
pub struct CreateMediaRequest { | ||
name: Option<String>, | ||
kind: Option<String>, | ||
} | ||
|
||
#[OpenApi] | ||
impl MediaApi { | ||
#[oai(path = "/media/:media_id", method = "get")] | ||
async fn get_media( | ||
&self, | ||
state: Data<&Arc<AppState>>, | ||
auth: AuthToken, | ||
media_id: Path<i32>, | ||
) -> Result<Json<Media>> { | ||
Media::get_by_id(&state.database, media_id.0) | ||
.await | ||
.or(Err(poem::Error::from_status( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
)))? | ||
.ok_or(poem::Error::from_status(StatusCode::NOT_FOUND)) | ||
.map(|x| Json(x)) | ||
} | ||
|
||
#[oai(path = "/media/:media_id", method = "delete")] | ||
async fn delete_media( | ||
&self, | ||
auth: AuthToken, | ||
state: Data<&Arc<AppState>>, | ||
media_id: Path<i32>, | ||
) -> Result<()> { | ||
Media::get_by_id(&state.database, media_id.0) | ||
.await | ||
.unwrap() | ||
.unwrap() | ||
.delete(&state.database) | ||
.await | ||
.unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[oai(path = "/media", method = "get")] | ||
async fn get_all_media( | ||
&self, | ||
auth: AuthToken, | ||
state: Data<&Arc<AppState>>, | ||
) -> Result<Json<Vec<Media>>> { | ||
match auth.ok() { | ||
Some(user) => Ok(Json( | ||
Media::get_all(&state.database) | ||
.await | ||
.unwrap(), | ||
)), | ||
None => Err(StatusCode::UNAUTHORIZED.into()), | ||
} | ||
} | ||
|
||
// #[oai(path = "/media", method = "post")] | ||
// async fn create_media( | ||
// &self, | ||
// auth: AuthToken, | ||
// state: Data<&Arc<AppState>>, | ||
// request: Query<CreateMediaRequest>, | ||
// ) -> Json<Media> { | ||
// Json( | ||
// Media { | ||
// ..Default::default() | ||
// } | ||
// .insert(&state.database) | ||
// .await | ||
// .unwrap() | ||
// .index_search(&state.search, &state.database) | ||
// .await | ||
// .unwrap(), | ||
// ) | ||
// } | ||
} |
Oops, something went wrong.