VertexAICodeGenerator
VertexAICodeGenerator
Google Vertex AI의 생성 모델로 코드를 생성할 수 있게 해 주는 컴포넌트예요. 현재 지점 앞의 코드(prefix)와 뒤의 코드(suffix)를 기반으로 이어질 코드를 만들어 줘요.
필수 run 변수: prefix — 현재 지점 앞의 코드 문자열 / suffix — 현재 지점 뒤의 선택적 코드 문자열
출력 변수: replies — 모델이 생성한 코드
API 레퍼런스: Google Vertex
GitHub 링크: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_vertex
패키지 이름: google-vertex-haystack
VertexAICodeGenerator는 code-bison, code-bison-32k, code-gecko를 지원해요.
출처: 문서
본문
파라미터 개요 (Parameters Overview)
VertexAICodeGenerator는 인증에 Google Cloud Application Default Credentials (ADC)를 사용해요. ADC를 설정하는 방법은 공식 문서를 참고하세요.
Google Vertex AI 엔드포인트를 사용하도록 승인된 프로젝트에 접근 권한이 있는 계정을 쓰는 게 중요하다는 점을 기억하세요.
프로젝트 ID는 GCP 리소스 매니저에서 찾거나, 터미널에서 gcloud projects list를 실행해 찾을 수 있어요. gcloud CLI에 대한 자세한 내용은 공식 문서를 참고하세요.
사용법 (Usage)
VertexAICodeGenerator를 쓰려면 먼저 google-vertex-haystack 패키지를 설치해야 해요:
pip install google-vertex-haystack
기본 사용법:
from haystack_integrations.components.generators.google_vertex import (
VertexAICodeGenerator,
)
generator = VertexAICodeGenerator()
result = generator.run(prefix="def to_json(data):")
for answer in result["replies"]:
print(answer)
# >> ```python
# >> import json
# >>
# >> def to_json(data):
# >> """Converts a Python object to a JSON string.
# >>
# >> Args:
# >> data: The Python object to convert.
# >>
# >> Returns:
# >> A JSON string representing the Python object.
# >> """
# >>
# >> return json.dumps(data)
# >> ```
출력 토큰 수, temperature, 중지 시퀀스, 후보(candidate) 수 같은 다른 파라미터도 설정할 수 있어요.
다른 모델도 시도해 볼게요:
from haystack_integrations.components.generators.google_vertex import (
VertexAICodeGenerator,
)
generator = VertexAICodeGenerator(
model="code-gecko", temperature=0.8, candidate_count=3
)
result = generator.run(prefix="def convert_temperature(degrees):")
for answer in result["replies"]:
print(answer)
# >>
# >> return degrees * (9/5) + 32
# >>
# >> return round(degrees * (9.0 / 5.0) + 32, 1)
# >>
# >> return 5 * (degrees - 32) /9
# >>
# >> def convert_temperature_back(degrees):
# >> return 9 * (degrees / 5) + 32