当前位置: 首页 > news >正文

可以看设计的网站有哪些湖人排名最新

可以看设计的网站有哪些,湖人排名最新,涿州网站建设,遵义企业网站建设在调用 OpenAI(比如:GPT-4o)接口时,希望返回的结果是能够在后续任务中自动化处理的结构化 / JSON 输出。GPT 版本:gpt-4o-2024-08-06,提供了这样的功能。 目标:从非结构化输入到结构化数据&…

        在调用 OpenAI(比如:GPT-4o)接口时,希望返回的结果是能够在后续任务中自动化处理的结构化 / JSON 输出。GPT 版本:gpt-4o-2024-08-06,提供了这样的功能。

        目标:从非结构化输入到结构化数据(比如:JSON)。

目录

1. 结构化输出

1.1 简介

 1.2 优点

2. 接口

2.1 官方代码

2.2 Pydantic

2.2.1 简介

2.2.2 示例

2.2.3 特点

2.3 Python 代码

3. 异常

3.1 ValidationError

3.2 解决

3.3 例子

3.3.1 Prompt

3.3.2 Pydantic

3.3.3 API

3.3.4 数据验证


1. 结构化输出

1.1 简介

        来源:Introducing Structured Outputs in the API | OpenAI

Introducing Structured Outputs in the API

We are introducing Structured Outputs in the API—model outputs now reliably adhere to developer-supplied JSON Schemas.

在 API 中引入结构化输出

我们在 API 中引入了结构化输出 — 模型输出现在可靠地遵循开发人员提供的 JSON 架构。

         来源:Structured Outputs - OpenAI API

JSON is one of the most widely used formats in the world for applications to exchange data.

Structured Outputs is a feature that ensures the model will always generate responses that adhere to your supplied JSON Schema, so you don't need to worry about the model omitting a required key, or hallucinating an invalid enum value.

在 Pydantic 中,可以通过 Optional 或者 default 参数来设置可选字段和默认值。
结构化输出是一项功能,可确保模型始终生成符合您提供的 JSON 模式的响应,因此您不必担心模型会遗漏必需的键或产生无效枚举值的幻觉

 1.2 优点

  • Reliable type-safety: No need to validate or retry incorrectly formatted responses
  • Explicit refusals: Safety-based model refusals are now programmatically detectable
  • Simpler prompting: No need for strongly worded prompts to achieve consistent formatting

2. 接口

2.1 官方代码

        官方的文档指出:

In addition to supporting JSON Schema in the REST API, the OpenAI SDKs for Python and JavaScript also make it easy to define object schemas using Pydantic and Zod respectively.

除了在 REST API 中支持 JSON Schema 之外,OpenAI 的 Python 和 JavaScript SDK 还可以轻松使用 Pydantic 和 Zod 分别定义对象模式

        表明,对于 Python 程序,可选的方法有两种:一种是 JSON Schema,另一种是使用 Pydantic

2.2 Pydantic

2.2.1 简介

        Pydantic Python 使用最广泛的数据验证库。

2.2.2 示例

        这里先展示一个 Pydantic 官方文档给的示例。

from datetime import datetimefrom pydantic import BaseModel, PositiveIntclass User(BaseModel):id: int  name: str = 'John Doe'  signup_ts: datetime | None  tastes: dict[str, PositiveInt]  external_data = {'id': 123,'signup_ts': '2019-06-01 12:22',  'tastes': {'wine': 9,b'cheese': 7,  'cabbage': '1',  },
}user = User(**external_data)  print(user.id)  
#> 123
print(user.model_dump())  
"""
{'id': 123,'name': 'John Doe','signup_ts': datetime.datetime(2019, 6, 1, 12, 22),'tastes': {'wine': 9, 'cheese': 7, 'cabbage': 1},
}
"""

        这里,类 User 继承自 pydantic.BaseModel,其定义了四个属性:id, name, signup_ts, tastes,同时定义了他们的数据类型

        后续定义了一个 Python dict 型变量 external_data 赋值

        在 user = User(**external_data) 中,将 dict 字典解包的方式传递给类 User,得到对象 user

        需要注意的是,如果 external_data 中有多余的字段或类型不匹配Pydantic 会抛出相应的错误。如下展示官方提供的示例。

# continuing the above example...from datetime import datetime
from pydantic import BaseModel, PositiveInt, ValidationErrorclass User(BaseModel):id: intname: str = 'John Doe'signup_ts: datetime | Nonetastes: dict[str, PositiveInt]external_data = {'id': 'not an int', 'tastes': {}}  try:User(**external_data)  
except ValidationError as e:print(e.errors())"""[{'type': 'int_parsing','loc': ('id',),'msg': 'Input should be a valid integer, unable to parse string as an integer','input': 'not an int','url': 'https://errors.pydantic.dev/2/v/int_parsing',},{'type': 'missing','loc': ('signup_ts',),'msg': 'Field required','input': {'id': 'not an int', 'tastes': {}},'url': 'https://errors.pydantic.dev/2/v/missing',},]"""

        这里报异常:ValidationError。由于 external_data 中的 id 类型不是 int,且缺少了 signup_ts。故异常为两个方面。 

