OpenAI首届开发者大会发布内容概要

11/11/2023 OpenAI开发者大会GPTsGPT-StoreAPI升级与降价多模态APIAssistant-API文档问答助手

# 1. OpenAI开发者大会

# 1.1 基本介绍

美国时间11月6日,OpenAI的历史首届开发者大会揭幕。OpenAI首席执行官 Sam Altman 在接近45分钟的开幕演讲中,向全球开发者和 ChatGPT 用户展示了他们即将推出的一系列产品更新。

OpenAI开发者大会

# 1.2 大会概要

Sam Altman 阐述了公司一系列重要进展,不限于 GPT- 4 Turbo,这是一个功能更强大、上下文更⻓、控制更多的新模型,另外还推出了 Assistance API,以简化开发者构建辅助代理的体验,OpenAI 强调了自然语言将成为未来与计算机互动的重要方式,同时也介绍了通过对话编程 GPT 的能力,Sam Altman 宣布了 GPT 商店的即将推出,这将允许用户分享和发现创新的 GPT 应用,并提供收入分享激励。除此之外,Sam Altman 还谈到了与微软的深化合作,展示了新的文本到语音模型和改进的功能调用。

OpenAI首届开发者大会

发布会核心更新如下:

发布会核心更新总结

# 2. 面向普通用户

# 2.1 界面简洁化

GPT-4用户不用再点模型的各种下拉菜单了,以后只有一个窗口,模型可以自主调用代码解释器、联网工具、DALL•E3等

ChatGPT界面简洁化

# 2.2 GPTs

现在GPTs已经可以供ChatGPT Plus和企业用户使用。任何人都可以轻松创建自己的GPT,无需编程。可以为自己创建一个GPT,仅用于公司内部使用,或与他人分享。创建一个GPT就像开始对话一样简单,只需给它指示和额外的知识,并选择它可以做的事情,比如搜索网页、制作图像或分析数据。

GPTs

# 2.2.1 用户自定义创建GPTs

可以在这个地址创建GPTs:https://chat.openai.com/create (opens new window)(自带引导教你如何创建,右侧还有预览功能,这里就不赘述了)

创建GPTs

# 2.2.2 使用Canva的GPTs

一些可用的示例包括 Canva (opens new window)Zapier AI Actions (opens new window) ,这里以开发者大会上演示过的 Canva 为例。

  • 注:使用时它会提示你是否允许连接到 Canva,选择允许之后,就会将其生成出来。
使用GPTs-Canva创建海报

# 2.3 GPT Store

任何人都可以创建并公开分享GPTs,官方将推出GPT Store,展示经过验证的构建者的作品。一旦进入商店,GPTs将可以被搜索,之后可以根据使用您的GPTs的人数来赚取收入。即使不是开发者的普通用户都可以创建自己的应用并发布到GPT Store(类似苹果的APP Store),需求和创意为王的时代来了。

官方的 GPT Store 目前尚未发布,现在有一些第三方的收集站:GPTsHunter (opens new window)AwesomeGPTs (opens new window)BestGPTs (opens new window)GPTs Works (opens new window)GPTs Today (opens new window)

# 3. 面向开发者

# 3.1 API大幅升级与降价

# 3.1.1 API功能升级

GPT-4 API升级为 GPT-4 Turbo,知识库更新到2023年4月,上下文从8K提升到128K,引入JSON与可重现输出。

OpenAI-API升级

所有 GPT-4 付费客户的每分钟 Token 限制增加了一倍。

OpenAI-API更高的速率限制

详见:https://platform.openai.com/account/rate-limits (opens new window)

现在的OpenAI-API速率限制

# 3.1.2 API价格降低

API大幅降价,可以给开发者节省更多资金,GPT-4 Turbo API 的价格仅为 GPT-4 API 的三分之一,GPT-3.5 Turbo的微调也降价了。

OpenAI-API降价

详见:https://openai.com/pricing (opens new window)

GPT4-API最新价格

# 3.2 发布多模态API

这次开发者大会,OpenAI开放了GPT-4 Turbo with vision、DALL•E 3、Text-to-speech等多模态API供开发者使用。

OpenAI的多模态API

# 3.2.1 使用 DALL·E API

DALL·E 的接口文档相见:https://platform.openai.com/docs/api-reference/images/create (opens new window)

DALL·E-API接口文档

