Skip to content

Commit 3665250

Browse files
Initial Commit
0 parents  commit 3665250

59 files changed

Lines changed: 7180 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/changelog.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "Update Changelog"
2+
3+
on:
4+
release:
5+
types: [prereleased, released]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
update:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
with:
18+
ref: main
19+
20+
- name: Update Changelog
21+
uses: stefanzweifel/changelog-updater-action@v1
22+
with:
23+
latest-version: ${{ github.event.release.name }}
24+
release-notes: ${{ github.event.release.body }}
25+
26+
- name: Commit updated CHANGELOG
27+
uses: stefanzweifel/git-auto-commit-action@v5
28+
with:
29+
branch: main
30+
commit_message: Update CHANGELOG
31+
file_pattern: CHANGELOG.md

.github/workflows/test.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: tests
2+
3+
on: [ 'push', 'pull_request' ]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
9+
strategy:
10+
fail-fast: true
11+
matrix:
12+
php: [ 8.1, 8.2, 8.3 ]
13+
dependency-version: [ prefer-lowest, prefer-stable ]
14+
15+
name: Tests on PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v3
20+
21+
- name: Cache dependencies
22+
uses: actions/cache@v3
23+
with:
24+
path: ~/.composer/cache/files
25+
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
26+
27+
- name: Setup PHP
28+
uses: shivammathur/setup-php@v2
29+
with:
30+
php-version: ${{ matrix.php }}
31+
extensions: dom, mbstring, zip
32+
coverage: none
33+
34+
- name: Install Composer dependencies
35+
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist
36+
37+
- name: Run tests
38+
run: composer test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
.idea

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Changelog
2+
3+
All notable changes to `jinja-php` will be documented in this file.

README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Jinja PHP
2+
3+
A minimalistic **zero-dependency** PHP implementation of the Jinja templating engine, specifically designed for parsing and rendering
4+
machine learning (ML) chat templates. This project is heavily inspired by HuggingFace's Jinja template engine in
5+
JavaScript, intended primarily for ML chat templates, but is versatile enough to be used for general purposes of parsing
6+
most Jinja templates.
7+
8+
## Installation
9+
10+
Install Jinja PHP through Composer:
11+
12+
```shell
13+
composer require codewithkyrian/jinja-php
14+
```
15+
16+
## Usage
17+
18+
Here’s how you can use Jinja PHP to render a template:
19+
20+
```php
21+
$sourceString = "{{ bos_token }}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ message['content'] + eos_token + ' ' }}{% else %}{{ raise_exception('Only user and assistant roles are supported!') }}{% endif %}{% endfor %}";
22+
$args = [
23+
'messages' => [
24+
['role' => 'user', 'content' => 'Hello!'],
25+
['role' => 'assistant', 'content' => 'Hi! How are you?'],
26+
['role' => 'user', 'content' => 'I am doing great.'],
27+
['role' => 'assistant', 'content' => 'That is great to hear.'],
28+
],
29+
"add_generation_prompt" => true,
30+
"bos_token" => "<s>",
31+
"eos_token" => "</s>",
32+
"unk_token" => "<unk>",
33+
];
34+
35+
$template = new Template($sourceString);
36+
37+
$rendered = $template->render($args);
38+
// <s>[INST] Hello! [/INST]Hi! How are you?</s> [INST] I am doing great. [/INST]That is great to hear.</s>
39+
```
40+
41+
## Advanced Usage
42+
43+
Jinja PHP supports various control structures from Jinja templates. Here are some examples:
44+
45+
### Conditional Statements:
46+
47+
```php
48+
{% if user.isActive %}
49+
Hello, {{ user.name }}!
50+
{% else %}
51+
Hello, Guest!
52+
{% endif %}
53+
54+
```
55+
56+
### For Loops
57+
58+
```php
59+
<ul>
60+
{% for user in users %}
61+
<li>{{ user.name }}</li>
62+
{% else %}
63+
<li>No users found.</li>
64+
{% endfor %}
65+
</ul>
66+
```
67+
68+
```php
69+
{% for user in users %}
70+
{{ loop.index }} - {{ user.name }}
71+
{% endfor %}
72+
```
73+
74+
### Variable Filters
75+
76+
```php
77+
{{ "Hello World!"|lower }}
78+
```
79+
80+
## Comprehensive Feature Support
81+
82+
Jinja PHP is designed to be robust and feature-rich, offering support for a wide range of functionalities beyond the
83+
basics of templating. This includes handling exceptions, using default string methods like `strip()` or `upper()`,
84+
working with dictionaries, and much more. If there's a feature you need that isn't currently implemented in Jinja PHP,
85+
we encourage you to [request it](https://github.com/codewithkyrian/jinja-php/issues/new). Additionally, if you're
86+
proficient with PHP and understand the internals of templating engines, consider contributing to the project by
87+
submitting a pull request with your proposed feature.
88+
89+
## Testing
90+
91+
Jinja PHP comes with a suite of tests to ensure functionality remains consistent and reliable. To run the tests:
92+
93+
```
94+
composer test
95+
```
96+
97+
## Contributors
98+
99+
- [Kyrian Obikwelu](https://github.com/CodeWithKyrian)
100+
- Other contributors are welcome.
101+
102+
## Acknowledgements
103+
104+
- [Hugging Face](https://huggingface.co/) for their work on ML chat templates.
105+
- The Jinja template engine for Python, which served as the primary inspiration for this project.
106+
107+
## Support
108+
109+
If you find any issues or have a question, feel free
110+
to [open an issue](https://github.com/CodeWithKyrian/jinja-php/issues/new/choose) in the repo.
111+
112+
## License
113+
114+
This project is licensed under the MIT License. See
115+
the [LICENSE](https://github.com/codewithkyrian/jinja-php/blob/main/LICENSE) file for more information.

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "codewithkyrian/jinja-php",
3+
"description": "A minimalistic PHP implementation of the Jinja templating engine, specifically designed for parsing and rendering ML chat templates.",
4+
"type": "library",
5+
"require": {
6+
"php": "^8.1"
7+
},
8+
"require-dev": {
9+
"symfony/var-dumper": "^7.0",
10+
"pestphp/pest": "^2.34"
11+
},
12+
"license": "MIT",
13+
"autoload": {
14+
"psr-4": {
15+
"Codewithkyrian\\Jinja\\": "src/"
16+
},
17+
"files": [
18+
"src/Core/Utils.php"
19+
]
20+
},
21+
"authors": [
22+
{
23+
"name": "Kyrian Obikwelu",
24+
"email": "koshnawaza@gmail.com"
25+
}
26+
],
27+
"config": {
28+
"allow-plugins": {
29+
"pestphp/pest-plugin": true
30+
}
31+
},
32+
"scripts": {
33+
"test": "vendor/bin/pest",
34+
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage"
35+
}
36+
}

0 commit comments

Comments
 (0)