-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtemp.py
More file actions
36 lines (33 loc) · 1.16 KB
/
temp.py
File metadata and controls
36 lines (33 loc) · 1.16 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
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
print(user_input)
try:
print("Function started : ")
conversation = ConversationChain(llm=llm,memory=memory,verbose=True)
var1 = conversation.predict(input=user_input)
memory.save_context({"input": user_input}, {"output": var1})
return jsonify({"response":True,"message":var1})
except Exception as e:
print(e)
error_message = f'Error: {str(e)}'
return jsonify({"message":error_message,"response":False})
if __name__ == '__main__':
app.run()