시가총액: $4.0994T 0.55%
거래량(24시간): $171.0937B -10.93%
  • 시가총액: $4.0994T 0.55%
  • 거래량(24시간): $171.0937B -10.93%
  • 공포와 탐욕 지수:
  • 시가총액: $4.0994T 0.55%
암호화
주제
암호화
소식
cryptostopics
비디오
최고의 뉴스
암호화
주제
암호화
소식
cryptostopics
비디오
bitcoin
bitcoin

$117366.968408 USD

0.60%

ethereum
ethereum

$4611.537173 USD

-0.02%

xrp
xrp

$3.089373 USD

0.06%

tether
tether

$1.000286 USD

-0.03%

bnb
bnb

$986.505381 USD

-0.03%

solana
solana

$247.629906 USD

0.68%

usd-coin
usd-coin

$0.999771 USD

-0.03%

dogecoin
dogecoin

$0.281380 USD

-0.26%

cardano
cardano

$0.931695 USD

1.71%

tron
tron

$0.352059 USD

2.40%

hyperliquid
hyperliquid

$58.226337 USD

-0.94%

chainlink
chainlink

$24.805082 USD

3.27%

avalanche
avalanche

$35.625687 USD

10.55%

ethena-usde
ethena-usde

$1.000922 USD

-0.02%

sui
sui

$3.883984 USD

2.13%

암호화폐 뉴스 기사

구조화된 출력으로 더 깊이 알아보기

2024/09/04 06:03

구조화된 출력 및 LLM에 대한 이해와 최적의 사용을 향상시키는 데 도움이 됩니다.

구조화된 출력으로 더 깊이 알아보기

In the previous article, we were introduced to structured outputs using OpenAI. Since the general availability release in ChatCompletions API (v1.40.0), structured outputs have been applied across dozens of use cases, and spawned numerous threads on OpenAI forums.

이전 기사에서는 OpenAI를 사용하여 구조화된 출력을 소개했습니다. ChatCompletions API(v1.40.0)의 일반 가용성 릴리스 이후 구조화된 출력이 수십 가지 사용 사례에 적용되었으며 OpenAI 포럼에 수많은 스레드가 생성되었습니다.

In this article, our aim is to provide you with an even deeper understanding, dispel some misconceptions, and give you some tips on how to apply them in the most optimal manner possible, across different scenarios.

이 글에서 우리의 목표는 여러분에게 더 깊은 이해를 제공하고, 몇 가지 오해를 없애고, 다양한 시나리오에 걸쳐 가능한 가장 최적의 방식으로 적용하는 방법에 대한 몇 가지 팁을 제공하는 것입니다.

## Structured outputs overview

## 구조화된 출력 개요

Structured outputs are a way of enforcing the output of an LLM to follow a pre-defined schema — usually a JSON schema. This works by transforming the schema into a context free grammar (CFG), which during the token sampling step, is used together with the previously generated tokens, to inform which subsequent tokens are valid. It’s helpful to think of it as creating a regex for token generation.

구조화된 출력은 LLM의 출력이 사전 정의된 스키마(일반적으로 JSON 스키마)를 따르도록 강제하는 방법입니다. 이는 스키마를 CFG(Context Free Grammar)로 변환하여 작동합니다. CFG는 토큰 샘플링 단계에서 이전에 생성된 토큰과 함께 사용되어 어떤 후속 토큰이 유효한지 알려줍니다. 토큰 생성을 위한 정규식을 만드는 것으로 생각하면 도움이 됩니다.

OpenAI API implementation actually tracks a limited subset of JSON schema features. With more general structured output solutions, such as Outlines, it is possible to use a somewhat larger subset of the JSON schema, and even define completely custom non-JSON schemas — as long as one has access to an open weight model. For the purpose of this article, we will assume the OpenAI API implementation.

OpenAI API 구현은 실제로 JSON 스키마 기능의 제한된 하위 집합을 추적합니다. 아웃라인과 같은 보다 일반적인 구조화된 출력 솔루션을 사용하면 JSON 스키마의 다소 큰 하위 집합을 사용할 수 있으며, 개방형 가중치 모델에 액세스할 수 있는 한 완전히 사용자 정의된 비 JSON 스키마를 정의할 수도 있습니다. 이 기사에서는 OpenAI API 구현을 가정합니다.

