GJ007 commited on
Commit
fdad4bd
·
verified ·
1 Parent(s): 14ceb32

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import random, string
4
+
5
+ from langchain.chains import LLMChain
6
+ from langchain_core.prompts import (
7
+ ChatPromptTemplate,
8
+ HumanMessagePromptTemplate,
9
+ MessagesPlaceholder,
10
+ )
11
+ from langchain_core.messages import SystemMessage
12
+ from langchain.chains.conversation.memory import ConversationBufferWindowMemory
13
+ from langchain_groq import ChatGroq
14
+
15
+ if 'chat_list' not in st.session_state:
16
+ st.session_state.chat_list = []
17
+
18
+
19
+ def arr():
20
+ for c_list in st.session_state.chat_list:
21
+ with st.chat_message("user"):
22
+ st.write("Question : " + c_list["ques"])
23
+ with st.chat_message("machine"):
24
+ st.write("Answer : " + c_list["ans"])
25
+
26
+ def main():
27
+
28
+ """
29
+ This function is the main entry point of the application. It sets up the Groq client, the Streamlit interface, and handles the chat interaction.
30
+ """
31
+
32
+ # Get Groq API key
33
+ groq_api_key = "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"
34
+ model = 'llama3-8b-8192'
35
+ # Initialize Groq Langchain chat object and conversation
36
+ groq_chat = ChatGroq(
37
+ groq_api_key=groq_api_key,
38
+ model_name=model
39
+ )
40
+
41
+ st.title('Langchain Chatbot With llama3-8b-8192 model')
42
+
43
+ # print("Hello! I'm your friendly Groq chatbot. I can help answer your questions, provide information, or just chat. I'm also super fast! Let's start our conversation!")
44
+ st.markdown("Hello! I'm your friendly Groq chatbot, dev by GJ. I can help answer your questions, provide information, or just chat. I'm also super fast! Let's start our conversation!")
45
+
46
+ system_prompt = 'You are a friendly conversational chatbot'
47
+ conversational_memory_length = 5 # number of previous messages the chatbot will remember during the conversation
48
+
49
+ if 'memory' not in st.session_state:
50
+ st.session_state.memory = ConversationBufferWindowMemory(k=conversational_memory_length, memory_key="chat_history", return_messages=True)
51
+ # st.write(st.session_state.memory)
52
+
53
+
54
+ # user_question = st.text_input("Ask a question: ")
55
+ user_question = st.chat_input("Ask a question:")
56
+ if user_question:
57
+ # Construct a chat prompt template using various components
58
+ prompt = ChatPromptTemplate.from_messages(
59
+ [
60
+ SystemMessage(
61
+ content=system_prompt
62
+ ), # This is the persistent system prompt that is always included at the start of the chat.
63
+
64
+ MessagesPlaceholder(
65
+ variable_name="chat_history"
66
+ ), # This placeholder will be replaced by the actual chat history during the conversation. It helps in maintaining context.
67
+
68
+ HumanMessagePromptTemplate.from_template(
69
+ "{human_input}"
70
+ ), # This template is where the user's current input will be injected into the prompt.
71
+ ]
72
+ )
73
+
74
+ # Create a conversation chain using the LangChain LLM (Language Learning Model)
75
+ conversation = LLMChain(
76
+ llm=groq_chat, # The Groq LangChain chat object initialized earlier.
77
+ prompt=prompt, # The constructed prompt template.
78
+ verbose=False, # TRUE Enables verbose output, which can be useful for debugging.
79
+ memory=st.session_state.memory, # The conversational memory object that stores and manages the conversation history.
80
+ )
81
+ # The chatbot's answer is generated by sending the full prompt to the Groq API.
82
+ response = conversation.predict(human_input=user_question)
83
+ # st.text("Question: " + user_question)
84
+ # st.text("Chatbot: " + response)
85
+ result = {"ques":user_question, "ans":response}
86
+ st.session_state.chat_list.append(result)
87
+ arr()
88
+ # st.write(st.session_state.memory)
89
+
90
+ if __name__ == "__main__":
91
+ main()