Rohan Kataria commited on
Commit
1a59d21
β€’
1 Parent(s): 1457ac5
Files changed (2) hide show
  1. app.py +20 -19
  2. src/main.py +41 -18
app.py CHANGED
@@ -5,11 +5,12 @@ import os
5
  # Constants
6
  ROLE_USER = "user"
7
  ROLE_ASSISTANT = "assistant"
 
8
 
9
  st.set_page_config(page_title="Chat with Git", page_icon="🦜")
10
  st.title("Chat with Git πŸ€–πŸ“š")
11
  st.markdown("by [Rohan Kataria](https://www.linkedin.com/in/imrohan/) view more at [VEW.AI](https://vew.ai/)")
12
- st.markdown("This app allows you to chat with Git code files. You can paste link to the Git repository and ask questions about it. In the backround uses the Git Loader and ConversationalRetrival chain from langchain, Streamlit for UI.")
13
 
14
  @st.cache_resource(ttl="1h")
15
  def load_agent(url, branch, file_filter):
@@ -19,12 +20,6 @@ def load_agent(url, branch, file_filter):
19
  return agent
20
 
21
  def main():
22
- api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")
23
- if api_key:
24
- os.environ["OPENAI_API_KEY"] = api_key
25
- else:
26
- st.sidebar.error("Please enter your OpenAI API Key.")
27
- return
28
 
29
  git_link = st.sidebar.text_input("Enter your Git Link")
30
  branch = st.sidebar.text_input("Enter your Git Branch")
@@ -32,12 +27,14 @@ def main():
32
 
33
  if "agent" not in st.session_state:
34
  st.session_state["agent"] = None
 
35
 
36
  if st.sidebar.button("Load Agent"):
37
  if git_link and branch and file_filter:
38
  try:
39
  st.session_state["agent"] = load_agent(git_link, branch, file_filter)
40
  st.session_state["messages"] = [{"role": ROLE_ASSISTANT, "content": "How can I help you?"}]
 
41
  except Exception as e:
42
  st.sidebar.error(f"Error loading Git repository: {str(e)}")
43
  return
@@ -46,21 +43,25 @@ def main():
46
  for msg in st.session_state.messages:
47
  st.chat_message(msg["role"]).write(msg["content"])
48
 
49
- user_query = st.chat_input(placeholder="Ask me anything!")
 
50
 
51
- if user_query:
52
- st.session_state.messages.append({"role": ROLE_USER, "content": user_query})
53
- st.chat_message(ROLE_USER).write(user_query)
 
54
 
55
- # Generate the response
56
- with st.spinner("Generating response"):
57
- response = st.session_state["agent"](user_query)
58
 
59
- # Display the response immediately
60
- st.chat_message(ROLE_ASSISTANT).write(response)
61
 
62
- # Add the response to the message history
63
- st.session_state.messages.append({"role": ROLE_ASSISTANT, "content": response})
 
 
64
 
65
  if __name__ == "__main__":
66
- main()
 
5
  # Constants
6
  ROLE_USER = "user"
7
  ROLE_ASSISTANT = "assistant"
8
+ MAX_MESSAGES = 4
9
 
10
  st.set_page_config(page_title="Chat with Git", page_icon="🦜")
11
  st.title("Chat with Git πŸ€–πŸ“š")
12
  st.markdown("by [Rohan Kataria](https://www.linkedin.com/in/imrohan/) view more at [VEW.AI](https://vew.ai/)")
13
+ st.markdown("This app allows you to chat with Git code files. You can paste link to the Git repository and ask questions about it. In the background uses the Git Loader and ConversationalRetrieval chain from langchain, Streamlit for UI.")
14
 
15
  @st.cache_resource(ttl="1h")
16
  def load_agent(url, branch, file_filter):
 
20
  return agent
21
 
22
  def main():
 
 
 
 
 
 
23
 
24
  git_link = st.sidebar.text_input("Enter your Git Link")
25
  branch = st.sidebar.text_input("Enter your Git Branch")
 
27
 
28
  if "agent" not in st.session_state:
29
  st.session_state["agent"] = None
30
+ st.session_state["user_message_count"] = 0
31
 
32
  if st.sidebar.button("Load Agent"):
33
  if git_link and branch and file_filter:
34
  try:
35
  st.session_state["agent"] = load_agent(git_link, branch, file_filter)
36
  st.session_state["messages"] = [{"role": ROLE_ASSISTANT, "content": "How can I help you?"}]
37
+ st.session_state["user_message_count"] = 0
38
  except Exception as e:
39
  st.sidebar.error(f"Error loading Git repository: {str(e)}")
40
  return
 
43
  for msg in st.session_state.messages:
44
  st.chat_message(msg["role"]).write(msg["content"])
45
 
46
+ if st.session_state["user_message_count"] < MAX_MESSAGES:
47
+ user_query = st.chat_input(placeholder="Ask me anything!")
48
 
49
+ if user_query:
50
+ st.session_state.messages.append({"role": ROLE_USER, "content": user_query})
51
+ st.chat_message(ROLE_USER).write(user_query)
52
+ st.session_state["user_message_count"] += 1
53
 
54
+ # Generate the response
55
+ with st.spinner("Generating response"):
56
+ response = st.session_state["agent"](user_query)
57
 
58
+ # Display the response immediately
59
+ st.chat_message(ROLE_ASSISTANT).write(response)
60
 
61
+ # Add the response to the message history
62
+ st.session_state.messages.append({"role": ROLE_ASSISTANT, "content": response})
63
+ else:
64
+ st.warning("Your message limit is over. Contact [Rohan Kataria](https://www.linkedin.com/in/imrohan/) to increase the limit.")
65
 
