watsonx.ai 제공되는 리랭커 모델과 텍스트 리랭커 API를 사용하여 지정된 쿼리에 대한 답변 가능성이 가장 높은 구절부터 가장 낮은 구절까지 일련의 구절의 순위를 매길 수 있습니다.
발전하는 방법
다음과 같은 프로그래밍 방법을 사용하여 문서에서 텍스트를 추출할 수 있습니다
개요
검색 및 검색 작업과 채팅 워크플로에서 특히 근거가 있는 사실에 근거한 답변이 중요한 경우, 검색어와 가장 관련성이 높은 순서대로 일련의 구절을 정렬하는 것이 유용합니다.
지원되는 기초 모델
watsonx.ai 에서 사용할 수 있는 리랭커 모델에 대한 자세한 내용은 지원되는 인코더 모델을 참조하십시오.
사용 가능한 재랭커 모델 목록을 얻으려면 watsonx.ai as a service API에서 사용 가능한 기초 모델 나열 메서드를 사용할 수 있습니다. 사용 가능한 리랭커 모델만 반환하려면 ' filters=function_rerank
매개변수를 지정하세요.
예를 들어,
curl -X GET \
'https://{region}.ml.cloud.ibm.com/ml/v1/foundation_model_specs?version=2024-10-18&filters=function_rerank'
Python
watsonx.ai 의 Python 라이브러리에 있는 Reranker 클래스를 참조하십시오.
휴식 (REST API)
다음 다이어그램에 표시된 것처럼, watsonx.ai API의 리랭커 방법은 쿼리와 문서 구절 목록이 포함된 REST API 요청을 받아들입니다. API는 이러한 텍스트 문자열을 재랭킹 모델에 제출합니다. 리랭커 모델은 쿼리를 각 구절과 차례로 연결하고, 텍스트를 임베딩 벡터로 변환한 다음, 각 쌍의 벡터를 비교하고 서로의 유사성을 점수로 매깁니다. 그런 다음, 모델은 생성된 유사성 점수에 따라 구절의 순위를 다시 매깁니다. 이 모델은 텍스트를 텍스트 임베딩으로 변환하지만, 임베딩은 반환되지 않습니다. 텍스트 임베딩은 유사성 점수를 생성하는 데 사용되며, 점수만 반환됩니다.
API 참조
자세한 내용은 API 참조 문서를 참조하세요.
REST API 예
이 예의 코드는 ' ms-marco-minilm-l-12-v2
모델을 사용하여 다음 텍스트 구절의 순위를 다시 지정하여 쿼리에 대한 답변 가능성이 가장 높은 정보가 있는 구절이 먼저 나열되도록 합니다.
- query
상담원 중심 AI 워크플로란 무엇인가요?
- 단락
입력 구절은 watsonx.ai 용어집의 정의입니다:
항목 0. foundation model 다양한 다운스트림 작업에 적용할 수 있는 대규모 생성 AI 모델입니다.
항목 1. 생성형 AI는 텍스트, 소스 코드, 이미지, 오디오, 합성 데이터 등 다양한 유형의 콘텐츠를 제작할 수 있는 AI 알고리즘의 한 종류입니다.
항목 2. 에이전트 AI는 프롬프트를 여러 작업으로 분해하여 적절한 에이전트 AI 에이전트에 작업을 할당하고 사람의 개입 없이 답변을 합성할 수 있는 생성형 AI 흐름입니다.
항목 3. AI 윤리는 AI의 유익한 영향을 최적화하는 동시에 위험과 부정적인 결과를 줄이는 방법을 연구하는 다학제적 분야입니다. AI 윤리 이슈의 예로는 데이터 책임 및 개인정보 보호, 공정성, 설명 가능성, 견고성, 투명성, 환경 지속 가능성, 포용성, 도덕적 기관, 가치 정렬, 책임성, 신뢰, 기술 오용 등이 있습니다.
항목 4. AI 거버넌스는 조직의 지침, 직원, 프로세스 및 시스템을 통해 AI 수명 주기 전반에 걸쳐 AI 시스템을 지시, 평가, 모니터링 및 시정 조치를 취하여 조직이 의도한 대로, 이해관계자가 기대하는 대로, 관련 규정에서 요구하는 대로 운영되고 있음을 보장하기 위한 조직의 관리 행위입니다.
REST API 요청 예
이 예에서는 순위 재지정을 위해 5개의 구절만 제출되었습니다. 최대 1,000개의 입력을 지정할 수 있습니다. 그러나 지정하는 구절이 많을수록 교차 인코더 모델이 각 구절을 쿼리와 함께 차례로 처리하기 때문에 재랭크 프로세스가 더 오래 걸립니다.
제출하는 각 입력은 재랭커 모델에 정의된 최대 입력 토큰 한도를 준수해야 합니다.
구절에 모델에서 허용하는 것보다 많은 토큰이 있을 수 있는 경우를 해결하기 위해 이 예에서는 ' truncate_input_tokens
매개변수를 지정하여 줄을 강제로 잘라내도록 했습니다. 그렇지 않으면 요청이 실패할 수 있습니다.
이 예제에는 ' inputs
반환 옵션 매개변수가 포함되어 있어 원본 구절의 텍스트가 응답에 포함되므로 원본 구절의 순위가 어떻게 재조정되는지 더 쉽게 이해할 수 있습니다. 실제 워크플로에서는 ' inputs
매개변수를 포함할 필요가 없을 수도 있습니다.
텍스트 리랭커 메서드의 페이로드에 ' model_id
'로 사용할 리랭커 모델을 지정합니다.
다음 예제에서{url}variable with the right value for your instance, such as
us-south.ml.cloud.ibm.com 대체합니다. 고유한 무기명 토큰과 프로젝트 ID를 추가합니다.
curl -X POST \
'https://{url}/ml/v1/text/rerank?version=2024-10-17' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJraWQiOi...' \
--data '{
"inputs": [
{
"text": "A foundation model is a large-scale generative AI model that can be adapted to a wide range of downstream tasks."
},
{
"text": "Generative AI is a class of AI algorithms that can produce various types of content including text, source code, imagery, audio, and synthetic data."
},
{
"text": "Agentic AI is a generative AI flow that can decompose a prompt into multiple tasks, assign tasks to appropriate gen AI agents, and synthesize an answer without human intervention."
},
{
"text": "AI ethics is a multidisciplinary field that studies how to optimize AI'\''s beneficial impact while reducing risks and adverse outcomes. Examples of AI ethics issues are data responsibility and privacy, fairness, explainability, robustness, transparency, environmental sustainability, inclusion, moral agency, value alignment, accountability, trust, and technology misuse."
},
{
"text": "AI governance is an organization'\''s act of governing, through its corporate instructions, staff, processes and systems to direct, evaluate, monitor, and take corrective action throughout the AI lifecycle, to provide assurance that the AI system is operating as the organization intends, as its stakeholders expect, and as required by relevant regulation."
}
],
"query": "What is an agent-driven AI workflow?",
"parameters":{
"truncate_input_tokens": 512,
"return_options":{
"inputs":true
}
},
"model_id": "cross-encoder/ms-marco-minilm-l-12-v2",
"project_id": "51f3a990-4372-4ac3-9ddb-ed99d9b50840"
}'
가장 높은 순위의 구절만 반환하려면 ' top_n
매개변수를 지정하면 됩니다. 다음 샘플은 모델에서 쿼리와 가장 관련성이 높은 한 구절만 반환하도록 지정하는 방법을 보여 줍니다:
"parameters":{
"truncate_input_tokens": 512,
"return_options":{
"inputs":true,
"top_n": 1
}
}
REST API 응답 예
예제 요청에 대해 다음과 같은 응답이 반환됩니다. 몇 가지 주의해야 할 사항이 있습니다:
가장 높은 점수를 받은 구절이 먼저 표시되도록 구절의 순서가 변경됩니다.
이 모델은 에이전트 중심 워크플로에 대한 질문과 가장 밀접한 관련이 있기 때문에 에이전트 AI에 대한 정의를 목록의 맨 위에 정확하게 반환합니다.
0-4 범위의 인덱스 값은 요청에 제출된 '
inputs
배열에 있는 구절의 원래 위치를 나타냅니다.예를 들어 에이전트 AI에 대한 구절은 목록에서 세 번째 구절이므로 색인 값은 '
2
' 입니다.'
input_token_count
' 필드에는 요청에 의해 처리된 총 토큰 수가 표시됩니다.토큰 계산 방법에 대한 자세한 내용은 청구 정보를 참조하세요.
{
"model_id": "cross-encoder/ms-marco-minilm-l-12-v2",
"created_at": "2024-10-17T14:01:47.322Z",
"results": [
{
"index": 2,
"score": 5.063366413116455,
"input": {
"text": "Agentic AI is a generative AI flow that can decompose a prompt into multiple tasks, assign tasks to appropriate gen AI agents, and synthesize an answer without human intervention."
}
},
{
"index": 4,
"score": -0.992393970489502,
"input": {
"text": "AI governance is an organization's act of governing, through its corporate instructions, staff, processes and systems to direct, evaluate, monitor, and take corrective action throughout the AI lifecycle, to provide assurance that the AI system is operating as the organization intends, as its stakeholders expect, and as required by relevant regulation."
}
},
{
"index": 1,
"score": -3.5372314453125,
"input": {
"text": "Generative AI is a class of AI algorithms that can produce various types of content including text, source code, imagery, audio, and synthetic data."
}
},
{
"index": 0,
"score": -4.646212100982666,
"input": {
"text": "A foundation model is a large-scale generative AI model that can be adapted to a wide range of downstream tasks."
}
},
{
"index": 3,
"score": -4.926990032196045,
"input": {
"text": "AI ethics is a multidisciplinary field that studies how to optimize AI's beneficial impact while reducing risks and adverse outcomes. Examples of AI ethics issues are data responsibility and privacy, fairness, explainability, robustness, transparency, environmental sustainability, inclusion, moral agency, value alignment, accountability, trust, and technology misuse."
}
}
],
"input_token_count": 292,
"system": {
"warnings": [
{
"message": "This model is a Non-IBM Product governed by a third-party license that may impose use restrictions and other obligations. By using this model you agree to its terms as identified in the following URL.",
"id": "disclaimer_warning",
"more_info": "https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-models.html?context=wx"
}
]
}
}
이전 샘플과 같이 매개변수 객체에 ' "top_n": 1
'을 포함하면 응답은 다음과 같이 표시됩니다:
{
"model_id": "cross-encoder/ms-marco-minilm-l-12-v2",
"created_at": "2024-10-17T20:18:50.867Z",
"results": [
{
"index": 2,
"score": 5.063366413116455,
"input": {
"text": "Agentic AI is a generative AI flow that can decompose a prompt into multiple tasks, assign tasks to appropriate gen AI agents, and synthesize an answer without human intervention."
}
}
],
"input_token_count": 292,
"system": {
"warnings": [
{
"message": "This model is a Non-IBM Product governed by a third-party license that may impose use restrictions and other obligations. By using this model you agree to its terms as identified in the following URL.",
"id": "disclaimer_warning",
"more_info": "https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-models.html?context=wx"
}
]
}
}
대금 청구 정보
청구는 사용 중인 리랭커 모델에 의해 처리된 토큰 수를 기준으로 합니다. 단어당 사용되는 토큰 수는 모델에 따라 다릅니다.
- 토큰에 대한 자세한 내용은 토큰 및 토큰화를 참조하세요.
- 모델당 허용되는 최대 토큰 수에 대한 자세한 내용은 지원되는 인코더 모델을 참조하세요.
- 가격 책정에 대한 자세한 내용은 생성 AI 에셋에 대한 청구 세부 정보를 참조하세요.
크로스 인코더 모델은 쿼리와 각 입력 구절을 함께 처리하여 각 입력에 대한 점수를 생성한다는 점을 기억하세요. 따라서 토큰을 합산하려면 다음과 같은 공식을 사용해야 합니다:
(쿼리에 있는 토큰 x 입력 수) + (모든 입력에 있는 토큰의 합계 x 쿼리 수)
API는 하나의 쿼리만 허용합니다. 따라서 ' Number of queries
'은 항상 1과 같습니다.
제출된 텍스트 외에도 모델에서 사용하는 마커는 다음과 같습니다. 각 마커는 토큰으로 계산됩니다:
CLS
' : 분류를 의미하며 모델에 제출되는 시퀀스의 시작 부분에 추가되어 해당 시퀀스의 시작을 식별합니다.SEP
' : 구분 기호를 의미하며 한 시퀀스의 끝과 다음 시퀀스의 시작을 식별하는 데 사용됩니다.
각 입력에는 쿼리 + 입력 시퀀스의 시작을 식별하는 ' CLS
마커 1개와 ' SEP
' 구분 마커 2개가 포함되어 있습니다. 따라서 앞서 도출한 합계에 (3 x 입력 수)를 더하면 합계를 구할 수 있습니다.
자세히 알아보기
상위 주제: 코딩 제너레이티브 AI 솔루션