2.2.3 特点

        ***个人感觉Pydantic Java 的类有很多相似之处,尤其是在数据模型和验证方面:

  • 数据结构定义

        在 Java 中,通常通过类(Class)来定义数据结构,其中属性由类成员变量表示。

        Pydantic 也是通过 Python 的类来定义数据模型,属性通常是类的字段(Field)

  • 类型约束

        Java 是强类型语言,类的成员变量通常都有明确的类型(如 int, String 等)。

        Pydantic 也允许在类定义时指定字段的类型,并且在创建实例时进行类型检查和验证。

  • 数据验证

        在 Java 中,可以通过构造函数、setter 方法或其他工具进行输入数据的验证。

        Pydantic 内置了强大的数据验证功能,它会根据你定义的类型自动进行验证并在必要时提供详细的错误信息

  • 默认值和可选值

        在 Java 类中,可以通过构造函数或者设置默认值来定义可选字段。

       在 Pydantic 中,可以通过 Optional 或者 default 参数来设置可选字段和默认值。 

2.3 Python 代码

        这里直接粘贴官方代码。

from pydantic import BaseModel
from openai import OpenAIapi_key = '你的KEY'
base_url = '你的URL'client = OpenAI(api_key=api_key, base_url=base_url)class Step(BaseModel):explanation: stroutput: strclass MathReasoning(BaseModel):steps: list[Step]final_answer: strcompletion = client.beta.chat.completions.parse(model="gpt-4o-2024-08-06",messages=[{"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."},{"role": "user", "content": "how can I solve 8x + 7 = -23"}],response_format=MathReasoning,
)math_reasoning = completion.choices[0].message.parsed# Example response 
"""
{"steps": [{"explanation": "Start with the equation 8x + 7 = -23.","output": "8x + 7 = -23"},{"explanation": "Subtract 7 from both sides to isolate the term with the variable.","output": "8x = -23 - 7"},{"explanation": "Simplify the right side of the equation.","output": "8x = -30"},{"explanation": "Divide both sides by 8 to solve for x.","output": "x = -30 / 8"},{"explanation": "Simplify the fraction.","output": "x = -15 / 4"}],"final_answer": "x = -15 / 4"
}
"""

        这里需要注意的是,调用的 API 接口是 client.beta.chat.completions.parse,接口参数中有一个 response_format=MathReasoning,其中赋值的是自定义继承pydantic.BaseModel 的类。

        官方给出两种形式的结构化输出:function callingjson_schema。前者就是这里的自定义类,后者是 JSON。具体使用哪种应需求选择。

  • If you are connecting the model to tools, functions, data, etc. in your system, then you should use function calling
  • If you want to structure the model's output when it responds to the user, then you should use a structured response_format

  • 如果要将模型连接到系统中的工具、函数、数据等,则应使用函数调用
  • 如果你想在响应用户时构建模型的输出,那么你应该使用结构化的 response_format

        说白了,就是 GPT-4o 返回的结果是需要后续程序调用,则选 function calling,如果直接返回给用户,则提供 JSON 格式。 

3. 异常

3.1 ValidationError

但实际操作时,执行官方提供的代码,我个人遇到了不可解决的问题。

pydantic_core._pydantic_core.ValidationError: 1 validation error for MathReasoningInvalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value="To solve the equation \\...ation or more examples!", input_type=str]For further information visit https://errors.pydantic.dev/2.8/v/json_invalid

        即,提示解析的 ValidationError

3.2 解决

        经多次尝试,仍提示数据验证异常。不清楚是什么原因导致,OpenAI 社区也有同样的问题:Official example MathResponse raise invalid json - API - OpenAI Developer Forum。但还没看到有效的解决方案。

        我的解决方案是,采用 json_object 格式来获取 OpenAI response,然后再交于自定义的 MathReasoning 进行数据验证。但这需要增加额外的 Prompt,且 Prompt必须严格给出 JSON 格式的示例,并强制要求 GPT-4o 返回 JSON 格式。官方就此给出了重要说明:

  • When using JSON mode, you must always instruct the model to produce JSON via some message in the conversation, for example via your system message. If you don't include an explicit instruction to generate JSON, the model may generate an unending stream of whitespace and the request may run continually until it reaches the token limit. To help ensure you don't forget, the API will throw an error if the string "JSON" does not appear somewhere in the context.
  • JSON mode will not guarantee the output matches any specific schema, only that it is valid and parses without errors. You should use Structured Outputs to ensure it matches your schema, or if that is not possible, you should use a validation library and potentially retries to ensure that the output matches your desired schema.

  • 使用 JSON 模式时,您必须始终指示模型通过对话中的某些消息生成 JSON,例如通过您的系统消息。如果您不包含生成 JSON 的明确指令,则模型可能会生成无休止的空格流,并且请求可能会持续运行,直到达到令牌限制。为了帮助确保您不会忘记,如果字符串 “JSON” 未出现在上下文中的某个位置,API 将引发错误。
  • JSON 模式不保证输出与任何特定架构匹配,只保证它是有效的并且解析没有错误您应该使用结构化输出来确保它与您的架构匹配,或者如果无法匹配,则应使用验证库并可能重试,以确保输出与所需的架构匹配。

        此时,需要修改调用的 API 接口,同时修改参数 response_format={"type": "json_object"}切记得额外输入带有强制要求输出 JSON Prompt

