yes . it is displayin the response as a single block
like this
{
“status”: true,
“response”: [
{
“role”: “user”,
“content”: “count 1 to 10”,
“created_at”: “2024-02-01T09:50:17.307313”
},
{
“role”: “user”,
“content”: “count 1 to 10”
},
{
“role”: “assistant”,
“content”: “Sure! I can count from 1 to 10 for you. Here you go:\n\n1, 2, 3, 4, 5, 6, 7, 8, 9, 10.\n\nIs there anything else I can assist you with?”
},
{
“role”: “user”,
“content”: “count 1 to 10”
},
{
“role”: “assistant”,
“content”: “Sure! Here’s the count from 1 to 10:\n\n1, 2, 3, 4, 5, 6, 7, 8, 9, 10.\n\nIs there anything else you would like me to do?”
},
{
“role”: “user”,
“content”: “count 1 to 10”
},
{
“role”: “assistant”,
“content”: “Sure! I can count from 1 to 10 for you. Here you go:\n\n1, 2, 3, 4, 5, 6, 7, 8, 9, 10.\n\nIs there anything else I can assist you with?”
},
{
“role”: “user”,
“content”: “count 1 to 10”
},
{
“role”: “assistant”,
“content”: “Certainly! I am here to assist you. Let’s count from 1 to 10:\n\n1, 2, 3, 4, 5, 6, 7, 8, 9, 10.\n\nIs there anything else you would like me to do?”
},
{
“role”: “user”,
“content”: “count 1 to 10”
},
{
“role”: “user”,
“content”: “You will follow the conversation and respond to the queries asked by the 'user’s content. You will act as the assistant.\nUser input: {‘role’: ‘user’, ‘content’: ‘count 1 to 10’, ‘created_at’: ‘2024-02-01T09:50:17.307313’}”
},
{
“role”: “assistant”,
“content”: “Certainly! I can count from 1 to 10 for you. Here you go:\n\n1, 2, 3, 4, 5, 6, 7, 8, 9, 10.\n\nIs there anything else I can assist you with?”
}
]
}
my api code look like this
class ChatGptView(APIView):
permission_classes =[IsAuthenticated]
def post(self, request):
# try:
data = request.data
conversation_history = []
user_prompt = data.get('user_prompt')
# assistant_prompt = data.get('assistant_prompt')
profile_id = data.get('profile_id')
user_id = request.user.id
regenerate = data.get('regenerate', False)
# validating request
try:
profile_data = Profile.objects.get(id=profile_id)
except Profile.DoesNotExist:
return error_response(error_message="Profile not found", status=status.HTTP_400_BAD_REQUEST)
# Initialize user_input variable
user_input = None
if user_prompt is not None:
user_input = {"role": "user", "content": user_prompt}
conversation_history.append(user_input)
print(conversation_history,"::::::>>>>>")
save_chat_to_mongodb(profile_data, user_input, user_id, regenerate)
recent_chats = get_recent_chats(int(profile_id))
print(recent_chats,"======")
# print("recent_chats",recent_chats,"::::")
old_chats = recent_chats.get('chat', []) if 'chat' in recent_chats else []
old_chats = [{k: v for k, v in item.items() if k != 'created_at'} for item in old_chats]
if len(old_chats) > 0:
conversation_history.extend(old_chats)
profile_name = f"{profile_data.first_name} {profile_data.last_name}" if profile_data.last_name else profile_data.first_name
# Add the AI response using chat_with_ai function
ai_response_generator = chat_with_ai(conversation_history, user_input, profile_name)
ai_response = ''.join(ai_response_generator)
if ai_response:
if isinstance(ai_response, str):
assistant_content = ai_response
elif isinstance(ai_response, dict):
assistant_content = ai_response.get('content', '')
else:
assistant_content = str(ai_response)
assistant = {"role": "assistant", "content": ai_response}
save_chat_to_mongodb(profile_data, assistant, user_id, regenerate)
conversation_history.append({"role": "assistant", "content": assistant_content})
# print('Conversation History', conversation_history,"\n\n")
# Fetch recent chats after AI response
# print(old_chats,"\n::::")
# print(conversation_history,"=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!===")
response_data = json.dumps({"status": True, "response": conversation_history})
return StreamingHttpResponse(response_data, content_type="text/event-stream", status=status.HTTP_200_OK)