Download the PHP package s2j/similarity-service without Composer

On this page you can find all versions of the php package s2j/similarity-service. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package similarity-service

S2J Similarity Service

License: GPL v3 PHP Composer PHPUnit

Description

本『S2J Similarity Service』は、任意の言語における文章 A と文章 B 間の「意味的な類似度」を数値化して返却する、純粋な PHP ライブラリです。WordPress プラグイン開発等において利用可能な Composer パッケージとして提供されています。

このライブラリは、OpenAI Embeddings API (text-embedding-3-small) を用いて意味的な類似度を判定しています。

Strategy パターンを採用した EmbeddingStrategyInterface により、将来、text-embedding-3-large や、他ベンダーモデル (Claude、Gemini 等) に差し替え可能な設計となっています。

特徴

🎯 コア機能

🛠️ 技術的特徴

🔧 拡張性

License

このプロジェクトは GPL v3以降の下でライセンスされています - 詳細は LICENSE ファイルを参照してください。

Support and Contact

サポート、機能リクエスト、またはバグ報告については、GitHub Issues ページをご覧ください。


Installation

前提条件

Composer 経由でのインストール

プラグイン/テーマ側での利用例:

プラグイン/テーマのメインファイルでの読み込み

Usage

基本的な使用例

OpenAIEmbeddingStrategy オプション

OpenAIEmbeddingStrategy のコンストラクタは、第1引数 apiKey のほか、任意で defaultModel / endpoint / timeoutSeconds を名前付き引数で渡せます (内部は cURL 1本で、HTTP クライアントの差し替え API はありません)。

互換 API やプロキシを使う場合は、OpenAI 互換の Embeddings エンドポイント URL を endpoint に指定します。

主なオプション:

その他の挙動・例外・応用例は docs/interfaces/usage_spec.md を参照してください。

Embedding のみを生成する (EmbeddingService)

類似度計算ではなく、Embedding ベクトル (および model/provider/dimension メタデータ) を取得したい場合は EmbeddingService を使用します。

キャッシュを有効化する例 (Decorator)

バッチ計算

パラメータ

パラメータ 型 説明
a string テキスト A
b string テキスト B
model string | null モデル名 (例: text-embedding-3-small)。未指定時は Strategy の default

API キー管理

戻り値

REST API (HTTP ランタイム/ WordPress REST Adapter)

HTTP 経由で利用する場合、本ライブラリは、独自の HTTP サーバーを持たず、WordPress Core の REST API (register_rest_route) を HTTP ランタイムとして用います。契約の詳細・責務境界は REST API 仕様 の「REST API (HTTP ランタイム/ WordPress REST Adapter)」を参照してください。

OpenAPI と実エンドポイントの対応

契約の source of truth は、schema/openapi.yaml です。

論理 API (OpenAPI) WordPress ランタイム URL (例)
POST /v1/similarity https://<サイト>/wp-json/s2j/v1/similarity
POST /v1/embedding https://<サイト>/wp-json/s2j/v1/embedding

パーマリンク設定により URL が変わる場合は、下記の形式になることがあります (いずれも WordPress の標準挙動です)。

プラグイン内では、rest_url('s2j/v1/similarity') 等で実 URL を取得できます。

プラグイン / テーマへの組み込み

rest_api_init でルートを登録し、SimilarityController / EmbeddingController にアプリケーションサービスと BearerTokenAuth を注入します。

REST 呼び出し例 (curl)

類似度 (POST /v1/similarity に相当):

Embedding (POST /v1/embedding に相当):

TypeScript SDK (@s2j/similarity-client)

OpenAPI 契約に沿った 公式の TypeScript ラッパー です。生成クライアント (tools/generated/ts) は公開せず、タイムアウト・軽量リトライ・ REST エラー正規化などをまとめた、安定した API のみを公開します。設計の詳細は SDK 仕様 の「TypeScript SDK (@s2j/similarity-client)」を参照してください。

モノレポ内の場所

項目 値
ディレクトリ packages/ts-client
npm パッケージ名 @s2j/similarity-client
ルートの workspaces package.json の workspaces

別プロジェクトから依存させる例

本リポジトリをローカルにクローン済みであれば、package.json にパス依存を追加します。

パスは、プロジェクト配置に合わせて調整してください。ビルド済みの dist/ が必要な場合は、依存先で下記のビルドを実行します。

createApiClient の最小例

クライアントは、内部で POST /v1/similarity および POST /v1/embedding を呼びます。WordPress REST アダプタと組み合わせる場合、baseUrl には サイトのオリジンから /wp-json/s2j まで (末尾スラッシュなし) を指定してください。これにより baseUrl + /v1/similarity が「上記 REST 節の表」と同じ実行時 URL (例: https://example.com/wp-json/s2j/v1/similarity) になります。

Bearer を使わない開発環境では apiKey を省略できます (サーバー側の BearerTokenAuth 設定に依存)。

ビルドと codegen の検証 (開発者向け)

リポジトリルートで Node.js v20以上を用意し、依存関係を入れたうえで下記を実行します。

verify:codegen は、OpenAPI からコード生成したあと、リポジトリに差分が残らないことを確認します (schema/openapi.yaml が契約の起点)。

より詳しい HTTP 利用の整理は 使用方法 の TypeScript SDK 節も参照してください。

FAQ

Q: このライブラリは、WordPress プラグイン以外でも使用できますか ?

A: はい、このライブラリは純粋な PHP ライブラリとして実装されているため、WordPress 以外のプロジェクトでも使用できます。

Q: OpenAI API 以外の Embedding API を使用できますか ?

A: 将来的には、Strategy パターンにより他ベンダーの API (Claude、Gemini 等) にも対応予定です。現在は OpenAIEmbeddingStrategy のみが実装されています。

Q: どのモデルを使用すべきですか ?

A: 通常利用では text-embedding-3-small を推奨します。精度重視の場合や多言語間 (特に低リソース言語) の類似度評価では text-embedding-3-large も検討してください。

Q: 必要な PHP のバージョンは ?

A: PHP v8.3以降が必要です。Composer に対応した環境が必要です。WordPress の 必須要件 で推奨される PHP v8.3以上に合わせています。

Q: 外部依存の関係はありますか ?

A: 外部依存はありません。cURL は PHP 標準機能を使用します。


Development

技術スタック

モデル選定方針

モデル名 用途 コメント
text-embedding-3-small 通常利用 「意味的な類似度」の判定、コスト効率に優れる
text-embedding-3-large 精度重視 研究・学習データの類似検索等に向く

プロジェクト構造

開発環境のセットアップ

Testing

このライブラリでは、下記の2種類のテスト手法を提供しています。

テスト手法の概要

  1. PHPUnit によるユニットテスト: phpunit.xml と tests/SimilarityTest.php を使用した自動テスト
  2. CLI による手動テスト: examples/test_similarity.php を使用した対話的なテスト

API キーの取得と設定

テストを実行する前に、OpenAI API キーを取得し、環境変数として設定する必要があります。

API キーの取得方法

  1. OpenAI Platform にアクセスし、アカウントにログインします。
  2. API Keys ページに移動します。
  3. 「Create new secret key」ボタンをクリックして新しい API キーを作成します。
  4. 作成された API キーをコピーします (このキーは一度しか表示されないため、必ず保存してください)。

環境変数の設定方法

macOS / Linux の場合

現在のセッションで一時的に設定する場合:

永続的に設定する場合 (.zshrc または .bashrc に追加):

Windows の場合

コマンドプロンプト (一時的):

PowerShell (一時的):

永続的に設定する場合は、「システムのプロパティ → 詳細設定 → 環境変数…」で、システム環境変数を設定してください。

PHPUnit によるユニットテスト

PHPUnit を使用した自動テストを実行します。このテストは、tests/SimilarityTest.php で定義されたテストケースを実行し、SimilarityService の動作を検証します。

依存関係のインストール

PHPUnit によるユニットテストの実行

テストが成功すると、下記のような結果が表示されます:

注意: OPENAI_API_KEY 環境変数が設定されていない場合、テストはスキップされます。

テストファイルの構成

CLI による手動テスト

CLI を使用した対話的なテストを実行します。この方法では、実際の API をコールして結果を確認できます。

CLI による手動テストの実行

環境変数 OPENAI_API_KEY を設定してから実行します:

実行すると、下記のような出力が表示されます:

テストファイルの構成

ドキュメント Lint

README および docs/** の品質チェックには @s2j/docs-linter を使用します。

CI と同一ルールをローカルで実行できます。

Contributing

貢献をお待ちしています ! 下記の手順に従ってください:

  1. リポジトリをフォークしてください。
  2. 機能ブランチを作成してください (git checkout -b feature/amazing-feature)。
  3. 変更をコミットしてください (git commit -m 'Add some amazing feature')。
  4. 機能ブランチにプッシュしてください (git push origin feature/amazing-feature)。
  5. Pull Request を開いてください。

詳細な情報については、docs/SPEC.md ファイルを参照してください。

開発ガイドライン

Contributors & Developers

"S2J Similarity Service" はオープンソース・ソフトウェアです。下記の皆様がこのライブラリに貢献しています。


Changelog

v1.0.0

Upgrade Notice

1.0.0

S2J Similarity Service の初回リリース。このバージョンには、意味的な類似度判定のための全コア機能が含まれています。


All versions of similarity-service with dependencies

PHP Build Version
Package Version
Requires php Version >=8.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package s2j/similarity-service contains the following files

Loading the files please wait ...