-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(developer-knowledge): add Developer Knowledge API samples #4424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b562083
42e625e
4fe594e
58688b2
cf91912
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| --- | ||
| rules: | ||
| no-console: off | ||
| node/no-unsupported-features/node-builtins: off | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Google Developer Knowledge API Node.js Samples | ||
|
|
||
| This directory contains Node.js code samples demonstrating how to use the [Google Developer Knowledge API](https://developers.google.com/knowledge) client library (`@google/developer-knowledge`). | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Enable the Developer Knowledge API on your Google Cloud project: | ||
|
|
||
| ```bash | ||
| gcloud services enable developerknowledge.googleapis.com | ||
| ``` | ||
|
|
||
| 2. Install dependencies: | ||
| ```bash | ||
| npm install | ||
| ``` | ||
|
|
||
| ## Samples | ||
|
|
||
| - **[Answer Query](answerQuery.js)**: Get a grounded, cited answer to a technical question (`developerknowledge_answer_query`). | ||
| - **[Get Document](getDocument.js)**: Retrieve a single documentation page with full markdown content (`developerknowledge_get_document`). | ||
| - **[Batch Get Documents](batchGetDocuments.js)**: Fetch multiple documentation pages in one call (`developerknowledge_batch_get_documents`). | ||
| - **[Search Document Chunks](searchDocumentChunks.js)**: Search public developer documentation chunks by query (`developerknowledge_search_document_chunks`). | ||
|
|
||
| ## Running Tests | ||
|
|
||
| ```bash | ||
| npm test | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START developerknowledge_answer_query] | ||
| 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; | ||
| } | ||
| // [END developerknowledge_answer_query] | ||
|
|
||
| module.exports = {answerQuery}; | ||
|
|
||
| if (require.main === module) { | ||
| answerQuery(...process.argv.slice(2)).catch(err => { | ||
| console.error(err); | ||
| process.exitCode = 1; | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START developerknowledge_batch_get_documents] | ||
| 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; | ||
| } | ||
| // [END developerknowledge_batch_get_documents] | ||
|
|
||
| module.exports = {batchGetDocuments}; | ||
|
|
||
| if (require.main === module) { | ||
| batchGetDocuments().catch(err => { | ||
| console.error(err); | ||
| process.exitCode = 1; | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||
| // Copyright 2026 Google LLC | ||||||||
| // | ||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||
| // you may not use this file except in compliance with the License. | ||||||||
| // You may obtain a copy of the License at | ||||||||
| // | ||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||
| // | ||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||
| // See the License for the specific language governing permissions and | ||||||||
| // limitations under the License. | ||||||||
|
|
||||||||
| 'use strict'; | ||||||||
|
|
||||||||
| // [START developerknowledge_get_document] | ||||||||
| 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`); | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||
|
|
||||||||
| return document; | ||||||||
| } | ||||||||
| // [END developerknowledge_get_document] | ||||||||
|
|
||||||||
| module.exports = {getDocument}; | ||||||||
|
|
||||||||
| if (require.main === module) { | ||||||||
| getDocument(...process.argv.slice(2)).catch(err => { | ||||||||
| console.error(err); | ||||||||
| process.exitCode = 1; | ||||||||
| }); | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "name": "nodejs-developer-knowledge-samples", | ||
| "description": "Node.js samples for Google Developer Knowledge API", | ||
| "version": "0.0.1", | ||
| "private": true, | ||
| "license": "Apache-2.0", | ||
| "author": "Google LLC", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git", | ||
| "directory": "developer-knowledge" | ||
| }, | ||
| "engines": { | ||
| "node": ">=18.0.0" | ||
| }, | ||
| "files": [ | ||
| "*.js" | ||
| ], | ||
| "scripts": { | ||
| "test": "mocha test/*.test.js --timeout 60000" | ||
| }, | ||
| "dependencies": { | ||
| "@google/developer-knowledge": "^0.5.0" | ||
| }, | ||
| "devDependencies": { | ||
| "mocha": "^10.0.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START developerknowledge_search_document_chunks] | ||
| 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`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| return chunks; | ||
| } | ||
| // [END developerknowledge_search_document_chunks] | ||
|
|
||
| module.exports = {searchDocumentChunks}; | ||
|
|
||
| if (require.main === module) { | ||
| searchDocumentChunks(...process.argv.slice(2)).catch(err => { | ||
| console.error(err); | ||
| process.exitCode = 1; | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
response.answeris undefined or null (for example, if no answer could be generated for the query), accessingresponse.answer.answerTextor its other properties directly will throw aTypeError. Use optional chaining or default values to safely handle cases where the answer is missing.