File size: 5,678 Bytes
204accd
27ac14f
b91f436
5242e1e
 
204accd
5242e1e
 
 
 
 
252481a
 
e4f4106
 
577ae7c
252481a
5242e1e
 
 
 
 
 
a092b77
 
 
 
 
 
 
 
5242e1e
252481a
5242e1e
 
 
b91f436
5d0c781
b91f436
ec98573
 
 
 
262860b
a092b77
5242e1e
fb91144
976a15b
a092b77
 
 
 
 
5242e1e
 
 
 
 
 
5d0c781
5ef15d2
 
 
ec98573
 
e94cac0
b91f436
e94cac0
292ea38
61e9b9a
 
 
 
b91f436
5242e1e
a092b77
5242e1e
 
 
 
 
 
 
c262e5a
0b7c2d1
 
 
 
 
 
5242e1e
 
f32dfca
292ea38
5242e1e
b91f436
89ccb7e
 
 
224d9a3
 
 
b91f436
5242e1e
 
 
 
 
 
 
 
 
224d9a3
5242e1e
 
 
 
 
 
e94cac0
0b7c2d1
b91f436
d0c4f33
5242e1e
 
 
377634a
5242e1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea313d8
9139997
5242e1e
 
 
 
 
 
 
2e360ff
 
5242e1e
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 5/1/2024

# This version added saving chat history to a log file (need persist data from a space to a dataset)
# Updated the GPT model to gpt-4
# Add timestamp and ip address
# upgrade llama-index to version 0.10: migrate from ServiceContext to Settings

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

# Start by setting token and debug mode before starting schedulers
import os
from huggingface_hub import logging, login
login(token=os.environ.get("HF_TOKEN"), write_permission=True)
#logging.set_verbosity_debug()

import openai
import json
import gradio as gr
from openai import OpenAI

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

# for llama-index 0.10
#from llama_index.core import StorageContext
#from llama_index.core import load_index_from_storage
#from llama_index.llms.openai import OpenAI
#from llama_index.core import Settings

# add datetime and ip to the log file
from datetime import datetime;
import socket;

# access data folder of persistent storage
from pathlib import Path
from huggingface_hub import CommitScheduler
from uuid import uuid4

# generate an unique identifier for the session
session_id = uuid4()

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

class Chatbot:
    def __init__(self, api_key, index):
        self.index = index
        openai.api_key = api_key
        self.chat_history = []

        # set chat history data path in data folder (persistent storage)
        dataset_dir = Path("logs")
        dataset_dir.mkdir(parents=True, exist_ok=True)
        #self.dataset_path = dataset_dir / f"chat_log_{uuid4()}.json"
        self.dataset_path = dataset_dir / f"chat_log_{session_id}.json"
        

        self.scheduler = CommitScheduler(
            repo_id="history_data",
            repo_type="dataset",
            folder_path=dataset_dir,
            path_in_repo="data",
        )
        
    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
    
    # do not need this function if use append mode when dump data in file
    #def load_chat_history(self):
    #    try:
    #        with open(self.dataset_path, 'r') as f:
    #            self.chat_history = json.load(f)
    #    except FileNotFoundError:
    #        pass
    
    def append_chat_history(self, user_input, output):
            # create a dictionary for the chat history
        #self.chat_history = []
        dt = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        #print(dt)
        
        #hostname = socket.gethostname()  # this returns server hostname
        #ip = socket.gethostbyname(hostname)
        client_socket = socket.socket()
        client_socket.connect(("huggingface.co",80))
        ip = client_socket.getpeername()[0]
        #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 self.scheduler.lock:
            with self.dataset_path.open("a") as f:
                json.dump(self.chat_history, f)
                f.write("\n")

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.components.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.components.Textbox(label="Response")

gr.Interface(fn=create_bot, inputs=inputs, outputs=outputs, title="Virtual TA",
             description="This is a virtual learning assistant designed for MIS 320 online section (Version 2.0, powered by GPT-4).\nNote: Chatbot can make mistakes. Consider checking important information."
            ).launch(share=True)