| import openai |
| import os |
| import time |
| from concurrent.futures import ThreadPoolExecutor |
|
|
| from openai import OpenAI |
| client = OpenAI( |
| api_key= os.environ["gptkey"] |
| ) |
| import json |
|
|
| def format_customer_journey(contents): |
| formatted_text = "" |
| for stage, details in contents.items(): |
| |
| formatted_text += f"【 {stage} 】\n\n" |
| formatted_text += f"#TA小故事#\n{details['TA小故事']}\n\n" |
| formatted_text += f"#接觸點#\n{details['接觸點']}\n\n" |
| formatted_text += f"#痛點#\n{details['痛點']}\n\n" |
| formatted_text += f"#痛點解決方案#\n{details['痛點解決方案']}\n\n" |
| formatted_text += f"#TA心情評分#\n{details['TA心情評分']}\n\n" |
| formatted_text += "-----------------------------\n\n" |
| return formatted_text |
|
|
|
|
| def generate_cj(brand_name, brand_info, product_info, cjstages_count, target_persona, target_ccs, target_info): |
| with ThreadPoolExecutor(max_workers=3) as executor: |
| fun_1 = executor.submit(generate_cj_stages,brand_name, brand_info, product_info, cjstages_count) |
| cj_stages_str = fun_1.result() |
| cj_stages = json.loads(cj_stages_str) |
| |
| fun_2 = executor.submit(generate_cj_contents,brand_name, brand_info, product_info, cjstages_count, target_persona, target_ccs, target_info,cj_stages["stages"]) |
| cj_contents_str = fun_2.result() |
| cj_contents = json.loads(cj_contents_str) |
|
|
| |
| return format_customer_journey(cj_contents["contents"]) |
|
|
|
|
| |
| def generate_cj_stages(brand_name, brand_info, product_info, cjstages_count): |
| print(cjstages_count) |
| messages_base = [ |
| {"role": "system", "content": "你是一個專業的UX設計師,擅長Customer Journey Map的規劃。"}, |
| {"role": "user", "content": f"本次要規劃的品牌名稱如下 品牌名稱:{brand_name},品牌資訊:{brand_info},產品資訊:{product_info}"} |
| ] |
|
|
| cj_stages_schema ={ |
| "name": "customer_journey_stages", |
| "description": f"請根據這個品牌的消費者消費行為,幫我列出Customer Journey Map的{cjstages_count}個階段,只要給我{cjstages_count}個階段的中文標題就好。", |
| "parameters": { |
| "type": "object", |
| "properties": { |
| "stages": { |
| "type": "array", |
| "items": { |
| "type": "string", |
| "enum": [] |
| }, |
| "description": f"這{cjstages_count}個階段的標題,每個階段的標題都以'階段'兩個字作為結尾。" |
| } |
| },"required": [ |
| "stages"] |
| } |
| } |
| |
| response = client.chat.completions.create( |
| model='gpt-4o', |
| messages= messages_base, |
| functions=[ |
| cj_stages_schema |
| ], |
| function_call={"name": "customer_journey_stages"} |
| ) |
| arguments_str = response.choices[0].message.function_call.arguments |
| return arguments_str |
|
|
| def generate_cj_contents_schema(cj_stages): |
| stage_template = { |
| "type": "object", |
| "properties": { |
| "TA小故事": { |
| "type": "string", |
| "description": "TA在這個階段發生的情境故事(150個中文字以上)" |
| }, |
| "接觸點": { |
| "type": "string", |
| "description": "品牌與TA的接觸點(3~5點)" |
| }, |
| "痛點": { |
| "type": "string", |
| "description": "TA在這個階段遭遇的痛點(30個中文字以上)" |
| }, |
| "痛點解決方案": { |
| "type": "string", |
| "description": "品牌能解決TA遇到這個痛點的方式(80個中文字以上)" |
| }, |
| "TA心情評分": { |
| "type": "number", |
| "description": "TA在這個階段發生的心情指數(1最低、5最高)", |
| "minimum": 1, |
| "maximum": 5 |
| } |
| },"required": [ |
| "TA小故事", |
| "接觸點", |
| "痛點", |
| "痛點解決方案", |
| "TA心情評分" |
| ] |
| } |
|
|
| stages_properties = {stage: stage_template for stage in cj_stages} |
| |
| cj_contents_schema = { |
| "name": "customer_journey_contents", |
| "description": f"請根據這個品牌的資訊、TA的資訊及每個顧客旅程階段幫我生成該階段的TA小故事、品牌與TA的接觸點、痛點、痛點解決方案及TA心情評分。", |
| "parameters": { |
| "type": "object", |
| "properties": { |
| "contents": { |
| "type": "object", |
| "properties": stages_properties, |
| "required": cj_stages, |
| "description": f"這{len(cj_stages)}個階段的TA小故事、品牌與TA的接觸點、痛點、痛點解決方案及TA心情評分" |
| } |
| },"required": [ |
| "contents"] |
| } |
| } |
|
|
| return cj_contents_schema |
| |
| def generate_cj_contents(brand_name, brand_info, product_info, cjstages_count, target_persona, target_ccs, target_info,cj_stages): |
| print(cjstages_count) |
| messages_base = [ |
| {"role": "system", "content": "你是一個專業的UX設計師,擅長Customer Journey Map的規劃。"}, |
| {"role": "user", "content": f"本次要規劃的品牌名稱如下 品牌名稱:{brand_name},品牌資訊:{brand_info},產品資訊:{product_info},TA人物誌:{target_persona},TA_CCS敘述:{target_ccs},TA人補充資訊:{target_info}"} |
| ] |
|
|
| |
| response = client.chat.completions.create( |
| model='gpt-4o', |
| messages= messages_base, |
| functions=[ |
| generate_cj_contents_schema(cj_stages) |
| ], |
| function_call={"name": "customer_journey_contents"} |
| ) |
| arguments_str = response.choices[0].message.function_call.arguments |
| return arguments_str |
|
|