Cleanlab 신뢰할 수 있는 언어 모델

Cleanlab 신뢰할 수 있는 언어 모델 (Trustworthy Language Model)

Cleanlab의 Trustworthy Language Model은 LLM을 위한 최신 불확실성 추정을 사용해 모든 LLM 응답의 신뢰도를 실시간으로 점수화해요. 신뢰 점수는 방치된 환각(hallucination)이나 기타 LLM 오류가 치명적인 애플리케이션에서 매우 중요해요.

이 페이지는 자체 LLM 대신 TLM을 사용해 응답을 생성하고 그 신뢰도를 점수화하는 방법을 보여드려요. 하지만 그것이 TLM을 사용하는 유일한 방법은 아니에요. 수정하지 않은 기존 RAG 애플리케이션에 신뢰 점수를 추가하려면 Trustworthy RAG 튜토리얼을 대신 확인하세요. RAG 애플리케이션을 넘어, 어떤 LLM에서든 이미 생성된 응답의 신뢰도를 TLM.get_trustworthiness_score()로 점수화할 수 있어요.

Cleanlab 문서에서 더 배워 보세요.

출처: 문서

본문

설정 (Setup)

Colab에서 이 노트북을 여는 경우라면 LlamaIndex 🦙 설치가 필요할 거예요.

%pip install llama-index-llms-cleanlab
%pip install llama-index
from llama_index.llms.cleanlab import CleanlabTLM
# set api key in env or in llm
# get free API key from: https://cleanlab.ai/
# import os
# os.environ["CLEANLAB_API_KEY"] = "your api key"


llm = CleanlabTLM(api_key="your_api_key")
resp = llm.complete("Who is Paul Graham?")
print(resp)
Paul Graham is an American computer scientist, entrepreneur, and venture capitalist. He is best known as the co-founder of the startup accelerator Y Combinator, which has helped launch numerous successful companies including Dropbox, Airbnb, and Reddit. Graham is also a prolific writer and essayist, known for his insightful and thought-provoking essays on topics ranging from startups and entrepreneurship to technology and society. He has been influential in the tech industry and is highly regarded for his expertise and contributions to the startup ecosystem.

위 응답의 신뢰도 점수를 additional_kwargs에서도 얻을 수 있어요. TLM은 모든 <prompt, response> 쌍에 대해 이 점수를 자동으로 계산해요.

print(resp.additional_kwargs)
{'trustworthiness_score': 0.8659043183923533}

점수가 높으면 LLM의 응답을 신뢰할 수 있다는 뜻이에요. 여기 또 다른 예시를 들어볼게요.

resp = llm.complete(
    "What was the horsepower of the first automobile engine used in a commercial truck in the United States?"
)
print(resp)
The first automobile engine used in a commercial truck in the United States was the 1899 Winton Motor Carriage Company Model 10, which had a 2-cylinder engine with 20 horsepower.
print(resp.additional_kwargs)
{'trustworthiness_score': 0.5820799504369166}

점수가 낮으면 LLM의 응답을 신뢰하면 안 된다는 뜻이에요.

이 두 간단한 예시에서 우리는 점수가 가장 높은 LLM 응답은 직접적이고 정확하며 적절히 상세하다는 것을 관찰할 수 있어요. 반면 신뢰도 점수가 낮은 LLM 응답은 도움이 안 되거나 사실적으로 부정확한 답변, 즉 환각(hallucination)이라고도 불리는 것을 전달해요.

스트리밍 (Streaming)

Cleanlab의 TLM은 응답과 신뢰도 점수 둘 다를 네이티브로 스트리밍하지는 않아요. 하지만 애플리케이션에 사용할 수 있는 저지연 스트리밍 응답을 달성하는 대안 접근법이 있어요. 접근법에 대한 자세한 정보와 예제 코드는 여기에서 확인할 수 있어요.

TLM의 고급 사용 (Advance use of TLM)

TLM은 다음 옵션으로 구성할 수 있어요.

  • model: 사용할 기본 LLM
  • max_tokens: 응답에서 생성할 최대 토큰 수
  • num_candidate_responses: TLM이 내부적으로 생성하는 대안 후보 응답 수
  • num_consistency_samples: LLM 응답 일관성을 평가하기 위한 내부 샘플링 양
  • use_self_reflection: LLM이 스스로 생성한 응답을 반성하고 자체 평가하도록 요청할지 여부
  • log: 반환할 추가 메타데이터를 지정해요. 응답이 낮은 신뢰도로 점수화된 이유를 얻으려면 여기에 "explanation"을 포함하세요.

이 구성들은 초기화 시 딕셔너리로 CleanlabTLM 객체에 전달돼요. 이 옵션에 대한 자세한 내용은 Cleanlab의 API 문서에서 참고할 수 있고, 이 옵션들의 몇 가지 사용 사례는 이 노트북에서 탐구할 수 있어요.

애플리케이션에 gpt-4 모델과 128 출력 토큰이 필요한 예시를 살펴볼게요.

options = {
    "model": "gpt-4",
    "max_tokens": 128,
}
llm = CleanlabTLM(api_key="your_api_key", options=options)
resp = llm.complete("Who is Paul Graham?")
print(resp)
Paul Graham is a British-born American computer scientist, entrepreneur, venture capitalist, author, and essayist. He is best known for co-founding Viaweb, which was sold to Yahoo in 1998 for over $49 million and became Yahoo Store. He also co-founded the influential startup accelerator and seed capital firm Y Combinator, which has launched over 2,000 companies including Dropbox, Airbnb, Stripe, and Reddit. Graham is also known for his essays on startup companies and programming languages.

앞선 마력(horsepower) 관련 질문에 대해 TLM이 낮은 신뢰도를 추정한 이유를 이해하려면 TLM을 초기화할 때 "explanation" 플래그를 지정하세요.

options = {
    "log": ["explanation"],
}
llm = CleanlabTLM(api_key="your_api_key", options=options)


resp = llm.complete(
    "What was the horsepower of the first automobile engine used in a commercial truck in the United States?"
)
print(resp)
The first automobile engine used in a commercial truck in the United States was in the 1899 "Motor Truck" built by the American company, the "GMC Truck Company." This early truck was equipped with a 2-horsepower engine. However, it's important to note that the development of commercial trucks evolved rapidly, and later models featured significantly more powerful engines.
print(resp.additional_kwargs["explanation"])
The proposed answer incorrectly attributes the first commercial truck in the United States to the GMC Truck Company and states that it was built in 1899 with a 2-horsepower engine. In reality, the first commercial truck is generally recognized as the "Motor Truck" built by the American company, the "GMC Truck Company," but it was actually produced by the "GMC" brand, which was established later. The first commercial truck is often credited to the "Benz Velo" or similar early models, which had varying horsepower ratings. The specific claim of a 2-horsepower engine is also misleading, as early trucks typically had more powerful engines. Therefore, the answer contains inaccuracies regarding both the manufacturer and the specifications of the engine.
This response is untrustworthy due to lack of consistency in possible responses from the model. Here's one inconsistent alternate response that the model considered (which may not be accurate either):
The horsepower of the first automobile engine used in a commercial truck in the United States was 6 horsepower.

더 알아보기 (Learn more)