# Lessons Learned While Embedding GenAI in a Product

In this blog, I want to collect the lessons I have learned whenever trying to somehow embed generative AI into any product. I am also planning to share your experience and references. Please comment here or send me a message on [LinkedIn](https://www.linkedin.com/in/mohammad-hadi-shadmehr/).

### Disclamer

When you are reading this article, some of this learning might seem obsulute or so obvious, but for beginners or past-us, it was something to be learned!

### Intro

There are many different ways to embed GenAI in a product or application. It can be as famous as a chat user interface or just a normal button, but behind the scenes, sending some data to a large model and reusing the output in complex ways, then as an outcome (response), just showing normal data in a table, chart, or just a simple change in the UI.

In most of the use cases, there are some common or similar practices. So from one use case to another, we can build on top of our learnings, and sharing learned lessons can help us to grow faster.

### Lessons

* July 12th, 2024, lessons from an internal project using [SAP AI Core](https://pages.community.sap.com/topics/ai-core-artificial-intelligence), thanks to [Mathis](https://www.linkedin.com/in/mathisboerner/) and [Sherri](https://www.linkedin.com/in/sherri-hadian/) for their mentorship and support.
    
    * **Use the output model** for enforcing output structure (e.g., JSON) and add a description to each field.
        
        * Using [Instructor: Structured LLM Outputs](https://github.com/jxnl/instructor)
            
        * More info [in this Medium blog post](https://medium.com/@nicolasmunozfidalgo/transforming-natural-language-into-structured-data-with-openai-and-langchain-e9c295be67a7)
            
        * **Note:** Based on [this](https://www.boundaryml.com/blog/type-definition-prompting-baml) it seems [BAML](https://github.com/boundaryml/baml) is a better way to go
            
    * **Minimize the requested output** as much as possible to get the response faster and cheaper.
        
    * **Set the scene** with roles and use system, user, and assistance roles when it makes sense.
        
        * system role's prompt is more restricted than user and is usually used to set the scene.
            
    * **Minimalistic prompt**: try to define prompt as minimal as possible.
        
        * Don't confuse the model with unnecessary additional info.
            
    * Use a **structured prompt**. Markdown, XML, or even JSON!
        
    * Don't give **much freedom to the user** if they don't know the underlying code; the functionality can be easily broken, and when it 50% doesn't work, the user gets frustrated!
        
        * No setting high expectations for the user
            
    * A **large output** usually has more hallucinations, so we can't rely on the whole output size.
        

### Similar Blogs or Other Learning References

* [OpenAI Prompt Engineering Best Practices](https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-the-openai-api)
    
* [Prompt Engineering Best Practices from Digital Ocean](https://www.digitalocean.com/resources/article/prompt-engineering-best-practices)
    

### Reference

* Cover image generated by [Canva](https://www.canva.com/ai-image-generator/)