[1] DALL·E2

YOUR_OPENAI_API_KEY处换成自己的API-KEY,接口URL也可以换成国内代理的地址,下同。

$ curl https://api.openai.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $YOUR_OPENAI_API_KEY" \
  -d '{
    "model": "dall-e-2",
    "prompt": "A cute baby sea otter",
    "n": 1,
    "size": "1024x1024"
  }'
  
 >>> 输出结果 
 {
  "created": 1699675161,
  "data": [
    {
      "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-0LVngeH2QQFj8ymm46YQc7tI/user-SgQhjgYdVfysnTICFYAZIEMt/img-3Fz8RcSKRmuruWRhMHRqddxF.png?st=2023-11-11T02%3A59%3A21Z&se=2023-11-11T04%3A59%3A21Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-11-10T21%3A41%3A32Z&ske=2023-11-11T21%3A41%3A32Z&sks=b&skv=2021-08-06&sig=xCPhNtS0iAY9fQcaLn3z6U9%2BQ4iXdfC6CbhwA/IzAVM%3D"
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
DALL·E2的API生成效果

[2] DALL·E3

$ curl https://api.openai.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $YOUR_OPENAI_API_KEY" \
  -d '{
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "n": 1,
    "size": "1024x1024"
  }'
  
 >>> 输出结果 
 {
  "created": 1699675717,
  "data": [
    {
      "revised_prompt": "Create an adorable image of a small, playful sea otter. The baby otter is in a serene aquatic environment. Its small, round body is covered with brown, water-resistant fur. The otter's expressive eyes and small, snub nose give it an irresistibly cute appearance. Underneath the azure sky, the otter is comfortably floating on its back, its small webbed feet gently paddling the water. In its tiny paws, it's holding a small stone, seemingly engrossed in its play. The water around it is a clear, tranquil blue, reflecting the otter's playful antics.",
      "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-P40Lfipgq0xW6XOTt3rYE9RB/user-69ZafOSTRgpaG0RhFKHLxWFD/img-pOCfPN7jX7Bj9JX5JWbuWtX9.png?st=2023-11-11T03%3A08%3A37Z&se=2023-11-11T05%3A08%3A37Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-11-10T21%3A51%3A12Z&ske=2023-11-11T21%3A51%3A12Z&sks=b&skv=2021-08-06&sig=x3Y2g/8vPPsxZEmo6FmeKtQ3aIkM1baBWQ79nRBgVzc%3D"
    }
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DALL·E3的API生成效果

# 3.2.2 使用 GPT-4 Turbo API

带有视觉的GPT-4 Turbo可以在Chat Completions API中接受图像作为输入,实现生成标题、详细分析现实世界图像以及阅读带有图表的文档等功能。开发者可以在API中使用gpt-4-vision-preview来访问这个功能。对于GPT-4 Turbo,定价取决于输入图像的尺寸,详见:https://platform.openai.com/docs/guides/vision (opens new window)

使用GPT-4-Turbo-API

这里把使用DALLE·3生成的水獭图片让GPT-4 Turbo描述其中的内容。

# -*- coding: utf-8 -*-

import base64
import requests

# OpenAI API Key
api_key = "YOUR_OPENAI_API_KEY"

# Function to encode the image
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

# Path to your image
image_path = "./test.png"

# Getting the base64 string
base64_image = encode_image(image_path)

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
}

payload = {
    "model": "gpt-4-vision-preview",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "What’s in this image?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": f"data:image/jpeg;base64,{base64_image}"
            }
          }
        ]
      }
    ],
    "max_tokens": 300
}

response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)

print(response.json())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

返回结果如下:

{
	'id': 'chatcmpl-8JaKX9UvWuZRRZIzmb8BOoLIMby7y',
	'object': 'chat.completion',
	'created': 1699678577,
	'model': 'gpt-4-1106-vision-preview',
	'usage': {
		'prompt_tokens': 778,
		'completion_tokens': 112,
		'total_tokens': 890
	},
	'choices': [{
		'message': {
			'role': 'assistant',
			'content': 'The image shows a highly detailed illustration of an adorable sea otter (Enhydra lutris) floating on its back in the water. The otter appears to be holding a stone between its forepaws, a behavior that is characteristic of the species when they use rocks to break open shellfish and other food items. The background features a blue sky with some clouds and the water has a calm and clear appearance, with visible light reflections on the surface. The image is likely designed to evoke a sense of tranquility and the cuteness of the animal.'
		},
		'finish_details': {
			'type': 'stop',
			'stop': '<|fim_suffix|>'
		},
		'index': 0
	}]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

将英文描述使用Google翻译成中文的结果如下:

该图像非常详细地展示了一只可爱的海獭 (EnHydra lutris) 仰面漂浮在水中。
水獭的前爪之间似乎夹着一块石头,这是该物种使用石头敲开贝类和其他食物时的特征。 
背景是蓝天,有一些云,水面平静而清澈,表面有可见的光反射。
该图像可能旨在唤起一种宁静感和动物的可爱感。
1
2
3
4

# 3.3 发布Assistant API

在发布会上,Sam Altman 指出了过去开发中的一些痛点:状态管理、指令和上下文管理、额外能力、检索。而现在,OpenAI 的后端替开发者做了大量的对话、长内容 chunk 甚至 retrieval 工作,引入了新的thread和message概念,开发者不需要自己管理对话线程和上下文内容,可以预想到开发体验会非常好,某种意义上甚至可以说 OpenAI 官方正在干掉 LangChain。

其实,自从 ChatGPT 发布以来,ChatPDF 之类的 RAG(大模型检索增强)产品已经成为了 LLMs 领域的 hello world 项目,而 Assistants API 对于构建这类应用就非常有帮助了,开发者不需要自己解析文件并 chunk,不需要自己分析并调用,全部交给 OpenAI 的后端,就几乎能实现 ChatGPT 网页版 All Tools 那样的体验——可以说,像 ChatPDF 那样的项目,已经变成了 OpenAI API 里的一行代码。

这里也会涉及到成本的比较,尽管 Assistants API 会按 Thread 和文件存储单独收费,但因为其 stateful 特性不需要带上下文提交,反而又会节约一定的 Token 费用,总之费用差异还是进一步测试

# 3.3.1 Assistant API是什么

Assistant API现在可以开启多种工具,编排出复杂的应用。目前支持三类工具:

  • Code Interpreter 代码解释器:允许助手API在受限执行环境中编写和运行Python代码。该工具可以处理具有不同数据和格式的文件,并生成包含数据和图像的文件。代码解释器允许您的助手迭代运行代码,以解决具有挑战性的代码和数学问题。
  • Knowledge Retrieval 知识检索:检索通过从模型外部获取的知识来增强助手的功能,例如专有产品信息或用户提供的文档。一旦文件被上传并传递给助手,OpenAI将自动对您的文档进行分块、索引和存储嵌入,并实施向量搜索以检索相关内容以回答用户的查询。
  • Function calling 函数调用:与Chat Completions API类似,Assistants API也支持函数调用。函数调用允许您向助手描述函数,并智能地返回需要调用的函数及其参数。当Assistants API调用函数时,运行将暂停执行,并且您可以提供函数调用的结果以继续运行执行。

Assistant-API简介

# 3.3.2 Assistant API原理

Assistant API旨在帮助开发人员构建功能强大的人工智能助手,能够执行各种任务,它可以用特定的指令调整OpenAI的模型的个性和能力。

  • Assistant可以同时访问多个工具。这些工具可以是OpenAI托管的工具,比如代码解释器和知识检索,也可以是您构建/托管的工具(通过函数调用)。
  • Assistant可以访问持久线程。线程通过存储消息历史并在对话超出模型上下文长度时进行截断,简化了AI应用程序的开发。您只需创建一个线程,然后在用户回复时简单地追加消息即可。
  • Assistant可以以多种格式访问文件 —— 无论是作为它们的创建部分,还是作为Assistant和用户之间的线程的一部分。 在使用工具时,Assistant还可以创建文件(例如图像、电子表格等),并在创建的消息中引用文件。

原理详见:https://platform.openai.com/docs/assistants/how-it-works (opens new window)

Assistants-API原理

对象 含义
Assistant 为了利用OpenAI的模型并调用工具而设计的AI。
Thread 助手和用户之间的交流会话。线程存储消息,并自动处理截断以适应模型的上下文。
Message 助手或用户创建的消息。消息可以包括文本、图片和其他文件。消息以列表形式存储在线程上。
Run 一个对线程上的助手的调用。助手使用它的配置和线程的消息来通过调用模型和工具执行任务。作为运行的一部分,助手将消息附加到线程中。
Run Step 一个详细的步骤列表,助手在运行过程中所采取的步骤。助手可以在运行过程中调用工具或创建消息。检查运行步骤可以帮助您分析助手达到最终结果的方式。

# 3.3.3 实现文档问答助手

用户可以在 API 中,通过自定义 Assistant 的方式,实现一些特定的功能,这里以实现一个文档问答助手为例:

如何使用详见:https://platform.openai.com/docs/assistants/overview (opens new window)

# -*- coding: utf-8 -*-

import time
from openai import OpenAI
import streamlit as st

api_key = 'sk-xxx'
client = OpenAI(api_key=api_key)


# 构建assistant
# 下面定义的是assistant的系统属性、使用的工具以及使用的模型
assistant = client.beta.assistants.create(
    name="Document Assistant",
    instructions="You can upload PDF, document, and spreadsheet. "
                 "The retrieval system is capable of handling files up to 200MB. "
                 "It allows for question-and-answer sessions based on the uploaded documents.",
    tools=[{"type": "code_interpreter"}],
    model="gpt-4-1106-preview"
)

# 创建 assistant 执行线程
thread = client.beta.threads.create()
file_id = None


def upload_file(st_file_obj):
    """上传文档到知识库"""
    file_obj = client.files.create(
        file=st_file_obj.read(),
        purpose='assistants'
    )
    # 更新 assistants 和 文档的关联关系
    client.beta.assistants.update(assistant.id, file_ids=[file_obj.id])
    return file_obj


def delete_file(file_id):
    """删除文档"""
    client.files.delete(file_id=file_id)


def query(question):
    """提问"""
    # 创建提问消息
    client.beta.threads.messages.create(
        thread_id=thread.id,
        role='user',
        content=question
    )
    # 执行 assistants
    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant.id
    )

    # 等待服务器执行完成
    while True:
        run = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run.id
        )
        if run.completed_at:
            break
        time.sleep(1)

    # 获取消息列表
    messages = client.beta.threads.messages.list(thread_id=thread.id)
    last_message = messages.data[0]
    return last_message.content[0].text.value


