Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @huggingface/ollama-utils #1111

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ngxson
Copy link
Member

@ngxson ngxson commented Jan 17, 2025

With the ollama compatibility layer already been up and running on HF hub for a while now, we want to open-source part of the integration for (1) provide more transparency, and (2) for encouraging the community to contribute.

Therefore, we decided to publish @huggingface/ollama-utils, a package containing tools that power this integration.

For now, the only tool that we provide is chat-template.ts, a tool that takes a parsed GGUF config from @huggingface/gguf as input, then returns the ollama Go template.

chat-template.ts

This module expose one single convertGGUFTemplateToOllama function. It works by trying (by order) these mechanisms:

  1. Try to find a matched template already exist on ollama hub. To make this possible, we regularly dump a list of public templates from ollama.com using scripts/generate-automap.ts
  2. If no exact match is find, we still check against the list of official templates, but now only match a list of special tokens. These tokens are extracted using regex RE_SPECIAL_TOKEN
  3. If we still can't find a match, we search inside CUSTOM_TEMPLATE_MAPPING, a list of hand-picked template mapping
  4. If all above still failed, we try parsing jinja template and try converting it to Go template. This works almost all of the time, except if the template is not supported by @huggingface/jinja. See convertJinjaToGoTemplate

## Chat template converter

```ts
const
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: complete this @ngxson

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel up to it you can use tshy instead, like in @hugginface/tasks.

It has more accurate exports / better compatibility with all that includes the package (and as long as you don't have different code in browser/node it should be easy to configure)

@coyotte508
Copy link
Member

btw the github action to publish should be added (with optionnally a check to check the @huggingface/jinja is up to date, like @huggingface/hub does with @huggingface/tasks)

Copy link
Member

@julien-c julien-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat!

Copy link
Member

@Vaibhavs10 Vaibhavs10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know enough typescript to review, but two questions/comments from myside:

  1. maybe in a follow-up PR we should add a scraper and something to update the mapping?
  2. via comments/ README/ docs we should clarify how users can add their own exception as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants