thats the function where i send the request:
def post_user_input(user_input, request, user_id):
conversation_history = [
{"role": "system",
"content": "You are a helpful assistant who answers in maximum 10 sentences, shortly, precise but nicely."
},
{"role": "user",
"content": f"Answer very shortly and precise: {user_input}" # Answer shortly precise:
}
]
data = { # choose model, give history, temperature means the creativity (0-2 high=super creative))
"model": "gpt-3.5-turbo",
"messages": conversation_history,
"temperature": .5
}
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {OPENAI_API_KEY}'
}
response = requests.post(
'https://api.openai.com/v1/chat/completions',
headers=headers,
data=json.dumps(data)
)
response_json = response.json()
print("JSON response.", response_json)
if response.status_code == 200 or response.status_code == 201 or 'choices' in response_json and len(
response_json['choices']) > 0:
print("Request successfull \n raw json:", response.json(), )
ai_message = response.json()['choices'][0]['message']['content']
else:
print("Request failed")
ai_message = "Nothing"
print("AI response: ", ai_message)
try:
# do the request not with objects.get this will rais an error when there are more than onbe object to return
existing_object = ChatModel.objects.filter(user_id=user_id, text=user_input)
print("Message allready exist from that user: ", existing_object.id)
except Exception as e:
print("Error while get the qs:", e)
message = ChatModel(
text=user_input,
user_id=request.data.get("user_id"),
response=ai_message
)
message.save()
return {
"message": ai_message,
"status": response.status_code
}