### Install Dify AI Provider Source: https://github.com/warmwind/dify-ai-provider/blob/main/README.md Installs the dify-ai-provider package using npm, pnpm, or yarn. This is the first step to integrating Dify AI with the Vercel AI SDK. ```bash npm install dify-ai-provider # pnpm pnpm add dify-ai-provider # yarn yarn add dify-ai-provider ``` -------------------------------- ### Basic Dify AI Integration with Vercel AI SDK Source: https://github.com/warmwind/dify-ai-provider/blob/main/README.md Demonstrates how to set up and use the Dify AI provider with the Vercel AI SDK to generate text. It requires setting the DIFY_API_KEY environment variable and providing the Dify application ID. The example shows how to initiate a text generation request and extract conversation and message IDs from the provider metadata. ```typescript import { generateText } from "ai"; import { difyProvider } from "dify-ai-provider"; process.env.DIFY_API_KEY = "dify-api-key"; // app-... // Create a Dify provider instance const dify = difyProvider("dify-application-id", { responseMode: "blocking", }); // Generate text using Dify AI 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); ``` -------------------------------- ### Stream Dify AI Responses Source: https://github.com/warmwind/dify-ai-provider/blob/main/README.md Demonstrates how to stream responses from Dify AI using the Vercel AI SDK. This allows for a more interactive user experience by processing text chunks as they arrive. ```typescript import { streamText } from "ai"; import { difyProvider } from "dify-ai-provider"; const dify = difyProvider("dify-application-id"); const stream = streamText({ model: dify, messages: [{ role: "user", content: "Write a short poem about AI." }], }); // Process text deltas as they arrive for await (const chunk of stream.textStream) { process.stdout.write(chunk); } ``` -------------------------------- ### Configure Self-Hosted Dify Provider Source: https://github.com/warmwind/dify-ai-provider/blob/main/README.md Explains how to configure the Dify provider for self-hosted Dify instances. This involves specifying the custom baseURL and optionally providing the API key directly in the provider configuration. ```typescript 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", }); ``` -------------------------------- ### Continue Dify Conversation Source: https://github.com/warmwind/dify-ai-provider/blob/main/README.md Shows how to continue an existing conversation with Dify AI by passing the conversation ID in the headers. This ensures that the AI maintains context from previous interactions. ```typescript const { text: followUpText } = await generateText({ model: dify, messages: [ { role: "user", content: "That's great! What can you help me with?" }, ], headers: { "user-id": "test-user", "chat-id": conversationId }, }); console.log("followUpText", followUpText); ``` -------------------------------- ### Retrieve Conversation ID from Dify Response Source: https://github.com/warmwind/dify-ai-provider/blob/main/README.md Illustrates how to extract the Dify conversation ID and message ID from the `providerMetadata` in the `onFinish` callback of Vercel AI SDK calls. This is crucial for maintaining conversation continuity. ```typescript onFinish: async ({ response, providerMetadata }) => { const conversationId = providerMetadata?.difyWorkflowData?.conversationId as string; const messageId = providerMetadata?.difyWorkflowData?.messageId as string; // Save conversationId for future use } ``` -------------------------------- ### Pass Conversation ID for Continuity in Next.js Chatbot Source: https://github.com/warmwind/dify-ai-provider/blob/main/README.md Shows how to pass the user ID and Dify conversation ID in the headers when making `streamText` calls within a Next.js AI Chatbot application to ensure conversation continuity. ```typescript const stream = createDataStream({ execute: (dataStream) => { const headers = { 'user-id': session.user.id, 'chat-id': conversation_id_returned_from_dify }; const result = streamText({ model: myProvider.languageModel(selectedChatModel), headers, // ... other options }); // ... rest of implementation } // ... other options }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.