• Privacy & Policy
  • Contact
2025-05-18 17:54:43
  • Login
  • Register
欢迎来到ClaudeAI博客社区
  • Home
  • AI教程
    • 大模型应用实践课程
      • 大型语言模型课程介绍
      • 第1-1章:使用 OpenAI 创建您的第一个聊天机器人-大模型应用实践教程
      • 第1-2章:使用 OpenAI 创建简单的自然语言到 SQL-大模型应用实践教程
    • Claude应用开发教程
      • 第1章:基本提示结构-Claude应用开发教程
      • 第 2 章:基本函数构建-Claude开发应用教程
      • 第3章:角色扮演提示-Claude应用开发教程
      • 第4章 分离数据和指令-Claude开发应用教程
      • 第 5 章:格式化输出-Claude应用开发教程
      • 第6章:预知(Thinking Step by Step)-Claude应用开发教程
    • Claude提示词教程
      • 第 1 课:基本提​​示词技术-Claude提示词工程课程
      • 第 2 课:医疗病例摘要助手[XML-JSON格式化输出]-Claude提示词工程教程
      • 第 3 课:提示工程迭代流程-Claude提示词工程课程
      • 第 4 课:客服聊天记录总结生成器-Claude提示词课程
    • Claude函数/工具调用教程
      • 第3章:使用工具调用强制 JSON结构输出[以提取维基百科页面文章为例]-Claude工具调用教程
      • 第2章:ClaudeAPI如何构建工具并调用[以计算器工具调用为例]-Claude工具调用教程
      • 第1章:工具使用/函数调用介绍和概述-Claude函数工具调用教程
    • ClaudeAPI基础入门教程
      • 第2章:构建简单的多轮聊天机器人-ClaudeAPI基础入门教程
      • 第1章:Claude SDK安装和使用-CLaudeAPI基础入门教程
      • Claude API基础知识课程简介
  • AI大模型
    • chatgpt
      • OpenAI o1-preview 简介和如何使用
      • 如何设置使用新的 ChatGPT o1 模型
      • OpenAI o1 性能评估和学习推理思维链介绍
      • OpenAI o1-mini和gpt4o性能对比介绍
      • OpenAI o1 模型与GPT-4o模型使用区别和场景
    • Claude
      • Claude的project介绍和创建
      • Claude Sonnet 3.5 API 教程
      • Claude 3 最新注册使用教程!(国内版)-性能完爆GPT4o!
      • Claude3.5 vs GPT4 谁才是最强大模型?
      • Claude国内无法登录的解决办法
      • Claude3.5 Sonnet的详细介绍
      • Claude如何写好提示词
      • Claude快速入门指南
    • Llama3
      • Llama3.2最新90b 11b 3b 1b模型介绍-AI大模型
      •  Llama Stack入门安装指南[结合Ollama]-AI大模型
      • Llama 3.2介绍最全技术报告-AI大模型
      • Llama 3.1技术报告:Meta推出最强开源大模型
      • Meta 的 Llama 3.1 405B工作原理、用例等
      • 如何在本地安装和运行 Meta Llama 3.1
      • 使用 Ollama和租用GPU 运行 Llama 3.1 405B:分步指南
      • Llama3.1 Colab 微调指南
  • AI论文
    • OpenAIo1原理解读:重复采样扩展推理计算[Large Language Monkeys: Scaling Inference Compute with Repeated Sampling]-AI论文
    • OpenIAo1原理解读:Q*强化学习与启发式搜索推理框架[Q*: Improving Multi-step Reasoning for LLMs with Deliberative Planning]-AI论文
    • OpenAIo1原理解读:基于蒙特卡罗树搜索的自我博弈互相推理[Self-play Mutual Reasoning]-AI论文
    • openAIo1原理解读:推理增加计算量提高大模型效果[Scaling LLM Test-Time Compute Optimally can be More Effective]-AI论文
    • OpenAI o1大模型原理解读:自博弈Self-play强化学习方法[A Survey on Self-play Methods in Reinforcement Learning]-AI论文
    • OpenAI o1大模型原理解读:Quiet-STaR 推理思维链[Language Models Can Teach Themselves to Think Before Speaking]-AI论文
    • OpenAI o1大模型原理论文汇总[Awesome LLM Strawberry]-AI论文
  • AI应用
    •  Crawl4AI:开源 LLM 友好型 Web 爬虫和抓取工具-AI应用
    • AI Scientist:用于全自动科学发现的写论文应用-AI应用
    • ai-data-analysis-MulitAgent:一种用于自动化复杂研究过程的先进多智能体系统-AI应用
    • Aider:最好的免费开源 AI 编码助手,自动git提交代码!-AI应用
    • AIHawk:你的 AI 求职助手,自动化申请、获得个性化推荐,更快找到您梦想的工作-AI应用
  • 加入会员社区
  • About Us
No Result
View All Result
  • Home
  • AI教程
    • 大模型应用实践课程
      • 大型语言模型课程介绍
      • 第1-1章:使用 OpenAI 创建您的第一个聊天机器人-大模型应用实践教程
      • 第1-2章:使用 OpenAI 创建简单的自然语言到 SQL-大模型应用实践教程
    • Claude应用开发教程
      • 第1章:基本提示结构-Claude应用开发教程
      • 第 2 章:基本函数构建-Claude开发应用教程
      • 第3章:角色扮演提示-Claude应用开发教程
      • 第4章 分离数据和指令-Claude开发应用教程
      • 第 5 章:格式化输出-Claude应用开发教程
      • 第6章:预知(Thinking Step by Step)-Claude应用开发教程
    • Claude提示词教程
      • 第 1 课:基本提​​示词技术-Claude提示词工程课程
      • 第 2 课:医疗病例摘要助手[XML-JSON格式化输出]-Claude提示词工程教程
      • 第 3 课:提示工程迭代流程-Claude提示词工程课程
      • 第 4 课:客服聊天记录总结生成器-Claude提示词课程
    • Claude函数/工具调用教程
      • 第3章:使用工具调用强制 JSON结构输出[以提取维基百科页面文章为例]-Claude工具调用教程
      • 第2章:ClaudeAPI如何构建工具并调用[以计算器工具调用为例]-Claude工具调用教程
      • 第1章:工具使用/函数调用介绍和概述-Claude函数工具调用教程
    • ClaudeAPI基础入门教程
      • 第2章:构建简单的多轮聊天机器人-ClaudeAPI基础入门教程
      • 第1章:Claude SDK安装和使用-CLaudeAPI基础入门教程
      • Claude API基础知识课程简介
  • AI大模型
    • chatgpt
      • OpenAI o1-preview 简介和如何使用
      • 如何设置使用新的 ChatGPT o1 模型
      • OpenAI o1 性能评估和学习推理思维链介绍
      • OpenAI o1-mini和gpt4o性能对比介绍
      • OpenAI o1 模型与GPT-4o模型使用区别和场景
    • Claude
      • Claude的project介绍和创建
      • Claude Sonnet 3.5 API 教程
      • Claude 3 最新注册使用教程!(国内版)-性能完爆GPT4o!
      • Claude3.5 vs GPT4 谁才是最强大模型?
      • Claude国内无法登录的解决办法
      • Claude3.5 Sonnet的详细介绍
      • Claude如何写好提示词
      • Claude快速入门指南
    • Llama3
      • Llama3.2最新90b 11b 3b 1b模型介绍-AI大模型
      •  Llama Stack入门安装指南[结合Ollama]-AI大模型
      • Llama 3.2介绍最全技术报告-AI大模型
      • Llama 3.1技术报告:Meta推出最强开源大模型
      • Meta 的 Llama 3.1 405B工作原理、用例等
      • 如何在本地安装和运行 Meta Llama 3.1
      • 使用 Ollama和租用GPU 运行 Llama 3.1 405B:分步指南
      • Llama3.1 Colab 微调指南
  • AI论文
    • OpenAIo1原理解读:重复采样扩展推理计算[Large Language Monkeys: Scaling Inference Compute with Repeated Sampling]-AI论文
    • OpenIAo1原理解读:Q*强化学习与启发式搜索推理框架[Q*: Improving Multi-step Reasoning for LLMs with Deliberative Planning]-AI论文
    • OpenAIo1原理解读:基于蒙特卡罗树搜索的自我博弈互相推理[Self-play Mutual Reasoning]-AI论文
    • openAIo1原理解读:推理增加计算量提高大模型效果[Scaling LLM Test-Time Compute Optimally can be More Effective]-AI论文
    • OpenAI o1大模型原理解读:自博弈Self-play强化学习方法[A Survey on Self-play Methods in Reinforcement Learning]-AI论文
    • OpenAI o1大模型原理解读:Quiet-STaR 推理思维链[Language Models Can Teach Themselves to Think Before Speaking]-AI论文
    • OpenAI o1大模型原理论文汇总[Awesome LLM Strawberry]-AI论文
  • AI应用
    •  Crawl4AI:开源 LLM 友好型 Web 爬虫和抓取工具-AI应用
    • AI Scientist:用于全自动科学发现的写论文应用-AI应用
    • ai-data-analysis-MulitAgent:一种用于自动化复杂研究过程的先进多智能体系统-AI应用
    • Aider:最好的免费开源 AI 编码助手,自动git提交代码!-AI应用
    • AIHawk:你的 AI 求职助手,自动化申请、获得个性化推荐,更快找到您梦想的工作-AI应用
  • 加入会员社区
  • About Us
