import openai
# chatgpt3.0
def chatGPT(keyword):
# 設(shè)置 API Key
openai.api_key = sk-******** #這個(gè)需要去 https://platform.openai.com/ 注冊(cè)賬戶申請(qǐng)
# 設(shè)置請(qǐng)求參數(shù)
# model_engine = "text-davinci-003" # "text-davinci-002" #接口類(lèi)型
completions = openai.Completion.create(
engine="text-davinci-003",
# 使用的模型ID。可以使用模型API列表接口https://platform.openai.com/docs/api-reference/models/list 查看所有可用的模型,有關(guān)模型的描述,請(qǐng)參閱模型概述 https://platform.openai.com/docs/models/overview
prompt=keyword,
# 生成完成的提示,編碼為字符串、字符串?dāng)?shù)組、token數(shù)組或token數(shù)組的數(shù)組。請(qǐng)注意,<|endoftext|>是模型在訓(xùn)練期間看到的文檔分隔符,因此,如果未指定提示,則模型將從新文檔的開(kāi)頭生成。
max_tokens=2048, # 完成時(shí)要生成的最大token數(shù)量。提示的token計(jì)數(shù)加上max_tokens不能超過(guò)模型的上下文長(zhǎng)度。大多數(shù)模型的上下文長(zhǎng)度為2048個(gè)token(最新模型除外,支持4096個(gè))。
n=1, # 每個(gè)提示要生成多少個(gè)完成。注意:由于此參數(shù)會(huì)生成許多完成,因此它可以快速消耗您的token配額。小心使用,并確保您對(duì)max_tokens和stop有合理的設(shè)置。
stop=None,
temperature=0.5, # 使用什么樣的采樣溫度,介于0和2之間。較高的值(如0.8)將使輸出更加隨機(jī),而較低的值(例如0.2)將使其更加集中和確定。 通常建議更改它或top_p,但不能同時(shí)更改兩者。
)
return completions.choices[0].text
# chatgpt3.5接口
def chatGPT35(keyword,modelCntent=''):
# 設(shè)置 API Key
openai.api_key = sk-******** #這個(gè)需要去 https://platform.openai.com/ 注冊(cè)賬戶申請(qǐng)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
# 3.5的調(diào)教說(shuō)明:https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
{"role": "system", "content": modelCntent}, #給gpt指定角色
{"role": "user", "content": keyword},#具體要查詢的關(guān)鍵詞
],
temperature=0,
)
return response['choices'][0]['message']['content']
def main():
keyword = ''
while keyword != 'exit':
if keyword != '':
chatGPT35(keyword)
#chatGPT(keyword) #3.0
keyword = input('請(qǐng)輸入要查詢的內(nèi)容:')
if __name__ == "__main__":
main()
運(yùn)行以上命令時(shí)需要滿足一下條件:
1、進(jìn)行科學(xué)上網(wǎng),具體的科學(xué)上網(wǎng)方式可以參考文章:http://www.xllyou.com/jkxw/da3a9b4347e72bc05eec7086384076f3.html
2、并且將節(jié)點(diǎn)地址設(shè)置到非大陸,香港和臺(tái)灣的IP,具體操作方法如下:
2、完成上述科學(xué)上網(wǎng)后,進(jìn)入chatGpt官網(wǎng):https://platform.openai.com/login/,注冊(cè)一個(gè)賬戶,生成對(duì)應(yīng)的key




