|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | // [START aiplatform_gemini_function_calling_content]
|
16 |
| -const { VertexAI, FunctionDeclarationSchemaType } = require('@google-cloud/vertexai'); |
| 16 | +const {VertexAI, FunctionDeclarationSchemaType} = require('@google-cloud/vertexai'); |
17 | 17 |
|
18 | 18 | const functionDeclarations = [
|
19 |
| - { |
20 |
| - function_declarations: [ |
21 |
| - { |
22 |
| - name: 'get_current_weather', |
23 |
| - description: 'get weather in a given location', |
24 |
| - parameters: { |
25 |
| - type: FunctionDeclarationSchemaType.OBJECT, |
26 |
| - properties: { |
27 |
| - location: {type: FunctionDeclarationSchemaType.STRING}, |
28 |
| - unit: { |
29 |
| - type: FunctionDeclarationSchemaType.STRING, |
30 |
| - enum: ['celsius', 'fahrenheit'], |
31 |
| - }, |
32 |
| - }, |
33 |
| - required: ['location'], |
34 |
| - }, |
35 |
| - }, |
36 |
| - ], |
37 |
| - }, |
38 |
| - ]; |
39 |
| - |
40 |
| - const functionResponseParts = [ |
41 |
| - { |
42 |
| - functionResponse: { |
| 19 | + { |
| 20 | + function_declarations: [ |
| 21 | + { |
43 | 22 | name: 'get_current_weather',
|
44 |
| - response: |
45 |
| - {name: 'get_current_weather', content: {weather: 'super nice'}}, |
46 |
| - }, |
47 |
| - }, |
48 |
| - ]; |
| 23 | + description: 'get weather in a given location', |
| 24 | + parameters: { |
| 25 | + type: FunctionDeclarationSchemaType.OBJECT, |
| 26 | + properties: { |
| 27 | + location: {type: FunctionDeclarationSchemaType.STRING}, |
| 28 | + unit: { |
| 29 | + type: FunctionDeclarationSchemaType.STRING, |
| 30 | + enum: ['celsius', 'fahrenheit'], |
| 31 | + }, |
| 32 | + }, |
| 33 | + required: ['location'], |
| 34 | + }, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }, |
| 38 | +]; |
| 39 | + |
| 40 | +const functionResponseParts = [ |
| 41 | + { |
| 42 | + functionResponse: { |
| 43 | + name: 'get_current_weather', |
| 44 | + response: |
| 45 | + {name: 'get_current_weather', content: {weather: 'super nice'}}, |
| 46 | + }, |
| 47 | + }, |
| 48 | +]; |
49 | 49 |
|
50 | 50 | /**
|
51 | 51 | * TODO(developer): Update these variables before running the sample.
|
52 | 52 | */
|
53 | 53 | async function functionCallingStreamChat(
|
54 |
| - projectId = 'PROJECT_ID', |
55 |
| - location = 'us-central1', |
56 |
| - model = 'gemini-pro' |
| 54 | + projectId = 'PROJECT_ID', |
| 55 | + location = 'us-central1', |
| 56 | + model = 'gemini-pro' |
57 | 57 | ) {
|
58 |
| - // Initialize Vertex with your Cloud project and location |
59 |
| - const vertexAI = new VertexAI({ project: projectId, location: location }); |
| 58 | + // Initialize Vertex with your Cloud project and location |
| 59 | + const vertexAI = new VertexAI({project: projectId, location: location}); |
60 | 60 |
|
61 |
| - // Instantiate the model |
62 |
| - const generativeModel = vertexAI.preview.getGenerativeModel({ |
63 |
| - model: model, |
64 |
| - }); |
| 61 | + // Instantiate the model |
| 62 | + const generativeModel = vertexAI.preview.getGenerativeModel({ |
| 63 | + model: model, |
| 64 | + }); |
65 | 65 |
|
66 |
| - const request = { |
67 |
| - contents: [ |
68 |
| - {role: 'user', parts: [{text: 'What is the weather in Boston?'}]}, |
69 |
| - {role: 'model', parts: [{functionCall: {name: 'get_current_weather', args: {'location': 'Boston'}}}]}, |
70 |
| - {role: 'function', parts: functionResponseParts} |
71 |
| - ], |
72 |
| - tools: functionDeclarations, |
73 |
| - }; |
74 |
| - const streamingResp = |
75 |
| - await generativeModel.generateContentStream(request); |
76 |
| - for await (const item of streamingResp.stream) { |
77 |
| - console.log(item.candidates[0].content.parts[0].text); |
78 |
| - } |
| 66 | + const request = { |
| 67 | + contents: [ |
| 68 | + {role: 'user', parts: [{text: 'What is the weather in Boston?'}]}, |
| 69 | + {role: 'model', parts: [{functionCall: {name: 'get_current_weather', args: {'location': 'Boston'}}}]}, |
| 70 | + {role: 'function', parts: functionResponseParts} |
| 71 | + ], |
| 72 | + tools: functionDeclarations, |
| 73 | + }; |
| 74 | + const streamingResp = |
| 75 | + await generativeModel.generateContentStream(request); |
| 76 | + for await (const item of streamingResp.stream) { |
| 77 | + console.log(item.candidates[0].content.parts[0].text); |
| 78 | + } |
79 | 79 | }
|
80 | 80 | // [END aiplatform_gemini_function_calling_content]
|
81 | 81 |
|
82 | 82 | functionCallingStreamChat(...process.argv.slice(2)).catch(err => {
|
83 |
| - console.error(err.message); |
84 |
| - process.exitCode = 1; |
| 83 | + console.error(err.message); |
| 84 | + process.exitCode = 1; |
85 | 85 | });
|
0 commit comments