def upload():
    global file_id
    file_obj = st.session_state.upload_file
    if file_obj:
        # 如果之前关联过文档,就将之前的文档删除
        if file_id:
            delete_file(file_id)
        file_id = upload_file(file_obj)


def question_on_change():
    question = st.session_state.question
    if question:
        res = query(question)
    else:
        res = '请输入要提问的内容'

    st.session_state.result = res


st.title('文档问答助手')
st.file_uploader('请上传你要提问的文档', key='upload_file', on_change=upload)
st.text_input('请输入你的问题:', key='question', on_change=question_on_change)
st.text_area('回答:', '', key='result')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

Streamlit 是一个用于构建数据应用的 Python 库,它可以创建交互式的 Web 应用,使用如下命令运行文档问答助手。

$ streamlit run document_assistant.py
1

实现效果:可以从本地文件里上传文档,然后基于这个文档进行问答。

基于Assistant-API实现文档问答助手

# 4. 参考资料

[1] OpenAI DevDay, Opening Keynote from YouTube (opens new window)

[2] 首届OpenAI开发者大会中文字幕 from YouTube (opens new window)

[3] New models and developer products announced at DevDay from OpenAI官网 (opens new window)

[4] OpenAI 举办首届开发者大会,有哪些信息值得关注?from 知乎 (opens new window)

[5] GPT-4 Turbo、功能融合:OpenAI 首个开发者大会回顾 from 少数派 (opens new window)

[6] 现场直击一文总结!OpenAI开发者大会,史上最强GPT-4 Turbo发布,AI圈地震 from 人人都是产品经理 (opens new window)

[7] 一图总结 OpenAI 开发者大会重点更新 from 巴比特 (opens new window)

[8] 基于openai api构建assistant from CSDN (opens new window)

[9] Assistants API 推出,包括代码解释器、检索及函数调用等功能,将对开发者带来哪些帮助? from 知乎 (opens new window)

[10] OpenAI DevDay #3 Assistants API 文档 from 知乎 (opens new window)

[11] How Assistants work from OpenAI官网 (opens new window)

[12] 基于 openai assistant api 实现的文档问答助手 from Github Gist (opens new window)

Last Updated: 12/3/2023, 2:34:41 AM