File size: 2,284 Bytes
f5e9435 597280b 2793324 597280b f5e9435 3295cfa f5e9435 | 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 | import openai
import os
from openai import OpenAI
client = OpenAI(
api_key= os.environ["gptkey"]
)
def ideagen(context):
total_prompt_tokens_used = 0
total_completion_tokens_used = 0
#print(context)
messages_base = [
{"role": "system", "content": "請扮演行銷策略的大師,善於觀察社會巨觀的的發展和人與人之間微觀的互動,從中挖掘出不為人察覺的行為洞察,好的洞察可以幫助找出問題背後的問題,透過一層層深入的分析,幫助找出問題的核心。"},
]
messages_base.extend([{"role": "user", "content": f"請幫助我分析{context}問題的成因。"}])
messages_base.extend([{"role": "user", "content": "請用以下的格式回答:我'問題A',因為'理由A',之後將'理由A'變成新的問題,重複以上的分析:我'理由A',因為'理由B',請重複以上的動作五次,每次都挖掘更深入的心理動機,但第三次請給我一個出乎意料的理由,理由來自於更尖銳、深刻的人群觀察,接露這些人不為人知的一面,請用markdown格式回覆我,包含'編號'、'問題'、'理由'欄位,最後給出一段結論,概述推理問題的過程,並給出解決問題的建議。"}])
messages_base.extend([{"role": "user", "content": "請發揮你的創意跟想像並盡可能描述更多細節,必須要讓看到的人有驚豔的感覺。請使用繁體中文。"}])
full_text = ""
total_price = 0
#for _ in range(loop):
response = client.chat.completions.create(
model='gpt-4-turbo',
max_tokens=2000,
temperature=0.7,
messages=messages_base
)
completed_text = response.choices[0].message.content
total_prompt_tokens_used += response.usage.prompt_tokens
total_completion_tokens_used += response.usage.completion_tokens
price = total_prompt_tokens_used*0.03/1000 + total_completion_tokens_used*0.06/1000
full_text += completed_text + "\n\n----------\n\n"
total_price += price
price = "price:" + str(total_prompt_tokens_used*0.03/1000 + total_completion_tokens_used*0.06/1000) + "$"
full_text += "\n\n" + "price:" + str(total_price)
#return response.choices[0].message.content
return full_text |