chatbot-demo / app.py
muryshev's picture
Prompt updates.
a868da0
from flask import Flask, request, Response, jsonify, send_from_directory
import os
import requests
import json
from dotenv import load_dotenv
from search import search
import prompts
# Загрузка переменных окружения из файла .env
load_dotenv()
app = Flask(__name__, static_url_path='/static', static_folder='static')
initial_prompt = """
{запрос пользователя}
Источники:
{источники}
"""
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/send_message', methods=['POST'])
def send_message():
data = request.json
user_message = data.get('message')
conversation_history = data.get('history', [])
if user_message:
# Добавляем системное сообщение, если его нет в истории
if not any(msg['role'] == 'system' for msg in conversation_history):
conversation_history.insert(0, {"role": "system", "content": prompts.SYSTEM_PROMPT})
search_request = ". ".join([msg['display'] for msg in conversation_history if msg['role'] == 'user']) + ". "+user_message
print('search', search_request)
_, sources = search(search_request)
print('results', sources)
formatted_user_message = prompts.MESSAGE_PROMPT_TEMPLATE.format(
ВОПРОС_ПОЛЬЗОВАТЕЛЯ=user_message,
ПОЛЕЗНАЯ_ИНФОРМАЦИЯ='\n\n'.join(sources)
)
# Добавляем сообщение пользователя в историю
conversation_history.append({"role": "user", "content": formatted_user_message, "display": user_message})
# Отправляем историю сообщений в API
response = fetch_deep_infra_response(conversation_history)
# Добавляем ответ бота в историю
conversation_history.append({"role": "assistant", "content": response, "display": response})
# Используем json.dumps с ensure_ascii=False
response_data = {
'response': response,
'history': conversation_history
}
return Response(json.dumps(response_data, ensure_ascii=False), content_type='application/json; charset=utf-8')
return jsonify({'response': 'Please provide a message.', 'history': conversation_history}), 400
def fetch_deep_infra_response(conversation_history):
api_url = 'https://api.deepinfra.com/v1/openai/chat/completions'
api_key = os.getenv('DEEPINFRA_API_KEY')
if not api_key:
raise ValueError("DEEPINFRA_API_KEY environment variable is not set")
response = requests.post(
api_url,
headers={
'Content-Type': 'application/json',
'Authorization': f'{api_key}'
},
json={
"model": os.getenv('LLM_NAME', "mistralai/Mixtral-8x7B-Instruct-v0.1"),
"messages": conversation_history,
"temperature": os.getenv('LLM_TEMPERATURE', 0),
"max_tokens": os.getenv('LLM_MAX_TOKENS', 2000),
"top_p": os.getenv('LLM_TOP_P', 0.95),
"presence_penalty": os.getenv('LLM_PRESENSE_PENALTY', 1),
"frequency_penalty": os.getenv('LLM_FREQUENCY_PENALTY', -0.001),
}
)
if response.status_code == 200:
data = response.json()
return data['choices'][0]['message']['content'].strip()
else:
print(response.json())
return 'Sorry, something went wrong.'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=os.getenv('APP_PORT', 7860), debug=False)