To feed PDF documents to a Large Language Model (LLM) and prompt questions about their content using Power Automate Web, you can create an automated workflow that:
- Extracts Text from the PDF: Reads and extracts text from the PDF document.
- Sends the Text to the LLM: Uses the extracted text as context for the LLM to answer questions.
- Handles User Queries: Accepts questions from the user and provides answers based on the PDF content.
Here's a step-by-step guide to setting up this workflow in Power Automate Web:
Prerequisites
- Access to Power Automate Web: Ensure you have the necessary permissions to create flows.
- OpenAI API Key: Sign up for an OpenAI account and obtain an API key from the OpenAI Dashboard.
- PDF Documents Accessible Online: Your PDF documents should be stored in an online location accessible by Power Automate, such as OneDrive, SharePoint, or Azure Blob Storage.
Step 1: Create a New Automated Flow
- Log into Power Automate Web: Go to Power Automate and sign in.
- Create a New Flow: Click on Create and select Automated cloud flow.
- Set a Trigger: Choose a trigger that suits your scenario, such as:
- When a file is created or modified (for automatic processing when new PDFs are added).
- When an HTTP request is received (to trigger the flow manually or via another application).
- Name Your Flow: Give your flow a descriptive name like "Process PDF with OpenAI".
Step 2: Extract Text from the PDF
- Add an Action to Extract Text: After the trigger, add a new action:
- Search for "PDF" Actions: Use the search bar to find PDF-related actions.
- Choose "Extract text from PDF": This action is available under the OneDrive for Business or SharePoint connectors, depending on where your PDFs are stored.
- Configure the Action:
- File: Select the PDF file from your trigger or specify the file path.
- Output Variable: The extracted text will be stored in an output variable for later use.
Note: If the built-in action is not available, you can use third-party connectors like Encodian or Muhimbi which offer PDF text extraction capabilities.
Step 3: Prepare the User's Question
- Capture the User's Question: If your flow is triggered by a user action (like a form submission or a message), extract the user's question from the trigger inputs.
- For HTTP Trigger: Parse the incoming JSON to get the question.
- For Forms or Teams Messages: Use the dynamic content from the trigger that contains the user's question.
- Store the Question in a Variable: Use the "Initialize variable" action to store the question if necessary.
Step 4: Compose the Prompt for the LLM
- Create the Prompt: Use a "Compose" action to create the prompt that will be sent to the LLM. The prompt should include both the extracted text and the user's question.
Step 5: Call the OpenAI GPT Model
-
Add an HTTP Action: Since there's no built-in OpenAI connector in Power Automate, use the "HTTP" action to make an API call.
-
Configure the HTTP Action:
- Method: POST
- URL:
https://api.openai.com/v1/chat/completions (for GPT-3.5 and GPT-4 models)
- Headers:
Content-Type: application/json
Authorization: Bearer YOUR_OPENAI_API_KEY (replace YOUR_OPENAI_API_KEY with your actual API key or store it securely using Azure Key Vault or Power Automate's Secure Inputs)
- Body:
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "@{outputs('Compose')}"
}
],
"max_tokens": 500,
"temperature": 0.7
}
- Note: Use the dynamic content from the "Compose" action in the
content field.
-
Handle Large Documents:
- Context Window Limitation: GPT-3.5-turbo has a token limit (approximately 4096 tokens including prompt and response). If your PDF text is too long:
- Summarize the Text: Before sending, you might need to summarize or extract key points from the text.
- Chunk the Text: Split the text into smaller chunks and process them separately.
- Alternative Models: Use GPT-4 with an 8K or 32K token limit if available.
Step 6: Process the LLM's Response
- Parse the Response: Add a "Parse JSON" action to extract the assistant's reply.
- Content: Use
body from the HTTP action's outputs.
- Schema: Generate from a sample response or use the following schema:
{
"type": "object",
"properties": {
"choices": {
"type": "array",
"items": {
"type": "object",
"properties": {
"message": {
"type": "object",
"properties": {
"content": {
"type": "string"
}
}
}
}
}
}
}
}
- Extract the Answer: Use an "Set variable" or "Compose" action to get the assistant's reply from the parsed JSON:
- Expression:
outputs('Parse_JSON')?['choices'][0]?['message']?['content']
Step 7: Return the Answer to the User
- Choose an Output Method: Decide how you want to deliver the answer to the user:
- Email: Use the "Send an email" action.
- Teams Message: Use the "Post a message in a chat or channel" action.
- HTTP Response: If using an HTTP trigger, use the "Response" action to return the answer.
- Configure the Output Action:
- Recipient: Specify the user or dynamically set it based on the trigger.
- Content: Include the assistant's answer from the previous step.
Additional Considerations
- Error Handling: Implement checks to handle errors from the API or issues with text extraction.
- Security:
- Protect API Keys: Never hard-code API keys. Use secure variables or Azure Key Vault.
- Data Privacy: Ensure compliance with data protection policies, especially when sending potentially sensitive documents to external services.
- Usage Limits: Be aware of OpenAI's rate limits and costs associated with API usage.
- Testing: Thoroughly test the flow with different PDFs and questions to ensure reliability.
Example Flow Outline
- Trigger: When a file is created in a OneDrive folder.
- Action: Extract text from the PDF.
- Action: Initialize variable for the user's question (if needed).
- Action: Compose the prompt with the extracted text and user's question.
- Action: HTTP POST to OpenAI API with the composed prompt.
- Action: Parse JSON response to extract the assistant's reply.
- Action: Send an email to the user with the answer.
By following these steps, you can successfully feed PDF documents to an LLM using Power Automate Web and interact with the content through prompted questions. This automated approach streamlines the process of querying document content and provides quick, AI-generated responses to user inquiries.