1 Commits

Author SHA1 Message Date
user 9e6fd7c24c Implement tools (function calling) for Claude 2024-09-08 00:04:03 +00:00
4 changed files with 24 additions and 1 deletions
@@ -144,6 +144,8 @@ function applyAwsStrictValidation(req: Request): unknown {
temperature: true, temperature: true,
top_k: true, top_k: true,
top_p: true, top_p: true,
tools: true,
tool_choice: true,
}) })
.strip() .strip()
.parse(req.body); .parse(req.body);
@@ -24,7 +24,6 @@ export const signGcpRequest: RequestPreprocessor = async (req) => {
req.isStreaming = String(stream) === "true"; req.isStreaming = String(stream) === "true";
// TODO: This should happen in transform-outbound-payload.ts // TODO: This should happen in transform-outbound-payload.ts
// TODO: Support tools
let strippedParams: Record<string, unknown>; let strippedParams: Record<string, unknown>;
strippedParams = AnthropicV1MessagesSchema.pick({ strippedParams = AnthropicV1MessagesSchema.pick({
messages: true, messages: true,
@@ -34,6 +33,8 @@ export const signGcpRequest: RequestPreprocessor = async (req) => {
temperature: true, temperature: true,
top_k: true, top_k: true,
top_p: true, top_p: true,
tools: true,
tool_choice: true,
stream: true, stream: true,
}) })
.strip() .strip()
+17
View File
@@ -19,7 +19,12 @@ const AnthropicV1BaseSchema = z
top_k: z.coerce.number().optional(), top_k: z.coerce.number().optional(),
top_p: z.coerce.number().optional(), top_p: z.coerce.number().optional(),
metadata: z.object({ user_id: z.string().optional() }).optional(), metadata: z.object({ user_id: z.string().optional() }).optional(),
tools: z.array(z.any()).optional(),
tool_choice: z.any().optional(),
}) })
.omit(
Boolean(config.allowOpenAIToolUsage) ? {} : { tools: true, tool_choice: true }
)
.strip(); .strip();
// https://docs.anthropic.com/claude/reference/complete_post [deprecated] // https://docs.anthropic.com/claude/reference/complete_post [deprecated]
@@ -44,6 +49,18 @@ const AnthropicV1MessageMultimodalContentSchema = z.array(
data: z.string(), data: z.string(),
}), }),
}), }),
z.object({
type: z.literal("tool_use"),
id: z.string(),
name: z.string(),
input: z.object({}).passthrough(),
}),
z.object({
type: z.literal("tool_result"),
tool_use_id: z.string(),
is_error: z.boolean().optional(),
content: z.union([z.string(), z.object({}).passthrough()]).optional(),
}),
]) ])
); );
+3
View File
@@ -67,6 +67,9 @@ async function getTokenCountForMessages({
case "image": case "image":
numTokens += await getImageTokenCount(part.source.data); numTokens += await getImageTokenCount(part.source.data);
break; break;
case "tool_use":
case "tool_result":
break;
default: default:
throw new Error(`Unsupported Anthropic content type.`); throw new Error(`Unsupported Anthropic content type.`);
} }