Skip to content

Commit

Permalink
move options from exchanges to conversations table, ensure system and…
Browse files Browse the repository at this point in the history
… assistant messages are loaded into db correctly, wip import conversation
  • Loading branch information
aprxi committed Jul 17, 2024
1 parent 40b71f4 commit d1c03c0
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 239 deletions.
11 changes: 5 additions & 6 deletions lumni/src/apps/builtin/llm/prompt/src/chat/db/display.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use lumni::api::error::ApplicationError;

use super::schema::{
Conversation, ConversationId, Message,
};
use super::schema::{Conversation, ConversationId, Message};
use super::ConversationDatabaseStore;
pub use crate::external as lumni;

Expand Down Expand Up @@ -39,9 +37,10 @@ impl ConversationDatabaseStore {
id: &str,
) -> Result<(), ApplicationError> {
let conversation_id = ConversationId(id.parse().map_err(|_| {
ApplicationError::NotFound(
format!("Conversation {id} not found in database"),
)})?);
ApplicationError::NotFound(format!(
"Conversation {id} not found in database"
))
})?);

if let Some((conversation, messages)) =
self.fetch_conversation(Some(conversation_id), None)?
Expand Down
4 changes: 3 additions & 1 deletion lumni/src/apps/builtin/llm/prompt/src/chat/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ mod display;
mod schema;
mod store;

pub use schema::{ConversationCache, Exchange, ExchangeId, ConversationId, Message, ModelId};
pub use schema::{
ConversationCache, ConversationId, Exchange, ExchangeId, Message, ModelId,
};
pub use store::ConversationDatabaseStore;

pub use super::PromptRole;
17 changes: 13 additions & 4 deletions lumni/src/apps/builtin/llm/prompt/src/chat/db/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ pub struct Conversation {
pub id: ConversationId,
pub name: String,
pub metadata: serde_json::Value,
pub model_id: ModelId,
pub parent_conversation_id: Option<ConversationId>,
pub fork_exchange_id: Option<ExchangeId>,
pub completion_options: Option<serde_json::Value>,
pub prompt_options: Option<serde_json::Value>,
pub schema_version: i64,
pub created_at: i64,
pub updated_at: i64,
Expand All @@ -43,10 +46,6 @@ pub struct Conversation {
pub struct Exchange {
pub id: ExchangeId,
pub conversation_id: ConversationId,
pub model_id: ModelId,
pub system_prompt: Option<String>,
pub completion_options: Option<serde_json::Value>,
pub prompt_options: Option<serde_json::Value>,
pub created_at: i64,
pub previous_exchange_id: Option<ExchangeId>,
pub is_deleted: bool,
Expand Down Expand Up @@ -223,4 +222,14 @@ impl ConversationCache {
})
.unwrap_or_default()
}

pub fn get_system_prompt(&self) -> Option<String> {
// system prompt is always set in the first exchange
self.get_exchanges().first().and_then(|exchange| {
self.get_exchange_messages(exchange.id)
.iter()
.find(|message| message.role == PromptRole::System)
.map(|message| message.content.clone())
})
}
}
12 changes: 5 additions & 7 deletions lumni/src/apps/builtin/llm/prompt/src/chat/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ CREATE TABLE conversations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
metadata TEXT, -- JSON string including description and other metadata
model_id INTEGER NOT NULL,
parent_conversation_id INTEGER,
fork_exchange_id INTEGER,
completion_options TEXT, -- JSON string
prompt_options TEXT, -- JSON string
schema_version INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand All @@ -26,21 +29,17 @@ CREATE TABLE conversations (
is_deleted BOOLEAN DEFAULT FALSE,
FOREIGN KEY (parent_conversation_id) REFERENCES conversations(id),
FOREIGN KEY (fork_exchange_id) REFERENCES exchanges(id)
FOREIGN KEY (model_id) REFERENCES models(model_id)
);

CREATE TABLE exchanges (
id INTEGER PRIMARY KEY AUTOINCREMENT,
conversation_id INTEGER NOT NULL,
model_id INTEGER NOT NULL,
system_prompt TEXT,
completion_options TEXT, -- JSON string
prompt_options TEXT, -- JSON string
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
previous_exchange_id INTEGER,
is_deleted BOOLEAN DEFAULT FALSE,
is_latest BOOLEAN DEFAULT TRUE,
FOREIGN KEY (conversation_id) REFERENCES conversations(id),
FOREIGN KEY (model_id) REFERENCES models(model_id)
FOREIGN KEY (previous_exchange_id) REFERENCES exchanges(id)
);

Expand Down Expand Up @@ -83,7 +82,6 @@ CREATE INDEX idx_parent_conversation ON conversations(parent_conversation_id);
CREATE INDEX idx_exchange_conversation_latest ON exchanges(conversation_id, is_latest);
CREATE INDEX idx_exchange_created_at ON exchanges(created_at);
CREATE INDEX idx_fork_exchange ON conversations(fork_exchange_id);
CREATE INDEX idx_model_id ON exchanges(model_id);
CREATE INDEX idx_conversation_model_id ON conversations(model_id);
CREATE INDEX idx_conversation_created_at ON exchanges(conversation_id, created_at);
CREATE INDEX idx_attachment_message ON attachments(message_id);

Loading

0 comments on commit d1c03c0

Please sign in to comment.