venkat charan commited on
Commit
dac5706
·
verified ·
1 Parent(s): 52346c9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain_core.prompts import ChatPromptTemplate
3
+ from langchain_openai import ChatOpenAI
4
+ from langchain_google_genai import ChatGoogleGenerativeAI
5
+ from langchain_core.prompts import MessagesPlaceholder
6
+ from langchain.memory import ConversationBufferWindowMemory
7
+ from operator import itemgetter
8
+ from langchain_core.runnables import RunnableLambda,RunnablePassthrough
9
+
10
+ genai_key = os.getenv("gen_key")
11
+
12
+ model = ChatGoogleGenerativeAI(temperature=0,model='gemini-1.5-pro',max_output_tokens=150,convert_system_message_to_human=True,api_key=genai_key)
13
+
14
+ prompt=ChatPromptTemplate.from_messages([
15
+ ("system","you are a good assistant"),
16
+ MessagesPlaceholder(variable_name="history"),
17
+ ("human","{input}")])
18
+
19
+
20
+ memory=ConversationBufferWindowMemory(k=3,return_messages=True)
21
+
22
+ chain=(RunnablePassthrough.assign(history=RunnableLambda(memory.load_memory_variables)|
23
+ itemgetter("history"))|prompt|model)
24
+
25
+ # Streamlit interface
26
+ st.title("chat bot")
27
+ st.write("Enter your input text:")
28
+
29
+ # User input
30
+ user_input = st.text_area("Input Text")
31
+
32
+ # Generate and display the response
33
+ if st.button("Generate Response"):
34
+ res = chain.invoke({"input": user_input})
35
+ st.write("Generated Response:")
36
+ st.write(res.content)
37
+
38
+ # Save the context
39
+ memory.save_context({"input": user_input}, {"output": res.content})
40
+
41
+ # Display the conversation history
42
+ #st.write("Conversation History:")
43
+ #for msg in memory.buffer:
44
+ # st.write(f"{msg['role']}: {msg['content']}")
45
+
46
+ # Optionally, you can also add a button to reset the conversation history
47
+ if st.button("End Conversation"):
48
+ st.write("Conversation ended.")
49
+ memory.clear()
50
+
51
+