-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
40 lines (31 loc) · 1.27 KB
/
app.py
File metadata and controls
40 lines (31 loc) · 1.27 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
from flask import Flask, request, jsonify
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.document_loaders.csv_loader import CSVLoader
from langchain.vectorstores import FAISS
app = Flask(__name__)
# Set API key and CSV file path here
user_api_key = "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"
csv_file_path = "Course_data_2.csv"
# Load and process the CSV file
loader = CSVLoader(file_path=csv_file_path, encoding="utf-8")
data = loader.load()
embeddings = OpenAIEmbeddings(openai_api_key=user_api_key)
vectors = FAISS.from_documents(data, embeddings)
chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(temperature=0.0, model_name='gpt-3.5-turbo', openai_api_key=user_api_key),
retriever=vectors.as_retriever()
)
# Function to interact with the chatbot
def conversational_chat(query):
result = chain({"question": query, "chat_history": []})
return result["answer"]
@app.route("/ask", methods=["POST"])
def ask():
data = request.get_json()
user_query = data["query"]
bot_response = conversational_chat(user_query)
return jsonify({"response": bot_response})
if __name__ == "__main__":
app.run(debug=True)