How To Do AI Prompt Templating

Поделиться
HTML-код
  • Опубликовано: 13 июн 2024
  • 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!

Комментарии • 33

  • @ArjanCodes
    @ArjanCodes  Месяц назад

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

  • @ramimashalfontenla1312
    @ramimashalfontenla1312 Месяц назад +7

    My vote for a jinja2 video!

  • @Mjhapp
    @Mjhapp Месяц назад +8

    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
    @hermandekoning9065 Месяц назад +2

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

  • @adrianojordao4634
    @adrianojordao4634 Месяц назад

    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.

  • @NoPlanXD
    @NoPlanXD Месяц назад +2

    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!

  • @loic1665
    @loic1665 Месяц назад +1

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

  • @emperorsixpacks
    @emperorsixpacks Месяц назад +1

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

  • @muriloozol812
    @muriloozol812 Месяц назад +1

    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.

  • @EWILD99
    @EWILD99 Месяц назад +18

    so its f strings?

    • @feldinho
      @feldinho Месяц назад +1

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

    • @EWILD99
      @EWILD99 Месяц назад +6

      @@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 Месяц назад +12

      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 Месяц назад +1

      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).

  • @tacozmacleo
    @tacozmacleo Месяц назад +2

    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 Месяц назад

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

  • @plaskut
    @plaskut Месяц назад

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

  • @TheEvertw
    @TheEvertw Месяц назад +1

    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.

  • @CharleswoodSpudzyofficial
    @CharleswoodSpudzyofficial Месяц назад +1

    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

  • @MicheleHjorleifsson
    @MicheleHjorleifsson Месяц назад

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

  • @user-os4lj3pi4q
    @user-os4lj3pi4q Месяц назад +2

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

  • @maleldil1
    @maleldil1 Месяц назад +1

    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  Месяц назад +11

      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 Месяц назад +3

      Would love an in-depth video 😊

  • @DaveParr
    @DaveParr Месяц назад

    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.

  • @andrewmenshicov2696
    @andrewmenshicov2696 Месяц назад +6

    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 Месяц назад

      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
    @andru5054 Месяц назад +1

    first!