微信扫码
添加专属顾问
我要投稿
「格式化输出」很重要
在上一篇中,你可以看到有关的起源、原理、用例,以及如何用它来搭建一个 AI 项目,这里就不再赘述:
看完这篇,你也能做 AI 搜索:论「结构化输出」
通过结构化输出,可以让 AI 输出一份思维导图或者表格:
而不是成篇的文本:
史蒂夫·乔布斯,1955年2月24日出生,2011年10月5日去世,美国人。他活跃于科技、创新、企业管理和动画领域。乔布斯创立了Apple、NeXT和Pixar公司,推出了Mac、iPod、iPhone等具有划时代意义的产品,重塑了个人电脑、音乐和手机行业,奠定了苹果在全球科技领域的领军地位。作为Pixar的创办人之一,他也在动画领域留下了深远的影响。乔布斯是20世纪末至21世纪初最具影响力的企业家和创新者之一。
Gemini Flash 的结构化输出
而这次的更新,可以让廉价模型 1.5 Flash 也用上了:
100 万 token 的上下文
每天前 1500 个请求免费
调用价格低至 $0.075 每 100 万 token(长度少于 128k 的上文)
可叠加 GCP 的赞助/优惠
四舍五入不要钱
智谱的 Flash 是完全不要钱
相信做 AI 项目的同学都知道这意味了什么:这便宜大碗,而且 AI 味不重的 Flash,可正儿八经用在决策 workflow 了!
目前,官方的调用方法还没出(会在本周末更新),但我可以给大家提供一份 sample code
我们把上一篇《看完这篇,你也能做 AI 搜索:论「结构化输出」》中,“将四大名著的信息进行结构化输出”的例子拿来做对比,通过 GPT,代码这么写
from pydantic import BaseModel
class theBook(BaseModel):
name: str
writer: str
class theFour(BaseModel):
books: list[theBook]
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "告诉我四大名著分别是什么,以及他们的作者是谁"},
],
response_format = theFour,
)
response = completion.choices[0].message.parsed
得到的结果是
theFour(books=[theBook(name='《红楼梦》', writer='曹雪芹'), theBook(name='《西游记》', writer='吴承恩'), theBook(name='《三国演义》', writer='罗贯中'), theBook(name='《水浒传》', writer='施耐庵')])
而通过 Flash,代码是类似这样的
"""
Install the Google AI Python SDK
$ pip install google-generativeai
$ pip install google.ai.generativelanguage
"""
import os
import google.generativeai as genai
from google.ai.generativelanguage_v1beta.types import content
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
# Create the model
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_schema": content.Schema(
type = content.Type.OBJECT,
enum = "[]",
required = "["books"]",
properties = {
"books": content.Schema(
type = content.Type.ARRAY,
items = content.Schema(
type = content.Type.OBJECT,
properties = {
"name": content.Schema(
type = content.Type.STRING,
),
"writer": content.Schema(
type = content.Type.STRING,
),
},
),
),
},
),
"response_mime_type": "application/json",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config=generation_config,
# safety_settings = Adjust safety settings
# See https://ai.google.dev/gemini-api/docs/safety-settings
system_instruction="Extract the event information.",
)
chat_session = model.start_chat(
history=[
{
"role": "user",
"parts": [
"告诉我四大名著分别是什么,以及他们的作者是谁",
],
},
{
"role": "model",
"parts": [
"```json\n{\"books\": [{\"name\": \"红楼梦\", \"writer\": \"曹雪芹\"}, {\"name\": \"三国演义\", \"writer\": \"罗贯中\"}, {\"name\": \"水浒传\", \"writer\": \"施耐庵\"}, {\"name\": \"西游记\", \"writer\": \"吴承恩\"}]} \n```",
],
},
]
)
response = chat_session.send_message("INSERT_INPUT_HERE")
print(response.text)
得到结果:
{"books": [{"name": "红楼梦", "writer": "曹雪芹"}, {"name": "三国演义", "writer": "罗贯中"}, {"name": "水浒传", "writer": "施耐庵"}, {"name": "西游记", "writer": "吴承恩"}]}
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-04-29
10万元跑满血版DeepSeek,这家公司掀了一体机市场的桌子|甲子光年
2025-04-29
谷歌大神首次揭秘Gemini预训练秘密:52页PPT干货,推理成本成最重要因素
2025-04-29
一文说清:什么是算法备案、大模型备案、大模型登记 2.0
2025-04-29
MCP:AI时代的“万能插座”,大厂竞逐的焦点
2025-04-29
打起来了!MCP VS A2A,谁才是Agent的未来事实标准?
2025-04-29
Google 的 A2A 与 MCP 该如何选择?还是两种都用?
2025-04-29
一站式AI应用开发平台 Firebase Studio
2025-04-29
精华好文!用LLM评估LLM,真的靠谱吗?技术上如何实现?
2024-08-13
2024-06-13
2024-08-21
2024-09-23
2024-07-31
2024-05-28
2024-08-04
2024-04-26
2024-07-09
2024-09-17
2025-04-29
2025-04-29
2025-04-29
2025-04-28
2025-04-28
2025-04-28
2025-04-28
2025-04-28