This repository has been archived by the owner on Jan 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from openchatai/feat/chat_conversation
Feat/chat conversation
- Loading branch information
Showing
15 changed files
with
403 additions
and
64 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 |
---|---|---|
|
@@ -43,6 +43,7 @@ git clone [email protected]:openchatai/OpenCopilot.git | |
|
||
``` | ||
OPENAI_API_KEY=YOUR_TOKEN_HERE | ||
MYSQL_URI=mysql+pymysql://dbuser:dbpass@mysql:3306/opencopilot | ||
``` | ||
|
||
- gpt-4: Ideal for more complex tasks, but may have slower processing times. | ||
|
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
const SESSION_ID_KEY = "@openchatai:session_id"; | ||
|
||
export function useSessionId() { | ||
const [sessionId, setSessionId] = useState<string | undefined>(undefined); | ||
useEffect(() => { | ||
const sessionId = localStorage.getItem(SESSION_ID_KEY); | ||
if (sessionId) { | ||
setSessionId(sessionId); | ||
} else { | ||
const newSessionId = Math.random().toString(36).substring(2, 15); | ||
localStorage.setItem(SESSION_ID_KEY, newSessionId); | ||
setSessionId(newSessionId); | ||
} | ||
}, []); | ||
return { sessionId, setSessionId }; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from utils.__sql import sql_db | ||
from sqlalchemy import Column, String, DateTime, Boolean, Integer | ||
import datetime | ||
from uuid import uuid4 | ||
|
||
|
||
class ChatHistory(sql_db.Model): | ||
__tablename__ = "chat_history" | ||
|
||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
chatbot_id = Column(String(36), nullable=True) | ||
session_id = Column(String(255), nullable=True) | ||
from_user = Column(Boolean, default=False) | ||
message = Column(String(8192)) | ||
created_at = Column(DateTime, default=datetime.datetime.utcnow) | ||
updated_at = Column( | ||
DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow | ||
) |
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from datetime import datetime | ||
from typing import Optional, cast, List | ||
from utils.__sql import sql_db | ||
from models.chat_history import ChatHistory | ||
|
||
|
||
def create_chat_history( | ||
chatbot_id: str, | ||
session_id: str, | ||
from_user: str, | ||
message: str, | ||
) -> ChatHistory: | ||
"""Creates a new chat history record. | ||
Args: | ||
chatbot_id: The ID of the chatbot that sent the message. | ||
session_id: The ID of the chat session. | ||
from_user: The user who sent the message. | ||
message: The message content. | ||
Returns: | ||
The newly created ChatHistory object. | ||
""" | ||
|
||
chat_history = ChatHistory( | ||
chatbot_id=chatbot_id, | ||
session_id=session_id, | ||
from_user=from_user, | ||
message=message, | ||
) | ||
sql_db.session.add(chat_history) | ||
sql_db.session.commit() | ||
return chat_history | ||
|
||
|
||
from datetime import datetime | ||
from typing import Optional | ||
|
||
|
||
def get_all_chat_history_by_session_id( | ||
session_id: str, limit: int = 20, offset: int = 0 | ||
) -> List[ChatHistory]: | ||
"""Retrieves all chat history records for a given session ID, sorted by created_at in descending order (most recent first). | ||
Args: | ||
session_id: The ID of the session to retrieve chat history for. | ||
limit: The maximum number of chat history records to retrieve. | ||
offset: The offset at which to start retrieving chat history records. | ||
Returns: | ||
A list of ChatHistory objects, sorted by created_at in descending order. | ||
""" | ||
|
||
chats = ( | ||
ChatHistory.query.filter_by(session_id=session_id) | ||
.order_by(ChatHistory.created_at.desc()) | ||
.limit(limit) | ||
.offset(offset) | ||
.all() | ||
) | ||
|
||
# Sort the chat history records by created_at in descending order. | ||
chats.sort(key=lambda chat: chat.created_at) | ||
|
||
return cast(List[ChatHistory], chats) | ||
|
||
|
||
def get_all_chat_history(limit: int = 10, offset: int = 0) -> List[ChatHistory]: | ||
"""Retrieves all chat history records. | ||
Args: | ||
limit: The maximum number of chat history records to retrieve. | ||
offset: The offset at which to start retrieving chat history records. | ||
Returns: | ||
A list of ChatHistory objects. | ||
""" | ||
|
||
chats = ChatHistory.query.limit(limit).offset(offset).all() | ||
return cast(List[ChatHistory], chats) | ||
|
||
|
||
def update_chat_history( | ||
chat_history_id: str, | ||
chatbot_id: Optional[str] = None, | ||
session_id: Optional[str] = None, | ||
from_user: Optional[str] = None, | ||
message: Optional[str] = None, | ||
) -> ChatHistory: | ||
"""Updates a chat history record. | ||
Args: | ||
chat_history_id: The ID of the chat history record to update. | ||
chatbot_id: The new chatbot ID. | ||
session_id: The new session ID. | ||
from_user: The new user name. | ||
message: The new message content. | ||
Returns: | ||
The updated ChatHistory object. | ||
""" | ||
|
||
chat_history = ChatHistory.query.get(chat_history_id) | ||
chat_history.chatbot_id = chatbot_id or chat_history.chatbot_id | ||
chat_history.session_id = session_id or chat_history.session_id | ||
chat_history.from_user = from_user or chat_history.from_user | ||
chat_history.message = message or chat_history.message | ||
chat_history.updated_at = datetime.now() | ||
sql_db.session.commit() | ||
return cast(ChatHistory, chat_history) | ||
|
||
|
||
def delete_chat_history(chat_history_id: str) -> None: | ||
"""Deletes a chat history record. | ||
Args: | ||
chat_history_id: The ID of the chat history record to delete. | ||
""" | ||
|
||
chat_history = ChatHistory.query.get(chat_history_id) | ||
sql_db.session.delete(chat_history) | ||
sql_db.session.commit() |
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
Oops, something went wrong.