Skip to content

Commit

Permalink
fix isse #46 mistral tool_choice (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored Mar 29, 2024
1 parent a46700a commit 35d8794
Show file tree
Hide file tree
Showing 14 changed files with 473 additions and 6 deletions.
1 change: 1 addition & 0 deletions .readme.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
title: OpenAI Adapter
description: The adapter integrates OpenAI models into Modelflow AI.
shortDescription: The adapter for integrating OpenAI models.
examples: true
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ you can add the package to your project by running the following command:
composer require modelflow-ai/openai-adapter
```

## Examples

Here are some examples of how you can use the Mistral package in your PHP applications. You can find more detailed
examples in the [examples directory](examples).

## Usage

Detailed usage instructions will be provided in the future. For now, you can refer to the source code and unit tests for
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"nyholm/psr7": "^1.8"
},
"require-dev": {
"modelflow-ai/prompt-template": "^0.1",
"php-cs-fixer/shim": "^3.15",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.10",
Expand All @@ -41,7 +42,8 @@
"rector/rector": "^0.18.1",
"phpspec/prophecy": "^1.0@dev",
"jangregor/phpstan-prophecy": "^1.0",
"phpspec/prophecy-phpunit": "^2.1@stable"
"phpspec/prophecy-phpunit": "^2.1@stable",
"symfony/dotenv": "7.1.x-dev"
},
"scripts": {
"test": [
Expand Down
168 changes: 166 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_KEY=
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env.local
39 changes: 39 additions & 0 deletions examples/WeatherTool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Modelflow AI package.
*
* (c) Johannes Wachter <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App;

class WeatherTool
{
/**
* Can be called foreach location to get the current weather. Also multiple times in one call.
*
* @return array{
* location: string,
* timestamp: int,
* weather: string,
* temperature: float,
* }
*/
public function getCurrentWeather(string $location, ?int $timestamp): array
{
$weather = ['rainy', 'sunny', 'cloudy', 'stormy', 'snowy'];

return [
'location' => $location,
'timestamp' => $timestamp ?? \time(),
'weather' => $weather[\random_int(0, \count($weather) - 1)],
'temperature' => \random_int(-20, 40) + \random_int(0, 9) / 10.0,
];
}
}
44 changes: 44 additions & 0 deletions examples/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Modelflow AI package.
*
* (c) Johannes Wachter <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App;

require_once \dirname(__DIR__) . '/vendor/autoload.php';

use ModelflowAi\Core\AIRequestHandler;
use ModelflowAi\Core\DecisionTree\AIModelDecisionTree;
use ModelflowAi\Core\DecisionTree\DecisionRule;
use ModelflowAi\Core\Request\Criteria\CapabilityCriteria;
use ModelflowAi\OpenaiAdapter\Model\OpenaiChatModelAdapter;
use Symfony\Component\Dotenv\Dotenv;

(new Dotenv())->bootEnv(__DIR__ . '/.env');

$adapter = [];

$openaiApiKey = $_ENV['OPENAI_API_KEY'];
if (!$openaiApiKey) {
throw new \RuntimeException('Openai API key is required');
}

$openaiClient = \OpenAI::client($openaiApiKey);

$gpt4Adapter = new OpenaiChatModelAdapter($openaiClient, 'gpt-4');
$gpt35Adapter = new OpenaiChatModelAdapter($openaiClient, 'gpt-3.5');

$adapter[] = new DecisionRule($gpt4Adapter, [CapabilityCriteria::SMART]);
$adapter[] = new DecisionRule($gpt35Adapter, [CapabilityCriteria::BASIC]);

$decisionTree = new AIModelDecisionTree($adapter);

return new AIRequestHandler($decisionTree);
44 changes: 44 additions & 0 deletions examples/chat-stream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Modelflow AI package.
*
* (c) Johannes Wachter <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App;

use ModelflowAi\Core\AIRequestHandlerInterface;
use ModelflowAi\Core\Request\Criteria\CapabilityCriteria;
use ModelflowAi\Core\Request\Message\AIChatMessage;
use ModelflowAi\Core\Request\Message\AIChatMessageRoleEnum;
use ModelflowAi\Core\Response\AIChatResponseStream;
use ModelflowAi\PromptTemplate\ChatPromptTemplate;

/** @var AIRequestHandlerInterface $handler */
$handler = require_once __DIR__ . '/bootstrap.php';

/** @var AIChatResponseStream $response */
$response = $handler->createChatRequest(
...ChatPromptTemplate::create(
new AIChatMessage(AIChatMessageRoleEnum::SYSTEM, 'You are an {feeling} bot'),
new AIChatMessage(AIChatMessageRoleEnum::USER, 'Hello {where}!'),
)->format(['where' => 'world', 'feeling' => 'angry']),
)
->addCriteria(CapabilityCriteria::BASIC)
->streamed()
->build()
->execute();

foreach ($response->getMessageStream() as $index => $message) {
if (0 === $index) {
echo $message->role->value . ': ';
}

echo $message->content;
}
Loading

0 comments on commit 35d8794

Please sign in to comment.