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

推荐订阅源

人人都是产品经理
人人都是产品经理
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
N
News and Events Feed by Topic
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
博客园 - 叶小钗
B
Blog
Vercel News
Vercel News
T
Tenable Blog
T
The Exploit Database - CXSecurity.com
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Microsoft Security Blog
Microsoft Security Blog
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
DataBreaches.Net
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
C
CERT Recently Published Vulnerability Notes
L
LangChain Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Check Point Blog
A
About on SuperTechFans
W
WeLiveSecurity
The GitHub Blog
The GitHub 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.