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
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/src/src/solutions/modules/agent/agent.ts[tag="scoped", indent=0]

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.

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?