این راهنما به شما نشان میدهد که چگونه با استفاده از کتابخانههای رسمی کلاینت، کار با Developer Knowledge API را شروع کنید. شما یاد خواهید گرفت که چگونه محیط خود را راهاندازی کنید، کتابخانه کلاینت را برای زبان مورد نظر خود نصب کنید و برای جستجو و بازیابی مستندات توسعهدهنده، فراخوانیهای API را انجام دهید.
قبل از اینکه شروع کنی
قبل از شروع استفاده از کتابخانههای کلاینت API دانش توسعهدهندگان، بخشهای زیر را تکمیل کنید.
فعال کردن API
- صفحه API دانش توسعهدهندگان را در کتابخانه APIهای گوگل باز کنید.
- بررسی کنید که پروژهی درستی را که قصد دارید در آن از API استفاده کنید، انتخاب کردهاید.
- روی فعال کردن کلیک کنید. برای فعال کردن یا استفاده از API، به هیچ نقش IAM خاصی نیاز نیست.
تنظیم احراز هویت
کتابخانههای کلاینت API دانش توسعهدهندگان از اعتبارنامههای پیشفرض برنامه (ADC) برای تأیید اعتبار درخواستها استفاده میکنند.
برای تنظیم اعتبارنامههای احراز هویت محلی، دستور زیر را اجرا کنید:
gcloud auth application-default login
برای کسب اطلاعات بیشتر در مورد گزینههای اعتبارنامه مانند حسابهای سرویس، به مستندات اعتبارنامههای پیشفرض برنامه مراجعه کنید.
کتابخانه کلاینت را نصب کنید
برای نصب کتابخانه کلاینت رسمی Developer Knowledge API، زبان برنامهنویسی خود را انتخاب کنید:
پایتون
pip install google-developer-knowledge
Node.js و TypeScript
npm install @google/developer-knowledge
برو
go get cloud.google.com/go/developerknowledge/apiv1
جاوا
<!-- Maven dependency -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-developer-knowledge</artifactId>
<version>0.3.0</version>
</dependency>
تولید پاسخ از مستندات
نقطه پایانی AnswerQuery با استخراج اطلاعات از منابع اسناد رسمی، به سوالات پیچیده و به زبان طبیعی، مانند سوالات مربوط به تنظیم کد، رویههای عیبیابی و قابلیتهای محصول، پاسخ میدهد.
برای مشاهده مثالی از نحوه فراخوانی AnswerQuery یک زبان را انتخاب کنید:
پایتون
from google.cloud import developer_knowledge_v1
def answer_query(
query: str = "How do I create a Google Cloud Storage bucket?",
) -> developer_knowledge_v1.AnswerQueryResponse:
"""Answers a developer question grounded in Google developer documentation.
Args:
query: The technical question to answer.
Returns:
The AnswerQueryResponse containing the grounded answer,
citations, and references.
"""
client = developer_knowledge_v1.DeveloperKnowledgeClient()
request = developer_knowledge_v1.AnswerQueryRequest(
query=query,
)
response = client.answer_query(request=request)
print(f"Answer:\n{response.answer.answer_text}\n")
print(f"Citations count: {len(response.answer.citations)}")
print(f"References count: {len(response.answer.references)}")
return response
Node.js و TypeScript
const {DeveloperKnowledgeClient} = require('@google/developer-knowledge');
/**
* Answers a developer question grounded in Google developer documentation.
*
* @param {string} query The technical question to answer.
*/
async function answerQuery(
query = 'How do I create a Google Cloud Storage bucket?'
) {
const client = new DeveloperKnowledgeClient();
const request = {
query,
};
const [response] = await client.answerQuery(request);
console.log(`Answer:\n${response.answer.answerText}\n`);
const citationsCount = response.answer.citations
? response.answer.citations.length
: 0;
const referencesCount = response.answer.references
? response.answer.references.length
: 0;
console.log(`Citations count: ${citationsCount}`);
console.log(`References count: ${referencesCount}`);
return response;
}
برو
import (
"context"
"fmt"
"io"
developerknowledge "cloud.google.com/go/developerknowledge/apiv1"
developerknowledgepb "cloud.google.com/go/developerknowledge/apiv1/developerknowledgepb"
)
// answerQuery answers a developer question grounded in Google developer documentation.
func answerQuery(w io.Writer, query string) (*developerknowledgepb.AnswerQueryResponse, error) {
ctx := context.Background()
client, err := developerknowledge.NewDeveloperKnowledgeClient(ctx)
if err != nil {
return nil, fmt.Errorf("developerknowledge.NewDeveloperKnowledgeClient: %w", err)
}
defer client.Close()
req := &developerknowledgepb.AnswerQueryRequest{
Query: query,
}
resp, err := client.AnswerQuery(ctx, req)
if err != nil {
return nil, fmt.Errorf("AnswerQuery: %w", err)
}
if resp.GetAnswer() != nil {
fmt.Fprintf(w, "Answer:\n%s\n\n", resp.GetAnswer().GetAnswerText())
fmt.Fprintf(w, "Citations count: %d\n", len(resp.GetAnswer().GetCitations()))
fmt.Fprintf(w, "References count: %d\n", len(resp.GetAnswer().GetReferences()))
}
return resp, nil
}
جاوا
import com.google.developers.knowledge.v1.AnswerQueryRequest;
import com.google.developers.knowledge.v1.AnswerQueryResponse;
import com.google.developers.knowledge.v1.DeveloperKnowledgeClient;
import java.io.IOException;
public class AnswerQuery {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String query = "How do I create a Google Cloud Storage bucket?";
answerQuery(query);
}
// Answers a developer question grounded in Google developer documentation.
public static AnswerQueryResponse answerQuery(String query) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DeveloperKnowledgeClient client = DeveloperKnowledgeClient.create()) {
AnswerQueryRequest request =
AnswerQueryRequest.newBuilder().setQuery(query).build();
AnswerQueryResponse response = client.answerQuery(request);
System.out.println("Answer:\n" + response.getAnswer().getAnswerText() + "\n");
System.out.println("Citations count: " + response.getAnswer().getCitationsCount());
System.out.println("References count: " + response.getAnswer().getReferencesCount());
return response;
}
}
}
جستجوی تکههای سند
برای یافتن بخشهای متنی دقیق و بومیسازیشده در مستندات به جای یک پاسخ تولیدشده، از نقطه پایانی SearchDocumentChunks استفاده کنید. این روش، متن را اسکن میکند و قطعههای محتوای جداگانه (قطعهها) را در کنار شناسههای سند والد برمیگرداند، که میتوانید از آنها برای بازیابی محتوای کامل سند استفاده کنید.
برای مشاهده نمونهای از نحوه جستجوی بخشهای مختلف سند، یک زبان را انتخاب کنید:
پایتون
from google.cloud import developer_knowledge_v1
def search_document_chunks(
query: str = "How to create a Cloud Storage bucket",
page_size: int = 5,
) -> (
developer_knowledge_v1.services.developer_knowledge.pagers.SearchDocumentChunksPager
):
"""Searches developer documentation chunks for a given query.
Args:
query: The natural language search query.
page_size: The maximum number of document chunks to return.
Returns:
The SearchDocumentChunksPager containing relevant document chunks.
"""
client = developer_knowledge_v1.DeveloperKnowledgeClient()
request = developer_knowledge_v1.SearchDocumentChunksRequest(
query=query,
page_size=page_size,
)
response = client.search_document_chunks(request=request)
count = 0
for chunk in response:
print(f"Parent Document: {chunk.parent}")
print(f"Chunk ID: {chunk.id}")
print(f"Content: {chunk.content[:100]}...\n")
count += 1
if page_size > 0 and count >= page_size:
break
return response
Node.js و TypeScript
const {DeveloperKnowledgeClient} = require('@google/developer-knowledge');
/**
* Searches developer documentation chunks for a given query.
*
* @param {string} query The search query string.
* @param {number} pageSize The maximum number of document chunks to return.
*/
async function searchDocumentChunks(
query = 'How to create a Cloud Storage bucket',
pageSize = 5
) {
const client = new DeveloperKnowledgeClient();
const request = {
query,
pageSize,
};
// Warning: Should always disable autoPaginate to avoid iterating through all pages.
// By default NodeJS SDK returns an iterable where you can iterate through all
// search results instead of only the limited number of results requested on pageSize.
const [chunks] = await client.searchDocumentChunks(request, {
autoPaginate: false,
});
for (const chunk of chunks) {
console.log(`Parent Document: ${chunk.parent}`);
console.log(`Chunk ID: ${chunk.id}`);
console.log(`Content Preview: ${chunk.content.substring(0, 100)}...\n`);
}
return chunks;
}
برو
import (
"context"
"fmt"
"io"
developerknowledge "cloud.google.com/go/developerknowledge/apiv1"
developerknowledgepb "cloud.google.com/go/developerknowledge/apiv1/developerknowledgepb"
"google.golang.org/api/iterator"
)
// searchDocumentChunks searches developer documentation chunks for a given query.
func searchDocumentChunks(w io.Writer, query string, pageSize int32) ([]*developerknowledgepb.DocumentChunk, error) {
ctx := context.Background()
client, err := developerknowledge.NewDeveloperKnowledgeClient(ctx)
if err != nil {
return nil, fmt.Errorf("developerknowledge.NewDeveloperKnowledgeClient: %w", err)
}
defer client.Close()
req := &developerknowledgepb.SearchDocumentChunksRequest{
Query: query,
PageSize: pageSize,
}
var results []*developerknowledgepb.DocumentChunk
it := client.SearchDocumentChunks(ctx, req)
for {
chunk, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, fmt.Errorf("SearchDocumentChunks: %w", err)
}
results = append(results, chunk)
fmt.Fprintf(w, "Parent Document: %s\n", chunk.GetParent())
fmt.Fprintf(w, "Chunk ID: %s\n", chunk.GetId())
fmt.Fprintf(w, "Content: %s\n\n", chunk.GetContent())
if pageSize > 0 && len(results) >= int(pageSize) {
break
}
}
return results, nil
}
جاوا
import com.google.developers.knowledge.v1.DeveloperKnowledgeClient;
import com.google.developers.knowledge.v1.DeveloperKnowledgeClient.SearchDocumentChunksPagedResponse;
import com.google.developers.knowledge.v1.DocumentChunk;
import com.google.developers.knowledge.v1.SearchDocumentChunksRequest;
import java.io.IOException;
public class SearchDocumentChunks {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String query = "How to create a Cloud Storage bucket";
int pageSize = 5;
searchDocumentChunks(query, pageSize);
}
// Searches developer documentation chunks for a given query.
public static SearchDocumentChunksPagedResponse searchDocumentChunks(
String query, int pageSize) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DeveloperKnowledgeClient client = DeveloperKnowledgeClient.create()) {
SearchDocumentChunksRequest request =
SearchDocumentChunksRequest.newBuilder()
.setQuery(query)
.setPageSize(pageSize)
.build();
SearchDocumentChunksPagedResponse response = client.searchDocumentChunks(request);
for (DocumentChunk chunk : response.getPage().getValues()) {
System.out.println("Parent Document: " + chunk.getParent());
System.out.println("Chunk ID: " + chunk.getId());
String preview = chunk.getContent();
if (preview.length() > 100) {
preview = preview.substring(0, 100) + "...";
}
System.out.println("Content: " + preview + "\n");
}
return response;
}
}
}
بازیابی یک سند
هر بخش از سند شامل یک فیلد parent با نام منبع سند والد خود است. برای بازیابی کل سند، GetDocument با آن نام منبع استفاده کنید.
نمونههای زیر یک سند نمونه را بازیابی میکنند. میتوانید نام سند نمونه را با هر نام منبع parent که از نتایج جستجوی شما برگردانده شده است، جایگزین کنید.
برای مشاهدهی مثالی از نحوهی فراخوانی GetDocument یک زبان را انتخاب کنید:
پایتون
from google.cloud import developer_knowledge_v1
def get_document(
name: str = "documents/docs.cloud.google.com/storage/docs/creating-buckets",
) -> developer_knowledge_v1.Document:
"""Retrieves a single developer documentation page by its resource name.
Args:
name: The resource name of the document in format
'documents/{uri_without_scheme}'.
Returns:
The Document containing the full Markdown content and metadata.
"""
client = developer_knowledge_v1.DeveloperKnowledgeClient()
request = developer_knowledge_v1.GetDocumentRequest(
name=name,
)
document = client.get_document(request=request)
print(f"Title: {document.title}")
print(f"URI: {document.uri}")
print(f"Data Source: {document.data_source}")
print(f"Content Length: {document.content_length_bytes} bytes")
print(f"Content Preview: {document.content[:150]}...\n")
return document
Node.js و TypeScript
const {DeveloperKnowledgeClient} = require('@google/developer-knowledge');
/**
* Retrieves a single developer documentation page by its resource name.
*
* @param {string} name The resource name in format 'documents/{uri_without_scheme}'.
*/
async function getDocument(
name = 'documents/docs.cloud.google.com/storage/docs/creating-buckets'
) {
const client = new DeveloperKnowledgeClient();
const request = {
name,
};
const [document] = await client.getDocument(request);
console.log(`Title: ${document.title}`);
console.log(`URI: ${document.uri}`);
console.log(`Data Source: ${document.dataSource}`);
console.log(`Content Length: ${document.contentLengthBytes} bytes`);
console.log(`Content Preview: ${document.content.substring(0, 150)}...\n`);
return document;
}
برو
import (
"context"
"fmt"
"io"
developerknowledge "cloud.google.com/go/developerknowledge/apiv1"
developerknowledgepb "cloud.google.com/go/developerknowledge/apiv1/developerknowledgepb"
)
// getDocument retrieves a single developer documentation page by its resource name.
func getDocument(w io.Writer, name string) (*developerknowledgepb.Document, error) {
ctx := context.Background()
client, err := developerknowledge.NewDeveloperKnowledgeClient(ctx)
if err != nil {
return nil, fmt.Errorf("developerknowledge.NewDeveloperKnowledgeClient: %w", err)
}
defer client.Close()
req := &developerknowledgepb.GetDocumentRequest{
Name: name,
}
doc, err := client.GetDocument(ctx, req)
if err != nil {
return nil, fmt.Errorf("GetDocument: %w", err)
}
fmt.Fprintf(w, "Title: %s\n", doc.GetTitle())
fmt.Fprintf(w, "URI: %s\n", doc.GetUri())
fmt.Fprintf(w, "Data Source: %s\n", doc.GetDataSource())
fmt.Fprintf(w, "Content Length: %d bytes\n\n", doc.GetContentLengthBytes())
return doc, nil
}
جاوا
import com.google.developers.knowledge.v1.DeveloperKnowledgeClient;
import com.google.developers.knowledge.v1.Document;
import com.google.developers.knowledge.v1.GetDocumentRequest;
import java.io.IOException;
public class GetDocument {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String name = "documents/docs.cloud.google.com/storage/docs/creating-buckets";
getDocument(name);
}
// Retrieves a single developer documentation page by its resource name.
public static Document getDocument(String name) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DeveloperKnowledgeClient client = DeveloperKnowledgeClient.create()) {
GetDocumentRequest request = GetDocumentRequest.newBuilder().setName(name).build();
Document document = client.getDocument(request);
System.out.println("Title: " + document.getTitle());
System.out.println("URI: " + document.getUri());
System.out.println("Data Source: " + document.getDataSource());
System.out.println("Content Length: " + document.getContentLengthBytes() + " bytes");
String preview = document.getContent();
if (preview.length() > 150) {
preview = preview.substring(0, 150) + "...";
}
System.out.println("Content Preview: " + preview + "\n");
return document;
}
}
}
بازیابی چندین سند
از BatchGetDocuments برای بازیابی حداکثر 20 سند بر اساس نام منبع در یک فراخوانی API استفاده کنید.
برای مشاهدهی مثالی از نحوهی فراخوانی BatchGetDocuments یک زبان را انتخاب کنید:
پایتون
from typing import List, Optional
from google.cloud import developer_knowledge_v1
def batch_get_documents(
names: Optional[List[str]] = None,
) -> developer_knowledge_v1.BatchGetDocumentsResponse:
"""Retrieves multiple developer documentation pages in a single request.
Args:
names: A list of resource names in format 'documents/{uri_without_scheme}'.
Returns:
The BatchGetDocumentsResponse containing the retrieved documents.
"""
if names is None:
names = [
"documents/docs.cloud.google.com/storage/docs/creating-buckets",
"documents/docs.cloud.google.com/storage/docs/deleting-buckets",
]
client = developer_knowledge_v1.DeveloperKnowledgeClient()
request = developer_knowledge_v1.BatchGetDocumentsRequest(
names=names,
)
response = client.batch_get_documents(request=request)
for doc in response.documents:
print(f"Title: {doc.title}")
print(f"URI: {doc.uri}")
print(f"Content Length: {doc.content_length_bytes} bytes\n")
return response
Node.js و TypeScript
const {DeveloperKnowledgeClient} = require('@google/developer-knowledge');
/**
* Retrieves multiple developer documentation pages in a single request.
*
* @param {string[]} names Array of resource names in format 'documents/{uri_without_scheme}'.
*/
async function batchGetDocuments(
names = [
'documents/docs.cloud.google.com/storage/docs/creating-buckets',
'documents/docs.cloud.google.com/storage/docs/deleting-buckets',
]
) {
const client = new DeveloperKnowledgeClient();
const request = {
names,
};
const [response] = await client.batchGetDocuments(request);
if (response.documents) {
for (const doc of response.documents) {
console.log(`Title: ${doc.title}`);
console.log(`URI: ${doc.uri}`);
console.log(`Content Length: ${doc.contentLengthBytes} bytes\n`);
}
}
return response;
}
برو
import (
"context"
"fmt"
"io"
developerknowledge "cloud.google.com/go/developerknowledge/apiv1"
developerknowledgepb "cloud.google.com/go/developerknowledge/apiv1/developerknowledgepb"
)
// batchGetDocuments retrieves multiple developer documentation pages in a single request.
func batchGetDocuments(w io.Writer, names []string) (*developerknowledgepb.BatchGetDocumentsResponse, error) {
ctx := context.Background()
client, err := developerknowledge.NewDeveloperKnowledgeClient(ctx)
if err != nil {
return nil, fmt.Errorf("developerknowledge.NewDeveloperKnowledgeClient: %w", err)
}
defer client.Close()
req := &developerknowledgepb.BatchGetDocumentsRequest{
Names: names,
}
resp, err := client.BatchGetDocuments(ctx, req)
if err != nil {
return nil, fmt.Errorf("BatchGetDocuments: %w", err)
}
for _, doc := range resp.GetDocuments() {
fmt.Fprintf(w, "Title: %s\n", doc.GetTitle())
fmt.Fprintf(w, "\tURI: %s\n", doc.GetUri())
fmt.Fprintf(w, "\tContent Length: %d bytes\n\n", doc.GetContentLengthBytes())
}
return resp, nil
}
جاوا
import com.google.developers.knowledge.v1.BatchGetDocumentsRequest;
import com.google.developers.knowledge.v1.BatchGetDocumentsResponse;
import com.google.developers.knowledge.v1.DeveloperKnowledgeClient;
import com.google.developers.knowledge.v1.Document;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class BatchGetDocuments {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
List<String> names =
Arrays.asList(
"documents/docs.cloud.google.com/storage/docs/creating-buckets",
"documents/docs.cloud.google.com/storage/docs/deleting-buckets");
batchGetDocuments(names);
}
// Retrieves multiple developer documentation pages in a single request.
public static BatchGetDocumentsResponse batchGetDocuments(List<String> names) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DeveloperKnowledgeClient client = DeveloperKnowledgeClient.create()) {
BatchGetDocumentsRequest request =
BatchGetDocumentsRequest.newBuilder().addAllNames(names).build();
BatchGetDocumentsResponse response = client.batchGetDocuments(request);
for (Document doc : response.getDocumentsList()) {
System.out.println("Title: " + doc.getTitle());
System.out.println("URI: " + doc.getUri());
System.out.println("Content Length: " + doc.getContentLengthBytes() + " bytes\n");
}
return response;
}
}
}
قدم بعدی چیست؟
- برای مشاهده لیست کامل مستندات موجود، مرجع Corpus را بررسی کنید.
- برای جزئیات بیشتر در مورد متدها و پارامترهای API، به مستندات مرجع API مراجعه کنید.
- یاد بگیرید که چگونه سرور MCP را در Google Antigravity راهاندازی کنید .