Skip to content

Commit c77200c

Browse files
committed
Initial commit
0 parents  commit c77200c

29 files changed

+832
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[Makefile]
12+
indent_style = tab

.gitattributes

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/examples export-ignore
2+
/tests export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
5+
/phpstan.neon.dist export-ignore
6+
/phpunit.xml.dist export-ignore

.github/workflows/tests.yml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Continuous Integration
2+
on: [push]
3+
4+
jobs:
5+
linter:
6+
name: Code Style
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v2
11+
- name: Setup PHP
12+
uses: shivammathur/setup-php@v2
13+
with:
14+
php-version: 8.1
15+
coverage: xdebug
16+
- name: Get Composer Cache Directory
17+
id: composer-cache
18+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
19+
- name: Cache dependencies
20+
uses: actions/cache@v2
21+
with:
22+
path: ${{ steps.composer-cache.outputs.dir }}
23+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
24+
restore-keys: ${{ runner.os }}-composer-
25+
- name: Install Dependencies
26+
run: composer install --no-progress
27+
- name: Run php-cs-fixture
28+
run: vendor/bin/php-cs-fixer fix -v --dry-run
29+
30+
phpstan:
31+
name: Static analysis
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Checkout
35+
uses: actions/checkout@v2
36+
- name: Setup PHP
37+
uses: shivammathur/setup-php@v2
38+
with:
39+
php-version: 8.1
40+
coverage: xdebug
41+
- name: Get Composer Cache Directory
42+
id: composer-cache
43+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
44+
- name: Cache dependencies
45+
uses: actions/cache@v2
46+
with:
47+
path: ${{ steps.composer-cache.outputs.dir }}
48+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
49+
restore-keys: ${{ runner.os }}-composer-
50+
- name: Install Dependencies
51+
run: composer install --no-progress
52+
- name: Run phpstan
53+
run: vendor/bin/phpstan
54+
55+
phpunit:
56+
name: Unit Tests
57+
runs-on: ubuntu-latest
58+
strategy:
59+
matrix:
60+
php: ['8.1', '8.2', '8.3']
61+
flags: ['', '--prefer-lowest', '--prefer-stable']
62+
steps:
63+
- name: Checkout
64+
uses: actions/checkout@v2
65+
- name: Setup PHP
66+
uses: shivammathur/setup-php@v2
67+
with:
68+
php-version: ${{ matrix.php }}
69+
coverage: xdebug
70+
- name: Get Composer Cache Directory
71+
id: composer-cache
72+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
73+
- name: Cache dependencies
74+
uses: actions/cache@v2
75+
with:
76+
path: ${{ steps.composer-cache.outputs.dir }}
77+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
78+
restore-keys: ${{ runner.os }}-composer-
79+
- name: Install Dependencies
80+
run: composer update --prefer-dist --no-interaction --optimize-autoloader --prefer-stable --no-progress $COMPOSER_FLAGS
81+
env:
82+
COMPOSER_FLAGS: ${{ matrix.flags }}
83+
- name: Run PHPUnit
84+
run: vendor/bin/phpunit

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
.php-cs-fixer.cache
3+
composer.lock

.php-cs-fixer.dist.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__)
5+
->exclude('vendor')
6+
;
7+
8+
$config = new PhpCsFixer\Config();
9+
$config
10+
->setRiskyAllowed(true)
11+
->setRules([
12+
'@PSR2' => true,
13+
'array_syntax' => ['syntax' => 'short'],
14+
'declare_strict_types' => true,
15+
'function_declaration' => ['closure_function_spacing' => 'none'],
16+
'single_import_per_statement' => false,
17+
])
18+
->setFinder($finder)
19+
;
20+
21+
return $config;

