-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathagent.py
51 lines (43 loc) · 1.65 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.chat_models import ChatCohere
from langchain.retrievers.tavily_search_api import TavilySearchAPIRetriever
from langchain.retrievers import CohereRagRetriever
from langchain_community.chat_models import ChatCohere
from langchain_core.documents import Document
from langchain.agents import create_structured_chat_agent
from langchain.pydantic_v1 import BaseModel, Field
from langchain.tools import BaseTool, StructuredTool, tool
from tools import *
import langchain
from dotenv import load_dotenv
import os
import dotenv
dotenv.load_dotenv()
langchain.verbose=True
COHERE_API_KEY = os.environ["COHERE_API_KEY"]
class chatagent():
def __init__(self):
return
def query(self, message: str, cfg, memory) -> str:
cohere = ChatCohere(model="command", temperature = 0, streaming=True, verbose=True)
tools = [buy_stock, sell_stock, mean_reversion, rag]
prompt = hub.pull("kenwu/react-json")
agent = create_structured_chat_agent(
cohere,
tools,
prompt
)
# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
memory=memory,
max_iterations=5,
handle_parsing_errors=True,
return_intermediate_steps=True
)
response = agent_executor.invoke({"input": message}, cfg, chat_history = [])
return response