Practical Prompting - ChatGPT & API Best Practices


7 min read

Practical Prompting - ChatGPT & API Best Practices

ChatGPT has proven to be a valuable tool in the tech realm. Through extensive work with it, I've gathered insights that can be helpful for both newcomers and those more familiar with the platform. While my insights primarily center on API interactions, they're equally useful to chatbot web application users.

Foundational ChatGPT Prompting tips

Interacting with models like ChatGPT requires a balance of technical knowledge and intuitive understanding. While there's much to uncover, adhering to some foundational practices can enhance the experience:

  • Starting simple is always a good strategy. When crafting a prompt, begin with the basics. As you understand the model's behavior, you can add more layers or details to your prompts. Think of it as building a structure, brick by brick.

  • Be specific and clear. If you're ambiguous or vague, the model might not grasp the essence of what you're after. Pinpoint exactly what you're hoping to achieve with your prompt. This ensures that the model's response aligns closely with your expectations.

  • Focus on positive instructions. Instead of instructing the model on what not to do, guide it on what it should do. It's like guiding someone by telling them the path to take, rather than all the paths they shouldn't.

By keeping these practices in mind, you're setting the stage for effective interactions with ChatGPT.

Crafting an Effective ChatGPT Prompt

The key to a successful ChatGPT interaction lies in a well-structured prompt. Although there is a maximum input length for models such as gpt.3-5 and gpt4, the goal remains to feed the model with as much relevant information as possible.

A methodical approach involves breaking the prompt into distinct sections. Segmenting promtps facilitates clarity and ensures all essential components are addressed without overloading the model.

Consider this example for a prompt structure:

  1. AI Role Description (~150 tokens): Here, outline the role or perspective you want the model to take. This can range from a neutral AI assistant to a historical figure.
    Example: "Assume the role of a technical support assistant with expertise in software troubleshooting."

  2. Rules and Preferences (~250 tokens): This section is where you can provide specific guidelines or biases for the model's responses, and put constraints.
    Example: "Avoid using technical jargon without providing a brief explanation. Prioritize user-friendly solutions over complex technical fixes. If suggesting any changes, ensure data safety and provide clear step-by-step instructions."

  3. Response Format (~100 tokens): Indicate how you'd like the answer to be structured - be it text with specific formatting or a JSON-like response.
    Example: "Provide a concise introduction, followed by a bullet-point list of steps for the solution. Conclude with a brief summary."

  4. Conversation history [optional, ~750 tokens]: Especially useful in back-and-forth interactions, this section ensures the model retains context (additional knowledge).
    Example: "User: My application keeps freezing every time I try to save my work. It started happening after the last update. I've already tried restarting my computer."

  5. Task / Trigger prompt: A concise statement of the specific task you want the model to perform.
    Example: "How can the user resolve the application freezing issue, especially when trying to save work?"

Handling Imperfect Inputs Gracefully

It is important to always account for failures and tasks that cannot be completed (due to the lack of information or bad user input). Bias your model with Rules to ignore unrelated requests, communicate errors, or unsolvable tasks. This will help with real-world use cases and inputs that are far from ideal and will often lead to unexpected results.

For example: “Ignore any requests that are not related to the Software Development” or “Respond with error for requests outside your role responsibility”

Harnessing the Power of Trigger Prompts

A "trigger prompt" is something like a final nudge that sets the AI into action. I usually place trigger prompt at the end of my prompts. The goal of the trigger prompt is to reiterate and emphasize the task you want the model to undertake.

Trigger prompt is a reminder for the LLM after your big and beefy prompt, that ensures the model remains focused on the task at hand.

While the main prompt provides a broader context, the trigger prompt sharpens the focus. Think of it as the difference between being told, "Today's theme is marine life" (the main prompt) and "Now, tell me about the Blue Whale" (the trigger prompt). The latter gives a precise directive command.

I also like to add a “Start now:” phrase to instruct the model to start output immediately and skip comments, for example, before JSON structure.

