api.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. export type ApiProvider =
  2. | "anthropic"
  3. | "openrouter"
  4. | "bedrock"
  5. | "vertex"
  6. | "openai"
  7. | "ollama"
  8. | "lmstudio"
  9. | "gemini"
  10. | "openai-native"
  11. export interface ApiHandlerOptions {
  12. apiModelId?: string
  13. apiKey?: string // anthropic
  14. anthropicBaseUrl?: string
  15. openRouterApiKey?: string
  16. openRouterModelId?: string
  17. openRouterModelInfo?: ModelInfo
  18. awsAccessKey?: string
  19. awsSecretKey?: string
  20. awsSessionToken?: string
  21. awsRegion?: string
  22. awsUseCrossRegionInference?: boolean
  23. vertexProjectId?: string
  24. vertexRegion?: string
  25. openAiBaseUrl?: string
  26. openAiApiKey?: string
  27. openAiModelId?: string
  28. ollamaModelId?: string
  29. ollamaBaseUrl?: string
  30. lmStudioModelId?: string
  31. lmStudioBaseUrl?: string
  32. geminiApiKey?: string
  33. openAiNativeApiKey?: string
  34. azureApiVersion?: string
  35. openRouterUseMiddleOutTransform?: boolean
  36. }
  37. export type ApiConfiguration = ApiHandlerOptions & {
  38. apiProvider?: ApiProvider
  39. }
  40. // Models
  41. export interface ModelInfo {
  42. maxTokens?: number
  43. contextWindow?: number
  44. supportsImages?: boolean
  45. supportsComputerUse?: boolean
  46. supportsPromptCache: boolean // this value is hardcoded for now
  47. inputPrice?: number
  48. outputPrice?: number
  49. cacheWritesPrice?: number
  50. cacheReadsPrice?: number
  51. description?: string
  52. }
  53. // Anthropic
  54. // https://docs.anthropic.com/en/docs/about-claude/models
  55. export type AnthropicModelId = keyof typeof anthropicModels
  56. export const anthropicDefaultModelId: AnthropicModelId = "claude-3-5-sonnet-20241022"
  57. export const anthropicModels = {
  58. "claude-3-5-sonnet-20241022": {
  59. maxTokens: 8192,
  60. contextWindow: 200_000,
  61. supportsImages: true,
  62. supportsComputerUse: true,
  63. supportsPromptCache: true,
  64. inputPrice: 3.0, // $3 per million input tokens
  65. outputPrice: 15.0, // $15 per million output tokens
  66. cacheWritesPrice: 3.75, // $3.75 per million tokens
  67. cacheReadsPrice: 0.3, // $0.30 per million tokens
  68. },
  69. "claude-3-5-haiku-20241022": {
  70. maxTokens: 8192,
  71. contextWindow: 200_000,
  72. supportsImages: false,
  73. supportsPromptCache: true,
  74. inputPrice: 1.0,
  75. outputPrice: 5.0,
  76. cacheWritesPrice: 1.25,
  77. cacheReadsPrice: 0.1,
  78. },
  79. "claude-3-opus-20240229": {
  80. maxTokens: 4096,
  81. contextWindow: 200_000,
  82. supportsImages: true,
  83. supportsPromptCache: true,
  84. inputPrice: 15.0,
  85. outputPrice: 75.0,
  86. cacheWritesPrice: 18.75,
  87. cacheReadsPrice: 1.5,
  88. },
  89. "claude-3-haiku-20240307": {
  90. maxTokens: 4096,
  91. contextWindow: 200_000,
  92. supportsImages: true,
  93. supportsPromptCache: true,
  94. inputPrice: 0.25,
  95. outputPrice: 1.25,
  96. cacheWritesPrice: 0.3,
  97. cacheReadsPrice: 0.03,
  98. },
  99. } as const satisfies Record<string, ModelInfo> // as const assertion makes the object deeply readonly
  100. // AWS Bedrock
  101. // https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html
  102. export type BedrockModelId = keyof typeof bedrockModels
  103. export const bedrockDefaultModelId: BedrockModelId = "anthropic.claude-3-5-sonnet-20241022-v2:0"
  104. export const bedrockModels = {
  105. "anthropic.claude-3-5-sonnet-20241022-v2:0": {
  106. maxTokens: 8192,
  107. contextWindow: 200_000,
  108. supportsImages: true,
  109. supportsComputerUse: true,
  110. supportsPromptCache: false,
  111. inputPrice: 3.0,
  112. outputPrice: 15.0,
  113. },
  114. "anthropic.claude-3-5-haiku-20241022-v1:0": {
  115. maxTokens: 8192,
  116. contextWindow: 200_000,
  117. supportsImages: false,
  118. supportsPromptCache: false,
  119. inputPrice: 1.0,
  120. outputPrice: 5.0,
  121. },
  122. "anthropic.claude-3-5-sonnet-20240620-v1:0": {
  123. maxTokens: 8192,
  124. contextWindow: 200_000,
  125. supportsImages: true,
  126. supportsPromptCache: false,
  127. inputPrice: 3.0,
  128. outputPrice: 15.0,
  129. },
  130. "anthropic.claude-3-opus-20240229-v1:0": {
  131. maxTokens: 4096,
  132. contextWindow: 200_000,
  133. supportsImages: true,
  134. supportsPromptCache: false,
  135. inputPrice: 15.0,
  136. outputPrice: 75.0,
  137. },
  138. "anthropic.claude-3-sonnet-20240229-v1:0": {
  139. maxTokens: 4096,
  140. contextWindow: 200_000,
  141. supportsImages: true,
  142. supportsPromptCache: false,
  143. inputPrice: 3.0,
  144. outputPrice: 15.0,
  145. },
  146. "anthropic.claude-3-haiku-20240307-v1:0": {
  147. maxTokens: 4096,
  148. contextWindow: 200_000,
  149. supportsImages: true,
  150. supportsPromptCache: false,
  151. inputPrice: 0.25,
  152. outputPrice: 1.25,
  153. },
  154. } as const satisfies Record<string, ModelInfo>
  155. // OpenRouter
  156. // https://openrouter.ai/models?order=newest&supported_parameters=tools
  157. export const openRouterDefaultModelId = "anthropic/claude-3.5-sonnet:beta" // will always exist in openRouterModels
  158. export const openRouterDefaultModelInfo: ModelInfo = {
  159. maxTokens: 8192,
  160. contextWindow: 200_000,
  161. supportsImages: true,
  162. supportsComputerUse: true,
  163. supportsPromptCache: true,
  164. inputPrice: 3.0,
  165. outputPrice: 15.0,
  166. cacheWritesPrice: 3.75,
  167. cacheReadsPrice: 0.3,
  168. description:
  169. "The new Claude 3.5 Sonnet delivers better-than-Opus capabilities, faster-than-Sonnet speeds, at the same Sonnet prices. Sonnet is particularly good at:\n\n- Coding: New Sonnet scores ~49% on SWE-Bench Verified, higher than the last best score, and without any fancy prompt scaffolding\n- Data science: Augments human data science expertise; navigates unstructured data while using multiple tools for insights\n- Visual processing: excelling at interpreting charts, graphs, and images, accurately transcribing text to derive insights beyond just the text alone\n- Agentic tasks: exceptional tool use, making it great at agentic tasks (i.e. complex, multi-step problem solving tasks that require engaging with other systems)\n\n#multimodal\n\n_This is a faster endpoint, made available in collaboration with Anthropic, that is self-moderated: response moderation happens on the provider's side instead of OpenRouter's. For requests that pass moderation, it's identical to the [Standard](/anthropic/claude-3.5-sonnet) variant._",
  170. }
  171. // Vertex AI
  172. // https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude
  173. export type VertexModelId = keyof typeof vertexModels
  174. export const vertexDefaultModelId: VertexModelId = "claude-3-5-sonnet-v2@20241022"
  175. export const vertexModels = {
  176. "claude-3-5-sonnet-v2@20241022": {
  177. maxTokens: 8192,
  178. contextWindow: 200_000,
  179. supportsImages: true,
  180. supportsComputerUse: true,
  181. supportsPromptCache: false,
  182. inputPrice: 3.0,
  183. outputPrice: 15.0,
  184. },
  185. "claude-3-5-sonnet@20240620": {
  186. maxTokens: 8192,
  187. contextWindow: 200_000,
  188. supportsImages: true,
  189. supportsPromptCache: false,
  190. inputPrice: 3.0,
  191. outputPrice: 15.0,
  192. },
  193. "claude-3-5-haiku@20241022": {
  194. maxTokens: 8192,
  195. contextWindow: 200_000,
  196. supportsImages: false,
  197. supportsPromptCache: false,
  198. inputPrice: 1.0,
  199. outputPrice: 5.0,
  200. },
  201. "claude-3-opus@20240229": {
  202. maxTokens: 4096,
  203. contextWindow: 200_000,
  204. supportsImages: true,
  205. supportsPromptCache: false,
  206. inputPrice: 15.0,
  207. outputPrice: 75.0,
  208. },
  209. "claude-3-haiku@20240307": {
  210. maxTokens: 4096,
  211. contextWindow: 200_000,
  212. supportsImages: true,
  213. supportsPromptCache: false,
  214. inputPrice: 0.25,
  215. outputPrice: 1.25,
  216. },
  217. } as const satisfies Record<string, ModelInfo>
  218. export const openAiModelInfoSaneDefaults: ModelInfo = {
  219. maxTokens: -1,
  220. contextWindow: 128_000,
  221. supportsImages: true,
  222. supportsPromptCache: false,
  223. inputPrice: 0,
  224. outputPrice: 0,
  225. }
  226. // Gemini
  227. // https://ai.google.dev/gemini-api/docs/models/gemini
  228. export type GeminiModelId = keyof typeof geminiModels
  229. export const geminiDefaultModelId: GeminiModelId = "gemini-2.0-flash-exp"
  230. export const geminiModels = {
  231. "gemini-2.0-flash-exp": {
  232. maxTokens: 8192,
  233. contextWindow: 1_048_576,
  234. supportsImages: true,
  235. supportsPromptCache: false,
  236. inputPrice: 0,
  237. outputPrice: 0,
  238. },
  239. "gemini-1.5-flash-002": {
  240. maxTokens: 8192,
  241. contextWindow: 1_048_576,
  242. supportsImages: true,
  243. supportsPromptCache: false,
  244. inputPrice: 0,
  245. outputPrice: 0,
  246. },
  247. "gemini-1.5-flash-exp-0827": {
  248. maxTokens: 8192,
  249. contextWindow: 1_048_576,
  250. supportsImages: true,
  251. supportsPromptCache: false,
  252. inputPrice: 0,
  253. outputPrice: 0,
  254. },
  255. "gemini-1.5-flash-8b-exp-0827": {
  256. maxTokens: 8192,
  257. contextWindow: 1_048_576,
  258. supportsImages: true,
  259. supportsPromptCache: false,
  260. inputPrice: 0,
  261. outputPrice: 0,
  262. },
  263. "gemini-1.5-pro-002": {
  264. maxTokens: 8192,
  265. contextWindow: 2_097_152,
  266. supportsImages: true,
  267. supportsPromptCache: false,
  268. inputPrice: 0,
  269. outputPrice: 0,
  270. },
  271. "gemini-1.5-pro-exp-0827": {
  272. maxTokens: 8192,
  273. contextWindow: 2_097_152,
  274. supportsImages: true,
  275. supportsPromptCache: false,
  276. inputPrice: 0,
  277. outputPrice: 0,
  278. },
  279. "gemini-exp-1206": {
  280. maxTokens: 8192,
  281. contextWindow: 2_097_152,
  282. supportsImages: true,
  283. supportsPromptCache: false,
  284. inputPrice: 0,
  285. outputPrice: 0,
  286. },
  287. } as const satisfies Record<string, ModelInfo>
  288. // OpenAI Native
  289. // https://openai.com/api/pricing/
  290. export type OpenAiNativeModelId = keyof typeof openAiNativeModels
  291. export const openAiNativeDefaultModelId: OpenAiNativeModelId = "gpt-4o"
  292. export const openAiNativeModels = {
  293. // don't support tool use yet
  294. "o1-preview": {
  295. maxTokens: 32_768,
  296. contextWindow: 128_000,
  297. supportsImages: true,
  298. supportsPromptCache: false,
  299. inputPrice: 15,
  300. outputPrice: 60,
  301. },
  302. "o1-mini": {
  303. maxTokens: 65_536,
  304. contextWindow: 128_000,
  305. supportsImages: true,
  306. supportsPromptCache: false,
  307. inputPrice: 3,
  308. outputPrice: 12,
  309. },
  310. "gpt-4o": {
  311. maxTokens: 4_096,
  312. contextWindow: 128_000,
  313. supportsImages: true,
  314. supportsPromptCache: false,
  315. inputPrice: 5,
  316. outputPrice: 15,
  317. },
  318. "gpt-4o-mini": {
  319. maxTokens: 16_384,
  320. contextWindow: 128_000,
  321. supportsImages: true,
  322. supportsPromptCache: false,
  323. inputPrice: 0.15,
  324. outputPrice: 0.6,
  325. },
  326. } as const satisfies Record<string, ModelInfo>
  327. // Azure OpenAI
  328. // https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation
  329. // https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#api-specs
  330. export const azureOpenAiDefaultApiVersion = "2024-08-01-preview"