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

推荐订阅源

V2EX - 技术
V2EX - 技术
P
Privacy International News Feed
Security Latest
Security Latest
H
Hacker News: Front Page
T
Tenable Blog
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Project Zero
Project Zero
O
OpenAI News
AI
AI
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
NISL@THU
NISL@THU
A
Arctic Wolf
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
U
Unit 42
S
Security Affairs
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
月光博客
月光博客
Engineering at Meta
Engineering at Meta
腾讯CDC
F
Full Disclosure
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
The Cloudflare Blog

Wulu's Blog

开放课程推荐 「面向所有人的 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.