How do I get AI to reliably return and display a formatted response?

I’m working on a feature where a Postgres Explain/Analyze is submitted to an LLM for analysis and returns the result. Typically when I directly do this in Chat GPT I get pretty formatted responses with headings and linespacing. I want something like that in Nextworld, but right now I can’t consistently get it to happen and am often getting a very long line of text like this:

The provided EXPLAIN ANALYZE output for a PostgreSQL query reveals several key points for potential optimization. Firstly, the query performs a sequential scan (Seq Scan) on the 'logicblocks' table, indicating it scanned through every row in the table to find matching records. This operation is relatively expensive, costing significant time, especially as the table grows in size. The filter applied during this scan suggests that the query is filtering by 'nwtenantstripe' and 'nwnateid'. The fact that a sequential scan is used instead of an index scan suggests there might not be appropriate indexes on these columns. Adding indexes on 'nwtenantstripe' and 'nwnateid' could significantly improve the performance by allowing PostgreSQL to quickly locate the relevant rows without scanning the entire table. Secondly, the query performs a sort operation, which is also costly, especially with a large number of rows. The sort is necessary to enforce the order specified in the query before applying the LIMIT clause. The sort operation uses a top-N heapsort method, which is efficient for returning a small number of rows from a larger set. However, if the sorting columns are frequently used in queries, considering an index that includes these columns could also improve the sort operation's efficiency. Lastly, the actual execution time of 66.041 ms and the usage of 86kB of memory for sorting indicate that while the query is not excessively slow for a small dataset, performance issues could arise as data grows. To summarize, the primary recommendations for improving this query's performance are: 1. Add indexes on the 'nwtenantstripe' and 'nwnateid' columns to speed up row filtering. 2. Consider creating a multi-column index that includes the sorting columns if these sorts are common in your queries. These changes should reduce the need for full table scans and decrease the overall execution time of similar queries.

My current System Prompt is You are an expert in Postgres responding to someone who has only an entry level knowledge of databases. and my User Prompt is Tell me how I could improve the query associated with this Postgres EXPLAIN ANALYZE result: ${ExplainAnalyzeResult}

Can anyone tell me what I should do to get this consistently displayed in something like a Nextworld Text Area or Text Editor with, at a minimum, headings and line spacing? Maybe not as verbose as what I get directly from the Chat GPT UI below but more like that:

Hi Ian! You should be able to get this to return a more reliable response. Here are a couple of ideas:

  1. You can describe to the LLM how to structure the input. Our text editors store HTML, so asking for headings in the correct format should get you what you need.
    1. You can describe the preferred structure of the response in the output field’s description, or in the system prompt (or both)
  2. Request structured output
    1. I am assuming right now your prompt definition returns a single output that is the response text? Instead, you could ask for each component of the response in a separate output field and concatenate them back together yourself in a logic block. (e.g. Heading, ImprovedQuery, Explanation, Summary all as separate outputs).

I would recommend starting by describing in the prompt definition what format you want the response text to be in and if you are still struggling with inconsistent results trying option 2.

1 Like

Thanks @sally.stern! I updated the System Prompt to You are an expert in Postgres responding to someone who has only an entry level knowledge of databases. You will respond in an HTML format that includes small headings and basic formatting. Do not use color in the response. and am getting much better results using a Text Editor field to display the response.

2 Likes