-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix isse #46 mistral tool_choice (#50)
- Loading branch information
1 parent
a46700a
commit 35d8794
Showing
14 changed files
with
473 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -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 |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 @@ | ||
OPENAI_KEY= |
This file contains 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 @@ | ||
.env.local |
This file contains 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,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, | ||
]; | ||
} | ||
} |
This file contains 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,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); |
This file contains 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,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; | ||
} |
Oops, something went wrong.