File size: 3,063 Bytes
b4f6e5d
 
 
 
 
 
 
 
 
 
 
 
3cd8399
b4f6e5d
 
 
 
 
 
 
 
 
 
 
 
 
3cd8399
 
 
 
 
b4f6e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cd8399
 
 
 
 
 
 
 
b4f6e5d
3cd8399
 
b4f6e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cd8399
b4f6e5d
 
3cd8399
b4f6e5d
 
 
 
 
3cd8399
b4f6e5d
 
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
import os
import gradio as gr
from langchain.chat_models import ChatOpenAI
from langchain import LLMMathChain
from langchain.utilities import GoogleSerperAPIWrapper, WikipediaAPIWrapper
from langchain.agents import initialize_agent, Tool, AgentType


def initialize():
    # Define models
    llm = ChatOpenAI(temperature=0) 

    search = GoogleSerperAPIWrapper()
    wiki = WikipediaAPIWrapper(top_k_results = 1)
    llm_math_chain = LLMMathChain(llm=llm)
    tools = [
        Tool(
            name="Calculator",
            func=llm_math_chain.run,
            description="useful for when you need to answer questions about math"
        ),
        Tool( 
            name = "wikipedia",
            func=wiki.run,
            description="useful for when you need to answer questions about historical entity. the input to this should be a single search term."
        ),
        Tool(
            name = "Current Search",
            func=search.run,
            description="useful for when you need to answer questions about current events or the current state of the world, also useful if there is no wikipedia result. the input to this should be a single search term."
        )
    ]

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

    chatbot_engine = initialize_agent(
        tools,
        llm, 
        agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, 
        verbose=True, 
        memory=memory)
    
    return chatbot_engine


def set_key(api_key):
    if not api_key:
        return "Key can't be empty!", None
    
    os.environ["OPENAI_API_KEY"] = api_key

    chatbot_engine = initialize()
    return "Key received", chatbot_engine


def chat(chat_history, chatbot_engine, message=""):
    # Empty msg
    if not message.strip():
        return chat_history, chat_history, ""
    
    # Execute message
    try:
        result = chatbot_engine.run(message.strip())
    except ValueError:
        result = "I can't handle this request, please try something else."

    chat_history.append((message, result))
    return chat_history, chat_history, ""


with gr.Blocks() as demo:
    # Declearing states    
    chat_history = gr.State([])
    chatbot_engine = gr.State()

    with gr.Row():
        openai_api_key_textbox = gr.Textbox(
            placeholder="Paste your OpenAI API key (sk-...)",
            show_label=False,
            lines=1,
            type="password",
        )

        api_key_set = gr.Button("Set key")

        api_key_set.click(
            fn=set_key,
            inputs=[openai_api_key_textbox],
            outputs=[api_key_set, chatbot_engine],
        )

    gr.Markdown("""<h1><center>Chat with your online-connected bot!</center></h1>""")
    chatbot = gr.Chatbot()
    message = gr.Textbox()
    submit = gr.Button("SEND")
    submit.click(chat, inputs=[chat_history, chatbot_engine, message], outputs=[chatbot, chat_history, message])


if __name__ == "__main__":
    demo.launch(debug = True)