File size: 4,102 Bytes
d1dbbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5223bf3
d1dbbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 4/28/2024
# This version added saving chat history to a log file
# Updated the GPT model to gpt-4
# Add timestamp and ip address

# 2/23/2024
# This version uses different method in llama index to define llm model
# Removed deprecated classes and replaced with newest dependencies

import openai
import json
import gradio as gr
from openai import OpenAI
#from langchain_community.chat_models import ChatOpenAI
from langchain_community.chat_models.openai import ChatOpenAI

# rebuild storage context and load knowledge index
# from llama_index import StorageContext, load_index_from_storage, LLMPredictor, ServiceContext
from llama_index import StorageContext, load_index_from_storage, ServiceContext
from llama_index.llms import OpenAI

from datetime import datetime;
import socket;

storage_context = StorageContext.from_defaults(persist_dir='./')
# gpt-3.5_turbo is the current default model
llm_predictor = OpenAI(temperature=0.5, model_name="gpt-4")
service_context = ServiceContext.from_defaults(llm=llm_predictor)
index = load_index_from_storage(storage_context, service_context=service_context)

class Chatbot:
    def __init__(self, api_key, index):
        self.index = index
        openai.api_key = api_key
        self.chat_history = []
        self.history_file = f"chat_log.json"
 
    def generate_response(self, user_input):
        query_engine = index.as_query_engine()
        response = query_engine.query(user_input)
        
        # generate response
        message = {"role": "assistant", "content": response.response}
        
        return message
    
    def load_chat_history(self):
        try:
            with open(self.history_file, 'r') as f:
                self.chat_history = json.load(f)
        except FileNotFoundError:
            pass
    
    def append_chat_history(self, user_input, output):
            # append chat history
        dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print(dt)
        hostname = socket.gethostname()
        ip = socket.gethostbyname(hostname)
        print(ip)
        
        #self.chat_history.append({"role": "datetime", "content": dt})
        #self.chat_history.append({"role": "IP", "content": ip})
        #self.chat_history.append({"role": "user", "content": user_input})
        #self.chat_history.append({"role": "assistant", "content": output})
        
        # save the data in dictionary format
        dictionary = {
            "datetime": dt,
            "ip": ip,
            "user": user_input,
            "assistant": output
        }
        self.chat_history.append(dictionary)

    def save_chat_history(self):
        with open(self.history_file, 'w') as f:
            json.dump(self.chat_history, f)

def create_bot(user_input):
    bot = Chatbot(os.getenv("OPENAI_API_KEY"), index=index)
    bot.load_chat_history();
    
    if user_input:
         # use moderations endpoint to check input
        client = openai.OpenAI()
        response_mod = client.moderations.create(input=user_input)
        response_dict = response_mod.model_dump()
        flagged = response_dict['results'][0]['flagged']
        #print("Flagged:", flagged)
    
        if not flagged:
            response_bot = bot.generate_response(user_input)
            output = response_bot['content']
        else:
             output = "Invalid request."
        
        bot.append_chat_history(user_input, output)
        bot.save_chat_history()
        
        return output

inputs = gr.inputs.Textbox(lines=7, label="Ask questions related to the course. For example, when is the due date for Excel Module 9, what is the assignment late policy, how to use NPV function in Excel, etc.")
outputs = gr.outputs.Textbox(label="Response")

gr.Interface(fn=create_bot, inputs=inputs, outputs=outputs, title="Virtual TA",
             description="This is a prototype of learning assistant designed for MIS 320 online section (Version 2.0). Powered by ChatGPT.\nNote: ChatGPT can make mistakes. Consider checking important information.",
             theme="compact").launch(share=True)