Skip to content

feat(template): read jinja templates from gguf files - #4332

Merged
mudler merged 8 commits into
masterfrom
read_template
Dec 8, 2024
Merged

feat(template): read jinja templates from gguf files#4332
mudler merged 8 commits into
masterfrom
read_template

Conversation

@mudler

@mudler mudler commented Dec 6, 2024

Copy link
Copy Markdown
Owner

Description

This PR adds automatic detection and parsing of jinja templates in gguf files. If we fail to identify a variant and we do not have already a specific template, it injects the jinja templates which is part of the model metadata if one is found.

Alternatively, it is possible to enable jinja templates manually in the model config file, in the template config section with jinja_template: true.

Notes for Reviewers

  • This is extracted from feat: Realtime API support #3722 as it refactors message templating in a way that is more re-usable by other endpoints.
  • Refactors mainly how the template is processed, removing the business logic from the handler and separate it so can be re-used from other parts (e.g. realtime needs same templating)
  • Refactors some of the dependency passed down to the routes as it adds another component (Template Evaluator) aside the model loader
  • It might cross with feat: Centralized Request Processing middleware #3847 ( didn't checked closely ) but with other PRs too as it refactors some core parts, however, if we don't move this forward there will be too many changes that requires this adaptation or introduces dups

Signed commits

  • Yes, I signed my commits.

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Comment thread pkg/templates/cache.go

// can either be a file in the system or a string with the template
if utils.ExistsInPath(tc.templatesPath, modelTemplateFile) {
d, err := os.ReadFile(file)

Check failure

Code scanning / gosec

Potential file inclusion via variable

Potential file inclusion via variable
@mudler mudler self-assigned this Dec 6, 2024
@mudler
mudler marked this pull request as draft December 6, 2024 21:07
@netlify

netlify Bot commented Dec 6, 2024

Copy link
Copy Markdown

Deploy Preview for localai ready!

Name Link
🔨 Latest commit 56f6ab4
🔍 Latest deploy log https://app.netlify.com/sites/localai/deploys/67557dade0632500089601d4
😎 Deploy Preview https://deploy-preview-4332--localai.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mudler

mudler commented Dec 6, 2024

Copy link
Copy Markdown
Owner Author

WIP as need to still add mapping between the transformer tokenizer and the templates (see TODO note in code comments)

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler mudler added the enhancement New feature or request label Dec 7, 2024
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler
mudler marked this pull request as ready for review December 8, 2024 11:02
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler

mudler commented Dec 8, 2024

Copy link
Copy Markdown
Owner Author

basic support should work (tested with llama3 prompt), probably is not going to cover all cases as gonja has limitations, but, since this kicks-in when no other template was defined it is safe to merge without drawbacks.

@mudler
mudler merged commit cea5a0e into master Dec 8, 2024
@mudler
mudler deleted the read_template branch December 8, 2024 12:50
sozercan pushed a commit to sozercan/LocalAI that referenced this pull request Dec 8, 2024
* Read jinja templates as fallback

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Move templating out of model loader

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Test TemplateMessages

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Set role and content from transformers

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Tests: be more flexible

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* More jinja

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Small refactoring and adaptations

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

---------

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants