-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathchatgpt.py
91 lines (54 loc) · 1.75 KB
/
chatgpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import openai
import requests
from flask import Flask, request, jsonify, render_template
from flask import current_app
import json
from PyPDF2 import PdfReader
from flask_sqlalchemy import SQLAlchemy
import pdfplumber
from bs4 import BeautifulSoup
from lib.config import Config
from lib.db_models import db
from lib.db_models import ExtractedData
from lib.db_models import create_app, db, store_data, query_data
from lib.search import simple_search
from flask import Flask
from lib.config import Config
from lib.db_models import db
from flask import Flask
from lib.db_models import db
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
# Initialize extensions with the app instance
db.init_app(app)
return app
@app.route('/')
def chatbot():
return render_template('ai_chatbot.html')
@app.route('/query', methods=['POST'])
def handle_query():
data_request = request.json
query = data_request.get('query')
if not query:
return jsonify({"error": "No query provided"}), 400
context = simple_search(query)
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": query},
{"role": "assistant", "content": context},
]
)
answer = response.choices[0].message['content']
except Exception as e:
return jsonify({"error": str(e)}), 500
return jsonify({"answer": answer})
@app.route('/test_error')
def test_error():
raise Exception('Test exception')
if __name__ == '__main__':
app.run(debug=False, port=5001) # Set to False in production