No Result
View All Result
欢迎来到ClaudeAI博客社区
No Result
View All Result
Home chatgpt

OpenAI API 结构化输出手把手实践

小远 by 小远
2024-09-13
in chatgpt
0

目录

Toggle
  • 摘要
  • 开发人工智能应用程序
  • 结构化输出在人工智能应用中的重要性
  • OpenAI 结构化输出入门
    • 设置你的环境
      • 先决条件
      • 设置 OpenAI API
  • 使用 Pydantic 定义输出架构
  • 使用解析助手
  • 嵌套 Pydantic 模型来定义复杂模式
    • 步骤 1:定义 Pydantic 模型
    • 第 2 步:在 API 调用中使用嵌套的 Pydantic 模型
  • 具有结构化输出的函数调用
  • 使用 OpenAI 结构化输出时的最佳实践
  • 结论

摘要

了解如何开始使用 OpenAI Structured Outputs,理解其新语法,并探索其关键应用。

2024 年 8 月,OpenAI 宣布了其 API 中一项强大的新功能——结构化输出。顾名思义,借助此功能,您可以确保 LLM 仅以您指定的格式生成响应。此功能将使构建需要精确数据格式的应用程序变得更加容易。 

在本教程中,您将学习如何开始使用 OpenAI Structured Outputs,了解其新语法,并探索其关键应用。

开发人工智能应用程序

学习使用 OpenAI API 构建 AI 应用程序。

免费开始提升技能

结构化输出在人工智能应用中的重要性

确定性响应,或者换句话说,格式一致的响应,对于许多任务(例如数据输入、信息检索、问答、多步骤工作流等)至关重要。您可能已经体验过 LLM 如何生成格式截然不同的输出,即使提示相同。

例如,考虑这个classify_sentiment由 GPT-4o 支持的简单函数:

# List of hotel reviews
reviews = [
   "The room was clean and the staff was friendly.",
   "The location was terrible and the service was slow.",
   "The food was amazing but the room was too small.",
]
# Classify sentiment for each review and print the results
for review in reviews:
   sentiment = classify_sentiment(review)
   print(f"Review: {review}\nSentiment: {sentiment}\n")

output:
Review: The room was clean and the staff was friendly.
Sentiment: Positive
Review: The location was terrible and the service was slow.
Sentiment: Negative
Review: The food was amazing but the room was too small.
Sentiment: The sentiment of the review is neutral.

尽管前两个响应采用相同的单词格式,但最后一个响应却是一整句话。如果其他下游应用程序依赖于上述代码的输出,它就会崩溃,因为它本来期望的是单词响应。

我们可以通过一些提示工程来解决这个问题,但这是一个耗时且反复的过程。即使有完美的提示,我们也不能 100% 确定响应在未来的请求中会符合我们的格式。当然,除非我们使用结构化输出:

def classify_sentiment_with_structured_outputs(review):
   """Sentiment classifier with Structured Outputs"""
   ...
# Classify sentiment for each review with Structured Outputs
for review in reviews:
   sentiment = classify_sentiment_with_structured_outputs(review)
   print(f"Review: {review}\nSentiment: {sentiment}\n")

output:
Review: The room was clean and the staff was friendly.
Sentiment: {"sentiment":"positive"}
Review: The location was terrible and the service was slow.
Sentiment: {"sentiment":"negative"}
Review: The food was amazing but the room was too small.
Sentiment: {"sentiment":"neutral"}

借助新功能,classify_sentiment_with_structured_outputs所有响应均采用相同的格式。

这种强制语言模型采用固定格式的能力非常重要,它可以为您节省大量时间进行快速工程或依赖其他开源工具的时间。

OpenAI 结构化输出入门

在本节中,我们将使用情绪分析器功能的示例来分解结构化输出。

设置你的环境

先决条件

开始之前,请确保您已准备好以下物品:

  • 您的系统上安装了 Python 3.7 或更高版本。
  • OpenAI API 密钥。您可以通过在OpenAI 网站上注册来获取此密钥。

设置 OpenAI API

1.安装OpenAI Python 包:打开终端并运行以下命令来安装或更新OpenAI Python 包到最新版本:

$ pip install -U openai供电

2. 设置 API 密钥:您可以将 API 密钥设置为环境变量,也可以直接在代码中设置。要将其设置为环境变量,请运行:

$ export OPENAI_API_KEY='your-api-key'

3.验证安装:创建一个简单的 Python 脚本来验证安装:

from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
   model="gpt-4o-mini",
   messages=[
       {"role": "system", "content": "You are a helpful assistant."},
       {"role": "user", "content": "Say hello!"}
   ],
   max_tokens=5
)
>>> print(response.choices[0].message.content.strip())
Hello! How can I

运行脚本以确保一切设置正确。您应该看到模型的响应打印在终端中。

除了 OpenAI 包之外,您还需要 Pydantic 库来定义和验证结构化输出的 JSON 模式。使用 pip 安装它:

$ pip install pydantic

通过这些步骤,您的环境现在已经设置为使用 OpenAI 的结构化输出功能。

使用 Pydantic 定义输出架构

要使用结构化输出,您需要使用 Pydantic 模型定义预期的输出结构。Pydantic 是一个 Python 数据验证和设置管理库,允许您使用 Python 类型注释定义数据模型。然后可以使用这些模型来强制执行 OpenAI 模型生成的输出的结构。

以下是用于指定我们的评论情绪分类器格式的 Pydantic 模型示例:

from pydantic import BaseModel
from typing import Literal
class SentimentResponse(BaseModel):
   sentiment: Literal["positive", "negative", "neutral"]

在此示例中:

  • SentimentResponse是一个 Pydantic 模型,定义输出的预期结构。
  • 该模型有一个字段sentiment,它只能取三个文字值之一:“积极”、“消极”或“中性”。

当我们将该模型作为 OpenAI API 请求的一部分传递时,输出将只是我们提供的单词之一。

让我们看看如何。

使用解析助手

为了在 OpenAI 请求中强制执行我们的 Pydantic 模式,我们要做的就是将其传递给response_format聊天完成 API 的参数。大致如下:

response = client.beta.chat.completions.parse(
   model=MODEL,
   messages=[...],
   response_format=SentimentResponse
)

如果您注意到,我们不使用,而是client.chat.completions.create使用client.beta.chat.completions.parse方法。.parse()这是 Chat Completions API 中专为结构化输出编写的新方法。

现在,让我们通过使用结构化输出重写评论情绪分类器来将所有内容整合在一起。首先,我们进行必要的导入,定义 Pydantic 模型、系统提示和提示模板:

from openai import OpenAI
from pydantic import BaseModel
from typing import Literal
class SentimentResponse(BaseModel):
   sentiment: Literal["positive", "negative", "neutral"]
client = OpenAI()
MODEL = "gpt-4o-mini"
SYSTEM_PROMPT = "You are a sentiment classifier assistant."
PROMPT_TEMPLATE = """
   Classify the sentiment of the following hotel review as positive, negative, or neutral:\n\n{review}
"""

然后,我们编写一个使用辅助方法的新函数.parse():

# Function to classify sentiment using OpenAI's chat completions API with structured outputs
def classify_sentiment_with_structured_outputs(review):
   response = client.beta.chat.completions.parse(
       model=MODEL,
       messages=[
           {"role": "system", "content": SYSTEM_PROMPT},
           {"role": "user", "content": PROMPT_TEMPLATE.format(review=review)},
       ],
       response_format=SentimentResponse
   )
   return response.choices[0].message

该函数中最重要的一行是response_format=SentimentResponse,它实际上启用了结构化输出。

让我们在其中一条评论上测试一下:

# List of hotel reviews
reviews = [
   "The room was clean and the staff was friendly.",
   "The location was terrible and the service was slow.",
   "The food was amazing but the room was too small.",
]
result = classify_sentiment_with_structured_outputs(reviews[0])
>>> print(result.content)
{"sentiment":"positive"}

这result是一个消息对象:

>>> type(result)
openai.types.chat.parsed_chat_completion.ParsedChatCompletionMessage[SentimentResponse]

除了.content检索响应的属性之外,它还有一个.parsed属性,可以将解析的信息作为类返回:

>>> result.parsed
SentimentResponse(sentiment='positive')

如您所见,我们得到了该类的一个实例SentimentResponse。这意味着我们可以使用属性以字符串而不是字典的形式访问情绪.sentiment:

>>> result.parsed.sentiment
'positive'

嵌套 Pydantic 模型来定义复杂模式

在某些情况下,您可能需要定义涉及嵌套数据的更复杂的输出结构。Pydantic 允许您将模型嵌套在一起,从而让您能够创建可以处理各种用例的复杂模式。这在处理分层数据或需要为复杂输出强制执行特定结构时特别有用。

让我们考虑一个例子,其中我们需要提取详细的用户信息,包括他们的姓名、联系方式和地址列表。每个地址都应包含街道、城市、州和邮政编码的字段。这需要多个 Pydantic 模型来构建正确的模式。

步骤 1:定义 Pydantic 模型

首先,我们为地址和用户信息定义 Pydantic 模型:

from pydantic import BaseModel
from typing import List
# Define the Pydantic model for an address
class Address(BaseModel):
   street: str
   city: str
   state: str
   zip_code: str
# Define the Pydantic model for user information
class UserInfo(BaseModel):
   name: str
   email: str
   phone: str
   addresses: List[Address]

在此示例中:

  • Address是一个定义地址结构的 Pydantic 模型。
  • UserInfo是一个 Pydantic 模型,其中包含对象列表Address以及用户姓名、电子邮件和电话号码的字段。

第 2 步:在 API 调用中使用嵌套的 Pydantic 模型

接下来,我们使用这些嵌套的 Pydantic 模型来强制 OpenAI API 调用中的输出结构:

SYSTEM_PROMPT = "You are a user information extraction assistant."
PROMPT_TEMPLATE = """ Extract the user information from the following text:\n\n{text}"""
# Function to extract user information using OpenAI's chat completions API with structured outputs
def extract_user_info(text):
   response = client.beta.chat.completions.parse(
       model=MODEL,
       messages=[
           {"role": "system", "content": SYSTEM_PROMPT},
           {"role": "user", "content": PROMPT_TEMPLATE.format(text=text)},
       ],
       response_format=UserInfo
   )
   return response.choices[0].message
# Example text containing user information
text = """John DoeEmail: john.doe@example.comPhone: 123-456-7890Addresses:- 123 Main St, Springfield, IL, 62701- 456 Elm St, Shelbyville, IL, 62702"""
# Extract user information and print the results
user_info = extract_user_info(text)

示例文本完全不可读,关键信息之间缺少空格。让我们看看模型是否成功。我们将使用该json库来美化响应:

import json
data = json.loads(user_info.content)
pretty_response = json.dumps(data, indent=2)
print(pretty_response)
{
 "name": "John Doe",
 "email": "john.doe@example.com",
 "phone": "123-456-7890",
 "addresses": [
   {
     "street": "123 Main St",
     "city": "Springfield",
     "state": "IL",
     "zip_code": "62701"
   },
   {
     "street": "456 Elm St",
     "city": "Shelbyville",
     "state": "IL",
     "zip_code": "62702"
   }
 ]
}

如您所见,根据我们提供的模式,模型正确地捕获了单个用户的信息及其两个单独的地址。

简而言之,通过嵌套 Pydantic 模型,您可以定义处理分层数据的复杂模式并为复杂输出强制特定结构。

具有结构化输出的函数调用

函数调用(也称为工具调用)是较新的语言模型的广泛特性之一。此功能允许您将语言模型连接到用户定义的函数,从而有效地使它们(模型)能够访问外部世界。

一些常见的例子是:

  • 检索实时数据(例如天气、股票价格、体育比分)
  • 执行计算或数据分析
  • 查询数据库或 API
  • 生成图像或其他媒体
  • 在多种语言之间翻译文本
  • 控制智能家居设备或物联网系统
  • 执行自定义业务逻辑或工作流

我们不会在这里详细介绍函数调用的工作原理,但您可以阅读我们的OpenAI 函数调用教程。

重要的是要知道,使用结构化输出,使用 OpenAI 模型进行函数调用变得更加容易。过去,传递给 OpenAI 模型的函数需要编写复杂的 JSON 模式,并使用类型提示概述每个函数参数。以下是一个例子:

{
   "type": "function",
   "function": {
       "name": "get_current_weather",
       "description": "Get the current weather",
       "parameters": {
           "type": "object",
           "properties": {
               "location": {
                   "type": "string",
                   "description": "The city and state, e.g. San Francisco, CA",
               },
               "format": {
                   "type": "string",
                   "enum": ["celsius", "fahrenheit"],
                   "description": "The temperature unit to use. Infer this from the users location.",
               },
           },
           "required": ["location", "format"],
       },
   }
}

尽管get_current_weather函数有两个参数,但其 JSON 模式变得非常庞大,并且手动编写容易出错。

通过再次使用 Pydantic 模型,结构化输出解决了这个问题:

from pydantic import BaseModel
from typing import Literal
def get_weather(location: str, unit: str, condition: str):
   # Implementation details...
   pass
