모델 게이트웨이를 통해 모델 추론하기

모델 게이트웨이를 통해 엔드포인트를 OpenAI-compatible 사용하여 모델에 요청을 전송하십시오. REST API 또는 SDK를 OpenAIPython 사용하여 텍스트 생성, 대화형 응답 생성, 임베딩 생성 및 특정 사용 사례에 맞춤화된 여러 모델에 걸쳐 확장 가능한 솔루션을 개발할 수 있습니다.

필수 권한

: 게이트웨이 모델을 추론하려면 다음 권한 중 하나를 보유해야 합니다: - 플랫폼 관리자 - 구성 관리

필수 자격 증명
watsonx.ai 의 API를 인증하려면 자격 증명을 생성해야 합니다. 자세한 내용은 베어러 토큰 생성을 참조하십시오.

일하는 방법

모델 게이트웨이 엔드포인트는 모든 공급자를 위한 통합 API( OpenAI-compatible )를 제공하며, 이는 모델 요청을 라우팅하는 데 사용됩니다.

다음과 같은 프로그래밍 방법을 사용하여 게이트웨이 파운데이션 모델을 추론할 수 있습니다:

주의: 일부 모델 제공업체는 백엔드에서 특정 엔드포인트의 서비스를 지원하지 않을 수 있습니다. 구성된 모델 공급자를 지원되지 않는 엔드포인트 서비스와 함께 사용할 경우 응답에 오류가 발생할 수 있습니다.

휴식 (REST API)

모델 게이트웨이 API에 대한 자세한 내용은 watsonx.ai API 참조 문서를 참조하십시오.

모델 게이트웨이는 다음 엔드포인트를 지원합니다:

리스팅 공급자 및 모델

구성한 공급업체와 모델을 모두 나열할 수 있습니다.

구성된 모든 모델 공급자를 나열하려면 다음 명령을 사용합니다:

curl -sS "https://cpd-<namespace-name>.apps.<OCP-domain>/ml/gateway/v1/providers" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}"

특정 공급업체에 대해 활성화된 모든 모델을 나열하려면 다음 명령을 사용하세요:

curl -sS "https://cpd-<namespace-name>.apps.<OCP-domain>/ml/gateway/v1/providers/${PROVIDER_UUID}/models" \
	-H "Content-Type: application/json" \
	-H "Authorization: Bearer ${TOKEN}"

활성화된 모든 모델을 나열하려면(구성된 모든 공급업체에서) 다음 명령을 사용합니다:

curl -sS "https://cpd-<namespace-name>.apps.<OCP-domain>/ml/gateway/v1/models" \
	-H "Content-Type: application/json" \
	-H "Authorization: Bearer ${TOKEN}"

채팅 완료

/v1/chat/completions 엔드포인트를 사용하려면 다음 예시를 참조하세요:

curl "https://cpd-<namespace-name>.apps.<OCP-domain>/ml/gateway/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '{
    "model": "azure/gpt-4o",
    "messages": [
      {
        "role": "system",
        "content": "Please explain everything in a way a 5th grader could understand—simple language, clear steps, and easy examples."
      },
      {
        "role": "user",
        "content": "Can you explain what TLS is and how I can use it?"
      }
    ]
  }'

자세한 내용과 예는 OpenAI API 문서에서 채팅 완료하기를 참조하세요.

텍스트 완성/생성

curl "https://cpd-<namespace-name>.apps.<OCP-domain>/ml/gateway/v1/completions" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer ${TOKEN}" \
 -d '{
   "model": "ibm/llama-3-3-70b-instruct",
   "prompt": "Say this is a test",
   "max_tokens": 7,
   "temperature": 0
 }'

자세한 내용과 예제는 OpenAI API 문서에서 텍스트 생성을 참조하세요.

임베딩 생성

/v1/embeddings 엔드포인트를 사용하려면 다음 예시를 참조하세요:

curl "https://cpd-<namespace-name>.apps.<OCP-domain>/ml/gateway/v1/embeddings" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d '{
    "input": "The food was delicious and the waiter...",
    "model": "text-embedding-3-large",
    "encoding_format": "float"
  }'

자세한 내용과 예시는 OpenAI API 문서에서 임베딩을 얻는 방법을 참조하세요.

Python SDK

모델 게이트웨이에서 파운데이션 모델을 사용하려면 watsonx.aiPython 라이브러리의 Gateway 클래스를 사용할 수 있습니다.

시작하려면 다음 샘플 노트북을 참조하세요:

  • LangGraph (LLM) 프레임워크와 모델 게이트웨이를 사용하여 요청을 공급자에게 라우팅하는 LLM 앱을 구축하려면 LangGraph Agent Template을 참조하십시오.

모델 게이트웨이는 OpenAI API와의 호환성을 유지합니다. 따라서, OpenAI SDK를 사용하면 OpenAI API 키 대신 베어러 토큰을 전달하여 게이트웨이 모델을 추론할 수 있습니다.

OpenAIPython SDK를 사용하여 모델 게이트웨이를 통해 채팅 완성 요청을 수행하려면 다음 예제를 참조하십시오:

import os
from openai import OpenAI


gateway_url = "https://cpd-<namespace-name>.apps.<OCP-domain>/ml/gateway/v1"
ibm_cloud_api_key = os.getenv("TOKEN")

client = OpenAI(
    base_url=gateway_url,
    api_key=bearer_token,
)

completion = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

print(completion.choices[0].message)