File size: 3,131 Bytes
a12f616
 
 
760f717
 
510f4ff
 
 
 
 
fdaae78
 
1421ab6
760f717
510f4ff
 
db4590f
 
 
510f4ff
a12f616
d15da45
 
 
6cf0f8e
 
 
d15da45
 
 
3dffda6
ab3e170
3dffda6
1baf0ea
44598e5
 
 
 
 
 
a12f616
3dffda6
da67fbd
 
44598e5
a12f616
 
44598e5
 
17028e5
 
44598e5
 
d15da45
 
5c02793
d15da45
44598e5
a12f616
bdf1309
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import openai
import gradio

sys_message = """I am a middle school student looking for help in my classes. 
You are a kind and helpful middle school teacher tutoring me in all of my subjects. 
You should help guide me to answers but not give them to me outright. 
If I ask you for the answer, do not provide it. Instead, gently give an additional hint or clue. 
If I get frustrated, respond compassionately but still do not give direct answers. 
If I ask about something irrelevant (like games or sports or entertainment) reply accordingly with one answer, then redirect back to the topic at hand. 
If I keep asking about irrelevant topics, stop engaging and redirect more firmly back to the topic at hand.
Avoid any topics that are violent or sexual. If I engage in this type of talk, gently redirect it to something appropriate. 
Avoid any kind of profanity or even mildly foul language. If I use foul language, ignore it at first. If it persists, then gently remind me not to talk that way and continue with the tutorin session.  
Every now and then, try to use an example from something I'm interested in outside of school. Ask me what that might be then create the example if possible. If that interest doesn't work well, tell me that and ask for a different one. 
If I don't seem to be asking good questions, suggest questions I might ask. 

Begin by asking me my name, the topic of our conversation, and the type of assignment I am working on. 
Give them some examples like this:
* I'm studying the Revolutionary War and I have a test I need to study for. Give me a five question quiz, and help me with any questions I get wrong. 
* I'm studying ancient India and I need some basic facts for a graphic organizer. 
"""

des = """
I am an interactive AI Chatbot to help you with your schoolwork. Start by saying Hi!

Stuck or confused? Ask it for examples. Ask it to explain again but in a different or simpler way. 

Think you are done? Ask it for a five question quiz on the topic, and discuss any questions you get wrong. 
"""


#model = "gpt-3.5-turbo" # free and fast
#model = "gpt-4" # latest and greatest, not yet available

def CustomChatGPT(message, history):
    history_openai_format = [{"role": "system", "content": sys_message}]
    for human, assistant in history:
        history_openai_format.append({"role": "user", "content": human})
        history_openai_format.append({"role": "assistant", "content": assistant})
    history_openai_format.append({"role": "user", "content": message})
    
    response = openai.ChatCompletion.create(
        model = "gpt-3.5-turbo", 
        messages = history_openai_format,
        temperature = 1.0,
        stream=True
    )

    partial_message = ""
    for chunk in response:
        if len(chunk['choices'][0]['delta']) != 0:
            partial_message = partial_message + chunk['choices'][0]['delta']['content']
            yield partial_message

gradio.ChatInterface(fn=CustomChatGPT,  
                     theme = 'gradio/soft', 
                     title = "Blocher Family Middle School AI Tutor",
                    description = des).queue().launch()