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 11 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

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)

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't get around the error with this line:

src/chat-template.ts:1:43 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@huggingface/jinja")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts' or create a local package.json file with `{ "type": "module" }`.

1 import { Template as JinjaTemplate } from "@huggingface/jinja";
                                            ~~~~~~~~~~~~~~~~~~~~

Do you have any ideas @coyotte508 ?

Copy link
Member

@coyotte508 coyotte508 Jan 28, 2025

Choose a reason for hiding this comment

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

Seems like @huggingface/jinja maybe needs to switch to tshy first to all corner cases (early/late node version, CJS importing MJS, MJS importing CJS, ...) are exported correctly cc @xenova

Copy link
Contributor

Choose a reason for hiding this comment

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

Strange, the exports should operate correctly
image

Maybe I need to restructure it like transformers.js does it here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do any other hf.js libs use tshy?

@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

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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.

yay! conceptually lgtm!

## How can I add a custom template?

Most templates will be converted automatically. You can debug the output template using:
- This space to retrieve the converted template: https://huggingface.co/spaces/ngxson/debug_ollama_manifest
Copy link
Member

Choose a reason for hiding this comment

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

maybe we can do both in the same space?

Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't figure out how to embed my script into gradio space, but will have a look later

(The template debugging space is static btw)

@ngxson ngxson requested a review from Vaibhavs10 January 30, 2025 13:04
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.

6 participants