66
  if __name__ == "__main__":
67
+ main()
src/main.py CHANGED
@@ -16,10 +16,19 @@ from langchain.llms import OpenAI
16
  from langchain.memory import ConversationBufferMemory
17
  from langchain.vectorstores import Chroma
18
  from langchain.embeddings.openai import OpenAIEmbeddings
19
- from langchain.prompts import PromptTemplate
20
  import datetime
21
  import shutil
22
 
 
 
 
 
 
 
 
 
 
23
  # Function to load the data from github using langchain with string type url, string type branch, string type file_filter
24
  def loader(url: str, branch: str, file_filter: str):
25
  repo_path = "./github_repo"
@@ -60,25 +69,39 @@ def ingest_chunks(chunks):
60
  return vector_store
61
 
62
  #Retreival function to get the data from the database and reply to the user
63
- def retreival(vector_store):
64
  # Selecting the right model
65
- llm_name = "gpt-3.5-turbo"
 
 
 
 
66
 
67
  #Creating LLM
68
- llm = ChatOpenAI(model=llm_name, temperature=0.5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # Creating Prompt template
71
- template = """
72
- You're a Git Code summarisation assistant who searches through "SOURCE DOCUMENTS" and provides helpful sumamries with "CODE SNIPPETS". Given the following extracted parts of a long document and a question, create a final answer with "CODE SNIPPETS" from "SOURCE DOCUMENTS".
73
- If you don't know the answer, just say that you don't know. Don't try to make up an answer.
74
- =========
75
- QUESTION: {question}
76
- =========
77
- CONTEXT: {context}
78
- =========
79
- FINAL ANSWER:"""
80
 
81
- PROMPT = PromptTemplate(input_variables=["context", "question"], template=template,)
82
 
83
  #Creating memory
84
  memory = ConversationBufferMemory(
@@ -88,7 +111,7 @@ def retreival(vector_store):
88
  return_messages=True)
89
 
90
  #Creating the retriever, this can also be a contextual compressed retriever
91
- retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5}) #search_type can be "similarity" or "mmr"
92
 
93
  chain = ConversationalRetrievalChain.from_llm(
94
  llm=llm,
@@ -111,8 +134,8 @@ class ConversationalResponse:
111
  self.chunks = split_data(self.data)
112
  self.vector_store = ingest_chunks(self.chunks)
113
  self.chain_type = "stuff"
114
- self.k = 5
115
- self.chain = retreival(self.vector_store)
116
 
117
  def __call__(self, question):
118
  agent = self.chain(question)
 
16
  from langchain.memory import ConversationBufferMemory
17
  from langchain.vectorstores import Chroma
18
  from langchain.embeddings.openai import OpenAIEmbeddings
19
+ from langchain.prompts import PromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate, ChatPromptTemplate
20
  import datetime
21
  import shutil
22
 
23
+
24
+ # Setting up environment variables
25
+ os.environ['LANGCHAIN_TRACING_V2'] = "True"
26
+ os.environ['LANGCHAIN_ENDPOINT']
27
+ os.environ['LANGCHAIN_API_KEY']
28
+ os.environ['LANGCHAIN_PROJECT']
29
+ os.environ["OPENAI_API_KEY"]
30
+
31
+
32
  # Function to load the data from github using langchain with string type url, string type branch, string type file_filter
33
  def loader(url: str, branch: str, file_filter: str):
34
  repo_path = "./github_repo"
 
69
  return vector_store
70
 
71
  #Retreival function to get the data from the database and reply to the user
72
+ def retreival(vector_store, k):
73
  # Selecting the right model
74
+ current_date = datetime.datetime.now().date()
75
+ if current_date < datetime.date(2023, 9, 2):
76
+ llm_name = "gpt-3.5-turbo-0301"
77
+ else:
78
+ llm_name = "gpt-3.5-turbo"
79
 
80
  #Creating LLM
81
+ llm = ChatOpenAI(model=llm_name, temperature=0)
82
+
83
+ # Define the system message template
84
+ system_template = """You're a code summarisation assistant. Given the following extracted parts of a long document as "CONTEXT" create a final answer.
85
+ If you don't know the answer, just say that you don't know. Don't try to make up an answer.
86
+ Only If asked to create a "DIAGRAM" for code use "MERMAID SYNTAX LANGUAGE" in your answer from "CONTEXT" and "CHAT HISTORY".
87
+
88
+ CONTEXT: {context}
89
+ =======
90
+ FINAL ANSWER:"""
91
+
92
+ human_template = """{question}"""
93
+
94
+ # ai_template = """
95
+ # FINAL ANSWER:"""
96
 
97
+ # Create the chat prompt templates
98
+ messages = [
99
+ SystemMessagePromptTemplate.from_template(system_template),
100
+ HumanMessagePromptTemplate.from_template(human_template),
101
+ # AIMessagePromptTemplate.from_template(ai_template)
102
+ ]
 
 
 
 
103
 
104
+ PROMPT = ChatPromptTemplate.from_messages(messages)
105
 
106
  #Creating memory
107
  memory = ConversationBufferMemory(
 
111
  return_messages=True)
112
 
113
  #Creating the retriever, this can also be a contextual compressed retriever
114
+ retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": k}) #search_type can be "similarity" or "mmr"
115
 
116
  chain = ConversationalRetrievalChain.from_llm(
117
  llm=llm,
 
134
  self.chunks = split_data(self.data)
135
  self.vector_store = ingest_chunks(self.chunks)
136
  self.chain_type = "stuff"
137
+ self.k = 15
138
+ self.chain = retreival(self.vector_store, self.k)
139
 
140
  def __call__(self, question):
141
  agent = self.chain(question)