python简单使用openai sdk和chatgpt对话

首先使用打开: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和教程

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值