Best Practices for LLM Powered Solutions

Dan Murphy on background

Dan Murphy

An abstract grid of blocks and cubes, some solid and others glowing, emphasizing the connections between vertices and the flow of information

In this series exploring Large Language Models, we've covered significant ground. Our first post, Demystifying LLMs, covered the inner workings of LLMs, exploring their architecture and the technologies that power them. We then shifted gears in our second post, An Effective Mental Framework for LLMs, introducing a mental framework that likens LLMs to early-career professionals, providing strategies for effective collaboration. Now, in this final installment, we'll delve into the practical aspects of working and building with LLMs, offering advanced techniques and best practices to help you harness the full potential of these powerful AI tools.

Maximizing LLM Performance

Prompting

Prompting is the cornerstone of LLM interaction, serving as the initial input that guides the model's output. While seemingly straightforward, crafting effective prompts (a practice known as prompt engineering) is a nuanced skill that significantly impacts LLM performance. Key strategies include:

  • Clarity and Specificity Provide clear, detailed instructions to minimize assumptions and ambiguity.
     
    • Worse: "Summarize the text."
       
    • Better: "Provide a 3-5 sentence summary of the key points in the given text, focusing on the main arguments and conclusions. Use clear and concise language suitable for a general audience."
       
  • Task Decomposition Break complex tasks into smaller, manageable prompts to reduce error rates.
     
    • Worse: "Analyze the financial performance of Company X over the past 5 years and make recommendations for improvement."
       
    • Better:
      • "List the key financial metrics for Company X for each of the past 5 years."
      • "Identify trends in these financial metrics over the 5-year period."
      • "Compare Company X's performance to industry benchmarks."
      • "Based on this analysis, suggest 3-5 areas for potential improvement."
         
  • Provide Examples Utilize "few-shot learning" (also known as in-context learning) by including examples that align with desired outputs. Be cautious, however, to use sufficiently representative examples to prevent overfitting.
     
    • Worse: "Generate creative names for a new line of eco-friendly cleaning products."
       
    • Better: "Generate creative names for a new line of eco-friendly cleaning products. Here are some examples of the style we're looking for:
      • 'GreenSweep' for a broom made from recycled materials
      • 'AquaPure' for a non-toxic all-purpose cleaner
      • 'EcoShine' for a biodegradable glass cleaner"
         
  • Elicit Reasoning Implement chain-of-thought (CoT) prompting to encourage step-by-step reasoning, thereby reducing errors and hallucinations where LLMs make things up. CoT prompting involves instructing the LLM to break down its problem-solving process into discrete steps and it can significantly improve the model's performance on complex tasks, especially those involving logic or multi-step reasoning.
     
    • Worse: "Solve this word problem: If a train travels 120 miles in 2 hours, how fast is it going?"
       
    • Better: "Solve this word problem step by step: If a train travels 120 miles in 2 hours, how fast is it going? Please show your reasoning for each step and explain each step clearly:
      • Identify the given information
      • Determine what we need to calculate
      • Choose the appropriate formula
      • Plug in the values and solve
      • Check if the answer makes sense in context"

These prompting techniques, along with others outlined in resources like OpenAI's prompt engineering guide, form the foundation of effective LLM utilization.

Retrieval-Augmented Generation (RAG)

RAG enhances LLM capabilities by incorporating external knowledge retrieval into the generation process. Examples of LLM powered applications that rely on RAG include the AI search products Perplexity and Phind. Additionally, Custom GPTs allow you to leverage RAG out of the box when you upload documents. This approach is particularly valuable when the model requires information beyond its training data. RAG offers several advantages:
 

  • Enhanced Accuracy: By providing relevant contextual information, RAG enables LLMs to generate more precise and knowledgeable responses, often with proper citations and fewer hallucinations and fabrications.
     
  • Knowledge Flexibility: RAG allows for fine-grained control over the information used by the model, facilitating the inclusion of up-to-date or domain-specific knowledge.
     
  • Cost-Efficiency: Compared to re-training or fine-tuning, RAG offers a more economical method of expanding an LLM's knowledge base.

However, implementing RAG effectively requires careful consideration:

  • Quality of Retrieved Information: The accuracy and relevance of RAG outputs depend heavily on the quality of the retrieved documents. Developing robust retrieval mechanisms is crucial.
     
  • Context Management: Including additional context increases the input size, which can impact processing time and costs, as most LLMs have context length limitations and often charge based on input size.
     

Fine-tuning

Fine-tuning involves additional training of an existing LLM on specific datasets to enhance its performance for particular tasks. This technique builds upon few-shot learning by allowing for the incorporation of many more examples. GitHub Copilot and Code Llama are examples of LLMs fine-tuned to specifically generate code. Another example is the part of the DALL·E 2 image generator that takes the user’s prompt and turns it into a format that the image generator can then use to create images that match what the user described. Most LLMs support fine-tuning, including ChatGPT-4o. Common applications of fine-tuning include:
 

  • Stylistic Adaptation: Shaping LLM responses to conform to specific tones, formats, or other qualitative aspects. An example might be fine tuning an LLM to consistently respond with sarcasm.
     
  • Domain Specialization: Improving the model's performance in niche fields or on specialized tasks.

The primary benefits of fine-tuning include:

  • Reduced Prompt Complexity: Once fine-tuned, models often require less detailed prompts, potentially reducing context length and associated costs.
     
  • Improved Inference Speed: Specialized models can generate relevant outputs more quickly.

However, fine-tuning comes with challenges:

  • Resource Intensity: The fine tuning process takes computational resources and time, and multiple iterations may be necessary to get the desired results. Therefore, fine tuning can be costly.
     
  • Overfitting Risk: Fine-tuned models may adopt patterns specific to the fine tuning training data that don't generalize well to new inputs.
     
  • Data Management: Careful curation of fine-tuning datasets is necessary to avoid overfitting or introducing biases.

Due to these considerations, fine-tuning is typically reserved for scenarios where prompting and RAG prove insufficient or when highly specialized behavior is required.

Building with AI

The advent of Large Language Models (LLMs) has revolutionized how we instruct computers. Unlike traditional programming, which requires explicit, step-by-step instructions, LLMs allow for more natural, language-based interactions. This capability opens up new avenues for innovative applications that can interpret and act on human-like instructions.

However, building effectively with LLMs introduces new challenges that must be addressed. The keys to success are:

  1. Leveraging LLM strengths
  2. Minimizing their weaknesses
  3. Accounting for the ambiguities inherent in natural language
     

Workflows

When incorporating LLMs into your systems, applications, or workflows, consider the following best practices:

  • Selective Implementation: Incorporate LLMs only at points where traditional computer programs fall short. As discussed earlier, traditional programs are generally cheaper and faster when they can accomplish the task.
     
  • Hybrid Systems: Combine LLMs with rule-based systems to leverage the strengths of both paradigms.
     
  • Continuous Evaluation: Regularly assess and refine LLM-powered components to ensure consistent performance and alignment with goals.
     

Advanced Features and Techniques

When you need to incorporate LLMs, consider taking advantage of these features and techniques:

  • Structured Output: Instruct LLMs to generate responses that adhere to predefined formats, such as JSON or XML. This allows for easy parsing, integration, and use in downstream applications or systems.
     
  • Function Calling (or Tool Use): Enhance LLM versatility by allowing them to interact with external functions, APIs, or specialized systems. Provide the necessary information about available functions and usage instructions to enable the LLM to leverage specific external tools or retrieve information from external sources.
     
  • Reproducible Outputs: While LLM outputs typically include some level of randomness, for some models it's possible to introduce more consistency into your systems by using certain controls to receive (mostly) consistent, deterministic outputs.
     
  • Caching: Implement caching strategies, including prompt caching, to speed up applications and reduce costs by storing frequently used data.

     

Benchmarking and Evaluation

Quality assurance is critical for any system or workflow that incorporates LLMs to ensure the system is operating as intended. Benchmarking is the process of evaluating and comparing the performance of LLMs against standardized tasks or datasets to assess the quality of their results. Systematic benchmarking and evaluations allow for:

  • Optimization: Measure and maximize LLM performance by identifying and addressing performance issues.
  • Iteration: Assess if changes and updates have quantitatively improved results.
  • Quality Assessment: Objectively evaluate quality and determine if standards are being met.

Benchmarking typically involves:

  1. Selecting or creating standardized tasks or datasets
  2. Establishing evaluation criteria
  3. Assessing performance against these criteria

While a human or traditional program may run the evaluation depending on the test design, it's even possible to use LLMs for self-evaluation. However, we generally don't recommend relying solely on LLMs to self-evaluate. Instead, consider prompting an LLM to self-assess and use that response as part of a broader evaluation strategy to improve performance.

Example: Legacy Data Migration

Let's consider an example where legacy data is being migrated to a new system with more structured fields. Imagine the legacy system had a simple "Full Name" text field, while the new system has distinct "First Name" and "Last Name" text fields. The legacy "Full Name" field was inconsistently used, sometimes including titles, compound names, middle initials, and suffixes. An extreme example might be "Dr. Billy Joe M. Van Gough Jr."

Writing a traditional program to handle this would be challenging and likely fail in certain edge cases. However, this type of language task is where an LLM excels. A hypothetical workflow might look like this:

  1. Send the legacy full name field to an LLM during the migration process.
     
  2. Provide a prompt like:
The following is a full name field. Parse this into a first name and last name.
The full name may include titles, compound first and/or last names, middle names or initials, and suffixes. 
Use your extensive knowledge of names to make your best guess about the person's first and last name. 
Share your confidence in your answer as either "Very Confident" or "Not Very Confident" and 
provide an explanation if you're "Not Very Confident". 
Always respond in JSON with the format:

{
    "firstName": "",
    "lastName": "",
    "confidence": "",
    "reason": ""
}

 

  1. Use the LLM response to determine the next steps:
    • If the LLM is "Very Confident" and the system has previously passed all benchmarks and evaluations, migrate the JSON firstName and lastName values into the new database structure.
    • If the LLM is "Not Very Confident," send the legacy data along with the LLM's response for human review and entry.

Monitoring Future Developments

As with any other dependency your application is built upon, we advise continuously monitoring for new developments, updates, releases, and features that may allow your application to deliver greater value. Likewise, benchmark evaluations and QA should be performed regularly to ensure the system continuously meets quality expectations and doesn't regress.

Summary

By following these guidelines and best practices, you can effectively harness the power of LLMs in your workflows while mitigating their limitations. Remember to treat LLMs like early-career professionals - intelligent yet in need of guidance - to maximize their potential in your applications.

Conclusion

This exploration of Large Language Models has covered their foundational architecture, provided a framework for conceptualizing their capabilities and limitations, and outlined strategies for their effective use and integration.

LLMs represent a paradigm shift in how we can apply computational power to complex, ambiguous problems traditionally reserved for human intelligence. However, their effective use demands a nuanced approach, balancing their remarkable capabilities with an understanding of their limitations. By approaching LLMs as we would early-career professionals—acknowledging their broad knowledge base while providing necessary guidance and oversight—we can harness their potential while mitigating risks. Combining this careful management with the best practices we outlined for building with LLMs is the key to leveraging them most effectively.

The future of AI and LLMs holds immense promise, and we look forward to sharing more insights with you about the innovative ways we are applying these technologies.