相关阅读
视频讲解
本期视频,我将教你如何从零开始创建你自己的ChatGPT聊天机器人,即便你从来没有接触过代码,但你对创建自己的ChatGPT机器人感兴趣,你也可以来观看本视频。我将演示三个不同版本的聊天机器人,从最简单的开始。你只需要使用Visual Studio Code,Python和一个带有ApiKey的OpenAI账户,简单四行代码便可以实现最基础的功能。接着,我将向你展示如何构建一个基于ChatGPT的聊天机器人,它可以记住以前的对话,建立上下文关系,甚至还可以教你如何测试它。在视频的最后一部分,我将介绍一个新工具,让你可以在浏览器中使用聊天机器人并与他人分享。
相关资料
- Vscode下载 https://code.visualstudio.com/Download
- Openai Api申请 https://platform.openai.com/account/api-keys
相关代码
ChatGPT API Tutorial.zip
下载
zip文件
如果你懒得下载,也可以直接从下面代码中复制
01 chatgpt simple.py
import openai
openai.api_key = "####"
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Give me 3 ideas for apps I could build with openai apis "}])
print(completion.choices[0].message.content)
02 chatgpt chat assistant copy.py
import openai
openai.api_key = "####"
messages = []
system_msg = input("What type of chatbot would you like to create?\n")
messages.append({"role": "system", "content": system_msg})
print("Your new assistant is ready!")
while input != "quit()":
message = input()
messages.append({"role": "user", "content": message})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages)
reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": reply})
print("\n" + reply + "\n")
03 chatgpt chat assistant website.py
import openai
import gradio
openai.api_key = "####"
messages = [{"role": "system", "content": "You are a financial experts that specializes in real estate investment and negotiation"}]
def CustomChatGPT(user_input):
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = messages
)
ChatGPT_reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": ChatGPT_reply})
return ChatGPT_reply
demo = gradio.Interface(fn=CustomChatGPT, inputs = "text", outputs = "text", title = "Real Estate Pro")
demo.launch(share=True)