Last active
September 23, 2024 19:24
-
-
Save dilin993/fca35e7af1d3575853b91630a78afa4a to your computer and use it in GitHub Desktop.
A Laravel chat controller in PHP using AWS Bedrock API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Http; | |
use Aws\BedrockRuntime\BedrockRuntimeClient; | |
class ChatController extends Controller | |
{ | |
protected $instruction = <<<EOD | |
[INST]You are now chatting with a friendly assistant! Feel free to ask any questions or share your thoughts. [/INST] | |
EOD; | |
protected $client = null; | |
public function __construct() { | |
// Constructor method, called when a new instance is created | |
$this->client = new BedrockRuntimeClient([ | |
'version' => 'latest', | |
'region' => 'us-east-1' | |
]); | |
} | |
public function getChatResponse(Request $request) | |
{ | |
// Method to handle chatbot requests | |
$input = $request->input('input'); | |
$formattedPrompt = sprintf("%s\n%s\n", $this->instruction, $input); | |
$session = $request->input('session'); | |
$response = $this->client->invokeModel([ | |
'modelId' => 'meta.llama2-13b-chat-v1', | |
'body' => json_encode([ | |
'prompt' => $formattedPrompt, | |
'max_gen_len' => 128, | |
'temperature' => 0.1, | |
'top_p' => 0.9 | |
]), | |
'accept' => 'application/json', | |
'contentType' => 'application/json' | |
]); | |
$output = json_decode($response['body'], true)['generation']; | |
return response()->json(['output' => $output, 'session' => $session]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment