transform.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import type { LanguageModelV1Prompt } from "ai"
  2. import { unique } from "remeda"
  3. export namespace ProviderTransform {
  4. export function message(
  5. msgs: LanguageModelV1Prompt,
  6. providerID: string,
  7. modelID: string,
  8. ) {
  9. if (providerID === "anthropic" || modelID.includes("anthropic")) {
  10. const system = msgs.filter((msg) => msg.role === "system").slice(0, 2)
  11. const final = msgs.filter((msg) => msg.role !== "system").slice(-2)
  12. for (const msg of unique([...system, ...final])) {
  13. msg.providerMetadata = {
  14. ...msg.providerMetadata,
  15. anthropic: {
  16. cacheControl: { type: "ephemeral" },
  17. },
  18. }
  19. }
  20. }
  21. if (providerID === "amazon-bedrock" || modelID.includes("anthropic")) {
  22. const system = msgs.filter((msg) => msg.role === "system").slice(0, 2)
  23. const final = msgs.filter((msg) => msg.role !== "system").slice(-2)
  24. for (const msg of unique([...system, ...final])) {
  25. msg.providerMetadata = {
  26. ...msg.providerMetadata,
  27. bedrock: {
  28. cachePoint: { type: "ephemeral" },
  29. },
  30. }
  31. }
  32. }
  33. return msgs
  34. }
  35. }