modelflow-ai / .github

MIT License
6 stars 3 forks source link

Examples crashes when using Mistral #46

Closed gplanchat closed 8 months ago

gplanchat commented 8 months ago

Hello,

Thanks for those tools.

I tried the tools.php and tools-stream.php with Mistral.

Unfortunately, I got the following error:

Fatal error: Uncaught Webmozart\Assert\InvalidArgumentException: Expected a string. Got: ModelflowAi\Core\ToolInfo\ToolChoiceEnum in /.../vendor/webmozart/assert/src/Assert.php on line 2074

Webmozart\Assert\InvalidArgumentException: Expected a string. Got: ModelflowAi\Core\ToolInfo\ToolChoiceEnum in /.../vendor/webmozart/assert/src/Assert.php on line 2074

Call Stack:
    0.0003     501464   1. {main}() /.../bin/console:0
    0.0258    2118616   2. ModelflowAi\Core\Request\AIChatRequest->execute() /.../bin/console:31
    0.0258    2118616   3. ModelflowAi\Core\AIRequestHandler->ModelflowAi\Core\{closure:/.../vendor/modelflow-ai/core/src/AIRequestHandler.php:54-59}($request = class ModelflowAi\Core\Request\AIChatRequest { protected $requestHandler = class Closure {  }; private readonly ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection ${ModelflowAi\Core\Request\AIRequest}criteria = class ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection { public readonly array $criteria = [...] }; private readonly array ${ModelflowAi\Core\Request\AIRequest}options = ['toolChoice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto'), 'streamed' => TRUE]; private readonly ModelflowAi\Core\Request\AIChatMessageCollection $messages = class ModelflowAi\Core\Request\AIChatMessageCollection { public $0 = class ModelflowAi\Core\Request\Message\AIChatMessage { ... } }; private readonly array $tools = ['get_current_weather' => [...]]; private readonly array $toolInfos = [0 => class ModelflowAi\Core\ToolInfo\ToolInfo { ... }] }) /.../vendor/modelflow-ai/core/src/Request/AIChatRequest.php:86
    0.0258    2118776   4. ModelflowAi\Core\AIRequestHandler->handle($request = class ModelflowAi\Core\Request\AIChatRequest { protected $requestHandler = class Closure {  }; private readonly ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection ${ModelflowAi\Core\Request\AIRequest}criteria = class ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection { public readonly array $criteria = [...] }; private readonly array ${ModelflowAi\Core\Request\AIRequest}options = ['toolChoice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto'), 'streamed' => TRUE]; private readonly ModelflowAi\Core\Request\AIChatMessageCollection $messages = class ModelflowAi\Core\Request\AIChatMessageCollection { public $0 = class ModelflowAi\Core\Request\Message\AIChatMessage { ... } }; private readonly array $tools = ['get_current_weather' => [...]]; private readonly array $toolInfos = [0 => class ModelflowAi\Core\ToolInfo\ToolInfo { ... }] }) /.../vendor/modelflow-ai/core/src/AIRequestHandler.php:55
    0.0258    2118776   5. ModelflowAi\MistralAdapter\Model\MistralChatModelAdapter->handleRequest($request = class ModelflowAi\Core\Request\AIChatRequest { protected $requestHandler = class Closure {  }; private readonly ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection ${ModelflowAi\Core\Request\AIRequest}criteria = class ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection { public readonly array $criteria = [...] }; private readonly array ${ModelflowAi\Core\Request\AIRequest}options = ['toolChoice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto'), 'streamed' => TRUE]; private readonly ModelflowAi\Core\Request\AIChatMessageCollection $messages = class ModelflowAi\Core\Request\AIChatMessageCollection { public $0 = class ModelflowAi\Core\Request\Message\AIChatMessage { ... } }; private readonly array $tools = ['get_current_weather' => [...]]; private readonly array $toolInfos = [0 => class ModelflowAi\Core\ToolInfo\ToolInfo { ... }] }) /.../vendor/modelflow-ai/core/src/AIRequestHandler.php:39
    0.0297    2852704   6. Webmozart\Assert\Assert::string($value = enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto'), $message = ???) /.../vendor/modelflow-ai/mistral-adapter/src/Model/MistralChatModelAdapter.php:64
    0.0297    2853024   7. Webmozart\Assert\Assert::reportInvalidArgument($message = 'Expected a string. Got: ModelflowAi\\Core\\ToolInfo\\ToolChoiceEnum') /.../vendor/webmozart/assert/src/Assert.php:49

I was surprised this sort of error could happen, but I presumed you are at an early stage of development and refactor a lot. I tried the following quick fix in the vendor to get further :

Subject: [PATCH] Test
---
Index: vendor/modelflow-ai/mistral-adapter/src/Model/MistralChatModelAdapter.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/modelflow-ai/mistral-adapter/src/Model/MistralChatModelAdapter.php b/vendor/modelflow-ai/mistral-adapter/src/Model/MistralChatModelAdapter.php
--- a/vendor/modelflow-ai/mistral-adapter/src/Model/MistralChatModelAdapter.php (revision de63c4098b1706025af1816f8eb872af78fd048e)
+++ b/vendor/modelflow-ai/mistral-adapter/src/Model/MistralChatModelAdapter.php (date 1711551232366)
@@ -22,6 +22,7 @@
 use ModelflowAi\Core\Response\AIChatResponseStream;
 use ModelflowAi\Core\Response\AIChatToolCall;
 use ModelflowAi\Core\Response\AIResponseInterface;
+use ModelflowAi\Core\ToolInfo\ToolChoiceEnum;
 use ModelflowAi\Core\ToolInfo\ToolTypeEnum;
 use ModelflowAi\Mistral\ClientInterface;
 use ModelflowAi\Mistral\Model;
@@ -61,7 +62,7 @@
             $parameters['tools'] = ToolFormatter::formatTools($request->getToolInfos());
             $toolChoice = $request->getOption('toolChoice');
             if (null !== $toolChoice) {
-                Assert::string($toolChoice);
+                Assert::isInstanceOf($toolChoice, ToolChoiceEnum::class);
                 $parameters['tool_choice'] = $toolChoice;
             }
         }

