Skip to content

Instantly share code, notes, and snippets.

@opejovic
Created November 21, 2025 15:11
Show Gist options
  • Select an option

  • Save opejovic/3e9f563fe58596b0a6c64dc0e0ce024d to your computer and use it in GitHub Desktop.

Select an option

Save opejovic/3e9f563fe58596b0a6c64dc0e0ce024d to your computer and use it in GitHub Desktop.
What You Need:

AWS Account with Bedrock enabled
Laravel app
About 2 hours


Step 1: Enable Bedrock in AWS Console (5 mins)
1. Go to AWS Console → Search "Bedrock"
2. Click "Get Started"
3. Go to "Model Access" → Request access to:
   - Claude 3.5 Sonnet (best model)
   - Titan Embeddings (for document search)
4. Usually approved instantly

Step 2: Create S3 Bucket for Documents (5 mins)
bash# In AWS Console → S3
Create bucket: "your-medical-docs"
Enable versioning
That's it!

Step 3: Create Knowledge Base (10 mins)

In Bedrock Console:
1. Go to "Knowledge Bases" → Create
2. Name: "Medical Knowledge"
3. Choose: Create new vector store (it does everything for you)
4. Select S3 bucket: "your-medical-docs"
5. Click Create
   
AWS automatically creates:
- OpenSearch database
- Vector storage
- Document processing pipeline

Step 4: Laravel Setup
Install AWS SDK:
bashcomposer require aws/aws-sdk-php
Add to .env:
envAWS_BEDROCK_REGION=us-east-1
AWS_BEDROCK_KB_ID=your-knowledge-base-id-here
AWS_MEDICAL_DOCS_BUCKET=your-medical-docs
Create Simple Service:
php// app/Services/BedrockService.php

use Aws\BedrockRuntime\BedrockRuntimeClient;
use Aws\S3\S3Client;

class BedrockService 
{
    private $bedrock;
    private $s3;
    private $kbId;
    
    public function __construct()
    {
        $this->bedrock = new BedrockRuntimeClient([
            'region' => env('AWS_BEDROCK_REGION'),
            'version' => 'latest'
        ]);
        
        $this->s3 = new S3Client([
            'region' => env('AWS_BEDROCK_REGION'),
            'version' => 'latest'
        ]);
        
        $this->kbId = env('AWS_BEDROCK_KB_ID');
    }
    
    // UPLOAD DOCUMENT
    public function uploadDocument($file, $category)
    {
        // 1. Upload to S3
        $filename = $category . '/' . time() . '_' . $file->getClientOriginalName();
        
        $this->s3->putObject([
            'Bucket' => env('AWS_MEDICAL_DOCS_BUCKET'),
            'Key' => $filename,
            'Body' => fopen($file->getRealPath(), 'r')
        ]);
        
        // 2. Bedrock automatically indexes it (wait ~2 mins)
        return $filename;
    }
    
    // CHAT WITH BOT
    public function chat($message)
    {
        $response = $this->bedrock->retrieveAndGenerate([
            'input' => ['text' => $message],
            'retrieveAndGenerateConfiguration' => [
                'type' => 'KNOWLEDGE_BASE',
                'knowledgeBaseConfiguration' => [
                    'knowledgeBaseId' => $this->kbId,
                    'modelArn' => 'arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20240620'
                ]
            ]
        ]);
        
        return $response['output']['text'];
    }
}

Step 5: Laravel Controller
php// app/Http/Controllers/ChatbotController.php

class ChatbotController extends Controller
{
    private $bedrock;
    
    public function __construct()
    {
        $this->bedrock = new BedrockService();
    }
    
    // UPLOAD ENDPOINT
    public function uploadDocument(Request $request)
    {
        $request->validate([
            'document' => 'required|file|mimes:pdf,docx,txt',
            'category' => 'required|in:general,medications,diet,procedures'
        ]);
        
        $filename = $this->bedrock->uploadDocument(
            $request->file('document'),
            $request->category
        );
        
        return response()->json([
            'message' => 'Document uploaded! Will be ready in 2 minutes.',
            'filename' => $filename
        ]);
    }
    
    // CHAT ENDPOINT
    public function chat(Request $request)
    {
        $request->validate([
            'message' => 'required|string'
        ]);
        
        $response = $this->bedrock->chat($request->message);
        
        return response()->json([
            'response' => $response
        ]);
    }
}

Step 6: Routes
php// routes/api.php

Route::post('/upload-document', [ChatbotController::class, 'uploadDocument']);
Route::post('/chat', [ChatbotController::class, 'chat']);

Step 7: Simple React Native Chat
jsx// Basic chat component
const ChatScreen = () => {
  const [message, setMessage] = useState('');
  const [responses, setResponses] = useState([]);
  
  const sendMessage = async () => {
    const response = await fetch('https://your-api.com/api/chat', {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({ message })
    });
    
    const data = await response.json();
    setResponses([...responses, data.response]);
    setMessage('');
  };
  
  return (
    <View>
      <FlatList data={responses} renderItem={({item}) => <Text>{item}</Text>} />
      <TextInput value={message} onChangeText={setMessage} />
      <Button title="Send" onPress={sendMessage} />
    </View>
  );
};

That's It! 🎉
What Happens Behind the Scenes:

Upload: Document goes to S3 → Bedrock chunks it → Creates vectors → Stores in OpenSearch
Chat: User message → Bedrock searches relevant chunks → Claude reads them → Generates response

Total Time to Working POC:

30 mins: AWS setup
1 hour: Laravel integration
30 mins: Basic React Native chat
2 hours total: Full working system

Monthly Cost Estimate:

Small usage (100 documents, 1000 chats): ~$100/month
Medium usage (1000 documents, 10k chats): ~$300/month
Large usage (10k documents, 100k chats): ~$1000/month

Next Steps:

Add patient-specific document folders in S3
Add authentication to endpoints
Store chat history in database
Add document management UI

That's literally all the code you need to get started! Want me to explain any part in more detail?```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment