-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe4944d
commit 960d377
Showing
15 changed files
with
421 additions
and
59 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -10,4 +10,9 @@ export PYTHONPATH="/Users/sarah.lauzeral/Library/CloudStorage/GoogleDrive-sarah. | |
python "/Users/sarah.lauzeral/Library/CloudStorage/[email protected]/Mon Drive/internal_projects/skaff-rag-accelerator/backend/main.py" | ||
``` | ||
|
||
- comment mettre des docs dans le chatbot | ||
- comment lancer l'API | ||
- gestion de la config | ||
- écrire des helpers de co, pour envoyer des messages... | ||
|
||
</div> |
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,17 +1,67 @@ | ||
from langchain.memory import chat_message_histories | ||
import json | ||
import os | ||
from datetime import datetime | ||
from typing import Any | ||
from uuid import uuid4 | ||
|
||
from langchain.memory import ConversationBufferWindowMemory | ||
from langchain.memory.chat_message_histories import SQLChatMessageHistory | ||
from langchain.memory.chat_message_histories.sql import DefaultMessageConverter | ||
from langchain.schema import BaseMessage | ||
from langchain.schema.messages import BaseMessage, _message_to_dict | ||
from sqlalchemy import Column, DateTime, Text | ||
from sqlalchemy.orm import declarative_base | ||
|
||
TABLE_NAME = "message" | ||
|
||
def get_chat_message_history(config): | ||
spec = getattr(chat_message_histories, config["chat_message_history_config"]["source"]) | ||
kwargs = { | ||
key: value for key, value in config["chat_message_history_config"].items() if key in spec.__fields__.keys() | ||
} | ||
return spec(**kwargs) | ||
|
||
def get_conversation_buffer_memory(config): | ||
def get_conversation_buffer_memory(config, chat_id): | ||
return ConversationBufferWindowMemory( | ||
memory_key="chat_history", | ||
chat_memory=get_chat_message_history(config), | ||
memory_key="chat_history", | ||
chat_memory=get_chat_message_history(chat_id), | ||
return_messages=True, | ||
k=config["chat_message_history_config"]["window_size"] | ||
) | ||
k=config["chat_message_history_config"]["window_size"], | ||
) | ||
|
||
|
||
def get_chat_message_history(chat_id): | ||
return SQLChatMessageHistory( | ||
session_id=chat_id, | ||
connection_string=os.environ.get("DATABASE_CONNECTION_STRING"), | ||
table_name=TABLE_NAME, | ||
session_id_field_name="chat_id", | ||
custom_message_converter=CustomMessageConverter(table_name=TABLE_NAME), | ||
) | ||
|
||
|
||
Base = declarative_base() | ||
|
||
|
||
class CustomMessage(Base): | ||
__tablename__ = TABLE_NAME | ||
|
||
id = Column( | ||
Text, primary_key=True, default=lambda: str(uuid4()) | ||
) # default=lambda: str(uuid4()) | ||
timestamp = Column(DateTime) | ||
chat_id = Column(Text) | ||
sender = Column(Text) | ||
content = Column(Text) | ||
message = Column(Text) | ||
|
||
|
||
class CustomMessageConverter(DefaultMessageConverter): | ||
def to_sql_model(self, message: BaseMessage, session_id: str) -> Any: | ||
print(message.content) | ||
sub_message = json.loads(message.content) | ||
return CustomMessage( | ||
id=sub_message["id"], | ||
timestamp=datetime.strptime(sub_message["timestamp"], "%Y-%m-%d %H:%M:%S.%f"), | ||
chat_id=session_id, | ||
sender=message.type, | ||
content=sub_message["content"], | ||
message=json.dumps(_message_to_dict(message)), | ||
) | ||
|
||
def get_sql_model_class(self) -> Any: | ||
return CustomMessage |
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 |
---|---|---|
|
@@ -37,4 +37,3 @@ | |
) | ||
|
||
chat() | ||
|
Oops, something went wrong.