File size: 3,714 Bytes
fe0c25c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
731907d
 
 
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
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
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

def conversation_agent():
    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?")
    print(resp)

    # resp = conversation_agent("if Mary has four apples and Giorgio brings two and a half apple "
    #             "boxes (apple box contains eight apples), how many apples do we "
    #             "have?")
    # print(resp)

    resp = conversation_agent("what is the capital of Norway?")
    print(resp)

    resp = conversation_agent("what's the most famous landmark of this city")
    print(resp)

    resp = conversation_agent("free -h")
    print(resp)

    print("--------")
    print(conversation_agent.agent.llm_chain.prompt.template)

from langchain.agents.react.base import DocstoreExplorer
from langchain.docstore import Wikipedia

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?")
    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 dial, history


    with gr.Blocks() as demo:
        chatbot = gr.Chatbot(elem_id="chatbot")
        state = gr.State([])

        with gr.Row():
            text = gr.Textbox(show_label=False, placeholder="enter your prompt")
        text.submit(answer, inputs=[text, state], outputs=[chatbot, state])
    
    demo.launch()

if __name__ == "__main__":
    demo8()