惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
U
Unit 42
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
腾讯CDC
I
InfoQ
GbyAI
GbyAI
博客园_首页

Wulu's Blog

用 ChatGPT 定时任务做一个 Hacker News 中文摘要网站 开放课程推荐 「面向所有人的 AI 提示词技巧」 2025 读书总结 树莓派5 优化与踩坑 在 Antigravity 使用 VS Code 应用扩展商店 树莓派安装 OpenWrt 及扩容教程 使用 Docker 在树莓派上安装 Moritx 下载工具 使用 Home Assistant 将米家设备接入 HomeKit 教程 Docker容器中的定时任务:使用Supercronic运行Python脚本 树莓派 | 欧路词典生词本 & 墨墨背单词 云同步教程 (eudic-maimemo-sync) 在 AI 时代,我为什么写博客? 记录 DIY 更换 CASIO G-SHOCK GBA-800 电池过程 2024 读书总结 使用 Audacity 制作立体音效果 使用 Audacity 进行音频降噪教程 Moonhack 2024: 缓解气候变化 谈谈 Follow 这款 RSS 阅读器 使用HomeAssistant将米家设备接入到HomeKit AutoHotkey 脚本实现一键复制文本到 ChatGPT 提示模板 开放课程推荐 「面向开发者的 ChatGPT 提示词工程」 指定 Whisper 输出为简体中文 Api2d 国内可用的chatgpt api代理 利用chatgpt实现编程想法的实践-PyPower 树莓派安装docker版百度网盘 近期音乐分享 AI绘画 6PEN 人工智能艺术?
OpenAI Python API 新版本示例
2023-11-12 · via Wulu's Blog

迁移至 openai>=1.0.0

最近升级了 OpenAI Python 包的最新版本。运行旧代码时出现编译错误:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

openai>=1.0.0 示例

在查看了提示中迁移说明的后,写一个基本示例来调用openai>=1.0.0版本,供大家参考:

说明:代码中设置 base_url 以自定义主机,从而能够使用第三方 OpenAI 代理,这个选项的可选的,如果你使直接访问openai的服务,可以删除这一个配置。

from openai import OpenAI

client = OpenAI(

api_key="xxx",

base_url = 'xxx'

)

def get_completion(prompt, model="gpt-3.5-turbo-1106"):

messages = [{"role": "user", "content": prompt}]

response = client.chat.completions.create(

model=model,

messages=messages,

temperature=0.7,

)

return response.choices[0].message.content

prompt = "你好,ChatGPT!"

print(get_completion(prompt))

如果要启用 OpenAI 新推出的 JSON mode

def get_completion(prompt, model="gpt-3.5-turbo-1106"):

messages = [{"role": "user", "content": prompt}]

response = client.chat.completions.create(

model=model,

messages=messages,

response_format={"type": "json_object"},

temperature=0.7,

)

return response.choices[0].message.content

openai==0.28 示例

import openai

openai.api_key = ''

openai.api_base = ''

def get_completion(prompt, model="gpt-3.5-turbo-1106"):

messages = [{"role": "user", "content": prompt}]

response = openai.ChatCompletion.create(

model=model,

messages=messages,

temperature=0.7,

)

return response.choices[0].message["content"]

prompt = "你好,ChatGPT!"

print(get_completion(prompt))

Some rights reserved

Except where otherwise noted, content on this page is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.