Skip to content
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

DPIB-496: AI Feature integrations. #1

Merged
merged 30 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3cfeeb4
Init composer.json.
dmiseev Sep 4, 2024
9aec9d0
Implemented SprykerEco/ImageSearchAi.
bohdanyevtukhov Sep 6, 2024
50a0650
Normalize ImageSearchAiWidget
supproduction Sep 7, 2024
2d58b12
Fixed code review comments.
bohdanyevtukhov Sep 9, 2024
9823d08
Fixed code review comments.
bohdanyevtukhov Sep 9, 2024
61f7582
Merge branch 'feature/demo/dev-ai-integrations' into feautre/ai-integ…
supproduction Sep 9, 2024
30ec25c
Fixed CSRF token.
bohdanyevtukhov Sep 9, 2024
e943a39
Fixed code style.
bohdanyevtukhov Sep 9, 2024
2f47709
Merge branch 'feature/demo/dev-ai-integrations' into feautre/ai-integ…
supproduction Sep 9, 2024
1d1f7e5
Merge pull request #2 from spryker-eco/feautre/ai-integrations-search…
supproduction Sep 9, 2024
5a59a0c
Fixed transfer quotes.
bohdanyevtukhov Sep 9, 2024
0fb58ed
Adjusted image search ai module.
dmiseev Sep 10, 2024
c75787f
Added CI.
dmiseev Sep 10, 2024
08dc66a
Adjusted CI.
dmiseev Sep 10, 2024
34f1d08
Adjusted CI.
dmiseev Sep 10, 2024
b66a878
add handling empty result
supproduction Sep 10, 2024
b69a1a5
Merge branch 'feature/demo/dev-ai-integrations' of github.com:spryker…
supproduction Sep 10, 2024
1137ae4
Added ImageSearchAiValidator.php.
bohdanyevtukhov Sep 11, 2024
cf8952b
add error state
supproduction Sep 11, 2024
d52cec1
Fixed CI.
bohdanyevtukhov Sep 11, 2024
ac54cf2
Fixed CI.
bohdanyevtukhov Sep 11, 2024
f167e61
Fixed CI issues.
dmiseev Sep 11, 2024
2d1c323
Fixed CI issues.
dmiseev Sep 11, 2024
bad7dee
fix image
supproduction Sep 11, 2024
b87c440
DPIB-496: Adjusted composer files [skip ci]
dmiseev Sep 11, 2024
9dcc9c5
DPIB-496: Adjusted composer files.
dmiseev Sep 11, 2024
bb2a833
added open ai success check
mantasmuliar Sep 11, 2024
7f6c6f1
Updated composer.json.
bohdanyevtukhov Sep 12, 2024
6acd785
Updated composer.json.
bohdanyevtukhov Sep 12, 2024
3307aa3
Fixed CI validation.
bohdanyevtukhov Sep 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at https://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.bat]
end_of_line = crlf
31 changes: 31 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* eol=lf
* text=auto

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary
*.jpeg binary
*.zip binary
*.phar binary
*.ttf binary
*.woff binary
*.woff2 binary
*.eot binary
*.ico binary
*.mo binary
*.pdf binary
*.xsd binary
*.ts binary
*.exe binary

# Remove files for archives generated using `git archive`
dependency.json export-ignore
.coveralls.yml export-ignore
.travis.yml export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
architecture-baseline.json export-ignore
psalm-report.json export-ignore linguist-generated=true
58 changes: 58 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: CI

on:
push:
branches:
- 'master'
pull_request:
workflow_dispatch:

jobs:
validation:
name: Validation
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: mbstring, intl, bcmath
coverage: none

- name: Composer Install
run: composer install --prefer-dist --no-interaction --profile

- name: Run validation
run: composer validate

- name: Syntax check
run: find ./src -path src -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 -P4 php -l -n | (! grep -v "No syntax errors detected" )

- name: Run CodeStyle checks
run: composer cs-check

- name: Run PHPStan
run: composer stan

lowest:
name: Prefer Lowest
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: mbstring, intl, bcmath
coverage: none

- name: Composer Install
run: composer install --prefer-dist --no-interaction --profile

- name: Composer Update
run: composer update --prefer-lowest --prefer-dist --no-interaction --profile -vvv
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# IDEs
/.idea
/.project
/nbproject
/.buildpath
/.settings
*.sublime-*
*.AppleDouble
*.AppleDB
*.AppleDesktop

# built client resources
/src/*/Zed/*/Static/Public
/src/*/Zed/*/Static/Assets/sprite

