|
| 1 | +const { |
| 2 | + VertexAI, |
| 3 | + HarmBlockThreshold, |
| 4 | + HarmCategory, |
| 5 | +} = require('@google-cloud/vertexai'); |
| 6 | + |
| 7 | +const project = 'cloud-llm-preview1'; |
| 8 | +const location = 'us-central1'; |
| 9 | + |
| 10 | +// Initialize Vertex with your Cloud project and location |
| 11 | +const vertex_ai = new VertexAI({project: project, location: location}); |
| 12 | + |
| 13 | +// Instantiate the models |
| 14 | +const generativeModel = vertex_ai.preview.getGenerativeModel({ |
| 15 | + model: 'gemini-pro', |
| 16 | + // The following parameters are optional |
| 17 | + // They can also be passed to individual content generation requests |
| 18 | + safety_settings: [ |
| 19 | + { |
| 20 | + category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, |
| 21 | + threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, |
| 22 | + }, |
| 23 | + ], |
| 24 | + generation_config: {max_output_tokens: 256}, |
| 25 | +}); |
| 26 | + |
| 27 | +const generativeVisionModel = vertex_ai.preview.getGenerativeModel({ |
| 28 | + model: 'gemini-pro-vision', |
| 29 | +}); |
| 30 | + |
| 31 | +async function streamContentTextOnly() { |
| 32 | + const req = { |
| 33 | + contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}], |
| 34 | + }; |
| 35 | + |
| 36 | + const streamingResp = await generativeModel.generateContentStream(req); |
| 37 | + |
| 38 | + for await (const item of streamingResp.stream) { |
| 39 | + console.log('stream chunk:', item); |
| 40 | + } |
| 41 | + |
| 42 | + console.log('aggregated response: ', await streamingResp.response); |
| 43 | +} |
| 44 | + |
| 45 | +async function nonStreamingTextOnly() { |
| 46 | + const req = { |
| 47 | + contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}], |
| 48 | + }; |
| 49 | + |
| 50 | + const nonstreamingResp = await generativeModel.generateContent(req); |
| 51 | + console.log('non-streaming response: ', await nonstreamingResp.response); |
| 52 | +} |
| 53 | + |
| 54 | +async function countTokens() { |
| 55 | + const req = { |
| 56 | + contents: [{role: 'user', parts: [{text: 'How are you doing today?'}]}], |
| 57 | + }; |
| 58 | + |
| 59 | + const countTokensResp = await generativeModel.countTokens(req); |
| 60 | + console.log('count tokens response: ', countTokensResp); |
| 61 | +} |
| 62 | + |
| 63 | +async function nonStreamingChat() { |
| 64 | + const chat = generativeModel.startChat({}); |
| 65 | + const result1 = await chat.sendMessage('hello'); |
| 66 | + console.log('send message result1: ', result1); |
| 67 | + const resp1 = result1.response; |
| 68 | + console.log('send message response1: ', resp1); |
| 69 | + const result2 = await chat.sendMessage('what day is it today?'); |
| 70 | + console.log('result2: ', result2); |
| 71 | + const resp2 = result2.response; |
| 72 | + console.log('send message response2: ', resp2); |
| 73 | + const result3 = await chat.sendMessage('what day is it tomorrow?'); |
| 74 | + console.log('result3: ', result3); |
| 75 | + const resp3 = result3.response; |
| 76 | + console.log('send message response3: ', resp3); |
| 77 | +} |
| 78 | + |
| 79 | +async function streamingChat() { |
| 80 | + const chat = generativeModel.startChat({}); |
| 81 | + const streamResult1 = await chat.sendMessageStream('hello again'); |
| 82 | + console.log('stream result1: ', streamResult1); |
| 83 | + const streamResp1 = await streamResult1.response; |
| 84 | + console.log('stream send message response1: ', streamResp1); |
| 85 | + const streamResult2 = await chat.sendMessageStream('what is the date today?'); |
| 86 | + console.log('stream result2: ', streamResult2); |
| 87 | + const streamResp2 = await streamResult2.response; |
| 88 | + console.log('stream send message response2: ', streamResp2); |
| 89 | + const streamResult3 = await chat.sendMessageStream( |
| 90 | + 'what is the date tomorrow?' |
| 91 | + ); |
| 92 | + console.log('stream result3: ', streamResult3); |
| 93 | + const streamResp3 = await streamResult3.response; |
| 94 | + console.log('stream send message response3: ', streamResp3); |
| 95 | +} |
| 96 | + |
| 97 | +async function multiPartContent() { |
| 98 | + const filePart = { |
| 99 | + file_data: { |
| 100 | + file_uri: 'gs://sararob_imagegeneration_test/kitten.jpeg', |
| 101 | + mime_type: 'image/jpeg', |
| 102 | + }, |
| 103 | + }; |
| 104 | + const textPart = {text: 'What is this a picture of?'}; |
| 105 | + |
| 106 | + const request = { |
| 107 | + contents: [{role: 'user', parts: [textPart, filePart]}], |
| 108 | + }; |
| 109 | + |
| 110 | + const generativeVisionModel = vertex_ai.preview.getGenerativeModel({ |
| 111 | + model: 'gemini-pro-vision', |
| 112 | + }); |
| 113 | + |
| 114 | + const resp = await generativeVisionModel.generateContentStream(request); |
| 115 | + const contentResponse = await resp.response; |
| 116 | + console.log(contentResponse.candidates[0].content); |
| 117 | +} |
| 118 | + |
| 119 | +nonStreamingTextOnly(); |
| 120 | +streamContentTextOnly(); |
| 121 | +countTokens(); |
| 122 | +nonStreamingChat(); |
| 123 | +streamingChat(); |
| 124 | +multiPartContent(); |
0 commit comments