-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Uncaught AnthropicError: running in a browser-like environment #6662
Comments
Hey there, @QAInsights! 👋 I'm here to help you with bugs, questions, and becoming a contributor. Let's squash those bugs together! The error you're encountering, The LangChain.js documentation does not specify any restrictions or guidelines for using the To resolve this issue, you should move the API call to a server-side environment where your API key can be securely stored and accessed. You can then make requests to your server from your React application, which will in turn call the Anthropic API. Here is a basic example of how you might set this up:
// server.js
const express = require('express');
const { ChatAnthropic } = require('@langchain/anthropic');
const app = express();
const port = 3000;
app.use(express.json());
app.post('/api/chat', async (req, res) => {
const { input } = req.body;
const model = new ChatAnthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model: 'claude-3-opus-20240229',
});
const response = await model.invoke({ input });
res.json(response);
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
// App.tsx
import React, { useState } from 'react';
const App: React.FC = () => {
const [response, setResponse] = useState(null);
const fetchChatResponse = async () => {
const res = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
input: 'What is the current weather in New York, and San Francisco? Also, what is the average of these numbers: 2273,7192,272,92737?',
}),
});
const data = await res.json();
setResponse(data);
};
return (
<>
<h1>Anthropic API</h1>
<button onClick={fetchChatResponse}>Fetch Response</button>
<div>App Component {JSON.stringify(response, null, 2)}</div>
</>
);
};
export default App; This way, your API key remains secure on the server, and your React application can still interact with the Anthropic API indirectly. |
Yes, we should set this field - if you're up for a PR we'd love that, otherwise, will have a look 👍 |
I believe that this will just require a dependency upgrade, as Anthropic SDK itself provides dangerouslyAllowBrowser Option in I have a small diff ready at https://github.com/rootCircle/langchainjs/tree/deps/%40anthropic-ai/sdk, passed all the tests locally, should I proceed? |
Yes please! |
Thanks! Did it in #6796! |
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
I am following a simple example as shown above. I have configured the API key in the
vite
config. When I run the program, the API key is displayed in the console. However, I'm getting an error when trying to make a request to the Anthropic endpoint. OpenAI's requests are functioning well.Recently, Anthropic implemented CORS feature as mentioned here. So, is this error potentially due to the missing property in the interface? Please clarify.
System Info
macOS mini Apple M2 14.6.1 (23G93)
node v22.7.0
[email protected]
The text was updated successfully, but these errors were encountered: