Add advisor model to anthropic

This commit is contained in:
Saoud Rizwan committed 2025-01-20 16:12:10 -08:00
1 parent 43bf383784
commit 7aeab15ecf
4 files changed
+85 -29

No files matched your search

+26 -5
View File
@@ -1,6 +1,14 @@
import { Anthropic } from "@anthropic-ai/sdk"
import { Stream as AnthropicStream } from "@anthropic-ai/sdk/streaming"
import { anthropicDefaultModelId, AnthropicModelId, anthropicModels, ApiHandlerOptions, ModelInfo } from "../../shared/api"
import {
anthropicDefaultAdvisorModelId,
anthropicDefaultModelId,
AnthropicModelId,
anthropicModels,
ApiHandlerOptions,
ModelInfo,
ModelType,
} from "../../shared/api"
import { ApiHandler } from "../index"
import { ApiStream } from "../transform/stream"
@@ -16,9 +24,10 @@ export class AnthropicHandler implements ApiHandler {
})
}
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[], modelType: ModelType): ApiStream {
const model = modelType === "advisor" ? this.getAdvisorModel() : this.getModel()
let stream: AnthropicStream<Anthropic.Beta.PromptCaching.Messages.RawPromptCachingBetaMessageStreamEvent>
const modelId = this.getModel().id
const modelId = model.id
switch (modelId) {
// 'latest' alias does not support cache_control
case "claude-3-5-sonnet-20241022":
@@ -37,7 +46,7 @@ export class AnthropicHandler implements ApiHandler {
stream = await this.client.beta.promptCaching.messages.create(
{
model: modelId,
max_tokens: this.getModel().info.maxTokens || 8192,
max_tokens: model.info.maxTokens || 8192,
temperature: 0,
system: [
{
@@ -104,7 +113,7 @@ export class AnthropicHandler implements ApiHandler {
default: {
stream = (await this.client.messages.create({
model: modelId,
max_tokens: this.getModel().info.maxTokens || 8192,
max_tokens: model.info.maxTokens || 8192,
temperature: 0,
system: [{ text: systemPrompt, type: "text" }],
messages,
@@ -185,4 +194,16 @@ export class AnthropicHandler implements ApiHandler {
info: anthropicModels[anthropicDefaultModelId],
}
}
getAdvisorModel(): { id: string; info: ModelInfo } {
const modelId = this.options.anthropicAdvisorModelId
if (modelId && modelId in anthropicModels) {
const id = modelId as AnthropicModelId
return { id, info: anthropicModels[id] }
}
return {
id: anthropicDefaultAdvisorModelId,
info: anthropicModels[anthropicDefaultAdvisorModelId],
}
}
}