{"id":17340994,"url":"https://github.com/synw/modprompt","last_synced_at":"2025-04-14T19:08:15.480Z","repository":{"id":193703559,"uuid":"689343357","full_name":"synw/modprompt","owner":"synw","description":"Prompt templates for language models","archived":false,"fork":false,"pushed_at":"2025-03-30T12:27:12.000Z","size":376,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T19:08:09.294Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://synw.github.io/modprompt/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/synw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-09T14:06:50.000Z","updated_at":"2025-03-30T12:27:16.000Z","dependencies_parsed_at":"2023-10-12T23:02:31.045Z","dependency_job_id":"e1ec914f-e86e-48a0-8ee3-d4110b1788d9","html_url":"https://github.com/synw/modprompt","commit_stats":null,"previous_names":["synw/modprompt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/synw%2Fmodprompt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/synw%2Fmodprompt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/synw%2Fmodprompt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/synw%2Fmodprompt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/synw","download_url":"https://codeload.github.com/synw/modprompt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248943456,"owners_count":21186958,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T15:47:21.565Z","updated_at":"2025-04-14T19:08:15.459Z","avatar_url":"https://github.com/synw.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Modprompt\n\n[![pub package](https://img.shields.io/npm/v/modprompt)](https://www.npmjs.com/package/modprompt)\n\nA collection of prompt templates for language models\n\n- Classic templates formats for different models\n- Easily modify and adapt templates on-the-fly\n- Few shots and conversation history support\n\n:books: [Api doc](https://synw.github.io/modprompt/)\n\n## Install\n\n```bash\nnpm install modprompt\n```\n\n## Usage\n\n### Available templates\n\nList the available templates:\n\n```js\nimport { templates } from \"modprompt\";\n\nconst templateNames = Object.keys(templates);\nconsole.log(templateNames);\n```\n\n### Load a template\n\nTo load a template:\n\n```js\nimport { templates, PromptTemplate } from \"modprompt\";\n\nconst tpl = new PromptTemplate(templates.alpaca)\n// or\nconst tpl = new PromptTemplate(\"alpaca\")\n```\n\n### Render a template\n\nTo render a template to string:\n\n```js\nconst templateText = tpl.render();\n```\n\nRendering for an Alpaca template:\n\n```\nBelow is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{prompt}\n\n### Response:\n```\n\n### Render a prompt\n\nSimilar to the `render` function above, but replaces `{prompt}` by the provided text:\n\n```js\nconst templateText = tpl.prompt(\"What is the capital of Kenya?\");\n```\n\n### System messages\n\nThe template have system messages support if the original template supports it.\n\nTo replace a system message:\n\n```js\ntpl.replaceSystem(\"You are a javascript specialist\");\n```\n\nRendering for an Alpaca template:\n\n```\nYou are a javascript specialist\n\n### Instruction:\n{prompt}\n\n### Response:\n```\n\nTo append to a system message:\n\n```js\ntpl.afterSystem(\"You are a javascript specialist\");\n```\n\nNote: some templates does have a system schema but no default system message. Some templates\ndon't even have a system block. The default `render` will show the system schema: exemple for the Vicuna system template:\n\n```\nSYSTEM: {system}\n\nUSER: {prompt}\n\n### ASSISTANT:\n```\n\nIn case of empty system message it is possible to skip it using the\n`skip_empty_system` option: outptut of `tpl.render(true)`:\n\n```\nUSER: {prompt}\n\n### ASSISTANT:\n```\n\n### Example shots\n\nThe templates have support for example shots. Add one shot:\n\n```js\ntpl.addShot(\n  \"fix this invalid json:\\n\\n```json\\n{'a':1,}\\n```\",\n  '\\n\\n```json\\n{\"a\":1}\\n```\\n',\n)\n```\n\nThe first param is the user question, and the second is the assistant response. It\nis assembled in a prompt template. Example with custom user and assistant messages:\n\n```js\n// modify system message\ntpl.afterSystem(\"You are a javascript specialist\");\n// modify assistant message\ntpl.afterAssistant(\" (answer in valid json)\")\n// modify the prompt message\ntpl.replacePrompt(\"fix this invalid json:\\n\\n```json\\n{prompt}\\n```\")\n// add a one shot example\ntpl.addShot(\n  \"{'a':1,}\",\n  '\\n\\n```json\\n{\"a\":1}\\n```\\n',\n)\n```\n\nRendering for an Alpaca template:\n\n```\nBelow is an instruction that describes a task. Write a response that appropriately completes the request. You are a javascript specialist\n\n### Instruction:\nfix this invalid json:\n\n'''json\n{'a':1,}\n'''\n\n### Response: (answer in valid json)\n\n'''json\n{\"a\":1}\n'''\n\n### Instruction:\nfix this invalid json:\n\n'''json\n{prompt}\n'''\n\n### Response: (answer in valid json)\n```\n\n### Chainable api\n\nThe calls can be chained. Example with the code above:\n\n```js\nconst tpl = new PromptTemplate(templates.llama)\n  .afterSystem(\"You are a javascript specialist\")\n  .afterAssistant(\" (answer in valid json)\")\n  .replacePrompt(\"fix this invalid json:\\n\\n```json\\n{prompt}\\n```\")\n  .addShot(\n    \"{'a':1,}\",\n    '\\n\\n```json\\n{\"a\":1}\\n```\\n',\n  );\n```\n\n## Types\n\nTemplate types:\n\n```ts\ninterface SpacingSlots {\n  system?: number;\n  user?: number;\n  assistant?: number;\n}\n\ninterface PromptBlock {\n  schema: string;\n  message?: string;\n}\n\ninterface TurnBlock {\n  user: string;\n  assistant: string;\n}\n\ninterface LmTemplate {\n  name: string;\n  user: string;\n  assistant: string;\n  system?: PromptBlock;\n  shots?: Array\u003cTurnBlock\u003e;\n  stop?: Array\u003cstring\u003e;\n  linebreaks?: SpacingSlots;\n}\n```\n\nExample raw template:\n\n```ts\nimport type { LmTemplate } from \"modprompt\";\n\nconst orca: LmTemplate = {\n  \"name\": \"Orca\",\n  \"system\": {\n    \"schema\": \"### System:\\n{system}\",\n    \"message\": \"You are an AI assistant that follows instruction extremely well. Help as much as you can.\",\n  },\n  \"user\": \"### User:\\n{prompt}\",\n  \"assistant\": \"### Response:\",\n  \"linebreaks\": {\n    \"system\": 2,\n    \"user\": 2\n  }\n}\n```\n\n:books: [Api doc](https://synw.github.io/modprompt/)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsynw%2Fmodprompt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsynw%2Fmodprompt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsynw%2Fmodprompt/lists"}