Persisting Responses

To complete this challenge, you must write the functions to save and retrieve conversation history.

In modules/agent/history.ts, you must:

  1. Modify the saveHistory() to save the history to the database

  2. Modify the getHistory() to retrieve the correct information from the database

Open history.ts in an Online IDE →

Connecting to Neo4j

The Neo4jGraph object has a .query() method that you can use to send both read and write workloads to Neo4j.

The function expects two parameters: the Cypher statement (a string), and an object containing parameters in key/value format. To reference the value from a parameter in a Cypher statement, prefix the key with a $.

The functions accept a generic that defines the shape of the results returned by the statement.

You can specify whether the Cypher statement executes in a read or write transaction by specifying "READ" or "WRITE" as the third parameter.

To learn how to integrate Neo4j into a TypeScript project, check out the Building Neo4j Applications with TypeScript course.

View graph.ts
typescript
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/graph.ts[]

Saving History

To save the history, modify the saveHistory() function.

typescript
saveHistory() Signature
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/modules/agent/history.ts[tag=save]

Call the initGraph() function to get the singleton Neo4jGraph instance, then call the .query() method using the following Cypher statement as the first parameter.

The statement should run in a write transaction.

cypher
Save Conversation History
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/cypher/save-response.cypher[]

Your code should resemble the following:

typescript
Save History
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/history.ts[tag=savetx]

Finally, use the id key from the first object in the res array to return the newly created response’s UUID.

typescript
Return the Response ID
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/history.ts[tag=savereturn]
View Solution
typescript
The implemented saveHistory() Function
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/history.ts[tag=save]

Getting History

To retrieve the history saved in the previous function, you must modify the getHistory() function.

typescript
getHistory() Signature
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/modules/agent/history.ts[tag=get]

Replace the // TODO comment with a call to the read() helper function imported from graph.ts.

Use the following Cypher statement as the first parameter to the read() function and an object containing the sessionId passed to the function as an argument.

cypher
Get Conversation History
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/cypher/get-history.cypher[]

Your code should resemble the following:

typescript
Return the messages
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/history.ts[tag=gettx]

Finally, you can return the res variable.

typescript
Return the messages
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/history.ts[tag=getreturn]
View Solution
typescript
The Implemented getHistory() Function
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/solutions/modules/agent/history.ts[tag=get]

Testing your changes

If you have followed the instructions, you should be able to run the following unit test to verify the response using the npm run test command.

sh
Running the Test
npm run test history.test.ts
View Unit Test
typescript
history.test.ts
Unresolved directive in ../../../../includes/test.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-python/main/src/modules/agent/history.test.ts[]

Verifying the Test

If every test in the test suite has passed, two (:Session) nodes and four (:Response) nodes will be created in your database.

Click the Check Database button below to verify the tests have succeeded.

Hint

You can compare your code with the solution in src/solutions/modules/agent/history.ts and double-check that the conditions have been met in the test suite.

Solution

You can compare your code with the solution in src/solutions/modules/agent/history.ts and double-check that the conditions have been met in the test suite.

You can also run the following Cypher statement to double-check that the information is being correctly added to your database.

cypher
MATCH (s:Session)-[r:HAS_MESSAGE]->(m)
WHERE s.id in ['test-1', 'test-2', 'test-3']
RETURN *

Once you have verified your code and re-ran the tests, click Try again…​* to complete the challenge.

Summary

In this lesson, you wrote the code to save and retrieve conversation history in a Neo4j database.

In the next lesson, you will construct a chain that will take this history to rephrase the user’s input into a standalone question.

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?