class WeatherData(BaseModel):
   location: str
   unit: Literal["celsius", "fahrenheit"]
   condition: Literal["sunny", "cloudy", "rainy", "snowy"]

首先,编写函数本身及其逻辑。然后,使用 Pydantic 模型再次定义它,并指定预期的输入参数。

然后,要将 Pydantic 模型转换为兼容的 JSON 模式,请调用pydantic_function_tool:

>>> import openai
>>> openai.pydantic_function_tool(WeatherData)
{'type': 'function',
'function': {'name': 'WeatherData',
 'strict': True,
 'parameters': {'properties': {'location': {'title': 'Location',
    'type': 'string'},
   'unit': {'enum': ['celsius', 'fahrenheit'],
    'title': 'Unit',
    'type': 'string'},
   'condition': {'enum': ['sunny', 'cloudy', 'rainy', 'snowy'],
    'title': 'Condition',
    'type': 'string'}},
  'required': ['location', 'unit', 'condition'],
  'title': 'WeatherData',
  'type': 'object',
  'additionalProperties': False}}}

以下是如何将此工具用作请求的一部分:

import openai
client = OpenAI()
tools = [openai.pydantic_function_tool(WeatherData)]
messages = [
   {
       "role": "system",
       "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user.",
   },
   {
       "role": "user",
       "content": "What is the weather in Tokyo?",
   }
]
response = client.chat.completions.create(
   model=MODEL, messages=messages, tools=tools
)
tool_call = response.choices[0].message.tool_calls[0]
>>> tool_call
ChatCompletionMessageToolCall(id='call_QnZZ0DmNN2cxw3bN433JQNIC', function=Function(arguments='{"location":"Tokyo","unit":"celsius","condition":"sunny"}', name='WeatherData'), type='function')

我们将兼容 JSON 格式的 Pydantic 模型传递给toolsChat Completions API 的参数。然后,根据我们的查询,模型决定是否调用该工具。

由于我们在上面的例子中的查询是“东京的天气如何?”,我们在tool_calls返回的消息对象中看到一个调用。

请记住,模型不会调用get_weather函数,而是根据我们提供的 Pydantic 模式为其生成参数:

arguments = json.loads(tool_call.function.arguments)
>>> arguments
{'location': 'Tokyo', 'unit': 'celsius', 'condition': 'sunny'}

我们可以使用提供的参数来调用该函数:

some_result = get_weather(**arguments)

如果您希望模型生成函数的参数并同时调用它,那么您正在寻找人工智能代理。 

如果您有兴趣,我们有单独的LangChain Agents 教程。

使用 OpenAI 结构化输出时的最佳实践

使用结构化输出时,需要牢记一些最佳实践和建议。在本节中,我们将概述其中的一些。

  1. 使用 Pydantic 模型来定义输出模式,因为它们提供了一种干净且类型安全的方式来定义预期的输出结构。
  2. 保持模式简单而具体以获得最准确的结果。
  3. 使用适当的数据类型(,,,,,,str)来准确表示您的数据。intfloatboolListDict
  4. 使用Literal枚举类型来定义字段的特定允许值。
  5. 处理模型拒绝。使用新.parse()方法时,消息对象有一个新.refusal属性来指示拒绝:
text = """John DoeEmail: john.doe@example.comPhone: 123-456-7890Addresses:- 123 Main St, Springfield, IL, 62701- 456 Elm St, Shelbyville, IL, 62702"""
user_info = extract_user_info(text)
if user_info.refusal:
   print(user_info.refusal)
else:
   print(user_info.content)

输出:

{"name":"John Doe","email":"john.doe@example.com","phone":"123-456-7890","addresses":[{"street":"123 Main St","city":"Springfield","state":"IL","zip_code":"62701"},{"street":"456 Elm St","city":"Shelbyville","state":"IL","zip_code":"62702"}]}

6.为Pydantic模型中的每个字段提供清晰简洁的描述,以提高模型输出精度:

from pydantic import BaseModel, Field
class Person(BaseModel):
   name: str = Field(..., description="The person's full name")
   age: int = Field(..., description="The person's age in years")
   occupation: str = Field(..., description="The person's current job or profession")

这些实践将大大有助于在您的应用程序中最有效地利用结构化输出。

结论

在本教程中,我们学习了如何开始使用新的 OpenAI API 功能:结构化输出。我们已经了解了此功能如何强制语言模型以我们指定的格式生成输出。我们已经学习了如何将其与函数调用结合使用,并探索了一些最佳实践以充分利用该功能。

