Skip to content

Commit 663e438

Browse files
authored
fix(vertex-ai): missing system prompt (#473)
1 parent 23fdb28 commit 663e438

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

package-lock.json

+10-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/instrumentation-vertexai/src/vertexai-instrumentation.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,22 @@ export class VertexAIInstrumentation extends InstrumentationBase {
157157
}
158158

159159
if (this._shouldSendPrompts() && "contents" in params) {
160-
attributes[`${SpanAttributes.LLM_PROMPTS}.0.role`] =
161-
params.contents[0].role ?? "user";
162-
attributes[`${SpanAttributes.LLM_PROMPTS}.0.content`] =
163-
this._formatPartsData(params.contents[0].parts);
160+
let i = 0;
161+
162+
if (instance["systemInstruction"]) {
163+
attributes[`${SpanAttributes.LLM_PROMPTS}.${i}.role`] = "system";
164+
attributes[`${SpanAttributes.LLM_PROMPTS}.${i}.content`] =
165+
this._formatPartsData(instance["systemInstruction"].parts);
166+
167+
i++;
168+
}
169+
170+
params.contents.forEach((content, j) => {
171+
attributes[`${SpanAttributes.LLM_PROMPTS}.${i + j}.role`] =
172+
content.role ?? "user";
173+
attributes[`${SpanAttributes.LLM_PROMPTS}.${i + j}.content`] =
174+
this._formatPartsData(content.parts);
175+
});
164176
}
165177
} catch (e) {
166178
this._diag.debug(e);

packages/sample-app/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"@anthropic-ai/sdk": "^0.27.1",
3737
"@aws-sdk/client-bedrock-runtime": "^3.499.0",
3838
"@azure/openai": "^1.0.0-beta.11",
39-
"@google-cloud/aiplatform": "^3.10.0",
40-
"@google-cloud/vertexai": "^1.2.0",
39+
"@google-cloud/aiplatform": "^3.32.0",
40+
"@google-cloud/vertexai": "^1.9.0",
4141
"@langchain/community": "^0.2.31",
4242
"@pinecone-database/pinecone": "^2.0.1",
4343
"@traceloop/node-server-sdk": "*",

packages/sample-app/src/vertexai/gemini.ts

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ async function createNonStreamingContent() {
2020
// Instantiate the model
2121
const generativeModel = vertexAI.getGenerativeModel({
2222
model: "gemini-1.5-flash",
23+
systemInstruction: {
24+
role: "system",
25+
parts: [{ text: "You are a helpful assistant" }],
26+
},
2327
});
2428

2529
const request = {
@@ -53,6 +57,10 @@ async function createStreamingContent() {
5357
// Instantiate the model
5458
const generativeModel = vertexAI.getGenerativeModel({
5559
model: "gemini-1.5-flash",
60+
systemInstruction: {
61+
role: "system",
62+
parts: [{ text: "You are a helpful assistant" }],
63+
},
5664
});
5765

5866
const request = {

0 commit comments

Comments
 (0)