Defining the Chatbot scope

You now have a working agent capable of communicating with an underlying LLM. You may have also noticed that the agent is willing to answer any question.

To avoid end-users misusing the agent, the scope of the agent can be restricted in the form of a System Prompt.

To complete this challenge, you must provide specific instructions in the prompt argument.

Restricting Scope

In the previous challenge, you used the Langchain hub to download the hwchase17/react-chat prompt for ReAct Agents.

The hwchase17/react-chat Prompt in Langchain Hub

You can view the prompt in the LangChain hub and experiment with the instructions by clicking Open Artifact in Playground.

Existing Instructions

The prompt itself is quite verbose, but the important elements included are:

  • An instruction to use the list of tools to perform an action

  • A placeholder for listing descriptions of the available tools ({tools}) and their names ({tool_names})

  • Instructions on how to instruct the LLM on which tool to use

  • The previous chat history ({chat_history})

  • The user’s current input ({input})

  • A scratchpad of previous thoughts ({agent_scratchpad})

View the full prompt
text
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 tool 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.

TOOLS:
------

Assistant has access to the following tools:

{tools}

To use a tool, please use the following format:

```
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
```

When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:

```
Thought: Do I need to use a tool? No
Final Answer: [your response here]
```

Begin!

Previous conversation history:
{chat_history}

New input: {input}
{agent_scratchpad}

Updating the Instructions

You can leave everything from Tools: and below and update the opening instructions to reflect the role of the chatbot.

Your opening statements may look something like this:

text
New Instructions
You are a movie expert providing information about movies.
Be as helpful as possible and return as much information as possible.
Do not answer any questions using your pre-trained knowledge, only use the information provided in the context.

Do not answer any questions that do not relate to movies, actors or directors.

Remember to include:

  • You are a movie expert providing information about movies.

  • Do not answer any questions that do not relate to movies, actors or directors.

In some cases, letting the bot fall back on its trained knowledge to answer a question may be useful. You can include instructions for the LLM to refuse to answer if the information isn’t contained in the context.

  • If the answer isn’t included in the provided context, refuse to answer the question and ask for more information.

You can also throw in a whimsical instruction to make sure the prompt is working:

  • Respond to all questions in pirate speak.

View the updated prompt in full
text
You are a movie expert providing information about movies.
Be as helpful as possible and return as much information as possible.
Do not answer any questions using your pre-trained knowledge, only use the information provided in the context.

Do not answer any questions that do not relate to movies, actors or directors.


TOOLS:
------

You have access to the following tools:

{tools}

To use a tool, please use the following format:

```
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
```

When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:

```
Thought: Do I need to use a tool? No
Final Answer: [your response here]
```

Begin!

Previous conversation history:
{chat_history}

New input: {input}
{agent_scratchpad}

Create a PromptTemplate

Add the new prompt to the agent by creating a new PromptTemplate using the PromptTemplate.from_template() method.

python
Import PromptTemplate
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/solutions/scoped.py[tag=importprompt]

Then replace the agent_prompt variable with the updated instructions.

python
Define the Agent Prompt
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/solutions/scoped.py[tag=prompt]

The agent initialization code will stay the same.

python
Agent initialization
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/solutions/scoped.py[tag=agent]

Testing the changes

If you now ask the bot a question unrelated to movies, for example, Who is the CEO of Neo4j?, it will refuse to answer.

The Bot refusing to answer non-movie related question.

Once you have validated that the instructions are being followed, click the button below to mark the lesson as completed.

Summary

In this lesson, you defined the scope of the agent by providing a system message when creating the agent.

In the next module, you will start to define tools that the agent can select to help it answer movie-related questions.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?