fix: nginx ai test (#603)

* chore: add config for ai/chat

* chore: update config
This commit is contained in:
Nathan.fooo 2024-06-04 20:11:19 +08:00 committed by GitHub
parent 093c3ef430
commit 6bb2a20888
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 4 deletions

View File

@ -113,6 +113,7 @@ CLOUDFLARE_TUNNEL_TOKEN=
# AppFlowy AI
APPFLOWY_AI_OPENAI_API_KEY=
APPFLOWY_AI_SERVER_PORT=5001
APPFLOWY_AI_SERVER_HOST=ai
APPFLOWY_AI_DATABASE_URL=postgresql+psycopg://postgres:password@postgres:5432/postgres
# AppFlowy History

View File

@ -104,6 +104,7 @@ CLOUDFLARE_TUNNEL_TOKEN=
# AppFlowy AI
APPFLOWY_AI_OPENAI_API_KEY=
APPFLOWY_AI_SERVER_PORT=5001
APPFLOWY_AI_SERVER_HOST=localhost
APPFLOWY_AI_DATABASE_URL=postgresql+psycopg://postgres:password@postgres:5432/postgres
# AppFlowy History

View File

@ -15,6 +15,18 @@ lazy_static! {
get_env_var("LOCALHOST_GOTRUE", "http://localhost:9999");
}
// Use following configuration when using local server with nginx
//
// #[cfg(not(target_arch = "wasm32"))]
// lazy_static! {
// pub static ref LOCALHOST_URL: Cow<'static, str> =
// get_env_var("LOCALHOST_URL", "http://localhost");
// pub static ref LOCALHOST_WS: Cow<'static, str> =
// get_env_var("LOCALHOST_WS", "ws://localhost/ws/v1");
// pub static ref LOCALHOST_GOTRUE: Cow<'static, str> =
// get_env_var("LOCALHOST_GOTRUE", "http://localhost/gotrue");
// }
// The env vars are not available in wasm32-unknown-unknown
#[cfg(target_arch = "wasm32")]
lazy_static! {

View File

@ -73,6 +73,21 @@ http {
return 204;
}
location /api/chat {
set $appflowy_cloud appflowy_cloud;
proxy_pass http://$appflowy_cloud:8000;
proxy_http_version 1.1;
proxy_set_header Connection "";
chunked_transfer_encoding on;
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 600s;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
}
location /api {
set $appflowy_cloud appflowy_cloud;
proxy_pass http://$appflowy_cloud:8000;

View File

@ -83,6 +83,7 @@ async fn create_chat_message_handler(
state.ai_client.clone(),
)
.await;
Ok(
HttpResponse::Ok()
.content_type("application/json")

View File

@ -16,6 +16,7 @@ use database_entity::dto::{
};
use futures::stream::Stream;
use sqlx::PgPool;
use tracing::error;
use validator::Validate;
@ -118,6 +119,7 @@ pub async fn create_chat_message(
).await {
Ok(question) => question,
Err(err) => {
error!("Failed to insert question message: {}", err);
yield Err(err);
return;
}
@ -125,14 +127,15 @@ pub async fn create_chat_message(
let question_id = question.message_id;
let question_bytes = match serde_json::to_vec(&question) {
Ok(bytes) => bytes,
Ok(s) => Bytes::from(s),
Err(err) => {
error!("Failed to serialize question message: {}", err);
yield Err(AppError::from(err));
return;
}
};
yield Ok::<Bytes, AppError>(Bytes::from(question_bytes));
yield Ok::<Bytes, AppError>(question_bytes);
// Insert answer message
match params.message_type {
@ -141,6 +144,7 @@ pub async fn create_chat_message(
let content = match ai_client.send_question(&chat_id, &params.content).await {
Ok(response) => response.content,
Err(err) => {
error!("Failed to send question to AI: {}", err);
yield Err(AppError::from(err));
return;
}
@ -149,20 +153,22 @@ pub async fn create_chat_message(
let answer = match insert_answer_message(&pg_pool, ChatAuthor::ai(), &chat_id, content.clone(),question_id).await {
Ok(answer) => answer,
Err(err) => {
error!("Failed to insert answer message: {}", err);
yield Err(err);
return;
}
};
let answer_bytes = match serde_json::to_vec(&answer) {
Ok(bytes) => bytes,
Ok(s) => Bytes::from(s),
Err(err) => {
error!("Failed to serialize answer message: {}", err);
yield Err(AppError::from(err));
return;
}
};
yield Ok::<Bytes, AppError>(Bytes::from(answer_bytes));
yield Ok::<Bytes, AppError>(answer_bytes);
}
}
};