/** * WPML compatibility functions * * @global array $duplicated_posts Array to store the posts being duplicated. * * @package Yoast\WP\Duplicate_Post * @since 3.2 */ add_action( 'admin_init', 'duplicate_post_wpml_init' ); /** * Add handlers for WPML compatibility. */ function duplicate_post_wpml_init() { if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { add_action( 'dp_duplicate_page', 'duplicate_post_wpml_copy_translations', 10, 3 ); add_action( 'dp_duplicate_post', 'duplicate_post_wpml_copy_translations', 10, 3 ); add_action( 'shutdown', 'duplicate_wpml_string_packages', 11 ); } } global $duplicated_posts; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: Renaming a global variable is a BC break. $duplicated_posts = []; /** * Copy post translations. * * @global SitePress $sitepress Instance of the Main WPML class. * @global array $duplicated_posts Array of duplicated posts. * * @param int $post_id ID of the copy. * @param WP_Post $post Original post object. * @param string $status Status of the new post. */ function duplicate_post_wpml_copy_translations( $post_id, $post, $status = '' ) { global $sitepress; global $duplicated_posts; remove_action( 'dp_duplicate_page', 'duplicate_post_wpml_copy_translations', 10 ); remove_action( 'dp_duplicate_post', 'duplicate_post_wpml_copy_translations', 10 ); $current_language = $sitepress->get_current_language(); $trid = $sitepress->get_element_trid( $post->ID ); if ( ! empty( $trid ) ) { $translations = $sitepress->get_element_translations( $trid ); $new_trid = $sitepress->get_element_trid( $post_id ); foreach ( $translations as $code => $details ) { if ( $code !== $current_language ) { if ( $details->element_id ) { $translation = get_post( $details->element_id ); if ( ! $translation ) { continue; } $new_post_id = duplicate_post_create_duplicate( $translation, $status ); if ( ! is_wp_error( $new_post_id ) ) { $sitepress->set_element_language_details( $new_post_id, 'post_' . $translation->post_type, $new_trid, $code, $current_language ); } } } } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: see above. $duplicated_posts[ $post->ID ] = $post_id; } } /** * Duplicate string packages. * * @global array() $duplicated_posts Array of duplicated posts. */ function duplicate_wpml_string_packages() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: renaming the function would be a BC-break. global $duplicated_posts; foreach ( $duplicated_posts as $original_post_id => $duplicate_post_id ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: using WPML native filter. $original_string_packages = apply_filters( 'wpml_st_get_post_string_packages', false, $original_post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: using WPML native filter. $new_string_packages = apply_filters( 'wpml_st_get_post_string_packages', false, $duplicate_post_id ); if ( is_array( $original_string_packages ) ) { foreach ( $original_string_packages as $original_string_package ) { $translated_original_strings = $original_string_package->get_translated_strings( [] ); foreach ( $new_string_packages as $new_string_package ) { $cache = new WPML_WP_Cache( 'WPML_Package' ); $cache->flush_group_cache(); $new_strings = $new_string_package->get_package_strings(); foreach ( $new_strings as $new_string ) { if ( isset( $translated_original_strings[ $new_string->name ] ) ) { foreach ( $translated_original_strings[ $new_string->name ] as $language => $translated_string ) { do_action( // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Reason: using WPML native filter. 'wpml_add_string_translation', $new_string->id, $language, $translated_string['value'], $translated_string['status'] ); } } } } } } } } プロンプトで精度を上げる方法 – Raqqa

プロンプトで精度を上げる方法

いまのSQLが60点になっている主因はだいたいこの3つです:

  • 件数が二重計上になり得る:明細JOINしてるのに COUNT(so.id)(→本当は COUNT(DISTINCT so.id) が安全)
  • LIMITがズレる:直近12ヶ月なのに LIMIT 200
  • 並び順がズレる:月次は普通 DESC で “直近が上” が期待されがち(あなたも上位12ヶ月と言ってる)

なので、プロンプトで「月次の定型」「ヘッダ×明細の数え方」「LIMIT/ORDER」を“規約”にしてしまうのが一番効きます。


追加するプロンプト(そのまま貼れる)

llm_sql.py の system(もしくは developer)メッセージに、既存ルールの下へ追記してください。

① まず汎用ルール(全ドメイン共通で効く)

  • Use only the tables in scope_tables when provided or inferred. Do not invent other tables.
  • Use only columns that exist in allowed_fields (or can be derived from them). Do not guess columns.
  • If a column name contains / or special characters, you MUST double-quote it.
    Example: sol."order_id/id"
  • For month time series:
    • month := date_trunc('month', <date_column>)
    • GROUP BY 1
    • Default sort: ORDER BY 1 DESC
  • If the user says “直近12ヶ月 / 上位12ヶ月” then LIMIT 12 (not 200).

② ヘッダ×明細の“数え方”ルール(今回の30点)

  • If joining a header table and a line table (1-to-many) and the user asks for order count / header count:
    • Use COUNT(DISTINCT <header_pk>) to avoid double counting.
    • Example: COUNT(DISTINCT so.id) AS order_count
  • If the user asks for line count / 明細件数:
    • Use COUNT(*) (or COUNT(sol.id)).

③ SAPB1のドメインルール(短くて強い)

  • sapb1_sales_orders is header (1 row per order, pk id).
  • sapb1_sales_order_lines is lines (many rows per order).
  • Join condition: so.id = sol."order_id/id".
  • Total amount should be SUM(sol.line_total).

これを入れるだけで、弱い質問でも「amount_total を生やす」「COUNTが爆増」「LIMIT 200」みたいなブレがかなり減ります。


期待される“満点”SQL(あなたの例の質問)

この質問:

「受注データで、月別の受注件数と合計金額を出して。直近12ヶ月で。」

なら、プロンプトが効くと最終形はこう寄せられます:

SELECT
  DATE_TRUNC('month', so.posting_date) AS month,
  COUNT(DISTINCT so.id) AS order_count,
  COALESCE(SUM(sol.line_total), 0) AS total_amount
FROM sapb1_sales_orders so
LEFT JOIN sapb1_sales_order_lines sol
  ON so.id = sol."order_id/id"
WHERE so.posting_date >= CURRENT_DATE - INTERVAL '12 months'
GROUP BY 1
ORDER BY 1 DESC
LIMIT 12

ポイントは COUNT(DISTINCT)LIMIT 12ORDER BY DESC です。


実装の置き場所(最短で効く場所)

join_planner.py ではなく、LLMに渡す最終プロンプトを作ってる場所です。

  • /analytics/querygenerate_sql()app/services/analytics/llm_sql.py
    ここに “上の規約” を入れるのが一番効きます。

さらに精度を上げるなら:

  • allowed_models(sample)sapb1.sales.order* を含む場合だけ
    “SAPB1ルール” を追記する(=条件付きプロンプト)

追加で1つだけやるなら(超効く)

プロンプトだけでも良いですが、最後の保険として SQL後処理を1個だけ入れると安定します:

  • 明細JOINが存在して、order_countCOUNT(so.id) になっていたら
    COUNT(DISTINCT so.id) に置換(安全側)

これは「LLMがうっかり」を潰す保険になります。


この方針でいくなら、次にこちらから **llm_sql.pyの “messages組み立て部分” を前提に、どこにどう追記するか(パッチ形式)**で書けます。
いまの llm_sql.py を貼ってくれれば、そこに合わせて最短の差分を出します。


Comments

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です