|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Platform\Tests\Bridge\Ollama; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\AI\Platform\Bridge\Ollama\Ollama; |
| 18 | +use Symfony\AI\Platform\Bridge\Ollama\OllamaClient; |
| 19 | +use Symfony\AI\Platform\Model; |
| 20 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 21 | +use Symfony\Component\HttpClient\Response\JsonMockResponse; |
| 22 | + |
| 23 | +#[CoversClass(OllamaClient::class)] |
| 24 | +#[UsesClass(Ollama::class)] |
| 25 | +#[UsesClass(Model::class)] |
| 26 | +final class OllamaClientTest extends TestCase |
| 27 | +{ |
| 28 | + public function testSupportsModel() |
| 29 | + { |
| 30 | + $client = new OllamaClient(new MockHttpClient(), 'http://localhost:1234'); |
| 31 | + |
| 32 | + $this->assertTrue($client->supports(new Ollama())); |
| 33 | + $this->assertFalse($client->supports(new Model('any-model'))); |
| 34 | + } |
| 35 | + |
| 36 | + public function testOutputStructureIsSupported() |
| 37 | + { |
| 38 | + $httpClient = new MockHttpClient([ |
| 39 | + new JsonMockResponse([ |
| 40 | + 'capabilities' => ['completion', 'tools'], |
| 41 | + ]), |
| 42 | + new JsonMockResponse([ |
| 43 | + 'model' => 'foo', |
| 44 | + 'response' => [ |
| 45 | + 'age' => 22, |
| 46 | + 'available' => true, |
| 47 | + ], |
| 48 | + 'done' => true, |
| 49 | + ]), |
| 50 | + ], 'http://127.0.0.1:1234'); |
| 51 | + |
| 52 | + $client = new OllamaClient($httpClient, 'http://127.0.0.1:1234'); |
| 53 | + $response = $client->request(new Ollama(), [ |
| 54 | + 'messages' => [ |
| 55 | + [ |
| 56 | + 'role' => 'user', |
| 57 | + 'content' => 'Ollama is 22 years old and is busy saving the world. Respond using JSON', |
| 58 | + ], |
| 59 | + ], |
| 60 | + 'model' => 'llama3.2', |
| 61 | + ], [ |
| 62 | + 'response_format' => [ |
| 63 | + 'type' => 'json_schema', |
| 64 | + 'json_schema' => [ |
| 65 | + 'name' => 'clock', |
| 66 | + 'strict' => true, |
| 67 | + 'schema' => [ |
| 68 | + 'type' => 'object', |
| 69 | + 'properties' => [ |
| 70 | + 'age' => ['type' => 'integer'], |
| 71 | + 'available' => ['type' => 'boolean'], |
| 72 | + ], |
| 73 | + 'required' => ['age', 'available'], |
| 74 | + 'additionalProperties' => false, |
| 75 | + ], |
| 76 | + ], |
| 77 | + ], |
| 78 | + ]); |
| 79 | + |
| 80 | + $this->assertSame(2, $httpClient->getRequestsCount()); |
| 81 | + $this->assertSame([ |
| 82 | + 'model' => 'foo', |
| 83 | + 'response' => [ |
| 84 | + 'age' => 22, |
| 85 | + 'available' => true, |
| 86 | + ], |
| 87 | + 'done' => true, |
| 88 | + ], $response->getData()); |
| 89 | + } |
| 90 | +} |
0 commit comments