### JSON Schema and Pydantic

### JSON 스키마 및 Pydantic

According to JSON Schema Core Specification, “JSON Schema asserts what a JSON document must look like, ways to extract information from it, and how to interact with it”. JSON schema defines six primitive types — null, boolean, object, array, number and string. It also defines certain keywords, annotations, and specific behaviours. For example, we can specify in our schema that we expect an array and add an annotation that minItems shall be 5 .

JSON 스키마 핵심 사양에 따르면, "JSON 스키마는 JSON 문서가 어떤 모습이어야 하는지, 문서에서 정보를 추출하는 방법 및 상호 작용하는 방법을 주장합니다." JSON 스키마는 null, 부울, 객체, 배열, 숫자 및 문자열의 6가지 기본 유형을 정의합니다. 또한 특정 키워드, 주석 및 특정 동작을 정의합니다. 예를 들어, 스키마에 배열이 필요하다고 지정하고 minItems가 5가 되어야 한다는 주석을 추가할 수 있습니다.

Pydantic is a Python library that implements the JSON schema specification. We use Pydantic to build robust and maintainable software in Python. Since Python is a dynamically typed language, data scientists do not necessarily think in terms of variable types — these are often implied in their code. For example, a fruit would be specified as:

Pydantic은 JSON 스키마 사양을 구현하는 Python 라이브러리입니다. 우리는 Python으로 강력하고 유지 관리가 가능한 소프트웨어를 구축하기 위해 Pydantic을 사용합니다. Python은 동적으로 유형이 지정되는 언어이기 때문에 데이터 과학자는 반드시 변수 유형 측면에서 생각하지 않습니다. 이는 종종 코드에 암시되어 있습니다. 예를 들어 과일은 다음과 같이 지정됩니다.

```python

``파이썬

fruit = "apple"

과일 = "사과"

```

````

…while a function declaration that returns “fruit” from some piece of data would often be specified as:

...일부 데이터에서 "과일"을 반환하는 함수 선언은 종종 다음과 같이 지정됩니다.

```python

``파이썬

def get_fruit(data):

def get_fruit(데이터):

return data

데이터 반환

```

````

Pydantic on the other hand allows us to generate a JSON-schema compliant class, with properly annotated variables and type hints, making our code more readable/maintainable and in general more robust, i.e.

반면에 Pydantic을 사용하면 적절하게 주석이 달린 변수와 유형 힌트를 사용하여 JSON 스키마 호환 클래스를 생성할 수 있으므로 코드를 더 읽기 쉽고 유지 관리하기 쉽고 일반적으로 더 견고하게 만들 수 있습니다.

```python

``파이썬

class Fruit(BaseModel):

클래스 과일(기본 모델):

fruit: str

과일: str

```

````

OpenAI actually strongly recommends the use of Pydantic for specifying schemas, as opposed to specifying the “raw” JSON schema directly. There are several reasons for this. Firstly, Pydantic is guaranteed to adhere to the JSON schema specification, so it saves you extra pre-validation steps. Secondly, for larger schemas, it is less verbose, allowing you to write cleaner code, faster. Finally, the openai Python package actually does some housekeeping, like setting additionalProperties to False for you, whereas when defining your schema “by-hand” using JSON, you would need to set these manually, for every object in your schema (failing to do so results in a rather annoying API error).

OpenAI는 실제로 "원시" JSON 스키마를 직접 지정하는 대신 스키마 지정을 위해 Pydantic을 사용할 것을 강력히 권장합니다. 여기에는 몇 가지 이유가 있습니다. 첫째, Pydantic은 JSON 스키마 사양을 준수하므로 추가 사전 검증 단계를 절약할 수 있습니다. 둘째, 더 큰 스키마의 경우 덜 장황하므로 더 깔끔한 코드를 더 빠르게 작성할 수 있습니다. 마지막으로 openai Python 패키지는 실제로 extraProperties를 False로 설정하는 등 몇 가지 관리 작업을 수행하는 반면, JSON을 사용하여 스키마를 "직접" 정의할 때는 스키마의 모든 객체에 대해 이를 수동으로 설정해야 합니다(실패함). 그래서 다소 성가신 API 오류가 발생합니다).

