Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jdecool committed Jan 28, 2024
0 parents commit c77200c
Show file tree
Hide file tree
Showing 29 changed files with 832 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[Makefile]
indent_style = tab
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/examples export-ignore
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
84 changes: 84 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Continuous Integration
on: [push]

jobs:
linter:
name: Code Style
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install --no-progress
- name: Run php-cs-fixture
run: vendor/bin/php-cs-fixer fix -v --dry-run

phpstan:
name: Static analysis
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install --no-progress
- name: Run phpstan
run: vendor/bin/phpstan

phpunit:
name: Unit Tests
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.1', '8.2', '8.3']
flags: ['', '--prefer-lowest', '--prefer-stable']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer update --prefer-dist --no-interaction --optimize-autoloader --prefer-stable --no-progress $COMPOSER_FLAGS
env:
COMPOSER_FLAGS: ${{ matrix.flags }}
- name: Run PHPUnit
run: vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
.php-cs-fixer.cache
composer.lock
21 changes: 21 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
;

$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'declare_strict_types' => true,
'function_declaration' => ['closure_function_spacing' => 'none'],
'single_import_per_statement' => false,
])
->setFinder($finder)
;

return $config;
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
define printSection
@printf "\033[36m\n==================================================\n\033[0m"
@printf "\033[36m $1 \033[0m"
@printf "\033[36m\n==================================================\n\033[0m"
endef

.PHONY: cs
cs:
$(call printSection,CODE STYLE)
vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --diff

.PHONY: cs-fix
cs-fix:
vendor/bin/php-cs-fixer fix

.PHONY: cs-ci
cs-ci:
$(call printSection,CODE STYLE)
vendor/bin/php-cs-fixer fix --ansi --dry-run --using-cache=no --verbose

.PHONY: phpstan
phpstan:
$(call printSection,PHPSTAN)
vendor/bin/phpstan analyse -c phpstan.neon --ansi --no-progress --no-interaction

.PHONY: phpunit
phpunit:
$(call printSection,PHPUNIT)
vendor/bin/phpunit

.PHONY: phpunit-coverage
phpunit-coverage:
$(call printSection,PHPUNIT COVERAGE)
vendor/bin/phpunit --coverage-text

.PHONY: clean-vendor
clean-vendor:
$(call printSection,CLEAN-VENDOR)
rm -f composer.lock
rm -rf vendor

.PHONY: composer-install
composer-install: vendor/composer/installed.json

vendor/composer/installed.json:
$(call printSection,COMPOSER INSTALL)
composer --no-interaction install --ansi --no-progress --prefer-dist
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Ollama PHP Client
=================

This is a PHP client for the Ollama API.

## Getting Started

```
$ composer require jdecool/ollama-client
```

## Usage

```php
use JDecool\OllamaClient\ClientBuilder;

$builder = new ClientBuilder();
$client = $builder->create();
```
42 changes: 42 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "jdecool/ollama-client",
"description": "Ollama PHP API client",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Jérémy DECOOL",
"email": "[email protected]"
}
],
"require": {
"php": ">=8.1",
"psr/log": "^3.0",
"symfony/http-client": "^5.4 || ^6.0 || ^7.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.41",
"friendsofphp/php-cs-fixer": "^3.47",
"nyholm/psr7": "^1.8",
"php-http/guzzle7-adapter": "^1.0",
"php-http/mock-client": "^1.6",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5"
},
"autoload": {
"psr-4": {
"JDecool\\OllamaClient\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"JDecool\\OllamaClient\\Tests\\": "tests/"
}
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"php-http/discovery": true
}
}
}
2 changes: 2 additions & 0 deletions examples/Modelfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM tinyllama
SYSTEM "You are mario from super mario bros."
22 changes: 22 additions & 0 deletions examples/chat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

require __DIR__.'/../vendor/autoload.php';

use JDecool\OllamaClient\ClientBuilder;
use JDecool\OllamaClient\Model\Message;
use JDecool\OllamaClient\Model\Request\ChatRequest;

$builder = new ClientBuilder();
$client = $builder->create();

$request = new ChatRequest('tinyllama', [
new Message('user', 'Why is the sky blue?'),
]);

// sync
var_dump($client->chat($request));

// async
foreach ($client->chatStream($request) as $chunk) {
var_dump($chunk);
}
25 changes: 25 additions & 0 deletions examples/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

require __DIR__.'/../vendor/autoload.php';

use JDecool\OllamaClient\ClientBuilder;
use JDecool\OllamaClient\Model\Request\CreateRequest;

$builder = new ClientBuilder();

$client = $builder->create();

$modelFile = <<<MODEL
FROM tinyllama
SYSTEM "You are mario from super mario bros."
MODEL;

// stream
$request = new CreateRequest('foo', $modelFile);
foreach ($client->createStream($request) as $chunk) {
var_dump($chunk);
}

// sync
$request = CreateRequest::fromFile('foo', __DIR__.'/Modelfile');
$client->create($request);
20 changes: 20 additions & 0 deletions examples/pull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

require __DIR__.'/../vendor/autoload.php';

use JDecool\OllamaClient\ClientBuilder;
use JDecool\OllamaClient\Model\Request\PullRequest;

$builder = new ClientBuilder();

$client = $builder->create();

$request = new PullRequest('tinyllama');

// sync
$client->pullStream($request);

// stream
foreach ($client->pullStream($request) as $chunk) {
var_dump($chunk);
}
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: max
paths:
- examples
- src
- tests

checkMissingIterableValueType: false
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<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">
<testsuites>
<testsuite name="Ollama Client">
<directory>tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src/</directory>
</include>
</source>
</phpunit>
Loading

0 comments on commit c77200c

Please sign in to comment.