|
| 1 | +# Perplexity API Integration |
| 2 | + |
| 3 | +This document describes the integration of the Perplexity API in the Medical Reports Explainer backend. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The integration enables the backend to leverage Perplexity's AI capabilities to: |
| 8 | +1. Explain medical text in simpler terms |
| 9 | +2. Support custom chat completions for more flexible AI interactions |
| 10 | + |
| 11 | +## Components |
| 12 | + |
| 13 | +The integration consists of the following components: |
| 14 | + |
| 15 | +1. **AWS Secrets Service**: Securely retrieves the Perplexity API key from AWS Secrets Manager |
| 16 | +2. **Perplexity Service**: Interacts with the Perplexity API |
| 17 | +3. **Perplexity Controller**: Exposes endpoints for the frontend to access the Perplexity functionality |
| 18 | + |
| 19 | +## Implementation Details |
| 20 | + |
| 21 | +### Configuration |
| 22 | + |
| 23 | +The configuration for the Perplexity API is defined in `src/config/configuration.ts`: |
| 24 | + |
| 25 | +```typescript |
| 26 | +export default () => ({ |
| 27 | + // ... existing config |
| 28 | + aws: { |
| 29 | + // ... existing aws config |
| 30 | + secretsManager: { |
| 31 | + perplexityApiKeySecret: process.env.PERPLEXITY_API_KEY_SECRET_NAME || 'medical-reports-explainer/perplexity-api-key', |
| 32 | + }, |
| 33 | + }, |
| 34 | + perplexity: { |
| 35 | + apiBaseUrl: 'https://api.perplexity.ai', |
| 36 | + model: process.env.PERPLEXITY_MODEL || 'mixtral-8x7b-instruct', |
| 37 | + maxTokens: parseInt(process.env.PERPLEXITY_MAX_TOKENS || '2048', 10), |
| 38 | + }, |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +### API Key Management |
| 43 | + |
| 44 | +The API key is securely managed using AWS Secrets Manager: |
| 45 | + |
| 46 | +1. The API key is stored in AWS Secrets Manager (not in the codebase) |
| 47 | +2. The application retrieves the key at runtime using the AWS SDK |
| 48 | +3. The key is cached for 15 minutes to minimize API calls to Secrets Manager |
| 49 | + |
| 50 | +### Service Functionality |
| 51 | + |
| 52 | +The `PerplexityService` provides the following methods: |
| 53 | + |
| 54 | +1. `createChatCompletion`: Sends a chat completion request to the Perplexity API |
| 55 | +2. `explainMedicalText`: Specializes in explaining medical text in simple terms |
| 56 | + |
| 57 | +### API Endpoints |
| 58 | + |
| 59 | +The `PerplexityController` exposes the following endpoints: |
| 60 | + |
| 61 | +1. `POST /api/perplexity/explain`: Explains medical text in simpler terms |
| 62 | +2. `POST /api/perplexity/chat/completions`: Creates a custom chat completion |
| 63 | + |
| 64 | +## Setup Instructions |
| 65 | + |
| 66 | +### AWS Secrets Manager Setup |
| 67 | + |
| 68 | +1. Create a secret in AWS Secrets Manager: |
| 69 | + ``` |
| 70 | + aws secretsmanager create-secret \ |
| 71 | + --name medical-reports-explainer/perplexity-api-key \ |
| 72 | + --description "Perplexity API Key for Medical Reports Explainer" \ |
| 73 | + --secret-string "your-perplexity-api-key" |
| 74 | + ``` |
| 75 | + |
| 76 | +2. Ensure the IAM role used by the application has permissions to access the secret: |
| 77 | + ```json |
| 78 | + { |
| 79 | + "Version": "2012-10-17", |
| 80 | + "Statement": [ |
| 81 | + { |
| 82 | + "Effect": "Allow", |
| 83 | + "Action": [ |
| 84 | + "secretsmanager:GetSecretValue" |
| 85 | + ], |
| 86 | + "Resource": "arn:aws:secretsmanager:region:account-id:secret:medical-reports-explainer/perplexity-api-key-*" |
| 87 | + } |
| 88 | + ] |
| 89 | + } |
| 90 | + ``` |
| 91 | + |
| 92 | +### Environment Variables |
| 93 | + |
| 94 | +Configure the following environment variables: |
| 95 | + |
| 96 | +| Variable | Description | Default Value | |
| 97 | +|----------|-------------|---------------| |
| 98 | +| `PERPLEXITY_API_KEY_SECRET_NAME` | Name of the secret in AWS Secrets Manager | `medical-reports-explainer/perplexity-api-key` | |
| 99 | +| `PERPLEXITY_MODEL` | Perplexity model to use | `mixtral-8x7b-instruct` | |
| 100 | +| `PERPLEXITY_MAX_TOKENS` | Maximum tokens to generate | `2048` | |
| 101 | +| `AWS_REGION` | AWS region for Secrets Manager | `us-east-1` | |
| 102 | + |
| 103 | +## Local Development |
| 104 | + |
| 105 | +For local development, you can set the API key directly as an environment variable: |
| 106 | + |
| 107 | +```bash |
| 108 | +export PERPLEXITY_API_KEY="your-api-key" |
| 109 | +``` |
| 110 | + |
| 111 | +Then modify the `getApiKey` method in `PerplexityService` to check for this environment variable before trying to access AWS Secrets Manager. |
| 112 | + |
| 113 | +## Frontend Integration |
| 114 | + |
| 115 | +The frontend can interact with the Perplexity API through the following endpoints: |
| 116 | + |
| 117 | +### Explain Medical Text |
| 118 | + |
| 119 | +```typescript |
| 120 | +// Example frontend code |
| 121 | +const explainMedicalText = async (text: string) => { |
| 122 | + const response = await axios.post('/api/perplexity/explain', { |
| 123 | + medicalText: text |
| 124 | + }); |
| 125 | + return response.data.explanation; |
| 126 | +}; |
| 127 | +``` |
| 128 | + |
| 129 | +### Custom Chat Completion |
| 130 | + |
| 131 | +```typescript |
| 132 | +// Example frontend code |
| 133 | +const createChatCompletion = async (messages: any[]) => { |
| 134 | + const response = await axios.post('/api/perplexity/chat/completions', { |
| 135 | + messages: messages, |
| 136 | + temperature: 0.7 |
| 137 | + }); |
| 138 | + return response.data.explanation; |
| 139 | +}; |
| 140 | +``` |
0 commit comments