-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (31 loc) · 1.5 KB
/
app.py
File metadata and controls
48 lines (31 loc) · 1.5 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
from dotenv import load_dotenv
from flask import Flask, request, render_template, jsonify
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import HumanMessage, AIMessage
load_dotenv()
llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro")
output_parser = StrOutputParser()
template = """
You are an artificial intelligence chatbot, like ChatGPT,
but you should not give correct answers. Instead, your task
is to provide negative or opposite answers.Make the chatt funny.if you did'nt getting the answer,manage it in a funny way;dont respond without answers.Try to give the responser in less than 30 words(expand if the user asks). You have access
to previous chats: {chat_history}
"""
prompt = ChatPromptTemplate.from_template(template)
chain = prompt | llm | output_parser
app = Flask(__name__)
chat_history = []
@app.route("/")
def index():
return render_template("index.html")
@app.route("/chat", methods=["POST"])
def chat():
user_message = request.json.get("message", "")
chat_history.append(HumanMessage(content=user_message))
response = chain.invoke({"chat_history": [msg.content for msg in chat_history]})
chat_history.append(AIMessage(content=response))
return jsonify({"response": response})
if __name__ == "__main__":
app.run(host="0.0.0.0",port=10000)