Designing Structured Responses with GPT

When you interact with OpenAI's models, you might want specific formatting for the output. This could be a markdown formatting, JSON, or something else. Here's how to guide the model to give you these outputs.

1. Building a Robust Prompt

To get a specific type of answer, you can describe the format you want for the model. By doing this, you're guiding the model to shape its response in a way that matches your needs. You can ask the model to provide a response in a list format, a table, or even something more structured like JSON or markdown.

Remember, structured formats like JSON can sometimes have issues. It's good to be aware and ready to handle any mistakes or inconsistencies in the output (More about this later in Fixing JSON Response)

2. Incorporating GPT Functions for Precise Outputs

GPT models allow you to provide a list of function choices that can be called. When you interact with these models, you can define certain functions. Based on your query, the model can then generate a JSON object designed to call those functions.

This means you provide a function that will accept structured responses from ChatGPT and ChatGPT will take care of formatting it. This approach is beneficial when you want shorter answers. It ensures that the model's response is structured and correct, making it easier to work with.

Important to note: the model won't call the function for you but will prepare the JSON so you can do it in your code.

By being clear with your instructions and knowing how to guide the model, you can make the most of the OpenAI's capabilities.

Navigating JSON Quirks with ChatGPT

When you work with the GPT-like models, you will notice that JSON responses are not perfect. We have no guarantees for a proper response format, so we need to keep that in mind when we work with LLMs and consume it with API.

Let's address how we can handle these issues on our side.

Identifying and Rectifying Common JSON Errors

  1. Missing Symbols:
  • Issue: The absence of {}, [], or other vital symbols can render the JSON invalid.

  • Example: {"name": "John".

  • Fix: Implement a post-processing step where you check the balance of brackets and braces. If they are unbalanced, try to correct them by adding the missing symbols at the probable locations.

  1. Quotes Trouble:
  • Issue: Field names or values might lack the necessary quotes.

  • Example: {name: "John"}.

  • Fix: Use a parser to identify fields or values missing their quotes. Add the missing double quotes around these entities to make the JSON valid.

  1. Unexpected Text:
  • Issue: Extra text might appear before or after the main JSON structure.

  • Example: Here's your data: {"name": "John"}.

  • Fix: Strip the response to only include text between the outermost {} or []. This can remove any unwanted prefixes or suffixes.

How to improve the quality of the GPT response:

  • Prompt Refinement: Enhance your prompt to make it clearer to the LLM model that you expect a clean JSON response. For instance, emphasize the format: "Please provide the answer in a valid JSON format." or “Make sure the response is readable with Python json.loads”

  • Post-processing Scripts: Develop scripts that automatically correct common errors in the model's output. For instance, a script can look for unbalanced braces or missing quotes and attempt to fix them.

  • Validation and Repair: Use JSON validation libraries available in most programming languages. After validating, if errors are detected, apply automated repair strategies if possible.

  • Model Tuning: If feasible, consider fine-tuning the model on tasks that involve generating JSON responses. This can make the model more adept at producing valid JSON.

  • Fallback Mechanism: In cases where the JSON can't be repaired, have a mechanism to handle such scenarios. This might involve requesting the model again or providing the user with a default or error response.

Funny enough, you can use GPT to fix invalid JSON responses. Not very practical, as there are no guarantees and additional costs, but always an option. By being proactive and having these strategies in place, you can better manage and handle imperfect JSON responses from the LLM model.

To sum it up

Navigating the realm of ChatGPT isn't just about understanding the algorithms. It's about the blend of technical know-how with the art of crafting prompts that communicate effectively. As we've explored, a well-structured prompt can be the difference between a generic response and a precise, actionable insight. From handling imperfect inputs to designing structured outputs, there's a myriad of strategies to enhance our interactions with this powerful tool.

To my fellow tech enthusiasts, what has been your most memorable experience with ChatGPT? Let's continue the conversation and learn from each other's experiences.