《Google Colab 搭建一个自己的ChatGPT》

1、点击文件——新建笔记

2、+代码 

输入pip install openai,点击运行 

3、输入以下代码

import os
import openai

openai.api_key = "这是你的api_key请全部删除再粘贴到引号里面来"

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": "你好,你可以和我打个招呼吗"}
  ]
  )
print(response.choices[0].message.content)

  4、点击运行

5、改进 

+代码,输入以下代码

import os
import openai

openai.api_key = "这是你自己的api_key"

def chatWithGPT(prompt):
  completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
  {"role": "user", "content": prompt}
  ]
  )
  
  return print(completion.choices[0].message.content)

然后再点击+代码,输入, chatWithGPT(“你想要问的问题”),点击运行

避坑,这个括号要英语的括号才行,不然就是报错

 这里记得点击左边的运行,才会出结果

好了,你自己的chatgpt就好了,总结一下只要三个步骤:

1、输入pip install openai,运行

2、

import osimport openaiopenai.api_key = "换成自己的key"def chatWithGPT(prompt):  completion = openai.ChatCompletion.create(  model="gpt-3.5-turbo",  messages=[  {"role": "user", "content": prompt}  ]  )  return print(completion.choices[0].message.content)

3、  chatWithGPT(“你想要问的问题”)

猜你喜欢

转载自blog.csdn.net/qq_22576071/article/details/130245573