Limiting Agent Scope

In this optional challenge, you will modify the agent prompt to make it more specific to the task.

In a previous lesson, you completed the code to create a chain that rephrased a user input into a standalone question and passed it on to the agent to act on the question.

Currently, the agent will respond to any question, regardless of how obscure or even obscene it is!

The agent currently uses a pre-written prompt from LangChain Hub. You can view this prompt in LangChain Hub.

The prompt is relatively basic. It consists of an array of messages consisting of a role definition, human input, and placeholders.

The prompt
SYSTEM: You are a helpful assistant

PLACEHOLDER: chat_history

HUMAN: {input}

PLACEHOLDER: agent_scratchpad

Modifying the Prompt

The createOpenAIFunctionsAgent() function expects the prompt to be an instance of ChatPromptTemplate, which has a .fromMessages() method.

Open agent.ts

You can use this function to provide instructions to the LLM as a series of messages (e.g. system, human, AI) and variable placeholders.

For example, the following prompt instructs the LLM to refuse to answer questions that don’t relate to movies and respond in pirate talk.

Modified Prompt
import { MessagesPlaceholder } from "@langchain/core/prompts";

const prompt = ChatPromptTemplate.fromMessages<{
  'chat_history': string, 
  agent_scratchpad: string, 
  rephrasedQuestion: string
}>([
  ['system', `
  You are Ebert, a movie recommendation chatbot.
  Your goal is to provide movie lovers with excellent recommendations
  backed by data from Neo4j, the world's leading graph database.

  Respond to any questions that don't relate to movies, actors or directors
  with a joke about parrots, before asking them to ask another question
  related to the movie industry.
  `,
  ],
  [ 'human', '{rephrasedQuestion}' ],
  new MessagesPlaceholder({variableName: 'chat_history', optional: true}),
  new MessagesPlaceholder('agent_scratchpad'),
]);

The prompt is a series of messages: . A 'system' message that defines the role of the agent and gives instructions. . A 'human' message that contains the rephrased user input. . Placeholders for the chat history and agent scratchpad.

Testing

The unit test will verify whether the agent has refused to answer a question that is not related to the movie industry.

Experiment

You can extend the prompt to instruct the agent to act in specific ways. Experiment with different prompts to see how the agent responds to various types of questions.

When you are ready, click Continue.

Summary

In this challenge, you made the agent more specific by experimenting with the prompt.

You now know how to build Generative AI applications with Neo4j and Langchain!

We’d love to see what you build. Let us know the Neo4j Discord server or our Community Forum.