2024 년 8 월에 Openai는 2024 년 8 월 API에서 강력한 새로운 기능 (구조화 된 출력)을 발표했습니다. 이 기능을 사용하면 이름에서 알 수 있듯이 LLMS가 지정한 형식으로 만 응답을 생성 할 수 있습니다. 이 기능을 사용하면 정확한 데이터 형식이 필요한 응용 프로그램을 훨씬 쉽게 구축 할 수 있습니다.
출력 :
.
출력 :
Python 3.7 이상 시스템에 설치되었습니다
3. 설치 확인 : 설치를 확인하기 위해 간단한 파이썬 스크립트를 작성하십시오.
이 예에서 <:> :
client.chat.completions.create를 사용하는 대신 indecte.beta.chat.pletions.parse method를 사용하고 있습니다. .parse ()는 채팅 완료의 새로운 메소드입니다. 구조화 된 출력을 위해 특별히 작성된 API.
이제, 구조화 된 출력으로 Reviews Sentiment Classifier를 다시 작성하여 모든 것을 정리해 봅시다. 먼저 필요한 수입을 만들고 Pydantic 모델, 시스템 프롬프트 및 프롬프트 템플릿을 정의합니다.
주소는 주소의 구조를 정의하는 피기 틱 모델입니다.
<_> get_current_weather 함수는 두 개의 매개 변수가 있지만 JSON 스키마가 거대하고 수동으로 쓸 수 있도록 오류가 발생합니다.
우리는 Pydantic 모델을 호환 가능한 JSON 형식으로 전달하여 채팅 완료 API의 도구 매개 변수로 전달합니다. 그런 다음 쿼리에 따라 모델은 도구를 호출할지 여부를 결정합니다.
.
출력 :
구조화 된 출력과 어떻게 작동 하는가? # 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")
1. Openai Python 패키지 설치 : 터미널을 열고 다음 명령을 실행하여 OpenAi Python 패키지를 최신 버전으로 설치하거나 업데이트하십시오.
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.
# 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")
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.
# 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")
1 단계 : Pydantic 모델을 정의하십시오
이 예에서 <:> :
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.
# 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")
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.
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")
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"}
6. 모델 출력 정밀도를 향상시키기 위해 Pydantic 모델의 각 필드에 대한 명확하고 간결한 설명을 제공하십시오.
결론
예, 구성된 출력은 기능 매개 변수의 프로세스를 단순화하기 위해 기능을 호출하는 데 사용될 수 있습니다. 출력.
구조화 된 출력을 사용하는 데있어 어떤 이점이 있습니까?
혜택은 일관된 응답 형식, 사후 처리에 대한 요구 감소, AI 애플리케이션에서의 개선 된 신뢰성, 기존 시스템과의 더 쉬운 통합이 포함됩니다. 출력?
위 내용은 OpenAI 구조화 된 출력을 시작합니다의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!