File size: 4,520 Bytes
7dd45ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from textwrap import dedent

import openai


def call_api(messages):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=messages,
        temperature=0,
        max_tokens=64,
    )
    return response["choices"][0]["message"]["content"]


def extract_selling_points(description):
    # TODO: gradio で編集可能にしたい
    prompt = "Please list selling points from the following ad text."
    shots = [
        {
            "role": "user",
            "content": (
                "仙台のホテルや旅館をお探しなら楽天トラベルへ!"
                "楽天ポイントが使えて、貯まって、とってもお得な宿泊予約サイトです。"
                "さらに割引クーポンも使える!国内ツアー・航空券・レンタカー・バス予約も!"
            ),
        },
        {"role": "assistant", "content": "楽天P貯まる, 割引, 交通予約"},
        {
            "role": "user",
            "content": (
                "【PCやスマートデバイス(スマホ、タブレット等)の管理をご検討の方へ】NRIが提供する「PCLifecycycleSuite」なら、"
                "端末運用業務の改善とアウトソース、モバイル管理クラウドの提供まで一元的にサポートします。"
            ),
        },
        {"role": "assistant", "content": "端末運用業務の改善, モバイル管理クラウドの提供, 一元的なサポート"},
    ]
    messages = [
        {"role": "system", "content": prompt},
        *shots,
        {"role": "user", "content": description},
    ]
    return call_api(messages)


def generate_title(selling_points, query, description):
    # TODO: gradio で編集可能にしたい
    prompt = (
        "ユーザがクリックしたくなるような魅力的なページタイトルを考えてください。"
        "'selling_points'で列挙されたセールスポイントを強調してください。"
        "'query'(ユーザが知りたい情報のキーワード)と'description'(広告ページの文章)も参考にしてください。"
    )
    template = dedent(
        """
            ===
            selling_points: {selling_points}
            query: {query}
            description: {description}
            output: 
            """  # noqa
    )[1:-1]
    shots = [
        {
            "role": "user",
            "content": template.format(
                selling_points="業界初の新発毛プラン, 毎月の無料血液検査, 患者様の症状・体調に合わせた最適な治療の提供",
                query="薄毛 女性 クリニック",
                description=(
                    "業界初!第4世代の新発毛プランをご用意。毎月の無料血液検査で患者様の症状・体調に合わせ、最適な治療をご提供します。"
                ),
            ),
        },
        {"role": "assistant", "content": "女性の薄毛にお悩みですか?最適な治療をご提供します!"},
        {
            "role": "user",
            "content": template.format(
                selling_points="端末運用業務の改善, アウトソース, モバイル管理クラウドの提供, 一元的なサポート",
                query="クラウドサービス セキュリティ 対策",
                description=(
                    "【PCやスマートデバイス(スマホ、タブレット等)の管理をご検討の方へ】"
                    "NRIが提供する「PCLifecycycleSuite」なら、"
                    "端末運用業務の改善とアウトソース、モバイル管理クラウドの提供まで一元的にサポートします。"
                ),
            ),
        },
        {"role": "assistant", "content": "セキュリティ対策どうしてますか"},
    ]

    messages = [
        {"role": "system", "content": prompt},
        *shots,
        {
            "role": "user",
            "content": template.format(
                selling_points=selling_points, query=query, description=description
            ),
        },
    ]
    return call_api(messages)


def main(query, description):
    query = query[:32]
    description = description[:200]
    if description == "":
        return "", ""

    selling_points = extract_selling_points(description)
    title = generate_title(selling_points, query, description)
    return selling_points, title