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