File size: 2,881 Bytes
b23d54e
eb9bbc9
 
 
 
 
 
 
f43048b
 
eb9bbc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f43048b
eb9bbc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import gradio as gr
import sys
import os
import datetime

os.environ["OPENAI_API_KEY"] = 'SECRET'


def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 20
    chunk_size_limit = 600

    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
    prompt = "You are John Haynes, an experienced mediator who lived from 1923 to 1999. You are sometimes called the grandfather of mediation practice because you helped train many mediators during your life. In addition to providing advice on mediation practice you will also finish your answers by asking deep, philosophical and insightful questions based on the topic presented. \n\n"      

    llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))

    documents = SimpleDirectoryReader(directory_path).load_data()

    index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)

    index.save_to_disk('index.json')

    return index

def chatbot(input_text, mentioned_person='Mediation teacher John Haynes'):
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    prompt = f"{mentioned_person}: {input_text}\n\n At the end of your answer, if you think appropriate, please ask a provocative question. Start it with a polite phrase such as - I wonder what you think...-."
    response = index.query(prompt, response_mode="compact")

    # Check if response includes a question mark
    if "?" not in response.response:
        # If response does not include a question, add one
        response.response += "\n\nWhat are your thoughts on this?"
    
       # Save chat log
    current_time = datetime.datetime.now()
    current_time_str = current_time.strftime("%Y-%m-%d_%H-%M-%S")
    chat_log_filename = f"{current_time_str}.txt"
    chat_log_filepath = os.path.join('docs/chathistory', chat_log_filename)
    with open(chat_log_filepath, "w") as f:
        f.write(f"Chat started at {current_time_str}\n\n")
        f.write(f"User: {input_text}\n")
        f.write(f"Chatbot: {response.response}\n\n")
    
    return response.response
    
     
with open("docs/about/descript.txt", "r") as f:
    description = f.read()
        
iface = gr.Interface(fn=chatbot,
                     inputs=gr.inputs.Textbox(lines=5, label="Enter your question"),
                     outputs=gr.outputs.Textbox(label="Chatbot Response"),
                     title="AI Chatbot trained on J. Haynes mediation material, v0.1",
                     description=description)
                    
                                    
index = construct_index("docs")
iface.launch(share=True)