Hello everyone,
I am creating agents that can be used with any HuggingFace Opensource models instead of using OpenAI.
Here is my code snippet
pipe = pipeline(task="text-generation", model = "https://huggingface.co/meta-llama/Llama-2-7b-chat-hf", tokenizer="https://huggingface.co/meta-llama/Llama-2-7b-chat-hf")
llm = HuggingFacePipeline(pipeline=pipe, model_id="https://huggingface.co/meta-llama/Llama-2-7b-chat-hf")
# prompt
prompt = ChatPromptTemplate(
input_variables=['agent_scratchpad', 'input'],
messages=[
SystemMessagePromptTemplate(
prompt=PromptTemplate(
partial_variables={
'tools':(
'wikipedia - A wrapper around Wikipedia. Useful for general questions about people, places, companies, facts, historical events, or other subjects. '
'Input should be a search query.\n'
'Calculator - Useful for math-related questions.'
),
'tool_names': 'wikipedia, Calculator'},
input_variables=[],
template=(
'<s>[INST]<<SYS>>Answer the following questions as best you can. You have access to the following tools:\n\n'
'{tools}\n\n'
'The way you use the tools is by specifying a json blob.\n'
'Specifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).\n\n'
'The only values that should be in the "action" field are: {tool_names}\n\n'
'The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions.'
'Here is an example of a valid $JSON_BLOB:\n\n'
'```\n{{\n "action": $TOOL_NAME,\n "action_input": $INPUT\n}}\n```\n\n'
'ALWAYS use the following format:\n\n'
'Question: the input question you must answer\n'
'Thought: you should always think about what to do\n'
'Action:\n```\n$JSON_BLOB\n```\n'
'Observation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\n'
'Thought: I now know the final answer\n'
'Final Answer: the final answer to the original input question\n\n'
'Begin! Reminder to always use the exact characters `Final Answer` when responding.<</SYS>>'
)
)
),
HumanMessagePromptTemplate(
prompt=PromptTemplate(
input_variables=['agent_scratchpad', 'input'],
template='{input}\n\n{agent_scratchpad}[/INST]'
)
)
]
)
## Math and wikipedia tools
tools = load_tools(["llm-math", "wikipedia"], llm=hf_pipe)
# initialize agent
agent = initialize_agent(
tools,
hf_pipe,
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
handle_parsing_errors=True
)
agent.agent.llm_chain.prompt = prompt
# math agent test
result = agent("What is the 25% of 300?")
Although the correct calculation result is returned, I found that it doesn’t call the math tool to calculate it. How can I improve my code to call the proper tool?