Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions .github/workflows/ollama-template-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: Ollama template update
on:
push: # for debugging
workflow_dispatch:
#schedule:
# - cron: '0 7 * * 1' # every monday at 7am, so I'll review it after having a 🥐

permissions:
pull-requests: write # for creating PR
issues: write # for adding labels to the created PR
contents: write # for git push new branch

jobs:
create-pull-request:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Prepare
id: prepare
shell: bash
run: |
git config --global user.email "[email protected]"
git config --global user.name "Xuan-Son Nguyen"
git config --global --add safe.directory "$GITHUB_WORKSPACE"
npm install -g pnpm
CURRENT_DATE=$(date -u +"%Y-%m-%d")
echo "CURRENT_DATE=$CURRENT_DATE"
echo "CURRENT_DATE=$CURRENT_DATE" >> $GITHUB_ENV

- name: Install dependencies
shell: bash
run: |
cd packages/ollama-utils
pnpm install --frozen-lockfile

- name: Run update script
shell: bash
run: |
cd packages/ollama-utils
# pnpm run build:automap
echo test > ./test

- name: Check for changed files
id: changes
shell: bash
env:
CURRENT_DATE: ${{ steps.prepare.outputs.CURRENT_DATE }}
run: |
set -x
git status
modified_files="$(git status -s)"
echo "Modified files: ${modified_files}"
if [ -n "${modified_files}" ]; then
NEW_BRANCH="ollama-${CURRENT_DATE}"
echo "NEW_BRANCH=${NEW_BRANCH}"
echo "Changes detected, will create a new branch:"
echo "${modified_files}"
git add -A
git commit -m "ollama update ${CURRENT_DATE}"
git checkout -b "${NEW_BRANCH}"
git push origin "${NEW_BRANCH}"
echo "HAS_CHANGES=true" >> $GITHUB_OUTPUT
echo "NEW_BRANCH=${NEW_BRANCH}" >> $GITHUB_OUTPUT
else
echo "No files changed, skipping..."
echo "HAS_CHANGES=false" >> $GITHUB_OUTPUT
fi

- name: Create PR
if: steps.changes.outputs.HAS_CHANGES == 'true'
uses: actions/github-script@v6
env:
CURRENT_DATE: ${{ steps.prepare.outputs.CURRENT_DATE }}
NEW_BRANCH: ${{ steps.prepare.outputs.NEW_BRANCH }}
with:
script: |
const { repo, owner } = context.repo;
const currDate = process.env.CURRENT_DATE;
const newBranch = process.env.NEW_BRANCH;

const result = await github.rest.pulls.create({
title: '[ollama-utils] 🤖 Auto-update chat templates (' + currDate + ')',
owner,
repo,
head: newBranch,
base: 'main',
body: [
'This PR is auto-generated by',
'[generate-automap.ts](https://github.com/huggingface/huggingface.js/blob/main/packages/ollama-utils/scripts/generate-automap.ts).'
].join('\n')
});

