How To Do AI Prompt Templating

In this video, I'll show you how to use Jinja2 for AI prompt templates. While Jinja2 is commonly used for generating HTML pages, it's also incredibly effective for tools designed to interact with Large Language Models (LLMs).
🔥 GitHub Repository: git.arjan.codes/2024/tuesday_...
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis.
💻 ArjanCodes Blog: www.arjancodes.com/blog
Try Learntail for FREE ➡️ www.learntail.com/
🎓 Courses:
The Software Designer Mindset: www.arjancodes.com/mindset
The Software Architect Mindset: www.arjancodes.com/architect
Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
The 30-Day Design Challenge: www.arjancodes.com/30ddc
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
Social channels:
💬 Discord: discord.arjan.codes
🐦Twitter: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
📱Instagram: / arjancodes
♪ Tiktok: / arjancodes
👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Dale Hagglund
- Kit Hygh
- Alexander Milden
- Bean
🎥 Video edited by Mark Bacskai: / bacskaimark
🔖 Chapters:
0:00 Intro
1:12 OpenAI helper function
2:57 Jinja2 template
4:31 Main file
6:36 Outro
#arjancodes #softwaredesign #python
DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Пікірлер: 34

  • @ArjanCodes
    @ArjanCodes2 ай бұрын

    👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis.

  • @ramimashalfontenla1312
    @ramimashalfontenla13122 ай бұрын

    My vote for a jinja2 video!

  • @Mjhapp
    @Mjhapp2 ай бұрын

    I’d give Langchain another look. Once your prompts get more complex, you swap LLMs frequently or start rolling in RAG, loops, etc etc, simple API calls and jinja templates will become unmanageable. Even without all of that, Langsmith makes it worthwhile.

  • @hermandekoning9065
    @hermandekoning90652 ай бұрын

    I'd be interested in more about your setup and Jinja.

  • @NoPlanXD
    @NoPlanXD2 ай бұрын

    Funny, was just facing the same questions and was playing around. As some others here suggested, I stuck with f-strings for now, as it removes a fairly big dependency and when developing I like to see the prompt as I am playing around with it. But in Production this can be a huge cleanup in the code!

  • @muriloozol812
    @muriloozol8122 ай бұрын

    I've started using Jinja for my prompts mainly because I found myself converting lists into strings with a line break just to be well-formatted. Another problem it solved was that my IDE always complained about lines over 79 characters, which is pretty common for prompts. I know it is possible to break the strings into multiple lines, but I think it is too much work when the prompt starts getting bigger. F-strings don't work well if you have to put some examples of JSON into the prompt, a common use if you are using the LLM to extract data from an unstructured text.

  • @adrianojordao4634
    @adrianojordao46342 ай бұрын

    In the 60s some professors raised a chimp in a human family enviroment. Hoping the chimp become more human. The oposite happen the child becames more chimp. Here you exemplify this perfectly, in the hope of get more context information from the machine, you satart being less human and more machine like. Obviusly you are the inteligent part. Love your channel.

  • @loic1665
    @loic16652 ай бұрын

    In my company we use jinga2 for code generation, it's also very nice for that

  • @tacozmacleo
    @tacozmacleo2 ай бұрын

    For those that are going to use Jinja, please note that it breaks easily, if anything changes. So make a FULL requirements.txt file, AND note the python version you have tested it with. I cannot count how many times I come back to a Jinja script a month later, and it does not work because something has been updated...

  • @TangoFoxtrotWhiskey

    @TangoFoxtrotWhiskey

    2 ай бұрын

    And there was that time when they broke Flask, and the response was, "don't trust semver, lol."

  • @EWILD99
    @EWILD992 ай бұрын

    so its f strings?

  • @feldinho

    @feldinho

    2 ай бұрын

    Imagine a whole file as a big fstring. That's Jinja. :)

  • @EWILD99

    @EWILD99

    2 ай бұрын

    @@feldinho i mean i have prompts in my job i use for agents that are 1k tokens long and i literally just use an f string lol

  • @petermleigh

    @petermleigh

    2 ай бұрын

    Arjan's use case is probably best with f-strings. Jinja is really good for embedding conditional statements and loops into your template, like creating HTML tables from a dictionary.

  • @maleldil1

    @maleldil1

    2 ай бұрын

    @@petermleigh not f-strings because you want to store a template in the file. Rather, you'd use str.format.

  • @TheEvertw

    @TheEvertw

    2 ай бұрын

    Templates separate the String from the code used to render it. They make it easier to imagine how a rendered document is going look, than having dozens of f-strings sprinkled throughout one or more code files. If you just want a single prompt, an f-string is fine. If you want a document generated from a data set, use Jinja2 (or another templating engine, like Mako or Django).

  • @emperorsixpacks
    @emperorsixpacks2 ай бұрын

    I have been using txt files, and they have worked perfectly for me

  • @TheEvertw
    @TheEvertw2 ай бұрын

    Jinja2 is great when you can re-use your special formatting over several templates. It is a bit restrictive but that makes writing correct templates easier. I use templates a lot for code generation, where the templates have almost no overlap, meaning there is very little re-use of special formatting filters. In that case, Mako is more powerful. But Mako is harder to debug.

  • @plaskut
    @plaskut2 ай бұрын

    marvin, pydantic, instructor, langchain and now I'm going to try jinj2 in that mix

  • @CharleswoodSpudzyofficial
    @CharleswoodSpudzyofficial2 ай бұрын

    I have a similar setup to this but I use functions with f strings. It's not entirely easy to read but I like that each prompt it a function that I can put behind a factory. I might pivot to jinja though for some things. Langchain however is a complicated mess and I hate it

  • @user-os4lj3pi4q
    @user-os4lj3pi4q2 ай бұрын

    It just hurts that the image showcase a shinobi (aka ninja) instead of a shrine (jinja). The words are not even related at all...

  • @MicheleHjorleifsson
    @MicheleHjorleifsson2 ай бұрын

    curious, why not use yield and streaming response to give a better user experience ?

  • @DaveParr
    @DaveParr2 ай бұрын

    0:34 please do a langchain roast video. I've also been using it for my stuff and getting both frustrated and confused the MORE familiar I become with it, which feels backwards.

  • @dankprole7884
    @dankprole78844 күн бұрын

    f-strings work fine for me but i don't have conditional logic (yet!)

  • @maleldil1
    @maleldil12 ай бұрын

    How is this different from storing a format string as a file, then reading it and using prompt.format(...)? It seems overkill to use a whole templating engine for something so simple.

  • @ArjanCodes

    @ArjanCodes

    2 ай бұрын

    Having the option to use conditional statements and other more advanced template features is really helpful, and that's not possible with f-strings. I'm using this in our Learntail platform quite a bit. I didn't have time to go over those things as this was supposed to be a short video, but if you're interested, I can do a longer video about it.

  • @emperorsixpacks

    @emperorsixpacks

    2 ай бұрын

    Would love an in-depth video 😊

  • @andrewmenshicov2696
    @andrewmenshicov26962 ай бұрын

    Not to sound mean but it doesn't really convince me to use Jinja now. the same thing can be achieved using something like "{hello}".format(hello="sup") and no matter where you keep the string template. What'd work better is showing off some smart features Jinja does (of which i am not aware tho).

  • @sorvex9

    @sorvex9

    2 ай бұрын

    Did you even pay attention? If you use all the features you can store the jinja templates seperate from your Python code, with complex Logic like conditionals and loops - which is the whole point of using them

  • @MrChronoJon

    @MrChronoJon

    2 ай бұрын

    If you would change similar products to an array of strings that could be empty, you can handle the simple logic around that directly in the template. Otherwise you would have to build up the format string (to handle the empty case) and join the product strings beforehand. Also, you can put parts of the template into different files and recompose in order to avoid duplication of template components and possibly having slightly different versions of the same thing in multiple places. If it becomes slightly more complicated then you will probably end up writing your own template engine or you could just use jinja.

  • @andru5054
    @andru50542 ай бұрын

    first!