-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,46 @@ | ||
import * as traceloop from "@traceloop/node-server-sdk"; | ||
import { VertexAI } from "@google-cloud/vertexai"; | ||
// import { ConsoleSpanExporter } from "@opentelemetry/sdk-trace-node"; | ||
|
||
traceloop.initialize({ | ||
appName: "sample_vertexai", | ||
apiKey: process.env.TRACELOOP_API_KEY, | ||
disableBatch: true, | ||
// exporter: new ConsoleSpanExporter(), | ||
}); | ||
|
||
async function createNonStreamingContent() { | ||
// Initialize Vertex with your Cloud project and location | ||
const vertexAI = new VertexAI({ | ||
project: process.env.VERTEXAI_PROJECT_ID ?? "", | ||
location: process.env.VERTEXAI_LOCATION ?? "", | ||
}); | ||
|
||
// Instantiate the model | ||
const generativeModel = vertexAI.preview.getGenerativeModel({ | ||
model: "gemini-pro-vision", | ||
}); | ||
// Initialize Vertex with your Cloud project and location | ||
const vertexAI = new VertexAI({ | ||
project: process.env.VERTEXAI_PROJECT_ID ?? "", | ||
location: process.env.VERTEXAI_LOCATION ?? "", | ||
}); | ||
|
||
const request = { | ||
contents: [{ role: "user", parts: [{ text: "What is Node.js?" }] }], | ||
}; | ||
async function createNonStreamingContent() { | ||
return await traceloop.withWorkflow("sample_completion", {}, async () => { | ||
// Instantiate the model | ||
const generativeModel = vertexAI.preview.getGenerativeModel({ | ||
model: "gemini-pro-vision", | ||
}); | ||
|
||
console.log("Prompt:"); | ||
console.log(request.contents[0].parts[0].text); | ||
console.log("Non-Streaming Response Text:"); | ||
const request = { | ||
contents: [{ role: "user", parts: [{ text: "What is Node.js?" }] }], | ||
}; | ||
|
||
// Create the response stream | ||
const responseStream = await generativeModel.generateContent(request); | ||
// Create the response stream | ||
const responseStream = await generativeModel.generateContent(request); | ||
|
||
// Wait for the response stream to complete | ||
const aggregatedResponse = await responseStream.response; | ||
// Wait for the response stream to complete | ||
const aggregatedResponse = await responseStream.response; | ||
|
||
// Select the text from the response | ||
const fullTextResponse = | ||
aggregatedResponse.candidates[0].content.parts[0].text; | ||
// Select the text from the response | ||
const fullTextResponse = | ||
aggregatedResponse.candidates[0].content.parts[0].text; | ||
|
||
console.log(fullTextResponse); | ||
return fullTextResponse; | ||
}); | ||
} | ||
|
||
createNonStreamingContent().catch((err) => { | ||
console.error(err.message); | ||
traceloop.withAssociationProperties({}, async () => { | ||
const completionResponse = await createNonStreamingContent(); | ||
console.log(completionResponse); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters