Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Context Chat Backend #397

Merged
merged 14 commits into from
Sep 6, 2023
85 changes: 85 additions & 0 deletions backend/alembic/versions/5809c0787398_add_chat_sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""Add Chat Sessions

Revision ID: 5809c0787398
Revises: d929f0c1c6af
Create Date: 2023-09-04 15:29:44.002164

"""
import fastapi_users_db_sqlalchemy
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "5809c0787398"
down_revision = "d929f0c1c6af"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.create_table(
"chat_session",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column(
"user_id",
fastapi_users_db_sqlalchemy.generics.GUID(),
nullable=True,
),
sa.Column("description", sa.Text(), nullable=False),
sa.Column("deleted", sa.Boolean(), nullable=False),
sa.Column(
"time_updated",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column(
"time_created",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.ForeignKeyConstraint(
["user_id"],
["user.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"chat_message",
sa.Column("chat_session_id", sa.Integer(), nullable=False),
sa.Column("message_number", sa.Integer(), nullable=False),
sa.Column("edit_number", sa.Integer(), nullable=False),
sa.Column("parent_edit_number", sa.Integer(), nullable=True),
sa.Column("latest", sa.Boolean(), nullable=False),
sa.Column("message", sa.Text(), nullable=False),
sa.Column(
"message_type",
sa.Enum(
"SYSTEM",
"USER",
"ASSISTANT",
"DANSWER",
name="messagetype",
native_enum=False,
),
nullable=False,
),
sa.Column(
"time_sent",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.ForeignKeyConstraint(
["chat_session_id"],
["chat_session.id"],
),
sa.PrimaryKeyConstraint("chat_session_id", "message_number", "edit_number"),
)


def downgrade() -> None:
op.drop_table("chat_message")
op.drop_table("chat_session")
5 changes: 3 additions & 2 deletions backend/alembic/versions/d929f0c1c6af_feedback_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def upgrade() -> None:
sa.Column("query", sa.String(), nullable=False),
sa.Column(
"selected_search_flow",
sa.Enum("KEYWORD", "SEMANTIC", name="searchtype"),
sa.Enum("KEYWORD", "SEMANTIC", name="searchtype", native_enum=False),
nullable=True,
),
sa.Column("llm_answer", sa.String(), nullable=True),
sa.Column(
"feedback",
sa.Enum("LIKE", "DISLIKE", name="qafeedbacktype"),
sa.Enum("LIKE", "DISLIKE", name="qafeedbacktype", native_enum=False),
nullable=True,
),
sa.Column(
Expand Down Expand Up @@ -65,6 +65,7 @@ def upgrade() -> None:
"HIDE",
"UNHIDE",
name="searchfeedbacktype",
native_enum=False,
),
nullable=True,
),
Expand Down
Empty file.
27 changes: 27 additions & 0 deletions backend/danswer/chat/chat_llm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from collections.abc import Iterator

from langchain.schema.messages import AIMessage
from langchain.schema.messages import BaseMessage
from langchain.schema.messages import HumanMessage
from langchain.schema.messages import SystemMessage

from danswer.configs.constants import MessageType
from danswer.db.models import ChatMessage
from danswer.llm.build import get_default_llm


def llm_chat_answer(previous_messages: list[ChatMessage]) -> Iterator[str]:
prompt: list[BaseMessage] = []
for msg in previous_messages:
content = msg.message
if msg.message_type == MessageType.SYSTEM:
prompt.append(SystemMessage(content=content))
if msg.message_type == MessageType.ASSISTANT:
prompt.append(AIMessage(content=content))
if (
msg.message_type == MessageType.USER
or msg.message_type == MessageType.DANSWER # consider using FunctionMessage
):
prompt.append(HumanMessage(content=content))

return get_default_llm().stream(prompt)
1 change: 1 addition & 0 deletions backend/danswer/configs/app_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
QA_TIMEOUT = int(os.environ.get("QA_TIMEOUT") or "10") # 10 seconds
# Include additional document/chunk metadata in prompt to GenerativeAI
INCLUDE_METADATA = False
HARD_DELETE_CHATS = os.environ.get("HARD_DELETE_CHATS", "True").lower() != "false"


#####
Expand Down
8 changes: 8 additions & 0 deletions backend/danswer/configs/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,11 @@ class SearchFeedbackType(str, Enum):
REJECT = "reject" # down-boost this document for all future queries
HIDE = "hide" # mark this document as untrusted, hide from LLM
UNHIDE = "unhide"


class MessageType(str, Enum):
# Using OpenAI standards, Langchain equivalent shown in comment
SYSTEM = "system" # SystemMessage
USER = "user" # HumanMessage
ASSISTANT = "assistant" # AIMessage
DANSWER = "danswer" # FunctionMessage
Loading
Loading