## Limitations

## 제한사항

As we alluded to previously, the ChatCompletions API provides a limited subset of the full JSON schema specification. There are numerous keywords that are not yet supported, such as minimum and maximum for numbers, and minItems and maxItems for arrays — annotations that would be otherwise very useful in reducing hallucinations, or constraining the output size.

이전에 언급했듯이 ChatCompletions API는 전체 JSON 스키마 사양의 제한된 하위 집합을 제공합니다. 숫자의 최소 및 최대, 배열의 minItems 및 maxItems와 같이 아직 지원되지 않는 수많은 키워드가 있습니다. 주석은 환각을 줄이거나 출력 크기를 제한하는 데 매우 유용합니다.

Certain formatting features are also unavailable. For example, the following Pydantic schema would result in API error when passed to response_format in ChatCompletions:

특정 서식 기능도 사용할 수 없습니다. 예를 들어 다음 Pydantic 스키마는 ChatCompletions의 response_format에 전달될 때 API 오류가 발생합니다.

```python

``파이썬

class Post(BaseModel):

클래스 포스트(BaseModel):

date_published: datetime

date_published: 날짜/시간

```

````

It would fail because openai package has no format handling for datetime , so instead you would need to set date_published as a str and perform format validation (e.g. ISO 8601 compliance) post-hoc.

openai 패키지에는 datetime에 대한 형식 처리가 없기 때문에 실패할 수 있으므로 대신 date_published를 str로 설정하고 사후에 형식 유효성 검사(예: ISO 8601 준수)를 수행해야 합니다.

Other key limitations include the following:

기타 주요 제한사항은 다음과 같습니다.

- Schemas must be able to be represented using Pydantic models.

- 스키마는 Pydantic 모델을 사용하여 표현될 수 있어야 합니다.

- Schemas must be able to be serialized to JSON.

- 스키마는 JSON으로 직렬화될 수 있어야 합니다.

- Schemas must be able to be validated using Pydantic models.

- 스키마는 Pydantic 모델을 사용하여 검증할 수 있어야 합니다.

- Schemas must be able to be deserialized from JSON.

- 스키마는 JSON에서 역직렬화할 수 있어야 합니다.

## Tips and tricks

## 팁과 요령

With all this in mind, we can now go through a couple of use cases with tips and tricks on how to enhance the performance when using structured outputs.

이 모든 것을 염두에 두고 이제 구조화된 출력을 사용할 때 성능을 향상시키는 방법에 대한 팁과 요령이 포함된 몇 가지 사용 사례를 살펴볼 수 있습니다.

### Creating flexibility using optional parameters

### 선택적 매개변수를 사용하여 유연성 만들기

Let’s say we are building a web scraping application where our goal is to collect specific components from the web pages. For each web page, we supply the raw HTML in the user prompt, give specific scraping instructions in the system prompt, and define the following Pydantic model:

웹 페이지에서 특정 구성 요소를 수집하는 것이 목표인 웹 스크래핑 애플리케이션을 구축한다고 가정해 보겠습니다. 각 웹 페이지에 대해 사용자 프롬프트에 원시 HTML을 제공하고 시스템 프롬프트에 특정 스크래핑 지침을 제공하며 다음 Pydantic 모델을 정의합니다.

```python

``파이썬

원본 소스:towardsdatascience

부인 성명:info@kdj.com

제공된 정보는 거래 조언이 아닙니다. kdj.com은 이 기사에 제공된 정보를 기반으로 이루어진 투자에 대해 어떠한 책임도 지지 않습니다. 암호화폐는 변동성이 매우 높으므로 철저한 조사 후 신중하게 투자하는 것이 좋습니다!

본 웹사이트에 사용된 내용이 귀하의 저작권을 침해한다고 판단되는 경우, 즉시 당사(info@kdj.com)로 연락주시면 즉시 삭제하도록 하겠습니다.

2025年09月20日 에 게재된 다른 기사