Skip to content

Commit

Permalink
support for conversation export and import
Browse files Browse the repository at this point in the history
  • Loading branch information
xesrc committed May 7, 2023
1 parent d3181b6 commit bb0aa6e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
64 changes: 64 additions & 0 deletions chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,70 @@ def gen_title(request):
})


@api_view(['POST'])
# @authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
def upload_conversations(request):
"""allow user to import a list of conversations"""
user=request.user
import_err_msg = 'bad_import'
conversation_ids = []
try:
imports = request.data.get('imports')
# verify
conversations = []
for conversation in imports:
topic = conversation.get('conversation_topic', None)
messages = []
for message in conversation.get('messages'):
msg = {}
msg['role'] = message['role']
msg['content'] = message['content']
messages.append(msg)
if len(messages) > 0:
conversations.append({
'topic': topic,
'messages': messages,
})
# dump
for conversation in conversations:
topic = conversation['topic']
messages = conversation['messages']
cobj = Conversation(
topic=topic if topic else '',
user=user,
)
cobj.save()
conversation_ids.append(cobj.id)
for idx, msg in enumerate(messages):
try:
Message._meta.get_field('user')
mobj = Message(
user=user,
conversation=cobj,
message=msg['content'],
is_bot=msg['role'] != 'user',
messages=messages[:idx + 1],
)
except:
mobj = Message(
conversation=cobj,
message=msg['content'],
is_bot=msg['role'] != 'user',
messages=messages[:idx + 1],
)
mobj.save()
except Exception as e:
logger.debug(e)
return Response(
{'error': import_err_msg},
status=status.HTTP_400_BAD_REQUEST
)

# return a list of new conversation id
return Response(conversation_ids)


@api_view(['POST'])
# @authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand Down
3 changes: 2 additions & 1 deletion chatgpt_ui_server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"""
from django.contrib import admin
from django.urls import path, include
from chat.views import conversation, gen_title
from chat.views import conversation, gen_title, upload_conversations

urlpatterns = [
path('api/chat/', include('chat.urls')),
path('api/conversation/', conversation, name='conversation'),
path('api/upload_conversations/', upload_conversations, name='upload_conversations'),
path('api/gen_title/', gen_title, name='gen_title'),
path('api/account/', include('account.urls')),
path('admin/', admin.site.urls),
Expand Down

0 comments on commit bb0aa6e

Please sign in to comment.