amirhoseinsedaghati commited on
Commit
01d405d
·
verified ·
1 Parent(s): a7290d0

add chatbot.py, main.py, and moderator.py

Browse files
data_science_tutor/chatbot.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import streamlit as st
3
+ from moderator import moderator
4
+
5
+
6
+
7
+ gpt_model = 'gpt-3.5-turbo'
8
+ client = OpenAI(
9
+ api_key= "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"
10
+ )
11
+
12
+ messages = [
13
+ {
14
+ 'role' : "system",
15
+ 'content' : "You are a data science tutor who provides short, simple, clear answers.\
16
+ You can't provide information on a wide range of topics beyond data science and programming."
17
+ },
18
+ {
19
+ 'role' : 'user',
20
+ 'content' : 'Tell me about yourself.'
21
+ },
22
+ {
23
+ 'role' : 'assistant',
24
+ 'content' : "I am a data sciece assistant. My purpose is to assist users by providing concise and\
25
+ accurate answers in the topics related to data science, programming, mathematics,\
26
+ Artificial Intelligence,Machine Learning, Deep Learning and related fields."
27
+ },
28
+ {
29
+ 'role' : 'user',
30
+ 'content' : 'Can you provide information in topics not related to data science and programming?'
31
+ },
32
+ {
33
+ 'role' : 'assistant',
34
+ 'content' : "No, I'm here to assist yout in data related fields such data science, programming,\
35
+ mathematics and more."
36
+ }
37
+ ]
38
+
39
+
40
+ # GPT-3.5 response generation
41
+ def chatbot(prompt:str):
42
+ flagged = moderator(prompt)
43
+ st.write('User: ', prompt)
44
+ if flagged == True:
45
+ st.write("Assistant: ", "The latest question violates OpenAI's content policy. Please change it.", "\n")
46
+
47
+ else:
48
+ user_dict = {'role' : 'user', 'content' : prompt}
49
+ messages.append(user_dict)
50
+
51
+ res = client.chat.completions.create(
52
+ model=gpt_model,
53
+ messages=messages
54
+ )
55
+ assistant_dict = {'role' : res.choices[0].message.role, 'content' : res.choices[0].message.content}
56
+ messages.append(assistant_dict)
57
+ st.write("Assistant: ", assistant_dict['content'], "\n")
58
+
59
+
data_science_tutor/main.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from chatbot import chatbot
3
+
4
+
5
+
6
+ # Streamlit application
7
+ def combined_model():
8
+ st.title("Data Science Assistant")
9
+ curr_question = st.text_area(
10
+ 'enter your prompt',
11
+ placeholder="Talk to your Data Science Tutor: ",
12
+ label_visibility="hidden"
13
+ )
14
+ if st.button("Generate Answer"):
15
+ try:
16
+ chatbot(curr_question)
17
+ except:
18
+ st.warning(body="Refresh the page or Try it again later.", icon="🤖")
19
+
20
+ combined_model()
21
+
data_science_tutor/moderator.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+
3
+
4
+
5
+ moderation_model = 'text-moderation-latest'
6
+ client = OpenAI(
7
+ api_key="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"
8
+ )
9
+
10
+
11
+ def moderator(prompt:str):
12
+ response = client.moderations.create(
13
+ input= prompt,
14
+ model= moderation_model,
15
+ )
16
+
17
+ return response.results[0].flagged