Skip to content

Commit

Permalink
chore: update ai api endpoint & rename structs (#953)
Browse files Browse the repository at this point in the history
* chore: revmap loader type

* chore: revamp context

* chore: fix test

* chore: remove unused handler

* chore: create new chat api endpoint

* chore: add docs

* chore: clippy

* chore: fix test
  • Loading branch information
appflowy authored Nov 11, 2024
1 parent 2028f4b commit 14adf25
Show file tree
Hide file tree
Showing 24 changed files with 526 additions and 593 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions libs/appflowy-ai-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dto::{
AIModel, ChatAnswer, ChatQuestion, CompleteTextResponse, CompletionType, CreateTextChatContext,
AIModel, ChatAnswer, ChatQuestion, CompleteTextResponse, CompletionType, CreateChatContext,
CustomPrompt, Document, EmbeddingRequest, EmbeddingResponse, LocalAIConfig, MessageData,
RepeatedLocalAIPackage, RepeatedRelatedQuestion, SearchDocumentsRequest, SummarizeRowResponse,
TranslateRowData, TranslateRowResponse,
Expand Down Expand Up @@ -188,10 +188,7 @@ impl AppFlowyAIClient {
.into_data()
}

pub async fn create_chat_text_context(
&self,
context: CreateTextChatContext,
) -> Result<(), AIError> {
pub async fn create_chat_text_context(&self, context: CreateChatContext) -> Result<(), AIError> {
let url = format!("{}/chat/context/text", self.url);
let resp = self
.http_client(Method::POST, &url)?
Expand Down
75 changes: 12 additions & 63 deletions libs/appflowy-ai-client/src/dto.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize, Serializer};
use serde_json::json;
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
Expand Down Expand Up @@ -274,87 +275,35 @@ pub struct LocalAIConfig {
pub plugin: AppFlowyOfflineAI,
}

#[derive(Debug, Clone)]
pub enum ChatContextLoader {
Txt,
Markdown,
}

impl Display for ChatContextLoader {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ChatContextLoader::Txt => write!(f, "text"),
ChatContextLoader::Markdown => write!(f, "markdown"),
}
}
}

impl FromStr for ChatContextLoader {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"text" => Ok(ChatContextLoader::Txt),
"markdown" => Ok(ChatContextLoader::Markdown),
_ => Err(anyhow::anyhow!("unknown context loader type")),
}
}
}
impl Serialize for ChatContextLoader {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
ChatContextLoader::Txt => serializer.serialize_str("text"),
ChatContextLoader::Markdown => serializer.serialize_str("markdown"),
}
}
}

impl<'de> Deserialize<'de> for ChatContextLoader {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.as_str() {
"text" => Ok(ChatContextLoader::Txt),
"markdown" => Ok(ChatContextLoader::Markdown),
_ => Err(serde::de::Error::custom("unknown value")),
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CreateTextChatContext {
pub struct CreateChatContext {
pub chat_id: String,
pub context_loader: ChatContextLoader,
pub context_loader: String,
pub content: String,
pub chunk_size: i32,
pub chunk_overlap: i32,
pub metadata: HashMap<String, serde_json::Value>,
pub metadata: serde_json::Value,
}

impl CreateTextChatContext {
pub fn new(chat_id: String, context_loader: ChatContextLoader, text: String) -> Self {
CreateTextChatContext {
impl CreateChatContext {
pub fn new(chat_id: String, context_loader: String, text: String) -> Self {
CreateChatContext {
chat_id,
context_loader,
content: text,
chunk_size: 2000,
chunk_overlap: 20,
metadata: HashMap::new(),
metadata: json!({}),
}
}

pub fn with_metadata(mut self, metadata: HashMap<String, serde_json::Value>) -> Self {
self.metadata = metadata;
pub fn with_metadata<T: Serialize>(mut self, metadata: T) -> Self {
self.metadata = json!(metadata);
self
}
}

impl Display for CreateTextChatContext {
impl Display for CreateChatContext {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"Create Chat context: {{ chat_id: {}, content_type: {}, content size: {}, metadata: {:?} }}",
Expand Down
6 changes: 3 additions & 3 deletions libs/appflowy-ai-client/tests/chat_test/context_test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::appflowy_ai_client;
use appflowy_ai_client::dto::{AIModel, ChatContextLoader, CreateTextChatContext};
use appflowy_ai_client::dto::{AIModel, CreateChatContext};
#[tokio::test]
async fn create_chat_context_test() {
let client = appflowy_ai_client();
let chat_id = uuid::Uuid::new_v4().to_string();
let context = CreateTextChatContext {
let context = CreateChatContext {
chat_id: chat_id.clone(),
context_loader: ChatContextLoader::Txt,
context_loader: "text".to_string(),
content: "I have lived in the US for five years".to_string(),
chunk_size: 1000,
chunk_overlap: 20,
Expand Down
2 changes: 1 addition & 1 deletion libs/client-api/src/http_chat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::http::log_request_id;
use crate::Client;

use client_api_entity::{
use client_api_entity::chat_dto::{
ChatMessage, CreateAnswerMessageParams, CreateChatMessageParams, CreateChatParams, MessageCursor,
RepeatedChatMessage, UpdateChatMessageContentParams,
};
Expand Down
1 change: 1 addition & 0 deletions libs/database-entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ bincode = "1.3.3"
appflowy-ai-client = { workspace = true, features = ["dto"] }
bytes.workspace = true
prost = "0.12"
infra.workspace = true
Loading

0 comments on commit 14adf25

Please sign in to comment.