미세 조정된 Gemini 모델에 컨텍스트 캐싱을 사용하면 대량의 컨텍스트가 포함된 프롬프트의 성능을 개선하고 비용을 절감할 수 있습니다. 자주 사용되는 컨텍스트를 캐시하면 미세 조정된 모델에 대한 각 요청과 함께 대량의 데이터를 다시 전송하지 않아도 됩니다.
조정된 Gemini의 컨텍스트 캐시 관리 작업(Read
, Update
, Delete
)은 기본 모델과 일관성을 유지합니다. 캐시된 콘텐츠 생성 및 추론에만 특정 조정이 필요하며, 자세한 내용은 다음을 참고하세요.
지원되는 모델
컨텍스트 캐싱은 다음 기본 모델에서 미세 조정된 모델에 지원됩니다.
gemini-2.0-flash-001
기본 요건
Gemini 모델 미세 조정: 지원되는 기본 모델을 기반으로 배포된 미세 조정된 Gemini 모델이 필요합니다(지원되는 모델 참고). Gemini 모델을 미세 조정하는 방법에 관한 자세한 내용은 Gemini 모델 미세 조정을 참고하세요. 배포된 조정된 모델의 엔드포인트를 가져오려면 조정된 모델 배포를 참고하세요.
다음 정보를 확보해 두어야 합니다.
- 조정된 Gemini 모델의 ID 및 버전
- 배포된 미세 조정된 모델의 엔드포인트 리소스 이름
미세 조정된 모델의 컨텍스트 캐시 만들기
지원되는 기본 모델은 지원되는 모델에 나열되어 있습니다.
미세 조정된 모델의 컨텍스트 캐시를 만드는 절차는 대체로 컨텍스트 캐시 만들기에 설명된 단계를 따릅니다. 일반적인 프로세스는 링크된 문서를 참고하세요. 이 가이드에서는 미세 조정된 Gemini 모델의 컨텍스트 캐시 생성의 차이점에 중점을 둡니다.
projects/{PROJECT}/locations/{LOCATION}/publishers/google/models/{MODEL}
형식의 기본 모델을 사용하는 대신 projects/{PROJECT}/locations/{LOCATION}/models/{MODEL}@{VERSION}
형식의 미세 조정된 모델을 사용해야 합니다.
다음 예는 조정된 Gemini 모델로 컨텍스트 캐시를 만드는 방법을 보여줍니다.
REST
Vertex AI API를 사용하여 게시자 모델 엔드포인트에 POST 요청을 보내면 REST를 사용하여 컨텍스트 캐시를 만들 수 있습니다. 다음 예시에서는 Cloud Storage 버킷에 저장된 파일을 사용하여 컨텍스트 캐시를 만드는 방법을 보여줍니다.
요청 데이터를 사용하기 전에 다음을 바꿉니다.
- PROJECT_ID: 프로젝트 ID
- LOCATION: 요청을 처리하고 캐시된 콘텐츠가 저장되는 리전. 지원되는 리전 목록은 사용 가능한 리전을 참조하세요.
- MODEL_ID: 미세 조정된 Gemini 모델 ID
- MODEL_VERSION: 미세 조정된 Gemini 모델 버전
- CACHE_DISPLAY_NAME: 각 컨텍스트 캐시를 설명하고 식별하는 데 도움이 되는 의미 있는 표시 이름
- MIME_TYPE: 캐시할 콘텐츠의 MIME 유형
- CONTENT_TO_CACHE_URI: 캐시할 콘텐츠의 Cloud Storage URI
HTTP 메서드 및 URL:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents
JSON 요청 본문:
{ "model": "projects/PROJECT_ID/locations/LOCATION/models/MODEL_ID@MODEL_VERSION", "displayName": "CACHE_DISPLAY_NAME", "contents": [{ "role": "user", "parts": [{ "fileData": { "mimeType": "MIME_TYPE", "fileUri": "CONTENT_TO_CACHE_URI" } }] }, { "role": "model", "parts": [{ "text": "This is sample text to demonstrate explicit caching." }] }] }
요청을 보내려면 다음 옵션 중 하나를 선택합니다.
curl
요청 본문을 request.json
파일에 저장하고 다음 명령어를 실행합니다.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents"
PowerShell
요청 본문을 request.json
파일에 저장하고 다음 명령어를 실행합니다.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/cachedContents" | Select-Object -Expand Content
다음과 비슷한 JSON 응답이 표시됩니다.
curl 명령어 예시
LOCATION="us-central1"
MODEL_ID="model-id"
PROJECT_ID="test-project"
MODEL_VERSION=1
MIME_TYPE="video/mp4"
CACHED_CONTENT_URI="gs://path-to-bucket/video-file-name.mp4"
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/cachedContents -d \
'{
"model":"projects/${PROJECT_ID}/locations/${LOCATION}/models/${MODEL_ID}@${MODEL_VERSION}",
"contents": [
{
"role": "user",
"parts": [
{
"fileData": {
"mimeType": "${MIME_TYPE}",
"fileUri": "${CACHED_CONTENT_URI}"
}
}
]
}
]
}'
미세 조정된 모델에 컨텍스트 캐시 사용
미세 조정된 모델에 컨텍스트 캐시를 사용하는 절차는 대체로 컨텍스트 캐시 사용에 설명된 단계를 따릅니다. 일반적인 프로세스는 링크된 문서를 참고하세요. 이 가이드에서는 미세 조정된 Gemini 모델에 컨텍스트 캐시를 사용하는 차이점에 중점을 둡니다.
요청을 projects/{PROJECT}/locations/{LOCATION}/publishers/google/models/{MODEL}
형식으로 기본 모델 엔드포인트에 전송하는 대신 projects/{PROJECT}/locations/{LOCATION}/endpoints/{ENDPOINT_ID}
형식으로 배포된 미세 조정된 모델의 엔드포인트에 전송해야 합니다.
다음 코드 예에서는 조정된 Gemini 모델에서 컨텍스트 캐시를 사용하는 방법을 보여줍니다.
컨텍스트 캐시를 사용하는 경우 다음 속성을 지정할 수 없습니다.
GenerativeModel.system_instructions
GenerativeModel.tool_config
GenerativeModel.tools
REST
Vertex AI API를 사용하여 게시자 모델 엔드포인트에 POST 요청을 보내면 REST를 사용하여 프롬프트가 포함된 컨텍스트 캐시를 지정할 수 있습니다.
요청 데이터를 사용하기 전에 다음을 바꿉니다.
- PROJECT_ID: 프로젝트 ID
- LOCATION: 컨텍스트 캐시 만들기 요청이 처리된 리전
- ENDPOINT_ID: 미세 조정된 모델이 배포된 엔드포인트
- MIME_TYPE: 모델에 제출할 텍스트 프롬프트
HTTP 메서드 및 URL:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID:generateContent
JSON 요청 본문:
{ "cachedContent": "projects/PROJECT_NUMBER/locations/LOCATION/cachedContents/CACHE_ID", "contents": [ {"role":"user","parts":[{"text":"PROMPT_TEXT"}]} ], "generationConfig": { "maxOutputTokens": 8192, "temperature": 1, "topP": 0.95, }, "safetySettings": [ { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" } ], }
요청을 보내려면 다음 옵션 중 하나를 선택합니다.
curl
요청 본문을 request.json
파일에 저장하고 다음 명령어를 실행합니다.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID:generateContent"
PowerShell
요청 본문을 request.json
파일에 저장하고 다음 명령어를 실행합니다.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID:generateContent" | Select-Object -Expand Content
다음과 비슷한 JSON 응답이 수신됩니다.
curl 명령어 예시
LOCATION="us-central1"
PROJECT_ID="test-project"
ENDPOINT_ID=987654321
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/endpoints/${ENDPOINT_ID}:generateContent" -d \
'{
"cachedContent": "projects/${PROJECT_NUMBER}/locations/${LOCATION}/cachedContents/${CACHE_ID}",
"contents": [
{"role":"user","parts":[{"text":"What are the benefits of exercise?"}]}
],
"generationConfig": {
"maxOutputTokens": 8192,
"temperature": 1,
"topP": 0.95,
},
"safetySettings": [
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
}
],
}'