首先使用打开:OpenAI
使用google账号登录,每个新登录的账号会有5或10美金的额度
然后到这个页面:https://platform.openai.com/api-keys
要是这里是灰色的,就说明需要验证手机号
但是没有国外的手机号,可以自行搜索虚拟手机号网站完成验证
注册成功后可以获得一个secret key,这个key待会儿要用,放到环境变量中
import os
from openai import OpenAI
#openai 1.0.0版本之后都推荐使用client方式访问api
client = OpenAI(api_key=os.environ.get('OPEN_AI_KEY'))
#国内访问openai域名必须要走代理,这个是现成的一个反向代理
client.base_url = 'https://api.openai-proxy.com/v1'
#这里输入你的问题
prompt = """
use python to make a openai program
"""
def get_response(prompt):
completions = client.chat.completions.create(
model='gpt-3.5-turbo',
max_tokens=512,
n=1,
stop=None,
temperature=0.0,
messages=[
{
"role": "user",
"content": prompt,
}
]
)
message = completions.choices[0].message.content
return message
print(get_response(prompt))
执行结果:(这里返回的是低于1.0.0版本openai的python写法)
To create an OpenAI program using Python, you can use the OpenAI Python library. Here is an example of a simple program that uses the OpenAI GPT-3 model to generate text:
```python
import openai
# Set your OpenAI API key
api_key = 'YOUR_API_KEY'
openai.api_key = api_key
# Prompt for the model to generate text
prompt = "Once upon a time"
# Generate text using the GPT-3 model
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=100
)
# Print the generated text
print(response.choices[0].text.strip())
```
Make sure to replace `'YOUR_API_KEY'` with your actual OpenAI API key. You can obtain an API key by signing up for an account on the OpenAI website.
This program sends a prompt to the GPT-3 model and prints the generated text. You can customize the prompt and model parameters to generate different types of text.
到这儿,使用chatgpt的hello world demo就完成了,最近openai又发布了sora,AGI指日可待,再不学ai真的来不及了,后续也会出文生文,文生图,图生图,图生视频的demo和教程