# Real-Time Text-to-Cypher Query Generation with Large Language Models for Graph Databases
## 5 Implementation
### Query Answering (QA) Pipeline  
The task of this pipeline is to answer the user question from the corresponding database. The first step in this process is to generate a suitable CQL statement to query the underlying Neo4j database. For this purpose, an additional customized prompt, Listing 4, was created, which will be used for the _Query LLM_. In addition to the user question, this contains the schema of the selected graph, which lists all nodes and relationships between them, as well as the properties of them both. This schema is dynamically retrieved from the graph database for prompt creation, using the connection details from the selected DB descriptor. Furthermore, example queries can also be added for few shot prompting, which the model can use as a guide for query generation. In addition to the temperature parameter of the _Query LLM_ the prompt also includes requests to strictly adhere to the provided schema. This is to prevent the model from creating new nodes/relationships or properties that do not exist in the graph and thus causing errors in the database during execution. It is also required not to explain the generated cipher statement to ensure that the query can be parsed for the database without problems. After the CQL statement has been generated by the _Query LLM_, it is first streamed back to the UI and presented to the user before being executed via the database client of the database descriptor. Depending on the result of the request, two different processing paths are now taken. If the query could be executed on the database without errors, the raw query result is first streamed back to the UI in JSON format. This allows the user to check it and recognize possible errors in the plain text response. The query result is then transferred to the _Chat LLM_. A customized prompt was also created for this similar to the chat from history prompt, which contains the raw JSON and user question. ```
'Task:GenerateCyberstatementtoqueryagraphdatabase. ```
1 Instructions: Useonlytheprovidedrelationshiptypesandpropertiesintheschema. Donotuseanyotherrelationshiptypesorpropertiesthatarenotprovided.Schema:{schema} Hint:Youcanunethefollowingqueriesasexamples: {self.few_shots} Note:Donotincludencyexplanationsorapologiesimyourresponses. DonotincludencytexceptthegeneratedCyberstatement. Thequestionis:{question}'' ```
Listing 4: Generate Query Prompt. The model is also asked to consider the result as authoritative and absolute and not to try to correct or explain it. This is to ensure that the generated full-text answers are concise, brief, and accurate to the initial question, and only contain information which can be found in the database. When the model has finished generating the answer text, it is streamed to the UI and presented to the user. This concludes the processing procedure. However, if the generated query has caused an error such as a syntax or semantic error during execution on the database, the processing path through the error correction module is taken. Due to the fact that the cipher language of Neo4j can be very error-prone, due to the extensive syntax, and a smooth query generation can therefore never be guaranteed, this module was created to automate the error correction in CQL queries as best as possible, and thus improve the user experience. The task of the module is to improve the initially generated query depending on the user question and the error message. In the event of an error, first an error correction prompt Listing 5 is created. Analogous to the generated query prompt, it contains the user query, the schema of the graph, and few shot examples. In addition, the incorrect CQL statement and the error that the Neo4j database has returned are also included. This is then sent to the _Query LLM_, which generates an improved query which is then executed a second time on the database. If the fixed CQL statement again causes an error, a new error correction prompt is created and the process starts again. As can be seen, the correction of the faulty query happens in a loop.