and then got the following error:


Fatal error: Uncaught Webmozart\Assert\InvalidArgumentException: Expected one of: "auto", "none". Got: ModelflowAi\Core\ToolInfo\ToolChoiceEnum in /.../vendor/webmozart/assert/src/Assert.php on line 2074

Webmozart\Assert\InvalidArgumentException: Expected one of: "auto", "none". Got: ModelflowAi\Core\ToolInfo\ToolChoiceEnum in /.../vendor/webmozart/assert/src/Assert.php on line 2074

Call Stack:
    0.0005     501008   1. {main}() /.../bin/console:0
    0.0496    2118344   2. ModelflowAi\Core\Request\AIChatRequest->execute() /.../bin/console:30
    0.0496    2118344   3. ModelflowAi\Core\AIRequestHandler->ModelflowAi\Core\{closure:/.../vendor/modelflow-ai/core/src/AIRequestHandler.php:54-59}($request = class ModelflowAi\Core\Request\AIChatRequest { protected $requestHandler = class Closure {  }; private readonly ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection ${ModelflowAi\Core\Request\AIRequest}criteria = class ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection { public readonly array $criteria = [...] }; private readonly array ${ModelflowAi\Core\Request\AIRequest}options = ['toolChoice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto')]; private readonly ModelflowAi\Core\Request\AIChatMessageCollection $messages = class ModelflowAi\Core\Request\AIChatMessageCollection { public $0 = class ModelflowAi\Core\Request\Message\AIChatMessage { ... } }; private readonly array $tools = ['get_current_weather' => [...]]; private readonly array $toolInfos = [0 => class ModelflowAi\Core\ToolInfo\ToolInfo { ... }] }) /.../vendor/modelflow-ai/core/src/Request/AIChatRequest.php:86
    0.0496    2118504   4. ModelflowAi\Core\AIRequestHandler->handle($request = class ModelflowAi\Core\Request\AIChatRequest { protected $requestHandler = class Closure {  }; private readonly ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection ${ModelflowAi\Core\Request\AIRequest}criteria = class ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection { public readonly array $criteria = [...] }; private readonly array ${ModelflowAi\Core\Request\AIRequest}options = ['toolChoice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto')]; private readonly ModelflowAi\Core\Request\AIChatMessageCollection $messages = class ModelflowAi\Core\Request\AIChatMessageCollection { public $0 = class ModelflowAi\Core\Request\Message\AIChatMessage { ... } }; private readonly array $tools = ['get_current_weather' => [...]]; private readonly array $toolInfos = [0 => class ModelflowAi\Core\ToolInfo\ToolInfo { ... }] }) /.../vendor/modelflow-ai/core/src/AIRequestHandler.php:55
    0.0497    2118504   5. ModelflowAi\MistralAdapter\Model\MistralChatModelAdapter->handleRequest($request = class ModelflowAi\Core\Request\AIChatRequest { protected $requestHandler = class Closure {  }; private readonly ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection ${ModelflowAi\Core\Request\AIRequest}criteria = class ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection { public readonly array $criteria = [...] }; private readonly array ${ModelflowAi\Core\Request\AIRequest}options = ['toolChoice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto')]; private readonly ModelflowAi\Core\Request\AIChatMessageCollection $messages = class ModelflowAi\Core\Request\AIChatMessageCollection { public $0 = class ModelflowAi\Core\Request\Message\AIChatMessage { ... } }; private readonly array $tools = ['get_current_weather' => [...]]; private readonly array $toolInfos = [0 => class ModelflowAi\Core\ToolInfo\ToolInfo { ... }] }) /.../vendor/modelflow-ai/core/src/AIRequestHandler.php:39
    0.0566    2852432   6. ModelflowAi\MistralAdapter\Model\MistralChatModelAdapter->create($request = class ModelflowAi\Core\Request\AIChatRequest { protected $requestHandler = class Closure {  }; private readonly ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection ${ModelflowAi\Core\Request\AIRequest}criteria = class ModelflowAi\Core\Request\Criteria\AIRequestCriteriaCollection { public readonly array $criteria = [...] }; private readonly array ${ModelflowAi\Core\Request\AIRequest}options = ['toolChoice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto')]; private readonly ModelflowAi\Core\Request\AIChatMessageCollection $messages = class ModelflowAi\Core\Request\AIChatMessageCollection { public $0 = class ModelflowAi\Core\Request\Message\AIChatMessage { ... } }; private readonly array $tools = ['get_current_weather' => [...]]; private readonly array $toolInfos = [0 => class ModelflowAi\Core\ToolInfo\ToolInfo { ... }] }, $parameters = ['model' => 'mistral-large-latest', 'messages' => [0 => [...]], 'tools' => [0 => [...]], 'tool_choice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto')]) /.../vendor/modelflow-ai/mistral-adapter/src/Model/MistralChatModelAdapter.php:74
    0.0584    2877984   7. ModelflowAi\Mistral\Resources\Chat->create($parameters = ['model' => 'mistral-large-latest', 'messages' => [0 => [...]], 'tools' => [0 => [...]], 'tool_choice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto')]) /.../vendor/modelflow-ai/mistral-adapter/src/Model/MistralChatModelAdapter.php:104
    0.0584    2877984   8. ModelflowAi\Mistral\Resources\Chat->validateParameters($parameters = ['model' => 'mistral-large-latest', 'messages' => [0 => [...]], 'tools' => [0 => [...]], 'tool_choice' => enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto')]) /.../vendor/modelflow-ai/mistral/src/Resources/Chat.php:38
    0.0584    2877984   9. Webmozart\Assert\Assert::inArray($value = enum ModelflowAi\Core\ToolInfo\ToolChoiceEnum::AUTO('auto'), $values = [0 => 'auto', 1 => 'none'], $message = ???) /.../vendor/modelflow-ai/mistral/src/Resources/Chat.php:103
    0.0584    2878304  10. Webmozart\Assert\Assert::reportInvalidArgument($message = 'Expected one of: "auto", "none". Got: ModelflowAi\\Core\\ToolInfo\\ToolChoiceEnum') /.../vendor/webmozart/assert/src/Assert.php:975

As it is the same type of issue, I applied the same type of changes as seen in the following patch:

Subject: [PATCH] Test
---
Index: vendor/modelflow-ai/mistral/src/Resources/Chat.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/modelflow-ai/mistral/src/Resources/Chat.php b/vendor/modelflow-ai/mistral/src/Resources/Chat.php
--- a/vendor/modelflow-ai/mistral/src/Resources/Chat.php    (revision de63c4098b1706025af1816f8eb872af78fd048e)
+++ b/vendor/modelflow-ai/mistral/src/Resources/Chat.php    (date 1711551300955)
@@ -16,6 +16,7 @@
 use ModelflowAi\ApiClient\Resources\Concerns\Streamable;
 use ModelflowAi\ApiClient\Transport\Payload;
 use ModelflowAi\ApiClient\Transport\TransportInterface;
+use ModelflowAi\Core\ToolInfo\ToolChoiceEnum;
 use ModelflowAi\Mistral\Model;
 use ModelflowAi\Mistral\Responses\Chat\CreateResponse;
 use ModelflowAi\Mistral\Responses\Chat\CreateStreamedResponse;
@@ -98,8 +99,8 @@
             Assert::keyNotExists($parameters, 'tool_choice');
         } else {
             if (isset($parameters['tool_choice'])) {
-                Assert::string($parameters['tool_choice']);
-                Assert::inArray($parameters['tool_choice'], ['auto', 'none']);
+                Assert::isInstanceOf($parameters['tool_choice'], ToolChoiceEnum::class);
+                Assert::inArray($parameters['tool_choice'], [ToolChoiceEnum::AUTO, ToolChoiceEnum::NONE]);
             }
             if (isset($parameters['tools'])) {
                 Assert::isArray($parameters['tools']);

With this patch, everything worked with Mistral.

Am I on the same page as you, and my changes are what you are currently refactoring? Should I make a PR?

wachterjohannes commented 8 months ago

@gplanchat hey - cool that you found my library :) i have refactored lots of things in the last days 😂 but yes you can create a PR for that! seems legit

Can i ask for what you are using the library?

wachterjohannes commented 8 months ago

ah sorry i see now :) the mistral lib does not know the enum ToolChoiceEnum as it is independent of all the other packages. Just convert the toolchoice to a string in the adapter $parameters['tool_choice'] = $toolChoice->value; the rest should then work without a change

wachterjohannes commented 8 months ago

@gplanchat thanks for the report :) i have fixed it in a bigger way! have found some issues more with openai.

Now also a test is there so should not happen anymore

gplanchat commented 8 months ago

Thank you @wachterjohannes for the fix

Can i ask for what you are using the library?

I am experimenting AI features for an iPaaS solution I am building at https://github.com/php-etl

wachterjohannes commented 7 months ago

@gplanchat nice - give me a hint when you want to contact about your usecase (you can contact me on twitter)

gplanchat commented 7 months ago

@gplanchat nice - give me a hint when you want to contact about your usecase (you can contact me on twitter)

I cannot reach you through twitter. You seem to have restricted the people that can send you a DM. The other way around should work: https://twitter.com/gplanchat