Skip to content

Commit 209d2e0

Browse files
committed
updated guardrails
1 parent 4d8203a commit 209d2e0

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

apps/sim/app/api/copilot/chat/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ export async function POST(req: NextRequest) {
290290
provider: 'azure-anthropic',
291291
model: envModel,
292292
apiKey: env.AZURE_ANTHROPIC_API_KEY,
293+
apiVersion: env.AZURE_ANTHROPIC_API_VERSION,
293294
endpoint: env.AZURE_ANTHROPIC_ENDPOINT,
294295
}
295296
} else if (providerEnv === 'vertex') {

apps/sim/blocks/blocks/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ Return ONLY the JSON array.`,
333333
id: 'azureApiVersion',
334334
title: 'Azure API Version',
335335
type: 'short-input',
336-
placeholder: '2024-07-01-preview',
336+
placeholder: 'Enter API version',
337337
connectionDroppable: false,
338338
condition: {
339339
field: 'model',

apps/sim/blocks/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function getProviderCredentialSubBlocks(): SubBlockConfig[] {
125125
id: 'azureApiVersion',
126126
title: 'Azure API Version',
127127
type: 'short-input',
128-
placeholder: '2024-07-01-preview',
128+
placeholder: 'Enter API version',
129129
connectionDroppable: false,
130130
condition: {
131131
field: 'model',

apps/sim/lib/copilot/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export type CopilotProviderConfig =
151151
provider: 'azure-anthropic'
152152
model: string
153153
apiKey?: string
154+
apiVersion?: string
154155
endpoint?: string
155156
}
156157
| {

apps/sim/lib/core/config/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export const env = createEnv({
9797
AZURE_OPENAI_API_KEY: z.string().min(1).optional(), // Shared Azure OpenAI API key
9898
AZURE_ANTHROPIC_ENDPOINT: z.string().url().optional(), // Azure Anthropic service endpoint
9999
AZURE_ANTHROPIC_API_KEY: z.string().min(1).optional(), // Azure Anthropic API key
100+
AZURE_ANTHROPIC_API_VERSION: z.string().min(1).optional(), // Azure Anthropic API version (e.g. 2023-06-01)
100101
KB_OPENAI_MODEL_NAME: z.string().optional(), // Knowledge base OpenAI model name (works with both regular OpenAI and Azure OpenAI)
101102
WAND_OPENAI_MODEL_NAME: z.string().optional(), // Wand generation OpenAI model name (works with both regular OpenAI and Azure OpenAI)
102103
OCR_AZURE_ENDPOINT: z.string().url().optional(), // Azure Mistral OCR service endpoint

apps/sim/lib/guardrails/validate_hallucination.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { db } from '@sim/db'
2+
import { account } from '@sim/db/schema'
13
import { createLogger } from '@sim/logger'
4+
import { eq } from 'drizzle-orm'
25
import { getBaseUrl } from '@/lib/core/utils/urls'
6+
import { refreshTokenIfNeeded } from '@/app/api/auth/oauth/utils'
37
import { executeProviderRequest } from '@/providers'
48
import { getApiKey, getProviderFromModel } from '@/providers/utils'
59

@@ -138,6 +142,26 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
138142

139143
const providerId = getProviderFromModel(model)
140144

145+
// Resolve Vertex AI OAuth credential to access token if needed
146+
let resolvedApiKey = apiKey
147+
const resolvedCredentials = { ...providerCredentials }
148+
if (providerId === 'vertex' && providerCredentials?.vertexCredential) {
149+
const credential = await db.query.account.findFirst({
150+
where: eq(account.id, providerCredentials.vertexCredential),
151+
})
152+
if (credential) {
153+
const { accessToken } = await refreshTokenIfNeeded(
154+
requestId,
155+
credential,
156+
providerCredentials.vertexCredential
157+
)
158+
if (accessToken) {
159+
resolvedApiKey = accessToken
160+
}
161+
}
162+
resolvedCredentials.vertexCredential = undefined
163+
}
164+
141165
const response = await executeProviderRequest(providerId, {
142166
model,
143167
systemPrompt,
@@ -148,8 +172,8 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
148172
},
149173
],
150174
temperature: 0.1, // Low temperature for consistent scoring
151-
apiKey,
152-
...providerCredentials,
175+
apiKey: resolvedApiKey,
176+
...resolvedCredentials,
153177
})
154178

155179
if (response instanceof ReadableStream || ('stream' in response && 'execution' in response)) {

0 commit comments

Comments
 (0)