以下是一些相关资料,可以增强您的理解:

  • 使用 OpenAI API 课程
  • OpenAI 基础课程
  • 使用 LangChain 课程开发 LLM 应用程序
  • LangChain 与 LlamaIndex:详细比较
Tags: APIJSONOpenAI
Previous Post

周公解梦AI助手-AI提示库

Next Post

Reflection Llama-3.1 70B:测试和已知内容的总结

小远

小远

大家好,我是小远,毕业于华南理工大学。作为一名AI算法工程师,我创立了个人博客,旨在成为连接AI技术前沿与广大爱好者的桥梁。宗旨是:记录并分享关于AI大模型的最新知识、研究成果及行业动态,致力于普及AI知识,降低技术门槛,让更多人能够了解并参与到这场科技革命中来。

Related Posts

MiniMind 3小时完全从0训练26M的小参数GPT,个人显卡即可推理训练!-AI应用
AI应用

MiniMind 3小时完全从0训练26M的小参数GPT,个人显卡即可推理训练!-AI应用

2024-10-17
OpenAI Swarm是什么和使用用例
chatgpt

OpenAI Swarm是什么和使用用例

2024-10-15
第3章:使用工具调用强制 JSON结构输出[以提取维基百科页面文章为例]-Claude工具调用教程
chatgpt

OpenAI o1 指南:工作原理、用例、API 等

2024-09-19
Load More
Next Post
Reflection Llama-3.1 70B:测试和已知内容的总结

Reflection Llama-3.1 70B:测试和已知内容的总结

Please login to join discussion
Do NOT Think That Much for 2+3=? On the Overthinking of o1-Like LLMs[不要过度思考2+3等于几 在类LLM的过度思考上]-AI论文
claude

Do NOT Think That Much for 2+3=? On the Overthinking of o1-Like LLMs[不要过度思考2+3等于几 在类LLM的过度思考上]-AI论文

by 小远
2025-01-12
0

图1:在图(a)中过度思考问题的示意图:...

Read more
Slow Perception: Let’s Perceive Geometric Figures Step-by-step[缓慢感知:让我们逐步感知几何图形]-AI论文
AI论文

Slow Perception: Let’s Perceive Geometric Figures Step-by-step[缓慢感知:让我们逐步感知几何图形]-AI论文

by 小远
2025-01-12
0

摘要 近期,“视觉感知”开始进入人们的视...

Read more
Ensembling Large Language Models with Process Reward-Guided Tree Search for Better Complex Reasoning[结合大型语言模型与过程奖励引导的树搜索以提升复杂推理能力]-AI论文
claude

Ensembling Large Language Models with Process Reward-Guided Tree Search for Better Complex Reasoning[结合大型语言模型与过程奖励引导的树搜索以提升复杂推理能力]-AI论文

by 小远
2025-01-12
0

摘要 尽管大型语言模型近期取得了显著进展...

Read more
Large Concept Models:Language Modeling in a Sentence Representation Space[大型概念模型:在句子表示空间中的语言建模]-AI论文
AI论文

Large Concept Models:Language Modeling in a Sentence Representation Space[大型概念模型:在句子表示空间中的语言建模]-AI论文

by 小远
2025-01-06
0

大型语言模型(LLMs)已经彻底改变了人...

Read more

Claude大模型学习社区

希望成为中国第一个大模型教程和AI论文的分享乐园!帮助每一位同学快速上入门大模型!

分类

  • AIRAG
  • AI应用
  • AI提示库
  • AI论文
  • artifacts
  • chatgpt
  • claude
  • claude教程
  • Cursor
  • gemini
  • llama
  • ollama
  • openAIo1
  • prompt工程
  • 文心一言

标签

Agent Agents AI工具 AI应用 AI提示库 AI论文 API chatgpt claude Claude3.5 Sonnet COT css Cursor CursorAI ernie html IDE Llama 3 llama3.1 llama3.2 LLM meta o1 o1-preview ollama OpenAI openAIo1 OpenAI o1 openAIo1原理 prompt rag Reasoning Swarm web 函数构建 原理解读 合成数据 多智能体 大模型 强化学习 思维链 接码平台 提示词 智能体 检索增强
  • Home
  • AI教程
  • AI大模型
  • AI论文
  • AI应用
  • 加入会员社区
  • About Us

© 2024 ClaudeAI大模型学习社区 所有属于ICP备案:豫ICP备2024068873号-1号.

No Result
View All Result
  • Home
  • AI教程
    • 大模型应用实践课程
      • 大型语言模型课程介绍
      • 第1-1章:使用 OpenAI 创建您的第一个聊天机器人-大模型应用实践教程
      • 第1-2章:使用 OpenAI 创建简单的自然语言到 SQL-大模型应用实践教程
    • Claude应用开发教程
      • 第1章:基本提示结构-Claude应用开发教程
      • 第 2 章:基本函数构建-Claude开发应用教程
      • 第3章:角色扮演提示-Claude应用开发教程
      • 第4章 分离数据和指令-Claude开发应用教程
      • 第 5 章:格式化输出-Claude应用开发教程
      • 第6章:预知(Thinking Step by Step)-Claude应用开发教程
    • Claude提示词教程
      • 第 1 课:基本提​​示词技术-Claude提示词工程课程
      • 第 2 课:医疗病例摘要助手[XML-JSON格式化输出]-Claude提示词工程教程
      • 第 3 课:提示工程迭代流程-Claude提示词工程课程
      • 第 4 课:客服聊天记录总结生成器-Claude提示词课程
    • Claude函数/工具调用教程
      • 第3章:使用工具调用强制 JSON结构输出[以提取维基百科页面文章为例]-Claude工具调用教程
      • 第2章:ClaudeAPI如何构建工具并调用[以计算器工具调用为例]-Claude工具调用教程
      • 第1章:工具使用/函数调用介绍和概述-Claude函数工具调用教程
    • ClaudeAPI基础入门教程
      • 第2章:构建简单的多轮聊天机器人-ClaudeAPI基础入门教程
      • 第1章:Claude SDK安装和使用-CLaudeAPI基础入门教程
      • Claude API基础知识课程简介
  • AI大模型
    • chatgpt
      • OpenAI o1-preview 简介和如何使用
      • 如何设置使用新的 ChatGPT o1 模型
      • OpenAI o1 性能评估和学习推理思维链介绍
      • OpenAI o1-mini和gpt4o性能对比介绍
      • OpenAI o1 模型与GPT-4o模型使用区别和场景
    • Claude
      • Claude的project介绍和创建
      • Claude Sonnet 3.5 API 教程
      • Claude 3 最新注册使用教程!(国内版)-性能完爆GPT4o!
      • Claude3.5 vs GPT4 谁才是最强大模型?
      • Claude国内无法登录的解决办法
      • Claude3.5 Sonnet的详细介绍
      • Claude如何写好提示词
      • Claude快速入门指南
    • Llama3
      • Llama3.2最新90b 11b 3b 1b模型介绍-AI大模型
      •  Llama Stack入门安装指南[结合Ollama]-AI大模型
      • Llama 3.2介绍最全技术报告-AI大模型
      • Llama 3.1技术报告:Meta推出最强开源大模型
      • Meta 的 Llama 3.1 405B工作原理、用例等
      • 如何在本地安装和运行 Meta Llama 3.1
      • 使用 Ollama和租用GPU 运行 Llama 3.1 405B:分步指南
      • Llama3.1 Colab 微调指南
  • AI论文
    • OpenAIo1原理解读:重复采样扩展推理计算[Large Language Monkeys: Scaling Inference Compute with Repeated Sampling]-AI论文
    • OpenIAo1原理解读:Q*强化学习与启发式搜索推理框架[Q*: Improving Multi-step Reasoning for LLMs with Deliberative Planning]-AI论文
    • OpenAIo1原理解读:基于蒙特卡罗树搜索的自我博弈互相推理[Self-play Mutual Reasoning]-AI论文
    • openAIo1原理解读:推理增加计算量提高大模型效果[Scaling LLM Test-Time Compute Optimally can be More Effective]-AI论文
    • OpenAI o1大模型原理解读:自博弈Self-play强化学习方法[A Survey on Self-play Methods in Reinforcement Learning]-AI论文
    • OpenAI o1大模型原理解读:Quiet-STaR 推理思维链[Language Models Can Teach Themselves to Think Before Speaking]-AI论文
    • OpenAI o1大模型原理论文汇总[Awesome LLM Strawberry]-AI论文
  • AI应用
    •  Crawl4AI:开源 LLM 友好型 Web 爬虫和抓取工具-AI应用
    • AI Scientist:用于全自动科学发现的写论文应用-AI应用
    • ai-data-analysis-MulitAgent:一种用于自动化复杂研究过程的先进多智能体系统-AI应用
    • Aider:最好的免费开源 AI 编码助手,自动git提交代码!-AI应用
    • AIHawk:你的 AI 求职助手,自动化申请、获得个性化推荐,更快找到您梦想的工作-AI应用
  • 加入会员社区
  • About Us

© 2024 ClaudeAI大模型学习社区 所有属于ICP备案:豫ICP备2024068873号-1号.

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In