File size: 4,253 Bytes
9921a5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bf0ea4
9921a5e
 
 
 
 
 
 
 
 
 
 
 
 
6bf0ea4
9921a5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67eb999
9921a5e
 
 
d1ea9cf
9921a5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# gradio imports
import gradio as gr
import os
import time

# Imports
import os

import openai
from langchain.chains import ConversationalRetrievalChain

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.document_loaders import TextLoader

from langchain.memory import ConversationBufferMemory
from langchain.chat_models import ChatOpenAI

css="""
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
"""

title = """
<div style="text-align: center;max-width: 700px;">
    <h1>Chat about Dialogues • Games • AI • AI Regulation</h1>
    <p style="text-align: left;">Chat is built from:<br />
    This is a Dialogue (https://www.jonnyjohnson.com/this-is-a-dialogue)<br />
    Game-Making articles (https://dialogues-ai.github.io/papers/docs/ai_regulation/gamemaking)<br />
    As well as 25 blog posts contributed to BMC <br />
</div>
"""


prompt_hints = """
<div style="text-align: center;max-width: 700px;">
    <p style="text-align: left;">Some things you can ask:<br />
    Should I be worried about AIs?<br />
    How do we improve the games between AIs and humans?<br />
    What is a dialogue? <br />
    Do you agree that everything is language? <br />
</div>
"""

# from index import PERSIST_DIRECTORY, CalendarIndex
PERSIST_DIRECTORY = "chromadb"
# Create embeddings

# # create memory object
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

def loading_pdf():
    return "Loading..."

def loading_database(open_ai_key):
    if open_ai_key is not None:
        if os.path.exists(PERSIST_DIRECTORY):
            embeddings = OpenAIEmbeddings(openai_api_key=open_ai_key)
            docs_retriever = Chroma(persist_directory=PERSIST_DIRECTORY, embedding_function=embeddings)

            global qa_chain
            qa_chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.0, openai_api_key=open_ai_key),  
                            retriever=docs_retriever.as_retriever(), 
                            memory=memory, 
                            return_source_documents=False
                            )
            return "Ready"
    else:
        return "You forgot OpenAI API key"

def add_text(history, text):
    history = history + [(text, None)]
    return history, ""


def bot(history):
    response = infer(history[-1][0], history)
    history[-1][1] = ""
    for character in response:     
        history[-1][1] += character
        time.sleep(0.05)
        yield history
    

def infer(question, history):
    res = []
    for human, ai in history[:-1]:
        pair = (human, ai)
        res.append(pair)
    
    chat_history = res
    query = question
    result = qa_chain({"question": query, "chat_history": chat_history})
    return result["answer"]

def update_message(question_component, chat_prompts):
    question_component.value = chat_prompts.get_name()
    return None

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.HTML(title)
        with gr.Column():
            with gr.Row():
                openai_key = gr.Textbox(label="OpenAI API key", type="password")
                submit_api_key = gr.Button("Submit")
            with gr.Row():
                langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
        
        chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
        question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
        submit_btn = gr.Button("Send Message")
        gr.HTML(prompt_hints)
        
    submit_api_key.click(loading_database, inputs=[openai_key], outputs=[langchain_status], queue=False)
    # demo.load(loading_database, None, langchain_status)
    question.submit(add_text, [chatbot, question], [chatbot, question]).then(
        bot, chatbot, chatbot
    )
    submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
        bot, chatbot, chatbot)

demo.queue(concurrency_count=2, max_size=20).launch()