File size: 2,338 Bytes
fe0c25c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905c20f
fe0c25c
 
905c20f
 
 
 
 
 
 
 
 
 
 
 
fe0c25c
 
 
 
 
 
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
import langchain as lc
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage
from langchain import PromptTemplate, LLMChain, HuggingFaceHub, OpenAI, FewShotPromptTemplate
from langchain.prompts.example_selector import LengthBasedExampleSelector
from langchain.chains import ConversationChain, MapReduceChain
from langchain.memory import ConversationBufferMemory, ConversationSummaryBufferMemory
from langchain.callbacks import get_openai_callback

import os

from langchain.chains import LLMMathChain, SQLDatabaseChain
from langchain.agents import Tool, load_tools, initialize_agent, AgentType
from langchain.agents.react.base import DocstoreExplorer

import gradio as gr 

def demo8():

    model = OpenAI(openai_api_key=os.environ['OPENAI_API_KEY'])
    tools = load_tools(['llm-math', 'terminal'], llm=model)

    prompt = PromptTemplate(template="{question}", input_variables=['question'])
    llm_chain = LLMChain(llm=model, prompt=prompt)
    llm_tool = Tool(name="Search", func=llm_chain.run, description="general QA")
    tools.append(llm_tool)

    memory = ConversationBufferMemory(memory_key="chat_history")
    conversation_agent = initialize_agent(tools=tools,
                                          llm=model,
                                       max_iterations=3,
                                       verbose=True,
                                       agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
                                       memory=memory)
    # resp = conversation_agent("what is (4.5*2.1)^2.2?")

    with gr.Blocks() as demo:
        state = gr.State([])

        chatbot = gr.Chatbot(elem_id="chatbot")
        with gr.Row():
            text = gr.Textbox(show_label=False, placeholder="enter your prompt")

        def answer(question, history=[]):
            history.append(question)
            resp = conversation_agent(question)
            print(f"resp: {resp}")
            history.append(resp['output'])
            dial = [(u, v) for u, v in zip(history[::2], history[1::2])]
            return {
                    chatbot: dial,
                    state: history
                }

        text.submit(answer, inputs=[text, state], outputs=[chatbot, state])
    
    demo.launch()

if __name__ == "__main__":
    demo8()