import openai
import os
from dotenv import load_dotenv, find_dotenv
ChatGPTを用いたプロンプト・エンジニアリング
ChatGPTで開発をするための方法
準備
openai, python-dotenv (python 3.8.1以上)をインストール
「command」+「shift」+「.」 で、隠しファイルを表示
https://platform.openai.com/account/api-keys でキーを得る. 3ヶ月で180$を無料で使用できる. .env にOPENAI_API_KEY = <キー> を追加
.gitignore に .env を追加
= load_dotenv(find_dotenv())
_
= os.getenv('OPENAI_API_KEY') openai.api_key
def get_completion(prompt, model="gpt-3.5-turbo"):
= [{"role": "user", "content": prompt}]
messages = openai.ChatCompletion.create(
response =model,
model=messages,
messages=0, # this is the degree of randomness of the model's output
temperature
)return response.choices[0].message["content"]
# text = f"""
# You should express what you want a model to do by \
# providing instructions that are as clear and \
# specific as you can possibly make them. \
# This will guide the model towards the desired output, \
# and reduce the chances of receiving irrelevant \
# or incorrect responses. Don't confuse writing a \
# clear prompt with writing a short prompt. \
# In many cases, longer prompts provide more clarity \
# and context for the model, which can lead to \
# more detailed and relevant outputs.
# """
# prompt = f"""
# Summarize the text delimited by triple backticks \
# into a single sentence.
# ```{text}```
# """
# response = get_completion(prompt)
# print(response)