Agent
Case Study: Agentic Chatbots for HR Applications
In this cookbook, we will demonstrate an implementation of an agentic chatbot that facilitates HR operations at a fictionalized version of Cohere. Through its tools, the chatbot is able to retrieve and act upon relevant information and fetch data from external data sources (such as a database or API).
Getting Started
Let's start by setting up our imports and tool definitions. We provide our agent with mock data on Cohere employees, along with these tools:
python_tool: allow the model to write and execute Python codeget_info_employee: fetch information regarding a (mock) Cohere employee from a databasesearch_cohere_policies: fetch relevant documents about Cohere's HR policiesget_employee_timezone: fetch current UTC offset for a locationinternet_search: browse the internet to find relevant information for the queryget_template: get the template for a specific kind of communication, such as offer to a candidate, refusal, etc.
To simplify the code below, we keep the definitions of these tools in the tools.py file. For this cookbook, you will also need two API keys: one from Cohere, and one from Tavily to allow the model to search the internet. You can create a .env file in this directory to store these.
True
Basic Prompts
We are now ready to run some basic queries. These initial queries do not depend on prior chat history, but require the usage of several tools.
In the first example, the Agent uses the "get_template" tool to load the relevant template, and fills it with the relevant information provided by the user.
> Entering new AgentExecutor chain... I will search for the number of days off Cohere employees have. {'tool_name': 'search_cohere_policies', 'parameters': {'query': 'number of days off'}} At Cohere, every employee has 30 days off per year.Relevant Documents: 0 Cited Documents: 0 Answer: Every Cohere employee has 30 days off per year. Grounded answer: Every Cohere employee has <co: 0>30 days off per year</co: 0>. > Finished chain.
In the second example, the Agent uses "search_cohere_policies" to perform a RAG task, and answer the user's question based on the retrieved information.
> Entering new AgentExecutor chain... I will search for the number of days off Aaron has used. {'tool_name': 'get_info_employee', 'parameters': {'employee': 'Aaron'}} {'name': 'Aaron', 'used_days_off': 12, 'location': 'London', 'role': 'ML eng'}Relevant Documents: 0 Cited Documents: 0 Answer: Aaron has used 12 days off. Grounded answer: Aaron has used <co: 0>12 days off</co: 0>. > Finished chain.
In the third example, the Agent uses the "get_info_employee" tool to retrieve information from a csv, and then generates the answers.
As we have seen, in the examples above the Agent receives the input query and selects the most appropriate tool ('get_template', 'search_cohere_policies' and 'get_info_employee') to generate the answer.
History-dependent Queries
We will now see how the Agent can engage in a conversations, answering queries based on the prior chat history. However, the usage pattern is exactly the same as before!
> Entering new AgentExecutor chain... I will use the 'get_info_employee' tool to find out how many days off Aaron has used, and then calculate how many days off he has left. {'tool_name': 'get_info_employee', 'parameters': {'employee': 'Aaron'}} {'name': 'Aaron', 'used_days_off': 12, 'location': 'London', 'role': 'ML eng'}Relevant Documents: 0 Cited Documents: 0 Answer: Aaron has 18 days off left. Grounded answer: Aaron has <co: 0>18 days off left.</co: 0> > Finished chain.
In order to understand q3, the Agent needs to remember who we are talking about - Aaron.
> Entering new AgentExecutor chain... I will search for the number of days off Harper has used, then subtract that from the total number of days off to calculate how many days off Harper has left. {'tool_name': 'get_info_employee', 'parameters': {'employee': 'Harper'}} {'name': 'Harper', 'used_days_off': 7, 'location': 'Singapore', 'role': 'ML eng'}Relevant Documents: 0 Cited Documents: 0 Answer: Harper has 23 days off left. Grounded answer: Harper has <co: 0>23 days off left</co: 0>. > Finished chain.
To answer q4, the Agent needs to remember what is the current topic, namely, the number of days off left for a specific employee.
> Entering new AgentExecutor chain... I will search for Harper's location using the 'get_info_employee' tool. {'tool_name': 'get_info_employee', 'parameters': {'employee': 'Harper'}} {'name': 'Harper', 'used_days_off': 7, 'location': 'Singapore', 'role': 'ML eng'}Relevant Documents: 0 Cited Documents: 0 Answer: Harper is based in Singapore. Grounded answer: Harper is based in <co: 0>Singapore</co: 0>. > Finished chain.
Again, the Agent here needs to solve the anaphora ("he") selecting the correct referent ("Harper").
> Entering new AgentExecutor chain... I will first find out where Aaron is based, then I will use the 'get_employee_timezone' tool to find the time difference between Harper and Aaron. {'tool_name': 'get_info_employee', 'parameters': {'employee': 'Aaron'}} {'name': 'Aaron', 'used_days_off': 12, 'location': 'London', 'role': 'ML eng'} Aaron is based in London. Now I will use the 'get_employee_timezone' tool to find the time difference between Harper and Aaron. {'tool_name': 'get_employee_timezone', 'parameters': {'city': 'Singapore'}} 8.0 {'tool_name': 'get_employee_timezone', 'parameters': {'city': 'London'}} 1.0Relevant Documents: 0,1,2 Cited Documents: 0,1,2 Answer: There is a 7-hour time difference between Harper and Aaron. Grounded answer: There is a <co: 1,2>7-hour</co: 1,2> time difference between Harper and Aaron. > Finished chain.
Here, the Agent has to perform multiple tool calls, to get the final answer.
> Entering new AgentExecutor chain... I will search for the locations of Harper and Aaron and then use an online distance calculator to find out how far apart they are. {'tool_name': 'get_info_employee', 'parameters': {'employee': 'Harper'}} {'name': 'Harper', 'used_days_off': 7, 'location': 'Singapore', 'role': 'ML eng'} {'tool_name': 'get_info_employee', 'parameters': {'employee': 'Aaron'}} {'name': 'Aaron', 'used_days_off': 12, 'location': 'London', 'role': 'ML eng'} Harper is based in Singapore and Aaron is based in London. Now I will use an online distance calculator to find out how far apart they are. {'tool_name': 'internet_search', 'parameters': {'query': 'distance between singapore and london'}} [{'url': 'https://www.rome2rio.com/s/Singapore/London', 'content': 'The distance between Singapore and London is 6828 miles. How long does it take to get from Singapore to London? It takes approximately 16h 24m to get from Singapore to London, including transfers.'}, {'url': 'https://www.travelmath.com/distance/from/Singapore/to/London,+United+Kingdom', 'content': 'The total straight line flight distance from Singapore to London, United Kingdom is 6,742 miles. This is equivalent to 10 850 kilometers or 5,858 nautical miles. Your trip begins in Singapore. It ends in London, United Kingdom. Your flight direction from Singapore to London, United Kingdom is Northwest (-38 degrees from North).'}, {'url': 'https://www.rome2rio.com/s/London/Singapore', 'content': 'The cheapest way to get from London to Singapore costs only $513, and the quickest way takes just 16 hours. Find the travel option that best suits you. Rome2Rio. ... The distance between London and Singapore is 10925 km. How long does it take to get from London to Singapore?'}, {'url': 'https://www.rome2rio.com/s/London/Singapore-Country', 'content': 'The distance between London and Singapore is 10915 km. How long does it take to get from London to Singapore? It takes approximately 15h 40m to get from London to Singapore, including transfers.'}, {'url': 'https://www.distance.to/London/Singapore', 'content': 'Distance: 6,738.95 mi (10,845.29 km) The shortest distance (air line) between London and Singapore is 6,738.95 mi (10,845.29 km). Driving route: -- (- ) The shortest route between London and Singapore is according to the route planner. The driving time is approx. . Half of the trip is reached in . Time difference: 7h'}]Relevant Documents: 0,1,2,3,4,5,6 Cited Documents: 0,1,2,3,4,5,6 Answer: Harper is based in Singapore and Aaron is based in London. The distance between Singapore and London is approximately 6,742 miles or 10,845 kilometres. Grounded answer: Harper is based in <co: 0>Singapore</co: 0> and Aaron is based in <co: 1>London</co: 1>. The distance between Singapore and London is approximately <co: 2,3>6,742 miles</co: 2,3> or <co: 4,5,6>10,845 kilometres</co: 4,5,6>. > Finished chain.
In the last example, the Agent has to use even more tools, to get the final answer.
Conclusions
In this notebook, we have seen how the Agent is able to select the right tools to answer users's question, leverage the information shared with the user during the conversations to answer follow-up question, and deal with complex queries which require using several tools.