Makefile

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
define printSection
2+
@printf "\033[36m\n==================================================\n\033[0m"
3+
@printf "\033[36m $1 \033[0m"
4+
@printf "\033[36m\n==================================================\n\033[0m"
5+
endef
6+
7+
.PHONY: cs
8+
cs:
9+
$(call printSection,CODE STYLE)
10+
vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --diff
11+
12+
.PHONY: cs-fix
13+
cs-fix:
14+
vendor/bin/php-cs-fixer fix
15+
16+
.PHONY: cs-ci
17+
cs-ci:
18+
$(call printSection,CODE STYLE)
19+
vendor/bin/php-cs-fixer fix --ansi --dry-run --using-cache=no --verbose
20+
21+
.PHONY: phpstan
22+
phpstan:
23+
$(call printSection,PHPSTAN)
24+
vendor/bin/phpstan analyse -c phpstan.neon --ansi --no-progress --no-interaction
25+
26+
.PHONY: phpunit
27+
phpunit:
28+
$(call printSection,PHPUNIT)
29+
vendor/bin/phpunit
30+
31+
.PHONY: phpunit-coverage
32+
phpunit-coverage:
33+
$(call printSection,PHPUNIT COVERAGE)
34+
vendor/bin/phpunit --coverage-text
35+
36+
.PHONY: clean-vendor
37+
clean-vendor:
38+
$(call printSection,CLEAN-VENDOR)
39+
rm -f composer.lock
40+
rm -rf vendor
41+
42+
.PHONY: composer-install
43+
composer-install: vendor/composer/installed.json
44+
45+
vendor/composer/installed.json:
46+
$(call printSection,COMPOSER INSTALL)
47+
composer --no-interaction install --ansi --no-progress --prefer-dist

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Ollama PHP Client
2+
=================
3+
4+
This is a PHP client for the Ollama API.
5+
6+
## Getting Started
7+
8+
```
9+
$ composer require jdecool/ollama-client
10+
```
11+
12+
## Usage
13+
14+
```php
15+
use JDecool\OllamaClient\ClientBuilder;
16+
17+
$builder = new ClientBuilder();
18+
$client = $builder->create();
19+
```

composer.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "jdecool/ollama-client",
3+
"description": "Ollama PHP API client",
4+
"license": "MIT",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "Jérémy DECOOL",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=8.1",
14+
"psr/log": "^3.0",
15+
"symfony/http-client": "^5.4 || ^6.0 || ^7.0"
16+
},
17+
"require-dev": {
18+
"ergebnis/composer-normalize": "^2.41",
19+
"friendsofphp/php-cs-fixer": "^3.47",
20+
"nyholm/psr7": "^1.8",
21+
"php-http/guzzle7-adapter": "^1.0",
22+
"php-http/mock-client": "^1.6",
23+
"phpstan/phpstan": "^1.10",
24+
"phpunit/phpunit": "^10.5"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"JDecool\\OllamaClient\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"JDecool\\OllamaClient\\Tests\\": "tests/"
34+
}
35+
},
36+
"config": {
37+
"allow-plugins": {
38+
"ergebnis/composer-normalize": true,
39+
"php-http/discovery": true
40+
}
41+
}
42+
}

examples/Modelfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM tinyllama
2+
SYSTEM "You are mario from super mario bros."

examples/chat.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
5+
use JDecool\OllamaClient\ClientBuilder;
6+
use JDecool\OllamaClient\Model\Message;
7+
use JDecool\OllamaClient\Model\Request\ChatRequest;
8+
9+
$builder = new ClientBuilder();
10+
$client = $builder->create();
11+
12+
$request = new ChatRequest('tinyllama', [
13+
new Message('user', 'Why is the sky blue?'),
14+
]);
15+
16+
// sync
17+
var_dump($client->chat($request));
18+
19+
// async
20+
foreach ($client->chatStream($request) as $chunk) {
21+
var_dump($chunk);
22+
}

examples/create.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
5+
use JDecool\OllamaClient\ClientBuilder;
6+
use JDecool\OllamaClient\Model\Request\CreateRequest;
7+
8+
$builder = new ClientBuilder();
9+
10+
$client = $builder->create();
11+
12+
$modelFile = <<<MODEL
13+
FROM tinyllama
14+
SYSTEM "You are mario from super mario bros."
15+
MODEL;
16+
17+
// stream
18+
$request = new CreateRequest('foo', $modelFile);
19+
foreach ($client->createStream($request) as $chunk) {
20+
var_dump($chunk);
21+
}
22+
23+
// sync
24+
$request = CreateRequest::fromFile('foo', __DIR__.'/Modelfile');
25+
$client->create($request);

examples/pull.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
5+
use JDecool\OllamaClient\ClientBuilder;
6+
use JDecool\OllamaClient\Model\Request\PullRequest;
7+
8+
$builder = new ClientBuilder();
9+
10+
$client = $builder->create();
11+
12+
$request = new PullRequest('tinyllama');
13+
14+
// sync
15+
$client->pullStream($request);
16+
17+
// stream
18+
foreach ($client->pullStream($request) as $chunk) {
19+
var_dump($chunk);
20+
}

phpstan.neon

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- examples
5+
- src
6+
- tests
7+
8+
checkMissingIterableValueType: false

phpunit.xml.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php">
4+
<testsuites>
5+
<testsuite name="Ollama Client">
6+
<directory>tests/</directory>
7+
</testsuite>
8+
</testsuites>
9+
<source>
10+
<include>
11+
<directory>./src/</directory>
12+
</include>
13+
</source>
14+
</phpunit>

0 commit comments

Comments
 (0)