Comments (8)
The error comes from the OpenAI API. Can you test whether you also receive it when using the OpenAI fetch API directly?
from ai.
When I use the OpenAI SDK directly (as shown below) - I get a valid response from OpenAI.
Note - I need to change the message
> content
> image
props to match OpenAI's message schema - maybe there is an issue mapping AI SDK image props to OpenAI image props in the AI SDK?
Using
"openai": "4.44.0"
Sent message (using OpenAI message schema)
const messages = [
{
role: 'user',
content: [
{
type: 'text',
text: 'What is in this image',
},
{
type: 'image_url',
image_url: {
url: 'https://static01.nyt.com/images/2024/05/12/.....',
},
},
],
}
];
Route handler
import OpenAI from "openai";
export async function POST(req: Request) {
const { messages } = await req.json();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const response = await openai.chat.completions.create({
model = 'gpt-4-turbo',
messages,
});
console.log(response.choices[0]);
}
I feel like I must be missing something very obvious if no one else sees this behavior - but I also don't see an example in the AI SDK for messages with image content.
My OPENAI_API_KEY
is in my env file and is valid.
from ai.
Maybe worth also noting is that client-side I am using the useChat
hook, manually constructing the user message with image data/URL shown in original post, and then using append
to send it to my app API.
from ai.
This is related to #1555 because when I remove the data:image/jpeg;base64,
from the beginning of my base64 encoded image data string, I get a valid response.
However, when I pass a URL I still get an error.
So part of the fix may be just a matter of providing better documentation for messages with image content, but there's still something funky when sending an image URL.
from ai.
@lucasishuman can you share the img?
from ai.
You can use any valid image URL to test, such as...
const result = await streamText({
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'What’s in this image?',
},
{
type: 'image',
image:
'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg',
},
],
},
],
model: openai('gpt-4-turbo'),
});
from ai.
@lucasishuman try using a URL
image: new URL('https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg')
See examples/ai-core/src/generate-text/openai-multimodal-url.ts for a full example
from ai.
@lgrammel - Great, thanks. That was the missing info I needed.
I feel like adding the image data and image URL examples to the AI SDK docs would be helpful.
You can close this issue.
from ai.
Related Issues (20)
- Add `stop` to open-ai input options so that we can finish gracefully
- Timeout error while using `Stream Text generation` from ai-sdk (Free plan) HOT 2
- Azure OpenAI support HOT 2
- Jest: cannot find module 'ai/rsc' HOT 1
- Documentation and examples how to use Vercel AI in React HOT 2
- Groq models don't return the usage HOT 3
- Missing links in OpenAI provider README HOT 2
- Erratic behavior of useChat - Could SWR be the issue?
- `onError` hook not called when an error occurs during streaming in `useAssistant`
- the example of bedrock/anthropic-v3 show error
- LangChainAdapter does not support `StringOutParser` HOT 2
- ai/packages/core/react /use-assistant.ts: expose `setThreadId` as helper
- Azure OpenAI provider HOT 9
- Add a Vue version of useAssistant hook
- [zod dependency] Is it okay to add zod dependency to the main package? HOT 3
- Prompt gets added as first message in messages array and looses its attention HOT 4
- Stop tokens? HOT 1
- Configure safety filters for google genai / vertex
- How to explicitly pass OPEN_AI_TOKEN into `generateText` function HOT 1
- streamUI and chatgpt: Failed after 3 attempts. Last error: Failed to process error response HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ai.