123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import os
- import re
- from typing import Callable, List, Union
- from dotenv import dotenv_values, load_dotenv
- from utils.prompt import Prompt
- from langchain import SerpAPIWrapper
- from langchain.agents import Tool
- from langchain.vectorstores import FAISS
- from langchain.embeddings import OpenAIEmbeddings
- from langchain.schema import Document
- from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser
- from langchain.prompts import StringPromptTemplate
- from langchain import OpenAI, SerpAPIWrapper, LLMChain
- from langchain.schema import AgentAction, AgentFinish
- from langchain.memory import ConversationBufferMemory
- from langchain.agents import initialize_agent
- from langchain.chat_models import ChatOpenAI
- from langchain.chains.conversation.memory import ConversationBufferWindowMemory
- from tools.ha import HALightControlTool
- load_dotenv()
- cli_prompt = Prompt('ici-bot')
- search = SerpAPIWrapper()
- ALL_TOOLS =[
- Tool(
- name = "Search",
- func=search.run,
- description=""" Useful for when you need to answer questions about current events on the internet
- Use it only when explicitly asked to search on internet.
- """
- ),
- HALightControlTool(),
- ]
- template = """Answer the following questions as best you can. You have access to the following tools:
- {tools}
- Use the following format:
- Question: the input question you must answer
- Thought: you should always think about what to do
- Action: the action to take, should be one of [{tool_names}]
- Action Input: the input to the action
- Observation: the result of the action
- ... (this Thought/Action/Action Input/Observation can repeat N times)
- Thought: I now know the final answer
- Final Answer: the final answer to the original input question
- Begin!
- Question: {input}
- {agent_scratchpad}"""
- class CustomPromptTemplate(StringPromptTemplate):
- template: str
- tools_getter: Callable
-
- def format(self, **kwargs) -> str:
- intermediate_steps = kwargs.pop("intermediate_steps")
- thoughts = ""
- for action, observation in intermediate_steps:
- thoughts += action.log
- thoughts += f"\nObservation: {observation}\nThought: "
- kwargs["agent_scratchpad"] = thoughts
- tools = self.tools_getter(kwargs["input"])
- kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in tools])
- kwargs["tool_names"] = ", ".join([tool.name for tool in tools])
- return self.template.format(**kwargs)
- class CustomOutputParser(AgentOutputParser):
-
- def parse(self, llm_output: str) -> Union[AgentAction, AgentFinish]:
- if "Final Answer:" in llm_output:
- return AgentFinish(
- return_values={"output": llm_output.split("Final Answer:")[-1].strip()},
- log=llm_output,
- )
- regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
- match = re.search(regex, llm_output, re.DOTALL)
- if not match:
- raise ValueError(f"Could not parse LLM output: `{llm_output}`")
- action = match.group(1).strip()
- action_input = match.group(2)
-
- return AgentAction(
- tool=action,
- tool_input=action_input.strip(" ").strip('"'),
- log=llm_output
- )
-
- class ChatBot(object):
- def __init__(self,
- tools: List[Tool],
- model_name: str = "gpt-3.5-turbo",
- model_temperature: float = 0,
- model_max_tokens: int = 500,
- verbose: bool = False):
-
- self.sys_msg = """Assistant is a large language model trained by OpenAI.
- 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.
- 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.
- 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.
- """
- self.tools = tools
- self.verbose = verbose
- docs = [
- Document(
- page_content=t.description,
- metadata={"index": i}) for i, t in enumerate(tools)
- ]
-
- vector_store = FAISS.from_documents(docs, OpenAIEmbeddings())
- self.retriever = vector_store.as_retriever()
-
- self.prompt = CustomPromptTemplate(
- template=template,
- tools_getter=self._get_tools,
- input_variables=["input", "intermediate_steps"]
- )
-
- self.output_parser = CustomOutputParser()
- self.llm = ChatOpenAI(
- temperature=model_temperature,
- max_tokens=model_max_tokens,
- model_name=model_name,
- )
-
- self.llm_chain = LLMChain(llm=self.llm, prompt=self.prompt)
- def _get_tools(self, query:str):
- docs = self.retriever.get_relevant_documents(query)
- return [self.tools[d.metadata["index"]] for d in docs]
-
- def _get_agent(self, input: str):
- memory = ConversationBufferWindowMemory(
- memory_key='chat_history',
- k=10,
- return_messages=True
- )
- tools = self._get_tools(input)
- tool_names = [tool.name for tool in tools]
-
- # agent = initialize_agent(
- # agent='chat-conversational-react-description',
- # system_message=self.sys_msg,
- # tools=self.tools,
- # llm=self.llm,
- # verbose=True,
- # max_iterations=3,
- # early_stopping_method='generate',
- # memory=memory,
- # output_parser=self.output_parser,
- # stop=["\nObservation:"],
- # allowed_tools=tool_names,
- # llm_chain=self.llm_chain,
- # )
- agent = LLMSingleActionAgent(
- llm_chain=self.llm_chain,
- output_parser=self.output_parser,
- stop=["\nObservation:"],
- allowed_tools=tool_names,
- memory=memory,
- max_iterations=3,
-
- )
- return agent
-
- def run(self, input: str):
- agent_executor = AgentExecutor.from_agent_and_tools(
- agent=self._get_agent(input),
- tools=self.tools,
- verbose=self.verbose
- )
- agent_executor.run(input)
- if __name__ == '__main__':
- bot = ChatBot(ALL_TOOLS, verbose=True)
- while True:
- input = cli_prompt.get().strip()
- if input == 'exit':
- break
- try:
- bot.run(input)
- except Exception as e:
- print("\n\nSystem:>{}\n".format(e))
-
|