-
-
Notifications
You must be signed in to change notification settings - Fork 54
[Platform] Ollama structured output support #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+130
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\AI\Agent\Agent; | ||
use Symfony\AI\Agent\StructuredOutput\AgentProcessor; | ||
use Symfony\AI\Fixtures\StructuredOutput\MathReasoning; | ||
use Symfony\AI\Platform\Bridge\Ollama\Ollama; | ||
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory; | ||
use Symfony\AI\Platform\Message\Message; | ||
use Symfony\AI\Platform\Message\MessageBag; | ||
|
||
require_once dirname(__DIR__).'/bootstrap.php'; | ||
|
||
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client()); | ||
$model = new Ollama(); | ||
|
||
$processor = new AgentProcessor(); | ||
$agent = new Agent($platform, $model, [$processor], [$processor], logger()); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are a helpful math tutor. Guide the user through the solution step by step.'), | ||
Message::ofUser('how can I solve 8x + 7 = -23'), | ||
); | ||
$result = $agent->call($messages, ['output_structure' => MathReasoning::class]); | ||
|
||
dump($result->getContent()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\AI\Platform\Tests\Bridge\Ollama; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\AI\Platform\Bridge\Ollama\Ollama; | ||
use Symfony\AI\Platform\Bridge\Ollama\OllamaClient; | ||
use Symfony\AI\Platform\Model; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\JsonMockResponse; | ||
|
||
#[CoversClass(OllamaClient::class)] | ||
#[UsesClass(Ollama::class)] | ||
#[UsesClass(Model::class)] | ||
final class OllamaClientTest extends TestCase | ||
{ | ||
public function testSupportsModel() | ||
{ | ||
$client = new OllamaClient(new MockHttpClient(), 'http://localhost:1234'); | ||
|
||
$this->assertTrue($client->supports(new Ollama())); | ||
$this->assertFalse($client->supports(new Model('any-model'))); | ||
} | ||
|
||
public function testOutputStructureIsSupported() | ||
{ | ||
$httpClient = new MockHttpClient([ | ||
new JsonMockResponse([ | ||
'capabilities' => ['completion', 'tools'], | ||
]), | ||
new JsonMockResponse([ | ||
'model' => 'foo', | ||
'response' => [ | ||
'age' => 22, | ||
'available' => true, | ||
], | ||
'done' => true, | ||
]), | ||
], 'http://127.0.0.1:1234'); | ||
|
||
$client = new OllamaClient($httpClient, 'http://127.0.0.1:1234'); | ||
$response = $client->request(new Ollama(), [ | ||
'messages' => [ | ||
[ | ||
'role' => 'user', | ||
'content' => 'Ollama is 22 years old and is busy saving the world. Respond using JSON', | ||
], | ||
], | ||
'model' => 'llama3.2', | ||
], [ | ||
'response_format' => [ | ||
'type' => 'json_schema', | ||
'json_schema' => [ | ||
'name' => 'clock', | ||
'strict' => true, | ||
'schema' => [ | ||
'type' => 'object', | ||
'properties' => [ | ||
'age' => ['type' => 'integer'], | ||
'available' => ['type' => 'boolean'], | ||
], | ||
'required' => ['age', 'available'], | ||
'additionalProperties' => false, | ||
], | ||
], | ||
], | ||
]); | ||
|
||
$this->assertSame(2, $httpClient->getRequestsCount()); | ||
$this->assertSame([ | ||
'model' => 'foo', | ||
'response' => [ | ||
'age' => 22, | ||
'available' => true, | ||
], | ||
'done' => true, | ||
], $response->getData()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.