"Google Colab builds its own ChatGPT"

1. Click File - New Note

2. +code 

Enter pip install openai, click to run 

3. Enter the following code

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. Click to run

5. Improvement 

+ code, enter the following code

 

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)

Then click + code, enter,  chatWithGPT ("the question you want to ask"), click to run

To avoid pitfalls, this bracket needs English brackets, otherwise it will report an error

 Here, remember to click the run on the left to get the result

Well, your own chatgpt is just fine, to sum up, it only takes three steps:

1. Enter pip install openai and run

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("The question you want to ask")

Guess you like

Origin blog.csdn.net/qq_22576071/article/details/130245573