Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dangerouslyAllowBrowser for Anthropic #3041

Open
maybebansky opened this issue Sep 17, 2024 · 5 comments
Open

Support dangerouslyAllowBrowser for Anthropic #3041

maybebansky opened this issue Sep 17, 2024 · 5 comments
Labels
ai/provider enhancement New feature or request

Comments

@maybebansky
Copy link

Feature Description

I need to set dangerouslyAllowBrowser to true which you're able to do with the Anthropic SKD. However I don't think you can do this with the ai library?

anthropics/anthropic-sdk-typescript#248 (comment)

import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: null,
  dangerouslyAllowBrowser: true
});

There are legitimate reasons for needing to do this: https://simonwillison.net/2024/Aug/23/anthropic-dangerous-direct-browser-access/

Use Case

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.

Additional context

No response

@lgrammel
Copy link
Collaborator

This is not the Anthropic SDK. You can use the Anthropic AI SDK provider in the browser.

@maybebansky
Copy link
Author

Sorry I'm not sure I understand your reply. Yes I know this is the ai repo and not Anthropics. Im trying to use ai with Anthropic:

import { createAnthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";

const anthropic = createAnthropic({
    apiKey
});

const { text } = await generateText({
    model: anthropic(model),
    prompt: 'What is the meaning of life?',
});

@lgrammel
Copy link
Collaborator

@maybebansky your question was referring to unrelated comments from the anthropic ai sdk.

@maybebansky
Copy link
Author

@lgrammel sorry if I'm dragging this out but I'm still confused. Hopefully this is a clearer code example:

async function test({ provider }: { provider: "openai" | "anthropic" }) {
  const openai = createOpenAI({
    apiKey: apiKeyOpenAi
  });

  const anthropic = createAnthropic({
    apiKey: aiKeyAnthropic
  });

  const result = await generateText({
    model:
      provider === "openai"
        ? openai("gpt-4o")
        : anthropic("claude-3-5-sonnet-20240620"),
    prompt: "What is the meaning of life?"
  });

  console.log({ provider, result });
}

The above code works with OpenAI but not for Anthropic. The error I get with Anthropic is:

Access to fetch at 'https://api.anthropic.com/v1/messages' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I do need to make the request from the browser. My understanding is that if I were using the Anthropic SDK I could fix this by setting dangerouslyAllowBrowser: true. Id like to use the ai package instead, but is there a way to set dangerouslyAllowBrowser: true for Anthropic with ai?

@lgrammel lgrammel reopened this Sep 18, 2024
@lgrammel
Copy link
Collaborator

Thanks that clarifies it.

@lgrammel lgrammel added enhancement New feature or request ai/provider labels Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/provider enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants