Updated app.py per BOLT's instructions
This commit is contained in:
parent
03a38cd29e
commit
b663cc8aee
69
app.py
69
app.py
|
|
@ -6,69 +6,58 @@ app = Flask(__name__)
|
|||
|
||||
# Load Markdown files
|
||||
def loadMarkdownFiles(directory):
|
||||
const files = fs.readdirSync(directory);
|
||||
const markdownFiles = [];
|
||||
files = os.listdir(directory)
|
||||
markdownFiles = []
|
||||
|
||||
for (const file of files) {
|
||||
if (path.extname(file).toLowerCase() === '.md') {
|
||||
const filePath = path.join(directory, file);
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
markdownFiles.push({ name: file, content });
|
||||
}
|
||||
}
|
||||
for file in files:
|
||||
if file.endswith('.md'):
|
||||
filePath = os.path.join(directory, file)
|
||||
with open(filePath, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
markdownFiles.append({'name': file, 'content': content})
|
||||
|
||||
return markdownFiles
|
||||
|
||||
# Initialize RAG model and tokenizer
|
||||
async def initRagModel():
|
||||
const tokenizer = await AutoTokenizer.from_pretrained('facebook/rag-token-nq');
|
||||
const model = await RagTokenForGeneration.from_pretrained('facebook/rag-token-nq');
|
||||
from transformers import AutoTokenizer, RagTokenForGeneration
|
||||
tokenizer = await AutoTokenizer.from_pretrained('facebook/rag-token-nq')
|
||||
model = await RagTokenForGeneration.from_pretrained('facebook/rag-token-nq')
|
||||
|
||||
return { tokenizer, model }
|
||||
return {'tokenizer': tokenizer, 'model': model}
|
||||
|
||||
# Retrieve relevant information from Markdown files using Ollama API
|
||||
async def retrieveInformation(query):
|
||||
try:
|
||||
response = await axios.post('http://localhost:8080/chat', { query });
|
||||
return response.data.response;
|
||||
config = configparser.ConfigParser()
|
||||
config.read('ollama.ini')
|
||||
host = config.get('Ollama', 'host')
|
||||
port = config.get('Ollama', 'port')
|
||||
|
||||
response = await axios.post(f'http://{host}:{port}/chat', {'query': query})
|
||||
return response.json()['response']
|
||||
except (axios.AxiosError, Exception) as error:
|
||||
print(f'Error: {error.message}')
|
||||
print(f'Error: {error}')
|
||||
raise
|
||||
|
||||
# Chatbot logic
|
||||
async def chatbot():
|
||||
const directory = './notes'; // Directory containing Markdown files
|
||||
const markdownFiles = loadMarkdownFiles(directory);
|
||||
const ragModel = await initRagModel();
|
||||
directory = './notes' # Directory containing Markdown files
|
||||
markdownFiles = loadMarkdownFiles(directory)
|
||||
ragModel = await initRagModel()
|
||||
|
||||
print('Chatbot is ready! Ask your questions.')
|
||||
|
||||
process.stdin.on('data', async (data) => {
|
||||
const query = data.toString().trim();
|
||||
if (query.toLowerCase() === 'exit') {
|
||||
process.exit(0);
|
||||
}
|
||||
while True:
|
||||
query = input().strip()
|
||||
if query.lower() == 'exit':
|
||||
break
|
||||
|
||||
try:
|
||||
const response = await retrieveInformation(query);
|
||||
response = await retrieveInformation(query)
|
||||
print(f'Chatbot: {response}')
|
||||
except Exception as error:
|
||||
print(f'Error: {error.message}')
|
||||
|
||||
# Flask route to handle chat requests
|
||||
@app.route('/chat', methods=['POST'])
|
||||
async def chat():
|
||||
data = request.json
|
||||
query = data.get('query')
|
||||
|
||||
if not query:
|
||||
return jsonify({'error': 'No query provided'}), 400
|
||||
|
||||
try:
|
||||
response = await retrieveInformation(query)
|
||||
return jsonify({'response': response})
|
||||
except Exception as error:
|
||||
return jsonify({'error': str(error)}), 500
|
||||
print(f'Error: {error}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(chatbot())
|
||||
|
|
|
|||
Loading…
Reference in New Issue