|
| 1 | +--- |
| 2 | +title: Dify |
| 3 | +description: Learn how to use the Dify provider for the AI SDK. |
| 4 | +--- |
| 5 | + |
| 6 | +# Dify Provider |
| 7 | + |
| 8 | +The **[Dify provider](https://github.com/warmwind/dify-ai-provider)** allows you to easily integrate Dify's application workflow with your applications using the AI SDK. |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +The Dify provider is available in the `dify-ai-provider` module. You can install it with: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install dify-ai-provider |
| 16 | + |
| 17 | +# pnpm |
| 18 | +pnpm add dify-ai-provider |
| 19 | + |
| 20 | +# yarn |
| 21 | +yarn add dify-ai-provider |
| 22 | +``` |
| 23 | + |
| 24 | +## Provider Instance |
| 25 | + |
| 26 | +You can import `difyProvider` from `dify-ai-provider` to create a provider instance: |
| 27 | + |
| 28 | +```ts |
| 29 | +import { difyProvider } from 'dify-ai-provider'; |
| 30 | +``` |
| 31 | + |
| 32 | +## Example |
| 33 | + |
| 34 | +### Use dify.ai |
| 35 | + |
| 36 | +```ts |
| 37 | +import { generateText } from 'ai'; |
| 38 | +import { difyProvider } from 'dify-ai-provider'; |
| 39 | + |
| 40 | +const dify = difyProvider('dify-application-id', { |
| 41 | + responseMode: 'blocking', |
| 42 | + apiKey: 'dify-api-key', |
| 43 | +}); |
| 44 | + |
| 45 | +const { text, providerMetadata } = await generateText({ |
| 46 | + model: dify, |
| 47 | + messages: [{ role: 'user', content: 'Hello, how are you today?' }], |
| 48 | + headers: { 'user-id': 'test-user' }, |
| 49 | +}); |
| 50 | + |
| 51 | +const { conversationId, messageId } = providerMetadata.difyWorkflowData; |
| 52 | +console.log(text); |
| 53 | +console.log('conversationId', conversationId); |
| 54 | +console.log('messageId', messageId); |
| 55 | +``` |
| 56 | + |
| 57 | +### Use self-hosted Dify |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { createDifyProvider } from 'dify-ai-provider'; |
| 61 | + |
| 62 | +const difyProvider = createDifyProvider({ |
| 63 | + baseURL: 'your-base-url', |
| 64 | +}); |
| 65 | + |
| 66 | +const dify = difyProvider('dify-application-id', { |
| 67 | + responseMode: 'blocking', |
| 68 | + apiKey: 'dify-api-key', |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +## Documentation |
| 73 | + |
| 74 | +Please refer to the **[Dify provider documentation](https://github.com/warmwind/dify-ai-provider)** for more detailed information. |
0 commit comments