-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmain.py
More file actions
34 lines (31 loc) · 1.1 KB
/
main.py
File metadata and controls
34 lines (31 loc) · 1.1 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
from flask import Flask, render_template,jsonify,request
from flask_cors import CORS
import requests,openai,os
from dotenv.main import load_dotenv
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
from langchain.memory import ConversationSummaryBufferMemory
llm = OpenAI()
memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/data', methods=['POST'])
def get_data():
data = request.get_json()
text=data.get('data')
user_input = text
try:
conversation = ConversationChain(llm=llm,memory=memory)
output = conversation.predict(input=user_input)
memory.save_context({"input": user_input}, {"output": output})
return jsonify({"response":True,"message":output})
except Exception as e:
print(e)
error_message = f'Error: {str(e)}'
return jsonify({"message":error_message,"response":False})
if __name__ == '__main__':
app.run()