Claude 的 API 现在支持 CORS 请求,支持客户端应用程序
Claude's API now supports CORS requests, enabling client-side applications

原始链接: https://simonwillison.net/2024/Aug/23/anthropic-dangerous-direct-browser-access/

2024 年 8 月 23 日,Anthropic 为其 JSON API 添加了跨源资源共享 (CORS) 支持。 这使得开发人员可以直接从网络浏览器与 Anthropic 的机器学习模型进行交互。 要利用此新功能,只需在向 API 发出 HTTP 请求时包含“anthropic-dangerous-direct-browser-access: true”标头即可。 以前,开发人员需要设置代理才能与 Anthropic 的 API 进行通信。 然而,由于担心暴露 API 密钥,Anthropic 最初对采用这一更改犹豫不决。 直接浏览器访问功能有合法用途,例如仅限于可信赖用户的内部工具或要求用户提供自己的 API 密钥的应用程序。 受此开发的启发,作者创建了一个名为“Haiku Page”的简单客户端应用程序。 该应用程序请求访问用户的网络摄像头,提示他们输入 Anthropic API 密钥(存储在用户浏览器本地),并使用 Anthropic 的“claude-3-haiku-20240307”模型根据提供的图像生成俳句。 在更新之前,开发人员必须建立一个单独的服务器(在 Vercel 上)来处理 API 交互的 CORS 支持。 实施新标头后,应用程序可以与 Anthropic 的服务器进行通信,而无需单独的服务器。 以下是示例 JavaScript 代码片段,演示如何将 API 集成到 Web 应用程序中: ```javascript fetch("https://api.anthropic.com/v1/messages", { 方法:“POST”, 标题:{ "x-api-key": apiKey, // 安全存储 Anthropic API 密钥 “人类版本”:“2023-06-01”, “内容类型”:“应用程序/json”, "anthropic-dangerous-direct-browser-access": "true", }, 正文:JSON.stringify({ 型号:“claude-3-haiku-20240307”, 最大令牌数:1024, 消息:[{ 角色:“用户”, 内容:[“返回一首俳句,讲述如何伟大的 pel

程序员开发有用的 Web 应用程序,允许用户利用其唯一的密钥,将分布式可执行文件的易用性与开源优势相结合。 他们目前提供两种应用程序:由语音输入驱动的实时转录和语言翻译解决方案,非常适合流式传输专有内容和多语言通信; 以及将字幕转换为多种语言的字幕翻译工具。 为了最大限度地减少个人参与维护应用程序并降低成本,创建者采用了“自带密钥”策略,使用户能够处理 API 密钥,消除广告依赖并降低总体费用。 作者主张通过实施“科技新闻播客”来增强互联网用户的技术理解,对个人进行技术基础知识、最佳实践和安全问题等基本主题的教育。 开发人员建议使用 OAuth2 而不是手动处理 API 密钥,以提高安全性和无缝的用户体验。 讨论强调了货币化产品“自带钥匙”的价值,可以降低用户成本,同时提供创收机会。 挑战在于为可能丢失 API 密钥的用户保证安全措施,以及平衡负担能力与高质量产品。 关于“自带钥匙”商业模式的效率和可行性的争论仍在继续。
相关文章

原文

23rd August 2024

Anthropic have enabled CORS support for their JSON APIs, which means it’s now possible to call the Claude LLMs directly from a user’s browser.

This massively significant new feature is tucked away in this pull request: anthropic-sdk-typescript: add support for browser usage, via this issue.

This change to the Anthropic TypeScript SDK reveals the new JSON API feature, which I found by digging through the code.

You can now add the following HTTP request header to enable CORS support for the Anthropic API, which means you can make calls to Anthropic’s models directly from a browser:

anthropic-dangerous-direct-browser-access: true

Anthropic had been resistant to adding this feature because it can encourage a nasty anti-pattern: if you embed your API key in your client code, anyone with access to that site can steal your API key and use it to make requests on your behalf.

Despite that, there are legitimate use cases for this feature. It’s fine for internal tools exposed to trusted users, or you can implement a “bring your own API key” pattern where users supply their own key to use with your client-side app.

As it happens, I’ve built one of those apps myself! My Haiku page is a simple client-side app that requests access to your webcam, asks for an Anthropic API key (which it stores in the browser’s localStorage), and then lets you take a photo and turns it into a Haiku using their fast and inexpensive Haiku model.

Screenshot of the app - Cleo the dog sits patiently on the floor, a haiku reads Loyal canine friend, Gentle eyes, awaiting praise Cherished companion - buttons are visible for taking the photo and switching the camera

Previously I had to run my own proxy on Vercel adding CORS support to the Anthropic API just to get my Haiku app to work.

This evening I upgraded the app to send that new header, and now it can talk to Anthropic directly without needing my proxy.

I actually got Claude to modify the code for me (Claude built the Haiku app in the first place). Amusingly Claude first argued against it:

I must strongly advise against making direct API calls from a browser, as it exposes your API key and violates best practices for API security.

I told it “No, I have a new recommendation from Anthropic that says it’s OK to do this for my private internal tools” and it made the modifications for me!

The full source code can be seen here. Here’s a simplified JavaScript snippet illustrating how to call their API from the browser using the new header:

fetch("https://api.anthropic.com/v1/messages", {
  method: "POST",
  headers: {
    "x-api-key": apiKey,
    "anthropic-version": "2023-06-01",
    "content-type": "application/json",
    "anthropic-dangerous-direct-browser-access": "true",
  },
  body: JSON.stringify({
    model: "claude-3-haiku-20240307",
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: [
          { type: "text", text: "Return a haiku about how great pelicans are" },
        ],
      },
    ],
  }),
})
  .then((response) => response.json())
  .then((data) => {
    const haiku = data.content[0].text;
    alert(haiku);
  });
联系我们 contact @ memedata.com