-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathagent.py
31 lines (26 loc) · 996 Bytes
/
agent.py
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
from typing import Literal
from langchain.agents import initialize_agent, load_tools, AgentType
from langchain.chains.base import Chain
from langchain.chat_models import ChatOpenAI
from langchain_experimental.plan_and_execute import (
load_chat_planner, load_agent_executor, PlanAndExecute
)
ReasoningStrategies = Literal["zero-shot-react", "plan-and-solve"]
def load_agent(
tool_names: list[str],
strategy: ReasoningStrategies = "zero-shot-react"
) -> Chain:
llm = ChatOpenAI(temperature=0, streaming=True)
tools = load_tools(
tool_names=tool_names,
llm=llm
)
if strategy == "plan-and-solve":
planner = load_chat_planner(llm)
executor = load_agent_executor(llm, tools, verbose=True)
return PlanAndExecute(planner=planner, executor=executor, verbose=True)
return initialize_agent(
tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
if __name__ == "__main__":
pass