Developer Knowledge API를 사용하면 Google Developers 제품에 대해 질문하고 자연어로 직접 답변을 받을 수 있습니다. 각 질문에 대해 API는 개발자 지식 코퍼스(API 참조에서 그라운딩 생성이라고 함)에서 가져온 대답을 작성하고 관련 문서 페이지에 대한 인용을 포함합니다.
시작하기 전에
원하는 도구의 환경을 설정합니다.
gcloud
REST
API를 사용 설정하고 Developer Knowledge API 키를 생성합니다. 그런 다음 키를 환경 변수에 저장합니다.
export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"
YOUR_API_KEY를 Developer Knowledge API 키로 바꿉니다.
질문에 답변하기
gcloud developer-knowledge answer-query 명령어 또는 answerQuery REST 메서드를 사용하여 질문합니다.
다음 예시에서는 BigQuery 데이터 세트를 만드는 방법을 묻는 질문을 전송합니다.
gcloud
gcloud developer-knowledge answer-query \ --query="How do I create a BigQuery dataset?"
REST
curl -X POST "https://developerknowledge.googleapis.com/v1:answerQuery?key=$DEVELOPERKNOWLEDGE_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "How do I create a BigQuery dataset?"}'
응답에는 answer.answerText 필드의 텍스트 답변과 answer 객체의 citations 및 references가 포함됩니다.
{
"answer": {
"answerText": "To create a BigQuery dataset, you can use the Google Cloud console, the bq command-line tool, or the BigQuery client libraries.",
"citations": [
{
"startIndex": 0,
"endIndex": 123,
"sources": [
{
"referenceIndex": 0
}
]
}
],
"references": [
{
"documentReference": {
"documentChunk": {
"parent": "documents/docs.cloud.google.com/bigquery/docs/datasets",
"content": "This page explains how to create BigQuery datasets...",
"document": {
"name": "documents/docs.cloud.google.com/bigquery/docs/datasets",
"title": "Introduction to datasets",
"uri": "https://docs.cloud.google.com/bigquery/docs/datasets"
}
}
}
}
]
}
}
문서 소스 필터링
답변을 생성하는 데 사용되는 문서 소스를 제한하려면 gcloud CLI의 --query-filter 플래그 또는 REST 요청 본문의 filter 필드를 사용하여 필터 표현식을 전달합니다. 지원되는 필터 필드 및 연산자에 대한 자세한 내용은 검색 결과 필터링을 참조하세요.
다음 예시에서는 문서 소스를 docs.cloud.google.com로 제한합니다.
gcloud
gcloud developer-knowledge answer-query \ --query="How do I create a BigQuery dataset?" \ --query-filter='data_source = "docs.cloud.google.com"'
REST
curl -X POST "https://developerknowledge.googleapis.com/v1:answerQuery?key=$DEVELOPERKNOWLEDGE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": "How do I create a BigQuery dataset?",
"filter": "data_source = \"docs.cloud.google.com\""
}'
AnswerQuery~SearchDocumentChunks 사이로 선택하세요.
API 또는 Developer Knowledge MCP 서버를 사용할 때는 찾고 있는 항목에 가장 적합한 메서드를 선택하세요.
AnswerQuery(또는answer_queryMCP 도구): 일반적인 '방법' 질문, 제품 비교, 소스 문서 링크가 포함된 전체 요약을 원하는 단계별 안내에 가장 적합합니다.SearchDocumentChunks(또는search_documentsMCP 도구): 정확한 CLI 플래그, 코드 문법, 매개변수 이름 또는 IAM 권한(예:service.resource.verb)을 빠른 검색하는 데 가장 적합합니다. 전체 질문 대신 2~5개의 구체적인 키워드를 사용하세요.
인용 및 참조 파싱
answerQuery를 호출하면 answer 객체는 생성된 answerText의 특정 세그먼트를 references의 소스 구절에 연결하는 세분화된 기여 분석을 제공합니다.
references의 단일 소스 문서는 여러 텍스트 세그먼트에서 여러 번 인용될 수 있으며 단일 세그먼트는 여러 참조를 인용할 수 있습니다.
인용이 포함된 응답 예시
다음 JSON 스니펫은 서로 다른 문서 참조를 가리키는 여러 인용이 있는 answer 객체를 보여줍니다.
{
"answer": {
"answerText": "You can create a BigQuery dataset using the Google Cloud console, the bq command-line tool, or the client libraries. Each dataset is assigned a region or multi-region location when created.",
"citations": [
{
"startIndex": 0,
"endIndex": 116,
"sources": [
{
"referenceIndex": 0
}
]
},
{
"startIndex": 117,
"endIndex": 189,
"sources": [
{
"referenceIndex": 1
}
]
}
],
"references": [
{
"documentReference": {
"documentChunk": {
"parent": "documents/docs.cloud.google.com/bigquery/docs/datasets",
"content": "You can create a dataset in BigQuery using the Google Cloud console, the bq command-line tool, or client libraries...",
"document": {
"name": "documents/docs.cloud.google.com/bigquery/docs/datasets",
"title": "Creating and managing datasets",
"uri": "https://docs.cloud.google.com/bigquery/docs/datasets"
}
}
}
},
{
"documentReference": {
"documentChunk": {
"parent": "documents/docs.cloud.google.com/bigquery/docs/locations",
"content": "When you create a dataset, you specify the location where your BigQuery data is stored...",
"document": {
"name": "documents/docs.cloud.google.com/bigquery/docs/locations",
"title": "Dataset locations",
"uri": "https://docs.cloud.google.com/bigquery/docs/locations"
}
}
}
}
]
}
}
인용 매핑 및 필드
인용을 문서 소스에 매핑하려면 다음 필드를 사용합니다.
answerText: 생성된 전체 자연어 응답 문자열입니다.citations: 인용된 세그먼트 출처의 배열입니다.startIndex: 인용된 세그먼트의 시작 오프셋(포함)으로, UTF-8 바이트로 측정됩니다.endIndex: 인용된 세그먼트의 배타적 종료 오프셋(UTF-8 바이트 단위)sources: 인용 소스의 배열입니다. 각 항목에는references배열의 항목에 해당하는 0 기반referenceIndex가 포함됩니다(예:referenceIndex: 0는references[0]에 매핑됨).
references: 답변을 생성하는 데 사용된 소스 문서 구절의 배열입니다. 각 항목에는 다음 메타데이터가 포함된documentReference.documentChunk가 있습니다.document.title: 문서 페이지의 제목입니다.document.uri: 문서 페이지의 공개 URL입니다.document.name: 문서의 표준 리소스 이름입니다.content: 답변을 합성하는 데 사용된 문서 청크에서 추출된 텍스트 스니펫입니다.parent: 상위 문서의 리소스 이름(예:documents/docs.cloud.google.com/bigquery/docs/datasets)으로,documents.get또는documents.batchGet에 전달하여 전체 문서를 검색할 수 있습니다.
애플리케이션에서 인용 구현
클라이언트 애플리케이션 또는 AI 에이전트 파이프라인에서 인용이 포함된 답변을 렌더링하려면 다음 안내를 따르세요.
- 인용된 세그먼트 추출:
answerText에서 관련 범위를 슬라이스하려면startIndex및endIndex바이트 오프셋을 사용합니다. - 소스 세부정보 확인:
document.title페이지와document.uri대상을 가져오려면sources[].referenceIndex를 사용하여references에서 해당 항목을 빠른 검색합니다. - 인라인 링크 또는 각주 렌더링: 소스 페이지에 직접 연결하려면 각주 번호(예:
[1]또는[2]), 미리보기 카드 또는document.uri를 가리키는 클릭 가능한 하이퍼링크를 삽입합니다. - 필요한 경우 전체 컨텍스트 가져오기:
content의 발췌문이 충분하지 않은 경우 전체 문서 페이지를 가져오려면parent를documents.get에 전달합니다.
- UTF-16 언어(JavaScript 또는 TypeScript): 슬라이스하기 전에 문자열을 UTF-8 바이트로 변환합니다. 예를 들어 Node.js에서는
Buffer.from(answerText, 'utf-8').subarray(start, end)를 사용하고 브라우저 환경에서는TextEncoder및TextDecoder를 사용합니다. - Python: 슬라이스하기 전에 문자열을 UTF-8 바이트로 인코딩한 후 디코딩합니다(
answer_text.encode('utf-8')[start:end].decode('utf-8')). - 기타 언어: UTF-8 바이트로 문자열을 기본적으로 색인 생성하는 언어(예: Go 또는 Rust)의 경우
startIndex및endIndex를 사용하여answerText를 직접 슬라이스합니다. 그렇지 않으면 슬라이스하기 전에 문자열을 UTF-8 바이트로 변환합니다.
다음 단계
- 문서를 검색하고 가져와 일치하는 텍스트 스니펫 또는 전체 마크다운 페이지를 가져옵니다.
- 개발자 지식 MCP 서버에 연결하고
retrieving-developer-knowledge에이전트 기술을 설치합니다. - 코퍼스 참조를 둘러보고 지원되는 모든 문서 소스를 확인하세요.