-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmychatbot.py
49 lines (36 loc) · 1.49 KB
/
mychatbot.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
import os
from dotenv import load_dotenv, find_dotenv
from langchain.agents import ConversationalChatAgent
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.agents.agent import AgentExecutor
from langchain.chat_models import ChatOpenAI
class MyChatBot:
def __new__(cls, *args, **kwargs):
return super().__new__(cls)
def initializeBot(self):
if self.ready:
return
tools = []
_ = load_dotenv(find_dotenv()) #read local .env file
llm=ChatOpenAI(
openai_api_key=os.getenv('OPENAI_API_KEY'),
temperature=0
)
system_message = "Ten una conversacion en lenguaje español con un humano, respondiendo las preguntas tan bien como puedas"
memory = ConversationBufferWindowMemory(
memory_key="chat_history",
k=5,
return_messages=True
)
custom_agent = ConversationalChatAgent.from_llm_and_tools(llm=llm, tools=tools, system_message=system_message)
agent_executor = AgentExecutor.from_agent_and_tools(agent=custom_agent, tools=tools, memory=memory)
agent_executor.verbose = True
self.agent_executor = agent_executor
print(
agent_executor.agent.llm_chain.prompt
)
self.ready= True
def __init__(self):
self.ready = False
def newmessage(self,telefonoCliente,humanmessage) ->str:
return self.agent_executor.run(input=humanmessage)