Refactor chat_with_assistant function to support messages array input: Enhance the chatbot API by allowing an array of messages for context, extracting system messages, and updating the response format. Maintain backward compatibility with the previous prompt structure while improving error handling for empty inputs.
This commit is contained in:
Binary file not shown.
@@ -932,15 +932,38 @@ def too_many_requests(e):
|
|||||||
def chat_with_assistant():
|
def chat_with_assistant():
|
||||||
"""Chatbot-API mit OpenAI Integration."""
|
"""Chatbot-API mit OpenAI Integration."""
|
||||||
data = request.json
|
data = request.json
|
||||||
prompt = data.get('prompt', '')
|
|
||||||
context = data.get('context', '')
|
|
||||||
|
|
||||||
if not prompt:
|
# Prüfen, ob wir ein einzelnes Prompt oder ein messages-Array haben
|
||||||
return jsonify({
|
if 'messages' in data:
|
||||||
'error': 'Prompt darf nicht leer sein.'
|
messages = data.get('messages', [])
|
||||||
}), 400
|
if not messages:
|
||||||
|
return jsonify({
|
||||||
try:
|
'error': 'Keine Nachrichten vorhanden.'
|
||||||
|
}), 400
|
||||||
|
|
||||||
|
# Extrahiere Systemnachricht falls vorhanden, sonst Standard-Systemnachricht
|
||||||
|
system_message = next((msg['content'] for msg in messages if msg['role'] == 'system'),
|
||||||
|
"Du bist ein hilfreicher Assistent, der Menschen dabei hilft, "
|
||||||
|
"Wissen zu organisieren und zu verknüpfen. Liefere informative, "
|
||||||
|
"sachliche und gut strukturierte Antworten.")
|
||||||
|
|
||||||
|
# Formatiere Nachrichten für OpenAI API
|
||||||
|
api_messages = [{"role": "system", "content": system_message}]
|
||||||
|
|
||||||
|
# Füge Benutzer- und Assistenten-Nachrichten hinzu
|
||||||
|
for msg in messages:
|
||||||
|
if msg['role'] in ['user', 'assistant']:
|
||||||
|
api_messages.append({"role": msg['role'], "content": msg['content']})
|
||||||
|
else:
|
||||||
|
# Alte Implementierung für direktes Prompt
|
||||||
|
prompt = data.get('prompt', '')
|
||||||
|
context = data.get('context', '')
|
||||||
|
|
||||||
|
if not prompt:
|
||||||
|
return jsonify({
|
||||||
|
'error': 'Prompt darf nicht leer sein.'
|
||||||
|
}), 400
|
||||||
|
|
||||||
# Zusammenfassen mehrerer Gedanken oder Analyse anfordern
|
# Zusammenfassen mehrerer Gedanken oder Analyse anfordern
|
||||||
system_message = (
|
system_message = (
|
||||||
"Du bist ein hilfreicher Assistent, der Menschen dabei hilft, "
|
"Du bist ein hilfreicher Assistent, der Menschen dabei hilft, "
|
||||||
@@ -951,20 +974,24 @@ def chat_with_assistant():
|
|||||||
if context:
|
if context:
|
||||||
system_message += f"\n\nKontext: {context}"
|
system_message += f"\n\nKontext: {context}"
|
||||||
|
|
||||||
|
api_messages = [
|
||||||
|
{"role": "system", "content": system_message},
|
||||||
|
{"role": "user", "content": prompt}
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
response = client.chat.completions.create(
|
response = client.chat.completions.create(
|
||||||
model="gpt-3.5-turbo-16k",
|
model="gpt-3.5-turbo-16k",
|
||||||
messages=[
|
messages=api_messages,
|
||||||
{"role": "system", "content": system_message},
|
|
||||||
{"role": "user", "content": prompt}
|
|
||||||
],
|
|
||||||
max_tokens=300,
|
max_tokens=300,
|
||||||
temperature=0.7
|
temperature=0.7
|
||||||
)
|
)
|
||||||
|
|
||||||
answer = response.choices[0].message.content
|
answer = response.choices[0].message.content
|
||||||
|
|
||||||
|
# Für das neue Format erwarten wir response statt answer
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'answer': answer
|
'response': answer
|
||||||
})
|
})
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user