Skip to content

Instantly share code, notes, and snippets.

@mishajib
Last active September 10, 2023 11:54
Show Gist options
  • Save mishajib/c0fbcf6800b67f8b62764417cb296d4e to your computer and use it in GitHub Desktop.
Save mishajib/c0fbcf6800b67f8b62764417cb296d4e to your computer and use it in GitHub Desktop.
Chatgpt summarizer
<?php
namespace App\Http\Controllers;
use App\Models\RawEvent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class SummarizeController extends Controller
{
protected $httpClient;
public function __construct()
{
$apiKey = 'api_key';
$this->httpClient = Http::withHeaders([
'Authorization' => 'Bearer ' . $apiKey,
'Content-Type' => 'application/json',
'model' => 'gpt-3.5-turbo',
])
->baseUrl('https://api.openai.com/v1/');
}
/**
* Handle the incoming request.
*/
public function __invoke(Request $request)
{
try {
$summary = null;
$impact = null;
if ($request->method() == 'POST') {
// Summary
// remove \r\n and spaces
$content = str_replace(["\r\n", "\r", "\n", " "], '', $request->get('content'));
$summaryPrompts = "Summarise the media release's key regulatory updates in [50] words or less,
using Australian English and common abbreviations/acronyms for regulatory bodies/courts.
Maintain a professional, factual, and authoritative tone without embellishments.
Relevant text: " . $content;
$summaryResponse = $this->httpClient->post('chat/completions', [
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'system', 'content' => 'You are'],
['role' => 'user', 'content' => $summaryPrompts],
],
]);
$summaryData = $summaryResponse->json();
$summary = $summaryData['choices'][0]['message']['content'];
// Impact
$impactPrompts = "Categorise regulatory updates as 'high', 'medium', or 'low' impact. High: new laws/regulations. Medium: charges, sentencing, enforcement actions, class orders, instruments, consultation papers. Low: legislative dates, industry news, other updates. Do not explain why. Only state the words ‘high’, ‘medium’ or ‘low’. Relevant text: " . $summary;
$impactResponse = $this->httpClient->post('chat/completions', [
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'system', 'content' => 'You are'],
['role' => 'user', 'content' => $impactPrompts],
],
]);
$impactData = $impactResponse->json();
$impact = $impactData['choices'][0]['message']['content'];
}
if ($request->expectsJson()) {
return response()->json([
'success' => true,
'message' => 'Successfully summarized the content!',
'summary' => $summary,
'impact' => $impact
]);
}
return view('summarize', compact('summary', 'impact'));
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Something went wrong, please try again later!',
'error' => $e->getMessage(),
]);
}
}
/*protected $httpClient;
public function __construct()
{
$this->httpClient = new Client([
'base_uri' => 'https://api.openai.com/v1/',
'headers' => [
'Authorization' => 'Bearer ' . env('CHATGPT_API_KEY'),
'Content-Type' => 'application/json',
],
]);
}
public function askToChatGpt()
{
$message = "what is laravel";
$response = $this->httpClient->post('chat/completions', [
'json' => [
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'system', 'content' => 'You are'],
['role' => 'user', 'content' => $message],
],
],
]);
return json_decode($response->getBody(), true)['choices'][0]['message']['content'];
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment