-
-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: complete text endpoints (#557)
* chore: complete text endpoints
- Loading branch information
Showing
17 changed files
with
140 additions
and
56 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
#[cfg(feature = "client-api")] | ||
pub mod client; | ||
|
||
#[cfg(feature = "dto")] | ||
pub mod dto; | ||
|
||
#[cfg(feature = "client-api")] | ||
pub mod error; |
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,66 @@ | ||
use crate::state::AppState; | ||
use actix_web::web::{Data, Json}; | ||
use actix_web::{web, Scope}; | ||
use app_error::AppError; | ||
use appflowy_ai_client::dto::CompleteTextResponse; | ||
use shared_entity::dto::ai_dto::{ | ||
CompleteTextParams, SummarizeRowData, SummarizeRowParams, SummarizeRowResponse, | ||
}; | ||
use shared_entity::response::{AppResponse, JsonAppResponse}; | ||
use tracing::{error, instrument}; | ||
|
||
pub fn ai_tool_scope() -> Scope { | ||
web::scope("/api/ai/{workspace_id}") | ||
.service(web::resource("/complete_text").route(web::post().to(complete_text_handler))) | ||
.service(web::resource("/summarize_row").route(web::post().to(summarize_row_handler))) | ||
} | ||
|
||
async fn complete_text_handler( | ||
state: Data<AppState>, | ||
payload: Json<CompleteTextParams>, | ||
) -> actix_web::Result<JsonAppResponse<CompleteTextResponse>> { | ||
let params = payload.into_inner(); | ||
let resp = state | ||
.ai_client | ||
.completion_text(¶ms.text, params.completion_type) | ||
.await | ||
.map_err(|err| AppError::Internal(err.into()))?; | ||
Ok(AppResponse::Ok().with_data(resp).into()) | ||
} | ||
|
||
#[instrument(level = "debug", skip(state, payload), err)] | ||
async fn summarize_row_handler( | ||
state: Data<AppState>, | ||
payload: Json<SummarizeRowParams>, | ||
) -> actix_web::Result<Json<AppResponse<SummarizeRowResponse>>> { | ||
let params = payload.into_inner(); | ||
match params.data { | ||
SummarizeRowData::Identity { .. } => { | ||
return Err(AppError::InvalidRequest("Identity data is not supported".to_string()).into()); | ||
}, | ||
SummarizeRowData::Content(content) => { | ||
if content.is_empty() { | ||
return Ok( | ||
AppResponse::Ok() | ||
.with_data(SummarizeRowResponse { | ||
text: "No content".to_string(), | ||
}) | ||
.into(), | ||
); | ||
} | ||
|
||
let result = state.ai_client.summarize_row(&content).await; | ||
let resp = match result { | ||
Ok(resp) => SummarizeRowResponse { text: resp.text }, | ||
Err(err) => { | ||
error!("Failed to summarize row: {:?}", err); | ||
SummarizeRowResponse { | ||
text: "No content".to_string(), | ||
} | ||
}, | ||
}; | ||
|
||
Ok(AppResponse::Ok().with_data(resp).into()) | ||
}, | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod ai_tool; | ||
pub mod chat; | ||
pub mod file_storage; | ||
pub mod metrics; | ||
|
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,20 @@ | ||
use appflowy_ai_client::dto::CompletionType; | ||
use client_api_test::TestClient; | ||
use shared_entity::dto::ai_dto::CompleteTextParams; | ||
|
||
#[tokio::test] | ||
async fn improve_writing_test() { | ||
let test_client = TestClient::new_user().await; | ||
let workspace_id = test_client.workspace_id().await; | ||
let params = CompleteTextParams { | ||
text: "I feel hungry".to_string(), | ||
completion_type: CompletionType::ImproveWriting, | ||
}; | ||
|
||
let resp = test_client | ||
.api_client | ||
.completion_text(&workspace_id, params) | ||
.await | ||
.unwrap(); | ||
assert!(resp.text.contains("hungry")); | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
mod complete_text; | ||
mod summarize_row; |