Dify

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.

Setup

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

Provider Instance

You can import difyProvider from dify-ai-provider to create a provider instance:

import { difyProvider } from 'dify-ai-provider';

Example

Use dify.ai

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);

Use self-hosted Dify

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',
});

Documentation

Please refer to the Dify provider documentation for more detailed information.