-
-
Notifications
You must be signed in to change notification settings - Fork 44
[Platform] Enhance in memory platform to support all ResultInterface
types
#252
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
[Platform] Enhance in memory platform to support all ResultInterface
types
#252
Conversation
a8c3a8f
to
a5e6fa0
Compare
public function invoke(Model $model, array|string|object $input, array $options = []): ResultPromise | ||
{ | ||
$resultText = $this->mockResult instanceof \Closure | ||
? ($this->mockResult)($model, $input, $options) | ||
: $this->mockResult; | ||
if ($this->mockResult instanceof \Closure) { | ||
$result = ($this->mockResult)($model, $input, $options); | ||
|
||
if ($result instanceof ResultInterface) { | ||
return new ResultPromise( | ||
static fn () => $result, | ||
rawResult: $result->getRawResult() ?? new InMemoryRawResult( | ||
['text' => $result->getContent()], | ||
), | ||
options: $options | ||
); | ||
} | ||
|
||
$resultText = (string) $result; | ||
} else { | ||
$resultText = $this->mockResult; | ||
} | ||
|
||
$textResult = new TextResult($resultText); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense, yes, but would like to see the code less complex please.
For example this could also work, right?
public function invoke(Model $model, array|string|object $input, array $options = []): ResultPromise
{
$result = is_string($this->mockResult) ? $this->mockResult : ($this->mockResult)($model, $input, $options);
if ($result instanceof ResultInterface) {
return $this->createPromise($result, $options);
}
return $this->createPromise(new TextResult($result), $options);
}
private function createPromise(ResultInterface $result, array $options): ResultPromise
{
$rawResult = $result->getRawResult() ?? new InMemoryRawResult(
['text' => $result->getContent()],
(object) ['text' => $result->getContent()],
);
return new ResultPromise(static fn () => $result, $rawResult, $options);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, that is simpler and less complex
ResultInterface
typesResultInterface
types
ResultInterface
typesResultInterface
types
d00b887
to
82e873f
Compare
Thank you @RamyHakam. |
Enhances the existing
InMemoryPlatform
to support allResultInterface
types, making it easier to test all result types.Previously limited to text responses, it now supports vectors, images, binary data, choices, and any custom result types.