Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
| 2 |
+
from langchain_community.chat_models.huggingface import ChatHuggingFace
|
| 3 |
+
from composio_langchain import ComposioToolset, App
|
| 4 |
+
from langchain import hub
|
| 5 |
+
from langchain.agents import AgentExecutor, load_tools
|
| 6 |
+
from langchain.agents.format_scratchpad import format_log_to_str
|
| 7 |
+
from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
|
| 8 |
+
from langchain.tools.render import render_text_description
|
| 9 |
+
from langchain_community.utilities import SerpAPIWrapper
|
| 10 |
+
import os
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
!composio-cli add github
|
| 14 |
+
!composio-cli add gmail
|
| 15 |
+
!composio-cli add notion
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
os.environ["SERPAPI_API_KEY"] = os.getenv("SERPAPI_API_KEY")
|
| 19 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 20 |
+
def setup_llm(repo_id):
|
| 21 |
+
return HuggingFaceEndpoint(repo_id=repo_id)
|
| 22 |
+
|
| 23 |
+
def setup_chat_model(llm):
|
| 24 |
+
return ChatHuggingFace(llm=llm)
|
| 25 |
+
|
| 26 |
+
def setup_tools(llm):
|
| 27 |
+
return load_tools(["serpapi", "llm-math", "stackexchange"], llm=llm)
|
| 28 |
+
|
| 29 |
+
def setup_prompt(tools):
|
| 30 |
+
prompt = hub.pull("hwchase17/react-json")
|
| 31 |
+
prompt = prompt.partial(tools=render_text_description(tools),
|
| 32 |
+
tool_names=", ".join([t.name for t in tools]))
|
| 33 |
+
return prompt
|
| 34 |
+
|
| 35 |
+
def setup_agent(chat_model_with_stop, tools, prompt):
|
| 36 |
+
return (
|
| 37 |
+
{
|
| 38 |
+
"input": lambda x: x["input"],
|
| 39 |
+
"agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]),
|
| 40 |
+
}
|
| 41 |
+
| prompt
|
| 42 |
+
| chat_model_with_stop
|
| 43 |
+
| ReActJsonSingleInputOutputParser()
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
def execute_agent(agent, tools, input_text):
|
| 47 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)
|
| 48 |
+
agent_executor.return_intermediate_steps = True
|
| 49 |
+
return agent_executor.invoke({"input": input_text})
|
| 50 |
+
|
| 51 |
+
llm = setup_llm(repo_id="HuggingFaceH4/zephyr-7b-beta")
|
| 52 |
+
chat_model = setup_chat_model(llm)
|
| 53 |
+
tools = ComposioToolset(apps=[App.GITHUB, App.GMAIL])
|
| 54 |
+
tools.extend(setup_tools(llm))
|
| 55 |
+
prompt = setup_prompt(tools)
|
| 56 |
+
chat_model_with_stop = chat_model.bind(stop=["\nInvalidStop"])
|
| 57 |
+
agent = setup_agent(chat_model_with_stop, tools, prompt)
|
| 58 |
+
|
| 59 |
+
def response(input,history=[]):
|
| 60 |
+
res = execute_agent(agent, tools, input)
|
| 61 |
+
if(res["intermediate_steps"]==[]):
|
| 62 |
+
return res["output"]
|
| 63 |
+
else:
|
| 64 |
+
return res["intermediate_steps"][0]+"\n"+res["output"]
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
gr.ChatInterface(response).launch(share=True, debug=True)
|