dragonSwing/LangChain-ChatGPT-plugins
1
1import os 2import gradio as gr3 4from langchain.chat_models import ChatOpenAI5from langchain.agents import load_tools, initialize_agent6from langchain.agents import AgentType7from langchain.tools import AIPluginTool8 9def run(prompt, plugin_json, openai_api_key):10 os.environ["OPENAI_API_KEY"] = openai_api_key11 tool = AIPluginTool.from_plugin_url(plugin_json)12 llm = ChatOpenAI(temperature=0, max_tokens=1000)13 tools = load_tools(["requests_all"])14 tools += [tool]15 agent_chain = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=False, max_tokens_limit=4097)16 return agent_chain.run(prompt)17 18title="""19<div style="text-align:center;">20 <h1>LangChain + ChatGPT Plugins playground</h1>21 <p>22 This is a demo for the <a href="https://python.langchain.com/en/latest/modules/agents/tools/examples/chatgpt_plugins.html" target="_blank">ChatGPT Plugins LangChain</a> usecase<br />23 Be aware that it currently only works with plugins that do not require auth.<br />24 Find more plugins <a href="https://www.getit.ai/gpt-plugins" target="_blank">here</a>25 </p>26</div>27"""28 29with gr.Blocks(css="style.css") as demo:30 with gr.Column(elem_id="col-container"):31 gr.HTML(title)32 prompt = gr.Textbox(label="Prompt", value="what t shirts are available in klarna?")33 plugin = gr.Textbox(label="Plugin json", info="You need the .json plugin manifest file of the plugin you want to use. Be aware that it currently only works with plugins that do not require auth.", value="https://www.klarna.com/.well-known/ai-plugin.json")34 openai_api_key = gr.Textbox(label="OpenAI API Key", info="*required", type="password")35 run_btn = gr.Button("Run")36 response = gr.Textbox(label="Response")37 run_btn.click(fn=run, 38 inputs=[prompt, plugin, openai_api_key],39 outputs=[response]40 )41 42demo.queue().launch()