一、代碼部分
import itchat
import openai
from itchat.content import TEXT
openai.api_key = "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"
def askbot(question,who):
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": question}] )
a = completion.choices[0].message.content
b = who
return completion.choices[0].message.content
@itchat.msg_register(TEXT) # 注冊(cè)處理文本信息
def wxreply(msg):
itchat.send(askbot(msg.text,msg.User.NickName), toUserName=msg.FromUserName)
itchat.login(enableCmdQR=2)
itchat.run()
效果如下
二.itchat相關(guān)依賴。解壓至lib路徑即可使用
itchat python依賴下載文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-646959.html
三.項(xiàng)目要在美國(guó)或其他服務(wù)器內(nèi)運(yùn)行
這里推薦這個(gè)。鏈接:衡天云文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-646959.html
到了這里,關(guān)于17行代碼用python對(duì)接openai的微信對(duì)話機(jī)器人的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!