Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 17 additions & 35 deletions models/postgres/pg_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,40 @@
from models.postgres.pg_connection import Base

class Conversation(Base):
__tablename__ = 'conversations'
__tablename__ = 'agent_conversations'
__table_args__ = {'extend_existing': True}

id = Column(Integer, primary_key=True)
org_id = Column(String)
thread_id = Column(String)
model_name = Column(String)
bridge_id = Column(String)
message = Column(Text)
message_by = Column(String)
function = Column(JSON)
type = Column(Enum('chat', 'completion', 'embedding', name='enum_conversations_type'), nullable=False)
createdAt = Column(DateTime, default=func.now())
updatedAt = Column(DateTime, default=func.now(), onupdate=func.now())
chatbot_message = Column(Text)
is_reset = Column(Boolean, default=False)
tools_call_data = Column(ARRAY(JSON))
user_message = Column(Text)
response = Column(Text)
chatbot_response = Column(Text)
tools_call_data = Column(JSON)
user_feedback = Column(Integer)
message_id = Column(UUID(as_uuid=True), nullable=True)
version_id = Column(String)
sub_thread_id = Column(String, nullable=True)
revised_prompt = Column(Text, nullable=True)
image_urls = Column(ARRAY(JSON), nullable=True)
urls = Column(ARRAY(String), nullable=True)
AiConfig = Column(JSON, nullable=True)
annotations = Column(ARRAY(JSON), nullable=True)
revised_response = Column(Text, nullable=True)
image_urls = Column(JSON, nullable=True)
urls = Column(JSON, nullable=True)
fallback_model = Column(String, nullable=True)

class RawData(Base):
__tablename__ = 'raw_data'
__table_args__ = {'extend_existing': True}


id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, nullable=False)
org_id = Column(String)
error = Column(Text, nullable=True)
status = Column(Integer, nullable=True) # 0: failed, 1: success, 2: second time is high
createdAt = Column(DateTime, default=func.now())
updatedAt = Column(DateTime, default=func.now(), onupdate=func.now())
authkey_name = Column(String)
latency = Column(Float)
service = Column(String)
status = Column(Boolean, nullable=False)
error = Column(Text, default='none')
model = Column(String)
input_tokens = Column(Float)
output_tokens = Column(Float)
expected_cost = Column(Float)
tokens = Column(JSON, nullable=True)
created_at = Column(DateTime, default=func.now())
chat_id = Column(Integer, ForeignKey('conversations.id'))
message_id = Column(UUID(as_uuid=True), nullable=True)
variables = Column(JSON)
is_present = Column(Boolean, default=False)
firstAttemptError = Column(Text, nullable=True)
# conversation = relationship("Conversation", back_populates="raw_data")
finish_reason = Column(String, nullable=True)
model_name = Column(String)
type = Column(String, nullable=False)
AiConfig = Column(JSON, nullable=True)
annotations = Column(JSON, nullable=True)

class system_prompt_versionings(Base):
__tablename__ = 'system_prompt_versionings'
Expand Down
104 changes: 2 additions & 102 deletions src/controllers/conversationController.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import copy
from datetime import datetime
from config import Config
from ..db_services import conversationDbService as chatbotDbService
import traceback
from globals import *
Expand All @@ -11,112 +8,15 @@
from ..services.cache_service import find_in_cache, store_in_cache



async def getThread(thread_id, sub_thread_id, org_id, bridge_id, bridgeType):
async def getThread(thread_id, sub_thread_id, org_id, bridge_id):
try:
chats = await chatbotDbService.find(org_id, thread_id, sub_thread_id, bridge_id)
if bridgeType:
filtered_chats = []
for chat in chats:
if chat['is_reset']:
filtered_chats = []
else:
filtered_chats.append(chat)
chats = filtered_chats
chats = await add_tool_call_data_in_history(chats)
return chats
except Exception as err:
logger.error(f"Error in getting thread:, {str(err)}, {traceback.format_exc()}")
raise err

async def savehistory(thread_id, sub_thread_id, userMessage, botMessage, org_id, bridge_id, model_name, type, messageBy, userRole="user", tools={}, chatbot_message = "",tools_call_data = [],message_id = None, version_id = None, image_url = None, revised_prompt = None, urls = None, AiConfig = None, annotations = None, fallback_model = None):
try:
chatToSave = [{
'thread_id': thread_id,
'sub_thread_id': sub_thread_id,
'org_id': org_id,
'model_name': model_name,
'message': userMessage or "",
'message_by': userRole,
'type': type,
'bridge_id': bridge_id,
'message_id' : message_id,
'version_id': version_id,
'revised_prompt' : revised_prompt,
'urls' : urls,
'AiConfig' : AiConfig,
'fallback_model' : fallback_model
}]

if tools:
chatToSave.append({
'thread_id': thread_id,
'sub_thread_id': sub_thread_id,
'org_id': org_id,
'model_name': model_name,
'message': "",
'message_by': "tools_call",
'type': type,
'bridge_id': bridge_id,
'function': tools,
'tools_call_data': tools_call_data,
'message_id' : message_id,
'version_id': version_id
})

if botMessage is not None:
chatToSave.append({
'thread_id': thread_id,
'sub_thread_id': sub_thread_id,
'org_id': org_id,
'model_name': model_name,
'message': botMessage or "",
'message_by': messageBy,
'type': type,
'bridge_id': bridge_id,
'function': botMessage if messageBy == "tool_calls" else {},
'chatbot_message' : chatbot_message or "",
'message_id' : message_id,
'revised_prompt' : revised_prompt,
"image_urls" : image_url,
'version_id': version_id,
"annotations" : annotations,
"fallback_model" : fallback_model
})
# sending data through rt layer
chatbotSaveCopy = copy.deepcopy(chatToSave)
for item in chatbotSaveCopy:
item["role"] = item.pop("message_by")
item["content"] = item.pop("message")
if item.get('created_at') is None:
item['created_at'] = str(datetime.now())
if item.get('createdAt') is None:
item['createdAt'] = str(datetime.now())

response_format_copy = {
'cred' : {
'channel': org_id + bridge_id,
'apikey': Config.RTLAYER_AUTH,
'ttl': '1'
},
'type' : 'RTLayer'
}
dataToSend={
'Thread':{
"thread_id" : thread_id,
"sub_thread_id": sub_thread_id,
"bridge_id":bridge_id
},
"Messages":chatbotSaveCopy
}
await sendResponse(response_format_copy, dataToSend, True)

result = chatbotDbService.createBulk(chatToSave)
return list(result)
except Exception as error:
logger.error(f"saveconversation error=>, {str(error)}, {traceback.format_exc()}")
raise error

async def add_tool_call_data_in_history(chats):
tools_call_indices = []

Expand Down Expand Up @@ -188,4 +88,4 @@ async def save_sub_thread_id_and_name(thread_id, sub_thread_id, org_id, thread_f
logger.error(f"Error in saving sub thread id and name:, {str(err)}")
return { 'success': False, 'message': str(err) }
# Exporting the functions
__all__ = ['getAllThreads', 'savehistory', 'getThread', 'getThreadHistory', 'getChatData']
__all__ = ['getAllThreads', 'getThread', 'getThreadHistory', 'getChatData']
Loading