-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from PRTIMES/feature/add-recommend
レコメンド機能の作成(フェーズ3)
- Loading branch information
Showing
19 changed files
with
442 additions
and
19 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
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
59 changes: 59 additions & 0 deletions
59
backend/app/Http/Controllers/Api/PressRelease/ViewController.php
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,59 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\PressRelease; | ||
|
||
use App\Exceptions\HttpJsonResponseException; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Keyword; | ||
use App\Models\User; | ||
use App\UseCases\User\FindByIdAction as UserFindByIdAction; | ||
use App\UseCases\PressRelease\FindByIdsAction; | ||
use Exception; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class ViewController extends Controller | ||
{ | ||
/** | ||
* @param Request $request | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function __invoke(Request $request) | ||
{ | ||
/* @var User $user */ | ||
$user = UserFindByIdAction::run(Auth::id()); | ||
if ($user === null) // @todo まともにExceptionする | ||
throw new Exception("test"); | ||
|
||
$company_id = $request->get("company_id"); | ||
$release_id = $request->get("release_id"); | ||
|
||
$press_release = FindByIdsAction::run(company_id: $company_id, release_id: $release_id); | ||
|
||
if (!$press_release) | ||
throw new HttpJsonResponseException( | ||
400, | ||
"notfound", | ||
"PressRelease Not Found", | ||
compact("company_id", "release_id") | ||
); | ||
|
||
/* @var Keyword $keyword */ | ||
foreach ($press_release->keywords() as $keyword) { | ||
|
||
|
||
$user->view_histories()->create([ | ||
|
||
]); | ||
|
||
|
||
|
||
|
||
} | ||
$press_release->keywords(); | ||
|
||
|
||
$press_release->keywords(); | ||
} | ||
} |
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,123 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Libs\Bedrock; | ||
use App\Libs\OpenAI; | ||
use App\UseCases\Keyword\UpdateOrCreateAction; | ||
use App\UseCases\PressRelease\CreateAction as PressReleaseCreateAction; | ||
use Exception; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use GuzzleHttp\Client as GuzzleClient; | ||
|
||
class PressReleaseAnalyze implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
const EXACT_MATCH_WEIGHT = 2; | ||
|
||
/** | ||
* @param int $company_id | ||
* @param int $release_id | ||
*/ | ||
public function __construct( | ||
private readonly int $company_id, | ||
private readonly int $release_id | ||
) | ||
{} | ||
|
||
/** | ||
* @return void | ||
* @throws GuzzleException | ||
* @throws Exception | ||
*/ | ||
public function handle(): void | ||
{ | ||
$headers = [ | ||
"Authorization" => "Bearer " . config("services.prtimes.token") | ||
]; | ||
|
||
$guzzle = new GuzzleClient([ | ||
"base_uri" => "https://hackathon.stg-prtimes.net/" | ||
]); | ||
|
||
$openai = new OpenAI(config("services.openai.token")); | ||
|
||
$bedrock = new Bedrock(); | ||
|
||
$response = $guzzle->get("/api/companies/112/releases/1078", ["headers" => $headers]); | ||
|
||
if ($response->getStatusCode() !== 200) | ||
throw new Exception("PR TIMES API Error"); | ||
|
||
/* @var array{ | ||
* body: string, | ||
* company_id: int, | ||
* company_name: string, | ||
* created_at: string, | ||
* lead_paragraph: string, | ||
* like: int, | ||
* main_category_id: int, | ||
* main_category_name: string, | ||
* main_image: string, | ||
* main_image_fastly: string, | ||
* release_id: int, | ||
* release_type: string, | ||
* sub_category_id: string, | ||
* sub_category_name: string, | ||
* subtitle: string, | ||
* title: string, | ||
* url: string | ||
* } $data | ||
*/ | ||
$data = json_decode($response->getBody()->getContents(), true); | ||
|
||
$body = strip_tags($data["body"]); | ||
|
||
$summary = $openai->answer( | ||
question: $body, | ||
background: "あなたは、プレスリリースの内容を元にして、若者に興味を持ってもらえる文章を生成するエージェントです。\n | ||
以下の条件を満たしてください。\n | ||
1. 生成した文章は100文字以内\n | ||
2. 絵文字は用いない\n | ||
3. 内容に基づく。感想を含めない。" | ||
); | ||
|
||
$press_release = PressReleaseCreateAction::run( | ||
company_id: $this->company_id, | ||
release_id: $this->release_id, | ||
title: $data["title"], | ||
summary: $summary, | ||
release_created_at: $data["created_at"] | ||
); | ||
|
||
$keywords_string = $bedrock->answer( | ||
question: "以下の文章から重要なキーワードを抜き出せ \n | ||
指示 \n | ||
名詞や固有名詞のみ出力すること \n | ||
年月日は重要ではないので出力しないこと \n | ||
以下のようなJSONの配列形式で出力すること \n | ||
[\"キーワード\", \"キーワード\", ...] \n | ||
JSONデータ以外一切出力しないこと \n\n" . $body | ||
); | ||
$keywords = json_decode($keywords_string) ?? []; | ||
|
||
foreach ($keywords as $keyword) { | ||
|
||
$keyword = UpdateOrCreateAction::run($keyword); | ||
|
||
$press_release->keywords() | ||
->syncWithPivotValues( | ||
$keyword->id, | ||
[ | ||
"weight" => self::EXACT_MATCH_WEIGHT | ||
], | ||
false | ||
); | ||
} | ||
|
||
// @todo さらに形態素解析をかまして、低い重みでキーワードを登録する。 | ||
} | ||
} |
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
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
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,33 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class ViewHistory extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
"score", | ||
"user_id", "keyword_id" | ||
]; | ||
|
||
/** | ||
* @return BelongsTo | ||
*/ | ||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
/** | ||
* @return BelongsTo | ||
*/ | ||
public function keyword(): BelongsTo | ||
{ | ||
return $this->belongsTo(Keyword::class); | ||
} | ||
} |
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
Oops, something went wrong.