-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbotinterface.py
More file actions
50 lines (41 loc) · 1.38 KB
/
chatbotinterface.py
File metadata and controls
50 lines (41 loc) · 1.38 KB
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
# Develop a chatbot interface for AI representative
# to communicate with US citizens and understand their needs and concerns related to overall wellbeing,
# and generate a survey based on the conversation.
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import openai
import config
# Replace with your OpenAI API key
openai.api_key = config.OPENAI_API_KEY
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
class Chat(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_input = db.Column(db.String(500))
ai_response = db.Column(db.String(500))
@app.route('/')
def index():
return "Chatbot API is up and running!"
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.form.get('user_input')
response = generate_ai_response(user_input)
chat = Chat(user_input=user_input, ai_response=response)
db.session.add(chat)
db.session.commit()
return jsonify({'response': response})
def generate_ai_response(prompt):
model_engine = "text-davinci-003"
response = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=150,
n=1,
stop=None,
temperature=0.7,
)
return response.choices[0].text.strip()
if __name__ == '__main__':
db.create_all()
app.run(debug=True)