- ENV: AI_LLM_BASE_URL/API_KEY/MODEL, AI_EMBEDDING_MODEL (gemini-embedding-001), QDRANT_URL
- ai_agent module: chunking materi, embed_text + chat_completion (9router, SSE parse), Qdrant repo (dimentorin_materi collection, 3072d cosine)
- routes: POST /ai/chat (RAG answer + sources), POST /ai/materials/{id}/index, POST /ai/reindex
- e2e verified: reindex 3 chunks; chat 'ownership' -> materi Rust paling relevan 0.88; chat 'endpoint axum' -> materi Axum 0.82; jawaban gronding konteks
54 lines
1.4 KiB
Rust
54 lines
1.4 KiB
Rust
use super::dto::{ChatRequestDto, IndexResultDto};
|
|
use crate::ai_agent::domain::{ChatRequest, RagService};
|
|
use axum::{
|
|
Extension, extract::Path,
|
|
http::HeaderMap, response::IntoResponse,
|
|
};
|
|
use imphnen_libs::{ValidatedJson, decode_access_token};
|
|
use imphnen_utils::{ApiSuccess, AppError};
|
|
use std::sync::Arc;
|
|
use uuid::Uuid;
|
|
|
|
pub async fn post_chat(
|
|
_headers: HeaderMap,
|
|
Extension(service): Extension<Arc<dyn RagService>>,
|
|
ValidatedJson(body): ValidatedJson<ChatRequestDto>,
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
let material_id = match body.material_id {
|
|
Some(m) => Some(Uuid::parse_str(&m).map_err(|_| {
|
|
AppError::BadRequestError("material_id tidak valid".into())
|
|
})?),
|
|
None => None,
|
|
};
|
|
let result = service
|
|
.chat(ChatRequest {
|
|
question: body.question,
|
|
material_id,
|
|
})
|
|
.await?;
|
|
Ok(ApiSuccess(result))
|
|
}
|
|
|
|
pub async fn post_index_material(
|
|
_headers: HeaderMap,
|
|
Extension(service): Extension<Arc<dyn RagService>>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
let indexed = service.index_material(id).await?;
|
|
Ok(ApiSuccess(IndexResultDto {
|
|
material_id: id,
|
|
chunks_indexed: indexed,
|
|
}))
|
|
}
|
|
|
|
pub async fn post_reindex_all(
|
|
_headers: HeaderMap,
|
|
Extension(service): Extension<Arc<dyn RagService>>,
|
|
) -> Result<impl IntoResponse, AppError> {
|
|
let indexed = service.reindex_all().await?;
|
|
Ok(ApiSuccess(IndexResultDto {
|
|
material_id: Uuid::nil(),
|
|
chunks_indexed: indexed,
|
|
}))
|
|
}
|