Learn how to use the Dify provider for the AI SDK.
The Dify provider allows you to easily integrate Dify's application workflow with your applications using the AI SDK.
The Dify provider is available in the dify-ai-provider module. You can install it with:
npm install dify-ai-provider
# pnpm
pnpm add dify-ai-provider
# yarn
yarn add dify-ai-provider
You can import difyProvider from dify-ai-provider to create a provider instance:
import { difyProvider } from 'dify-ai-provider';
import { generateText } from 'ai';
import { difyProvider } from 'dify-ai-provider';
const dify = difyProvider('dify-application-id', {
responseMode: 'blocking',
apiKey: 'dify-api-key',
});
const { text, providerMetadata } = await generateText({
model: dify,
messages: [{ role: 'user', content: 'Hello, how are you today?' }],
headers: { 'user-id': 'test-user' },
});
const { conversationId, messageId } = providerMetadata.difyWorkflowData;
console.log(text);
console.log('conversationId', conversationId);
console.log('messageId', messageId);
import { createDifyProvider } from 'dify-ai-provider';
const difyProvider = createDifyProvider({
baseURL: 'your-base-url',
});
const dify = difyProvider('dify-application-id', {
responseMode: 'blocking',
apiKey: 'dify-api-key',
});
Please refer to the Dify provider documentation for more detailed information.