3.3 例子

        这里展示我实际生产中的一个例子。

3.3.1 Prompt

        给一个 JSON 的例子,且强调返回 JSON 格式。

Background:
XXX.Task Description:
Given a text pair, such as '{"left": "XXX", "right": "XXX"}'. Step 1: First, XXX. Step 2: Then, XXX. Step 3: Finally, XXX.Example:
input:
{"left": "XXX","right": "XXX"
}output (must be in JSON format):
{"relation": {"type": "XXX","subtype": "XXX","description": "XXX"},"left": {"type": "XXX","content": "XXX","explanation": "XXX"},"right": {"type": "XXX","content": "XXX","explanation": "XXX"},"category": "XXX"
}Note: You have to return the correct JSON format.
3.3.2 Pydantic

        自定义的结构化数据类,可以有层次结构,详细见官方文档:https://platform.openai.com/docs/guides/structured-outputs/supported-schemas。

from pydantic import BaseModelclass Relation(BaseModel):type: strsubtype: strdescription: strclass Detail(BaseModel):type: strcontent: strexplanation: strclass Annotation(BaseModel):relation: Relationleft: Detailright: Detailcategory: str
3.3.3 API

        API 的参数额外输入 prompt,同时修改 response_format={"type": "json_object"}

prompt = 'XXX. Note: You have to return the correct JSON format.'
content = 'XXX'completion = client.chat.completions.create(model="gpt-4o-2024-08-06",messages=[{"role": "system","content": "You are a helpful assistant designed to output JSON."},{"role": "user","content": prompt},{"role": "user","content": content}],response_format={"type": "json_object"},
)
response = completion.choices[0].message.content
3.3.4 数据验证

        上述返回的 response str 类型的 JSON。首先需要使用 json.loads(response) 转换为 JSON 对象。

        然后使用自定义类 Annotation 3.3.2 中定义的) 验证 JSON 是否合规,即符合 Annotation 类定义的数据结构以及子结构

        如果无异常,则可通过对象.属性方法获取对应的值。

import jsontry:row: json = json.loads(response)
except json.decoder.JSONDecodeError as e:print(e)try:annotation: Annotation = Annotation.model_validate(row)
except ValidationError as e:print(e)print(annotation.relation.type)

        个人实践中,错误率1% 左右,可按照 3.2 中官方的重要说明中讲的,进行多次重试,我的经验是重试一次即可。

http://www.hkea.cn/news/492942/

相关文章:

  • 浙江住房和城乡建设厅报名网站下拉关键词排名
  • 银川哪里做网站百度网址名称是什么
  • 合肥公司网站建设价格低西安网络科技公司排名
  • 怎么样建设个人网站企业文化建设
  • 如何知道网站有没有备案成都seo公司
  • wordpress 艺术主题南京网络优化公司有哪些
  • 贵阳网站备案百度网站优化方案
  • 单位网站建设论文怎么做竞价托管
  • 建筑公司网站有哪些谈谈自己对市场营销的理解
  • 做ppt音乐怎么下载网站企业培训课程有哪些
  • magento网站建设网站优化排名软件网站
  • 做生鲜食品最好的网站网络推广及销售
  • 销售管理系统需求分析长沙seo代理
  • 站长网站查询深圳百度关键字优化
  • 用net语言做网站平台好不好企业培训师资格证报考2022
  • 成都定制网站设竞价推广遇到恶意点击怎么办
  • 制作视频网站建设友链交易网
  • 做外贸是不是要有网站腾讯企点app下载安装
  • 网站开发快递文件国外网站怎么推广
  • 网站和搜索引擎站长论坛
  • 做违法网站会怎样外贸独立站怎么建站
  • 云主机建网站教程深圳全网推互联科技有限公司
  • 做网站赚50万谷歌搜索引擎363入口
  • 台州网站设计外包网页制作公司排名
  • 网站建设投标文件范本亚马逊提升关键词排名的方法
  • 学做网站需要多长时间免费推广平台排行
  • wordpress运行php 404360优化大师下载
  • seo排名网站 优帮云线上推广的三种方式
  • 平凉哪有做网站的百度推广登录入口官网网
  • 娄底网站优化自建网站平台有哪些