amirhoseinsedaghati commited on
Commit
8f53255
1 Parent(s): 7155372
Files changed (2) hide show
  1. tools/chatbot.py +71 -0
  2. tools/summarizer.py +39 -0
tools/chatbot.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_nvidia_ai_endpoints import ChatNVIDIA
2
+ import streamlit as st
3
+ from tools.summarizer import Summarizer
4
+ from dotenv import load_dotenv
5
+ import os
6
+
7
+
8
+
9
+ load_dotenv()
10
+
11
+ class QuestionAnswering(object):
12
+ def __init__(self, model_name, memory) -> None:
13
+ self.model_name = model_name
14
+ self.memory = memory
15
+ self.llm = ChatNVIDIA(
16
+ model=self.model_name,
17
+ api_key=os.getenv('NV_API_KEY'),
18
+ max_tokens=700,
19
+ temperature=0.01,
20
+ top_p=.7
21
+ )
22
+ self.summarizer = Summarizer()
23
+
24
+
25
+ def generate_answer(self, question:str):
26
+ if self.model_name in ['google/gemma-2-27b-it', 'microsoft/phi-3-medium-128k-instruct']:
27
+ prompt = [
28
+ {
29
+ 'role' : "assistant",
30
+ 'content' : "You are a helpful and native chatbot who can guide clients to write and talk naturally\
31
+ and reduce their mistakes in the different aspects of their English skills. And please\
32
+ guide users in short and concise answers."
33
+ },
34
+ ]
35
+
36
+ else:
37
+ prompt = [
38
+ {
39
+ 'role' : "system",
40
+ 'content' : "You are a helpful and native chatbot who can guide clients to write and talk naturally\
41
+ and reduce their mistakes in the different aspects of their English skills. And please\
42
+ guide users in short and concise answers."
43
+ },
44
+ ]
45
+
46
+ with st.expander('Question'):
47
+ st.write('User: ', question)
48
+
49
+ if len(self.memory) != 0:
50
+ # Save the last 2 conversations
51
+ short_term_memory = self.memory[-4:]
52
+ try:
53
+ summarized_short_term_memory = self.summarizer.summarize(short_term_memory)
54
+ except:
55
+ st.warning(body="Refresh the page or Try it again later.", icon="🤖")
56
+ else:
57
+ prompt.extend(summarized_short_term_memory)
58
+
59
+ user_dict = {'role' : 'user', 'content' : question}
60
+ self.memory.append(user_dict)
61
+ prompt.append(user_dict)
62
+
63
+ res = self.llm.invoke(prompt)
64
+
65
+ assistant_dict = {'role' : res.response_metadata['role'], 'content' : res.content}
66
+ self.memory.append(assistant_dict)
67
+
68
+ with st.expander('Answer'):
69
+ st.write("Assistant: ", assistant_dict['content'])
70
+
71
+
tools/summarizer.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_nvidia_ai_endpoints import ChatNVIDIA
2
+ from langchain_core.prompts import PromptTemplate
3
+ from dotenv import load_dotenv
4
+ import os
5
+
6
+
7
+ load_dotenv()
8
+
9
+ template = """
10
+ You are a helpful Summarizer Chatbot who can just summarize the input text and return.
11
+
12
+ User : {input_text}
13
+
14
+ AI : """
15
+
16
+ prompt = PromptTemplate.from_template(template)
17
+
18
+ class Summarizer(object):
19
+ def __init__(self) -> None:
20
+ self.llm = ChatNVIDIA(
21
+ model='google/gemma-2-2b-it',
22
+ api_key=os.getenv('NV_API_KEY'),
23
+ max_tokens=128,
24
+ temperature=0.01,
25
+ top_p=.7
26
+ )
27
+ self.chain = prompt | self.llm
28
+
29
+
30
+ def summarize(self, mem:list):
31
+ summarized_memory = []
32
+ for item in mem:
33
+ if item['role'].lower() == 'user':
34
+ summarized_memory.append(item)
35
+ else:
36
+ summarized_content = self.chain.invoke(item['content']).content
37
+ summarized_memory.append({'role' : item['role'], 'content' : summarized_content})
38
+
39
+ return summarized_memory