console.log({ result });
// github.rest.issues.addLabels({
// owner,
// repo,
// issue_number: result.data.number,
// labels: ['feature', 'automated pr']
// });
86 changes: 54 additions & 32 deletions packages/ollama-utils/scripts/generate-automap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ const getSpecialTokens = (tmpl: string): string[] => {

nDoing = 0;
nAll = modelsWithTag.length;
const addedModels: string[] = [];
const skippedModelsDueToErr: string[] = [];

const workerGetTemplate = async () => {
while (true) {
const modelWithTag = modelsWithTag.shift();
Expand Down Expand Up @@ -137,44 +140,52 @@ const getSpecialTokens = (tmpl: string): string[] => {
try {
ggufData = await gguf(modelUrl);
} catch (e) {
console.log(" --> [X] FATAL: GGUF error", { model, tag, modelUrl });
throw e; // rethrow
console.log(` --> [X] Skipping ${modelWithTag} due to error while calling gguf()`, e);
skippedModelsDueToErr.push(modelWithTag);
continue;
}
const { metadata } = ggufData;
const ggufTmpl = metadata["tokenizer.chat_template"];
if (ggufTmpl) {
if (seenGGUFTemplate.has(ggufTmpl)) {
console.log(" --> Already seen this GGUF template, skip...");
try {
if (seenGGUFTemplate.has(ggufTmpl)) {
console.log(" --> Already seen this GGUF template, skip...");
continue;
}
seenGGUFTemplate.add(ggufTmpl);
console.log(" --> GGUF chat template OK");
const tmplBlob = manifest.layers.find((l) => l.mediaType.match(/\.template/));
if (!tmplBlob) continue;
const ollamaTmplUrl = getBlobUrl(tmplBlob.digest);
if (!ollamaTmplUrl) {
console.log(" --> [X] No ollama template");
continue;
}
const ollamaTmpl = await (await fetch(ollamaTmplUrl)).text();
console.log(" --> All OK");
const record: OutputItem = {
model: modelWithTag,
gguf: ggufTmpl,
ollama: {
template: ollamaTmpl,
tokens: getSpecialTokens(ggufTmpl),
},
};
// get params
const ollamaParamsBlob = manifest.layers.find((l) => l.mediaType.match(/\.params/));
const ollamaParamsUrl = ollamaParamsBlob ? getBlobUrl(ollamaParamsBlob.digest) : null;
if (ollamaParamsUrl) {
console.log(" --> Got params");
record.ollama.params = await (await fetch(ollamaParamsUrl)).json();
}
output.push(record);
addedModels.push(modelWithTag);
if (DEBUG) appendFileSync("ollama_tmp.jsonl", JSON.stringify(record) + "\n");
} catch (e) {
console.log(` --> [X] Skipping ${modelWithTag} due to error`, e);
skippedModelsDueToErr.push(modelWithTag);
continue;
}
seenGGUFTemplate.add(ggufTmpl);
console.log(" --> GGUF chat template OK");
const tmplBlob = manifest.layers.find((l) => l.mediaType.match(/\.template/));
if (!tmplBlob) continue;
const ollamaTmplUrl = getBlobUrl(tmplBlob.digest);
if (!ollamaTmplUrl) {
console.log(" --> [X] No ollama template");
continue;
}
const ollamaTmpl = await (await fetch(ollamaTmplUrl)).text();
console.log(" --> All OK");
const record: OutputItem = {
model: modelWithTag,
gguf: ggufTmpl,
ollama: {
template: ollamaTmpl,
tokens: getSpecialTokens(ggufTmpl),
},
};
// get params
const ollamaParamsBlob = manifest.layers.find((l) => l.mediaType.match(/\.params/));
const ollamaParamsUrl = ollamaParamsBlob ? getBlobUrl(ollamaParamsBlob.digest) : null;
if (ollamaParamsUrl) {
console.log(" --> Got params");
record.ollama.params = await (await fetch(ollamaParamsUrl)).json();
}
output.push(record);
if (DEBUG) appendFileSync("ollama_tmp.jsonl", JSON.stringify(record) + "\n");
} else {
console.log(" --> [X] No GGUF template");
continue;
Expand All @@ -190,7 +201,13 @@ const getSpecialTokens = (tmpl: string): string[] => {
.map(() => workerGetTemplate())
);

console.log("====================================");
console.log("DONE");
console.log("Added templates for:");
console.log(addedModels.join("\n"));
console.log("Skipped these models due to error:");
console.log(skippedModelsDueToErr.join("\n"));

output.sort((a, b) => a.model.localeCompare(b.model));

writeFileSync(
Expand All @@ -201,6 +218,11 @@ const getSpecialTokens = (tmpl: string): string[] => {

import { OllamaChatTemplateMapEntry } from "./types";

/**
* Skipped these models due to error:
${skippedModelsDueToErr.map((m) => ` * - ${m}`).join("\n")}
*/

export const OLLAMA_CHAT_TEMPLATE_MAPPING: OllamaChatTemplateMapEntry[] = ${JSON.stringify(output, null, "\t")};
`.trim()
);
Expand Down
Loading