-
Notifications
You must be signed in to change notification settings - Fork 628
default to stagehand LLM clients for evals #669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
489d585
b347602
18ae8fc
7f3d935
7ac680c
cbd66d0
e1c43a2
396d772
821fc7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,21 @@ | |||||||
import { LogLine } from "@/dist"; | ||||||||
import stringComparison from "string-comparison"; | ||||||||
const { jaroWinkler } = stringComparison; | ||||||||
import OpenAI from "openai"; | ||||||||
import { wrapAISDKModel, wrapOpenAI } from "braintrust"; | ||||||||
import { anthropic } from "@ai-sdk/anthropic"; | ||||||||
import { google } from "@ai-sdk/google"; | ||||||||
import { groq } from "@ai-sdk/groq"; | ||||||||
import { cerebras } from "@ai-sdk/cerebras"; | ||||||||
import { LLMClient } from "@/dist"; | ||||||||
import { AISdkClient } from "@/examples/external_clients/aisdk"; | ||||||||
import { CustomOpenAIClient } from "@/examples/external_clients/customOpenAI"; | ||||||||
import { OpenAIClient } from "@/lib/llm/OpenAIClient"; | ||||||||
import { AnthropicClient } from "@/lib/llm/AnthropicClient"; | ||||||||
import { GoogleClient } from "@/lib/llm/GoogleClient"; | ||||||||
import { CreateLLMClientOptions } from "@/types/evals"; | ||||||||
import { StagehandEvalError } from "@/types/stagehandErrors"; | ||||||||
import { openai } from "@ai-sdk/openai"; | ||||||||
|
||||||||
/** | ||||||||
* normalizeString: | ||||||||
|
@@ -119,3 +134,103 @@ export function logLineToString(logLine: LogLine): string { | |||||||
return "error logging line"; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
export function createLLMClient({ | ||||||||
modelName, | ||||||||
useExternalClients, | ||||||||
logger, | ||||||||
openAiKey, | ||||||||
googleKey, | ||||||||
anthropicKey, | ||||||||
togetherKey, | ||||||||
}: CreateLLMClientOptions): LLMClient { | ||||||||
const isOpenAIModel = | ||||||||
modelName.startsWith("gpt") || modelName.startsWith("o"); | ||||||||
const isGoogleModel = modelName.startsWith("gemini"); | ||||||||
const isAnthropicModel = modelName.startsWith("claude"); | ||||||||
const isGroqModel = modelName.includes("groq"); | ||||||||
const isCerebrasModel = modelName.includes("cerebras"); | ||||||||
|
||||||||
if (useExternalClients) { | ||||||||
if (isOpenAIModel) { | ||||||||
if (modelName.includes("/")) { | ||||||||
return new CustomOpenAIClient({ | ||||||||
modelName, | ||||||||
client: wrapOpenAI( | ||||||||
new OpenAI({ | ||||||||
apiKey: togetherKey, | ||||||||
baseURL: "https://api.together.xyz/v1", | ||||||||
}), | ||||||||
), | ||||||||
}); | ||||||||
} | ||||||||
return new AISdkClient({ | ||||||||
model: wrapAISDKModel(openai(modelName)), | ||||||||
}); | ||||||||
} else if (isGoogleModel) { | ||||||||
return new AISdkClient({ | ||||||||
model: wrapAISDKModel(google(modelName)), | ||||||||
}); | ||||||||
} else if (isAnthropicModel) { | ||||||||
return new AISdkClient({ | ||||||||
model: wrapAISDKModel(anthropic(modelName)), | ||||||||
}); | ||||||||
} else if (isGroqModel) { | ||||||||
const groqModel = modelName.substring(modelName.indexOf("/") + 1); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Potential error if '/' is not found in modelName. Add null check before substring operation.
Suggested change
|
||||||||
return new AISdkClient({ | ||||||||
model: wrapAISDKModel(groq(groqModel)), | ||||||||
}); | ||||||||
} else if (isCerebrasModel) { | ||||||||
const cerebrasModel = modelName.substring(modelName.indexOf("/") + 1); | ||||||||
return new AISdkClient({ | ||||||||
model: wrapAISDKModel(cerebras(cerebrasModel)), | ||||||||
}); | ||||||||
} | ||||||||
throw new StagehandEvalError(`Unknown modelName: ${modelName}`); | ||||||||
} else { | ||||||||
if (isOpenAIModel) { | ||||||||
if (modelName.includes("/")) { | ||||||||
return new CustomOpenAIClient({ | ||||||||
modelName, | ||||||||
client: wrapOpenAI( | ||||||||
new OpenAI({ | ||||||||
apiKey: togetherKey, | ||||||||
baseURL: "https://api.together.xyz/v1", | ||||||||
}), | ||||||||
), | ||||||||
}); | ||||||||
} | ||||||||
Comment on lines
+192
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: This block is duplicated from the external clients section. Consider extracting Together.ai model handling into a separate function to avoid duplication. |
||||||||
return new OpenAIClient({ | ||||||||
logger, | ||||||||
modelName, | ||||||||
enableCaching: false, | ||||||||
clientOptions: { | ||||||||
apiKey: openAiKey, | ||||||||
}, | ||||||||
}); | ||||||||
} else if (isGoogleModel) { | ||||||||
return new GoogleClient({ | ||||||||
logger, | ||||||||
modelName, | ||||||||
enableCaching: false, | ||||||||
clientOptions: { | ||||||||
apiKey: googleKey, | ||||||||
}, | ||||||||
}); | ||||||||
} else if (isAnthropicModel) { | ||||||||
return new AnthropicClient({ | ||||||||
logger, | ||||||||
modelName, | ||||||||
enableCaching: false, | ||||||||
clientOptions: { | ||||||||
apiKey: anthropicKey, | ||||||||
}, | ||||||||
}); | ||||||||
} else if (isGroqModel || isCerebrasModel) { | ||||||||
throw new StagehandEvalError( | ||||||||
`${modelName} can only be used when useExternalClients=true`, | ||||||||
); | ||||||||
} | ||||||||
throw new StagehandEvalError(`Unknown modelName: ${modelName}`); | ||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Strict comparison with boolean could cause issues if parsedArgs.useExternalClients is undefined. Consider using
!!parsedArgs.useExternalClients
instead.