ha.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. from langchain.chat_models import ChatOpenAI
  3. from langchain.chains.conversation.memory import ConversationBufferWindowMemory
  4. from langchain.agents import initialize_agent
  5. from typing import List, Any
  6. class ChatHA(object):
  7. def __init__(self, tools: List[Any] = None):
  8. if not tools:
  9. tools = list()
  10. self.llm = ChatOpenAI(
  11. temperature=0,
  12. model_name='gpt-3.5-turbo',
  13. max_tokens=500,
  14. )
  15. self.tools = tools
  16. self.llm.openai_api_key = os.environ.get('OPENAI_API_KEY', '')
  17. self.memory = ConversationBufferWindowMemory(
  18. memory_key='chat_history',
  19. k=10,
  20. return_messages=True
  21. )
  22. self.sys_msg = """Assistant is a large language model trained by OpenAI.
  23. Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
  24. Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
  25. Overall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
  26. """
  27. self.agent = None
  28. def get_agent(self):
  29. if not self.agent:
  30. self.agent = initialize_agent(
  31. agent='chat-conversational-react-description',
  32. system_message=self.sys_msg,
  33. tools=self.tools,
  34. llm=self.llm,
  35. verbose=True,
  36. max_iterations=3,
  37. early_stopping_method='generate',
  38. memory=self.memory
  39. )
  40. return self.agent
  41. def run(self, input):
  42. return self.get_agent().run({"input": input})