api.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. openRouterUseMiddleOutTransform?: boolean
  19. awsAccessKey?: string
  20. awsSecretKey?: string
  21. awsSessionToken?: string
  22. awsRegion?: string
  23. awsUseCrossRegionInference?: boolean
  24. awsUsePromptCache?: boolean
  25. awspromptCacheId?: string
  26. vertexProjectId?: string
  27. vertexRegion?: string
  28. openAiBaseUrl?: string
  29. openAiApiKey?: string
  30. openAiModelId?: string
  31. ollamaModelId?: string
  32. ollamaBaseUrl?: string
  33. lmStudioModelId?: string
  34. lmStudioBaseUrl?: string
  35. geminiApiKey?: string
  36. openAiNativeApiKey?: string
  37. azureApiVersion?: string
  38. useBedrockRuntime?: boolean // Force use of Bedrock Runtime API instead of SDK
  39. }
  40. export type ApiConfiguration = ApiHandlerOptions & {
  41. apiProvider?: ApiProvider
  42. }
  43. // Models
  44. export interface ModelInfo {
  45. maxTokens?: number
  46. contextWindow?: number
  47. supportsImages?: boolean
  48. supportsComputerUse?: boolean
  49. supportsPromptCache: boolean // this value is hardcoded for now
  50. inputPrice?: number
  51. outputPrice?: number
  52. cacheWritesPrice?: number
  53. cacheReadsPrice?: number
  54. description?: string
  55. }
  56. // Anthropic
  57. // https://docs.anthropic.com/en/docs/about-claude/models
  58. export type AnthropicModelId = keyof typeof anthropicModels
  59. export const anthropicDefaultModelId: AnthropicModelId = "claude-3-5-sonnet-20241022"
  60. export const anthropicModels = {
  61. "claude-3-5-sonnet-20241022": {
  62. maxTokens: 8192,
  63. contextWindow: 200_000,
  64. supportsImages: true,
  65. supportsComputerUse: true,
  66. supportsPromptCache: true,
  67. inputPrice: 3.0, // $3 per million input tokens
  68. outputPrice: 15.0, // $15 per million output tokens
  69. cacheWritesPrice: 3.75, // $3.75 per million tokens
  70. cacheReadsPrice: 0.3, // $0.30 per million tokens
  71. },
  72. "claude-3-5-haiku-20241022": {
  73. maxTokens: 8192,
  74. contextWindow: 200_000,
  75. supportsImages: false,
  76. supportsPromptCache: true,
  77. inputPrice: 1.0,
  78. outputPrice: 5.0,
  79. cacheWritesPrice: 1.25,
  80. cacheReadsPrice: 0.1,
  81. },
  82. "claude-3-opus-20240229": {
  83. maxTokens: 4096,
  84. contextWindow: 200_000,
  85. supportsImages: true,
  86. supportsPromptCache: true,
  87. inputPrice: 15.0,
  88. outputPrice: 75.0,
  89. cacheWritesPrice: 18.75,
  90. cacheReadsPrice: 1.5,
  91. },
  92. "claude-3-haiku-20240307": {
  93. maxTokens: 4096,
  94. contextWindow: 200_000,
  95. supportsImages: true,
  96. supportsPromptCache: true,
  97. inputPrice: 0.25,
  98. outputPrice: 1.25,
  99. cacheWritesPrice: 0.3,
  100. cacheReadsPrice: 0.03,
  101. },
  102. } as const satisfies Record<string, ModelInfo> // as const assertion makes the object deeply readonly
  103. // AWS Bedrock
  104. // https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html
  105. export interface MessageContent {
  106. type: 'text' | 'image' | 'video' | 'tool_use' | 'tool_result';
  107. text?: string;
  108. source?: {
  109. type: 'base64';
  110. data: string | Uint8Array; // string for Anthropic, Uint8Array for Bedrock
  111. media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
  112. };
  113. // Video specific fields
  114. format?: string;
  115. s3Location?: {
  116. uri: string;
  117. bucketOwner?: string;
  118. };
  119. // Tool use and result fields
  120. toolUseId?: string;
  121. name?: string;
  122. input?: any;
  123. output?: any; // Used for tool_result type
  124. }
  125. export type BedrockModelId = keyof typeof bedrockModels
  126. export const bedrockDefaultModelId: BedrockModelId = "anthropic.claude-3-5-sonnet-20241022-v2:0"
  127. export const bedrockModels = {
  128. "amazon.nova-pro-v1:0": {
  129. maxTokens: 5000,
  130. contextWindow: 300_000,
  131. supportsImages: true,
  132. supportsComputerUse: false,
  133. supportsPromptCache: false,
  134. inputPrice: 0.8,
  135. outputPrice: 3.2,
  136. cacheWritesPrice: 0.8, // per million tokens
  137. cacheReadsPrice: 0.2, // per million tokens
  138. },
  139. "amazon.nova-lite-v1:0": {
  140. maxTokens: 5000,
  141. contextWindow: 300_000,
  142. supportsImages: true,
  143. supportsComputerUse: false,
  144. supportsPromptCache: false,
  145. inputPrice: 0.06,
  146. outputPrice: 0.024,
  147. cacheWritesPrice: 0.06, // per million tokens
  148. cacheReadsPrice: 0.015, // per million tokens
  149. },
  150. "amazon.nova-micro-v1:0": {
  151. maxTokens: 5000,
  152. contextWindow: 128_000,
  153. supportsImages: false,
  154. supportsComputerUse: false,
  155. supportsPromptCache: false,
  156. inputPrice: 0.035,
  157. outputPrice: 0.14,
  158. cacheWritesPrice: 0.035, // per million tokens
  159. cacheReadsPrice: 0.00875, // per million tokens
  160. },
  161. "anthropic.claude-3-5-sonnet-20241022-v2:0": {
  162. maxTokens: 8192,
  163. contextWindow: 200_000,
  164. supportsImages: true,
  165. supportsComputerUse: true,
  166. supportsPromptCache: false,
  167. inputPrice: 3.0,
  168. outputPrice: 15.0,
  169. cacheWritesPrice: 3.75, // per million tokens
  170. cacheReadsPrice: 0.3, // per million tokens
  171. },
  172. "anthropic.claude-3-5-haiku-20241022-v1:0": {
  173. maxTokens: 8192,
  174. contextWindow: 200_000,
  175. supportsImages: false,
  176. supportsPromptCache: false,
  177. inputPrice: 1.0,
  178. outputPrice: 5.0,
  179. cacheWritesPrice: 1.0,
  180. cacheReadsPrice: 0.08,
  181. },
  182. "anthropic.claude-3-5-sonnet-20240620-v1:0": {
  183. maxTokens: 8192,
  184. contextWindow: 200_000,
  185. supportsImages: true,
  186. supportsPromptCache: false,
  187. inputPrice: 3.0,
  188. outputPrice: 15.0,
  189. },
  190. "anthropic.claude-3-opus-20240229-v1:0": {
  191. maxTokens: 4096,
  192. contextWindow: 200_000,
  193. supportsImages: true,
  194. supportsPromptCache: false,
  195. inputPrice: 15.0,
  196. outputPrice: 75.0,
  197. },
  198. "anthropic.claude-3-sonnet-20240229-v1:0": {
  199. maxTokens: 4096,
  200. contextWindow: 200_000,
  201. supportsImages: true,
  202. supportsPromptCache: false,
  203. inputPrice: 3.0,
  204. outputPrice: 15.0,
  205. },
  206. "anthropic.claude-3-haiku-20240307-v1:0": {
  207. maxTokens: 4096,
  208. contextWindow: 200_000,
  209. supportsImages: true,
  210. supportsPromptCache: false,
  211. inputPrice: 0.25,
  212. outputPrice: 1.25,
  213. },
  214. "meta.llama3-2-90b-instruct-v1:0" : {
  215. maxTokens: 8192,
  216. contextWindow: 128_000,
  217. supportsImages: true,
  218. supportsComputerUse: false,
  219. supportsPromptCache: false,
  220. inputPrice: 0.72,
  221. outputPrice: 0.72,
  222. },
  223. "meta.llama3-2-11b-instruct-v1:0" : {
  224. maxTokens: 8192,
  225. contextWindow: 128_000,
  226. supportsImages: true,
  227. supportsComputerUse: false,
  228. supportsPromptCache: false,
  229. inputPrice: 0.16,
  230. outputPrice: 0.16,
  231. },
  232. "meta.llama3-2-3b-instruct-v1:0" : {
  233. maxTokens: 8192,
  234. contextWindow: 128_000,
  235. supportsImages: false,
  236. supportsComputerUse: false,
  237. supportsPromptCache: false,
  238. inputPrice: 0.15,
  239. outputPrice: 0.15,
  240. },
  241. "meta.llama3-2-1b-instruct-v1:0" : {
  242. maxTokens: 8192,
  243. contextWindow: 128_000,
  244. supportsImages: false,
  245. supportsComputerUse: false,
  246. supportsPromptCache: false,
  247. inputPrice: 0.1,
  248. outputPrice: 0.1,
  249. },
  250. "meta.llama3-1-405b-instruct-v1:0" : {
  251. maxTokens: 8192,
  252. contextWindow: 128_000,
  253. supportsImages: false,
  254. supportsComputerUse: false,
  255. supportsPromptCache: false,
  256. inputPrice: 2.4,
  257. outputPrice: 2.4,
  258. },
  259. "meta.llama3-1-70b-instruct-v1:0" : {
  260. maxTokens: 8192,
  261. contextWindow: 128_000,
  262. supportsImages: false,
  263. supportsComputerUse: false,
  264. supportsPromptCache: false,
  265. inputPrice: 0.72,
  266. outputPrice: 0.72,
  267. },
  268. "meta.llama3-1-8b-instruct-v1:0" : {
  269. maxTokens: 8192,
  270. contextWindow: 8_000,
  271. supportsImages: false,
  272. supportsComputerUse: false,
  273. supportsPromptCache: false,
  274. inputPrice: 0.22,
  275. outputPrice: 0.22,
  276. },
  277. "meta.llama3-70b-instruct-v1:0" : {
  278. maxTokens: 2048 ,
  279. contextWindow: 8_000,
  280. supportsImages: false,
  281. supportsComputerUse: false,
  282. supportsPromptCache: false,
  283. inputPrice: 2.65,
  284. outputPrice: 3.5,
  285. },
  286. "meta.llama3-8b-instruct-v1:0" : {
  287. maxTokens: 2048 ,
  288. contextWindow: 4_000,
  289. supportsImages: false,
  290. supportsComputerUse: false,
  291. supportsPromptCache: false,
  292. inputPrice: 0.3,
  293. outputPrice: 0.6,
  294. },
  295. } as const satisfies Record<string, ModelInfo>
  296. // OpenRouter
  297. // https://openrouter.ai/models?order=newest&supported_parameters=tools
  298. export const openRouterDefaultModelId = "anthropic/claude-3.5-sonnet:beta" // will always exist in openRouterModels
  299. export const openRouterDefaultModelInfo: ModelInfo = {
  300. maxTokens: 8192,
  301. contextWindow: 200_000,
  302. supportsImages: true,
  303. supportsComputerUse: true,
  304. supportsPromptCache: true,
  305. inputPrice: 3.0,
  306. outputPrice: 15.0,
  307. cacheWritesPrice: 3.75,
  308. cacheReadsPrice: 0.3,
  309. description:
  310. "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._",
  311. }
  312. // Vertex AI
  313. // https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude
  314. export type VertexModelId = keyof typeof vertexModels
  315. export const vertexDefaultModelId: VertexModelId = "claude-3-5-sonnet-v2@20241022"
  316. export const vertexModels = {
  317. "claude-3-5-sonnet-v2@20241022": {
  318. maxTokens: 8192,
  319. contextWindow: 200_000,
  320. supportsImages: true,
  321. supportsComputerUse: true,
  322. supportsPromptCache: false,
  323. inputPrice: 3.0,
  324. outputPrice: 15.0,
  325. },
  326. "claude-3-5-sonnet@20240620": {
  327. maxTokens: 8192,
  328. contextWindow: 200_000,
  329. supportsImages: true,
  330. supportsPromptCache: false,
  331. inputPrice: 3.0,
  332. outputPrice: 15.0,
  333. },
  334. "claude-3-5-haiku@20241022": {
  335. maxTokens: 8192,
  336. contextWindow: 200_000,
  337. supportsImages: false,
  338. supportsPromptCache: false,
  339. inputPrice: 1.0,
  340. outputPrice: 5.0,
  341. },
  342. "claude-3-opus@20240229": {
  343. maxTokens: 4096,
  344. contextWindow: 200_000,
  345. supportsImages: true,
  346. supportsPromptCache: false,
  347. inputPrice: 15.0,
  348. outputPrice: 75.0,
  349. },
  350. "claude-3-haiku@20240307": {
  351. maxTokens: 4096,
  352. contextWindow: 200_000,
  353. supportsImages: true,
  354. supportsPromptCache: false,
  355. inputPrice: 0.25,
  356. outputPrice: 1.25,
  357. },
  358. } as const satisfies Record<string, ModelInfo>
  359. export const openAiModelInfoSaneDefaults: ModelInfo = {
  360. maxTokens: -1,
  361. contextWindow: 128_000,
  362. supportsImages: true,
  363. supportsPromptCache: false,
  364. inputPrice: 0,
  365. outputPrice: 0,
  366. }
  367. // Gemini
  368. // https://ai.google.dev/gemini-api/docs/models/gemini
  369. export type GeminiModelId = keyof typeof geminiModels
  370. export const geminiDefaultModelId: GeminiModelId = "gemini-1.5-flash-002"
  371. export const geminiModels = {
  372. "gemini-1.5-flash-002": {
  373. maxTokens: 8192,
  374. contextWindow: 1_048_576,
  375. supportsImages: true,
  376. supportsPromptCache: false,
  377. inputPrice: 0,
  378. outputPrice: 0,
  379. },
  380. "gemini-1.5-flash-exp-0827": {
  381. maxTokens: 8192,
  382. contextWindow: 1_048_576,
  383. supportsImages: true,
  384. supportsPromptCache: false,
  385. inputPrice: 0,
  386. outputPrice: 0,
  387. },
  388. "gemini-1.5-flash-8b-exp-0827": {
  389. maxTokens: 8192,
  390. contextWindow: 1_048_576,
  391. supportsImages: true,
  392. supportsPromptCache: false,
  393. inputPrice: 0,
  394. outputPrice: 0,
  395. },
  396. "gemini-1.5-pro-002": {
  397. maxTokens: 8192,
  398. contextWindow: 2_097_152,
  399. supportsImages: true,
  400. supportsPromptCache: false,
  401. inputPrice: 0,
  402. outputPrice: 0,
  403. },
  404. "gemini-1.5-pro-exp-0827": {
  405. maxTokens: 8192,
  406. contextWindow: 2_097_152,
  407. supportsImages: true,
  408. supportsPromptCache: false,
  409. inputPrice: 0,
  410. outputPrice: 0,
  411. },
  412. "gemini-exp-1206": {
  413. maxTokens: 8192,
  414. contextWindow: 2_097_152,
  415. supportsImages: true,
  416. supportsPromptCache: false,
  417. inputPrice: 0,
  418. outputPrice: 0,
  419. },
  420. "gemini-2.0-flash-exp": {
  421. maxTokens: 8192,
  422. contextWindow: 1_048_576,
  423. supportsImages: true,
  424. supportsPromptCache: false,
  425. inputPrice: 0,
  426. outputPrice: 0,
  427. },
  428. } as const satisfies Record<string, ModelInfo>
  429. // OpenAI Native
  430. // https://openai.com/api/pricing/
  431. export type OpenAiNativeModelId = keyof typeof openAiNativeModels
  432. export const openAiNativeDefaultModelId: OpenAiNativeModelId = "gpt-4o"
  433. export const openAiNativeModels = {
  434. // don't support tool use yet
  435. "o1-preview": {
  436. maxTokens: 32_768,
  437. contextWindow: 128_000,
  438. supportsImages: true,
  439. supportsPromptCache: false,
  440. inputPrice: 15,
  441. outputPrice: 60,
  442. },
  443. "o1-mini": {
  444. maxTokens: 65_536,
  445. contextWindow: 128_000,
  446. supportsImages: true,
  447. supportsPromptCache: false,
  448. inputPrice: 3,
  449. outputPrice: 12,
  450. },
  451. "gpt-4o": {
  452. maxTokens: 4_096,
  453. contextWindow: 128_000,
  454. supportsImages: true,
  455. supportsPromptCache: false,
  456. inputPrice: 5,
  457. outputPrice: 15,
  458. },
  459. "gpt-4o-mini": {
  460. maxTokens: 16_384,
  461. contextWindow: 128_000,
  462. supportsImages: true,
  463. supportsPromptCache: false,
  464. inputPrice: 0.15,
  465. outputPrice: 0.6,
  466. },
  467. } as const satisfies Record<string, ModelInfo>
  468. // Azure OpenAI
  469. // https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation
  470. // https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#api-specs
  471. export const azureOpenAiDefaultApiVersion = "2024-08-01-preview"