# propel classes
/src/*/Zed/*/Persistence/Propel/Base/*
/src/*/Zed/*/Persistence/Propel/Map/*

# OS
.DS_Store

# grunt stuff
.grunt
.sass-cache
/node_modules/
/tests/_output/*
!/tests/_output/.gitkeep
vendor
composer.lock
4 changes: 4 additions & 0 deletions .license
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ImageSearchAi Changelog

[Release Changelog](https://github.com/spryker-eco/image-search-ai/releases)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016-present Spryker Systems GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# image-search-ai
# ImageSearchAi Module

ImageSearchAi module provides the functionality to search by image using AI.

## Installation

```
composer require spryker-eco/image-search-ai
```
1 change: 1 addition & 0 deletions architecture-baseline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
48 changes: 48 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "spryker-eco/image-search-ai",
"type": "library",
"description": "ImageSearchAi module",
"license": "MIT",
"require": {
"php": ">=8.1",
"spryker-eco/open-ai": "^0.1.0",
"spryker/catalog": "^5.0.0",
"spryker/kernel": "^3.73.0",
"spryker/symfony": "^3.0.0",
"spryker/transfer": "^3.27.0",
"spryker/util-encoding": "^2.0.0",
"spryker/util-text": "^1.1.0"
},
"require-dev": {
"phpstan/phpstan": "*",
"spryker/code-sniffer": "*",
"spryker/router": "*"
},
"suggest": {
"spryker/router": "Use this module when you want to use the Router."
},
"autoload": {
"psr-4": {
"SprykerEco\\": "src/SprykerEco/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"cs-check": "phpcs -p -s --standard=vendor/spryker/code-sniffer/SprykerStrict/ruleset.xml src/",
"cs-fix": "phpcbf -p --standard=vendor/spryker/code-sniffer/SprykerStrict/ruleset.xml src/",
"stan": "phpstan analyse -c phpstan.neon -l 8 src/"
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"php-http/discovery": false
}
}
}
5 changes: 5 additions & 0 deletions dependency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": {
"spryker/transfer": "Provides transfer objects definition with strict types."
}
}
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: 8
paths:
- src/
ignoreErrors:
- '#Call to method .+ on an unknown class .+Transfer#'
- '#Instantiated class .+Transfer not found#'
- '#Method .+ has invalid return type .+Transfer#'
- '#Parameter .+ has invalid type .+Transfer#'
- '#Call .+ on an unknown class .+AutoCompletion.#'
5 changes: 5 additions & 0 deletions psalm-report.json

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<transfers xmlns="spryker:transfer-01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="spryker:transfer-01 http://static.spryker.com/transfer-01.xsd">

<transfer name="OpenAiChatRequest" strict="true">
<property name="promptData" type="array" singular="promptData"/>
<property name="model" type="string"/>
</transfer>

<transfer name="OpenAiChatResponse" strict="true">
<property name="message" type="string"/>
<property name="isSuccessful" type="bool"/>
</transfer>
dmiseev marked this conversation as resolved.
Show resolved Hide resolved

</transfers>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Yves\ImageSearchAi\Controller;

use Spryker\Service\UtilText\Model\Url\Url;
use Spryker\Yves\Kernel\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @method \SprykerEco\Yves\ImageSearchAi\ImageSearchAiFactory getFactory()
*/
class ImageSearchController extends AbstractController
{
/**
* @var string
*/
protected const REQUEST_BODY_CONTENT_KEY_IMAGE = 'image';

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function findTermsAction(Request $request): JsonResponse
dmiseev marked this conversation as resolved.
Show resolved Hide resolved
{
$requestContent = (string)$request->getContent();

if (!$requestContent) {
return $this->createAjaxErrorResponse();
}

$requestBodyContent = $this->getFactory()
->getUtilEncodingService()
->decodeJson($requestContent, true);

if (!is_array($requestBodyContent)) {
return $this->createAjaxErrorResponse();
}

$isRequestValid = $this->getFactory()
->createImageSearchAiValidator()
->validate($requestBodyContent);

if (!$isRequestValid) {
return $this->createAjaxErrorResponse();
}
dmiseev marked this conversation as resolved.
Show resolved Hide resolved

$openAiChatResponseTransfer = $this->getFactory()->createImageToSearchTermsTransformer()->transform(
$requestBodyContent[static::REQUEST_BODY_CONTENT_KEY_IMAGE],
);

if (!$openAiChatResponseTransfer->getIsSuccessful() || !$openAiChatResponseTransfer->getMessage()) {
return $this->jsonResponse();
}

$searchResults = $this
->getFactory()
->getCatalogClient()
->catalogSuggestSearch($openAiChatResponseTransfer->getMessage());

return new JsonResponse([
'success' => true,
'firstMatchProductUrl' => !empty($searchResults['suggestionByType']['product_abstract'][0]['url'])
? Url::generate($searchResults['suggestionByType']['product_abstract'][0]['url'])->build()
: '',
]);
}

/**
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
protected function createAjaxErrorResponse(): JsonResponse
{
return $this->jsonResponse(['success' => false], Response::HTTP_BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Yves\ImageSearchAi\Dependency\Client;

class ImageSearchAiToCatalogClientBridge implements ImageSearchAiToCatalogClientInterface
{
/**
* @var \Spryker\Client\Catalog\CatalogClientInterface
*/
protected $catalogClient;

/**
* @param \Spryker\Client\Catalog\CatalogClientInterface $catalogClient
*/
public function __construct($catalogClient)
{
$this->catalogClient = $catalogClient;
}

/**
* @param string $searchString
* @param array<string, mixed> $requestParameters
*
* @return array<string, mixed>
*/
public function catalogSuggestSearch(string $searchString, array $requestParameters = []): array
{
return $this->catalogClient->catalogSuggestSearch($searchString, $requestParameters);
}
}
Loading
Loading