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