Deploy: 2026-05-22 22:11:36
This commit is contained in:
@@ -4,12 +4,14 @@ namespace App\Services;
|
|||||||
|
|
||||||
class GeminiService
|
class GeminiService
|
||||||
{
|
{
|
||||||
|
public const DEFAULT_MODEL = 'gemini-3.1-flash-lite';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call Gemini API to generate a response
|
* Call Gemini API to generate a response
|
||||||
*/
|
*/
|
||||||
public static function generateResponse(string $apiKey, string $systemPrompt, string $userMessage): ?string
|
public static function generateResponse(string $apiKey, string $systemPrompt, string $userMessage): ?string
|
||||||
{
|
{
|
||||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-lite-latest:generateContent?key=' . $apiKey;
|
$url = 'https://generativelanguage.googleapis.com/v1beta/models/' . self::DEFAULT_MODEL . ':generateContent?key=' . $apiKey;
|
||||||
|
|
||||||
$payload = json_encode([
|
$payload = json_encode([
|
||||||
'contents' => [
|
'contents' => [
|
||||||
@@ -54,7 +56,7 @@ class GeminiService
|
|||||||
*/
|
*/
|
||||||
public static function generatePromptFromAudio(string $apiKey, string $audioBase64, string $mimeType): ?string
|
public static function generatePromptFromAudio(string $apiKey, string $audioBase64, string $mimeType): ?string
|
||||||
{
|
{
|
||||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-lite-latest:generateContent?key=' . $apiKey;
|
$url = 'https://generativelanguage.googleapis.com/v1beta/models/' . self::DEFAULT_MODEL . ':generateContent?key=' . $apiKey;
|
||||||
|
|
||||||
$payload = json_encode([
|
$payload = json_encode([
|
||||||
'contents' => [
|
'contents' => [
|
||||||
@@ -102,7 +104,7 @@ class GeminiService
|
|||||||
*/
|
*/
|
||||||
public static function generateResponseFromAudio(string $apiKey, string $systemPrompt, string $audioBase64, string $mimeType): ?string
|
public static function generateResponseFromAudio(string $apiKey, string $systemPrompt, string $audioBase64, string $mimeType): ?string
|
||||||
{
|
{
|
||||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-lite-latest:generateContent?key=' . $apiKey;
|
$url = 'https://generativelanguage.googleapis.com/v1beta/models/' . self::DEFAULT_MODEL . ':generateContent?key=' . $apiKey;
|
||||||
|
|
||||||
// Clean mimeType if it contains codec info
|
// Clean mimeType if it contains codec info
|
||||||
if (strpos($mimeType, ';') !== false) {
|
if (strpos($mimeType, ';') !== false) {
|
||||||
@@ -156,11 +158,11 @@ class GeminiService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transcribe incoming audio voice note to text using gemini-2.0-flash-lite
|
* Transcribe incoming audio voice note to text using gemini-3.1-flash-lite
|
||||||
*/
|
*/
|
||||||
public static function transcribeAudio(string $apiKey, string $audioBase64, string $mimeType): ?string
|
public static function transcribeAudio(string $apiKey, string $audioBase64, string $mimeType): ?string
|
||||||
{
|
{
|
||||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-lite:generateContent?key=' . $apiKey;
|
$url = 'https://generativelanguage.googleapis.com/v1beta/models/' . self::DEFAULT_MODEL . ':generateContent?key=' . $apiKey;
|
||||||
|
|
||||||
// Clean mimeType if it contains codec info
|
// Clean mimeType if it contains codec info
|
||||||
if (strpos($mimeType, ';') !== false) {
|
if (strpos($mimeType, ';') !== false) {
|
||||||
@@ -213,7 +215,7 @@ class GeminiService
|
|||||||
*/
|
*/
|
||||||
public static function generateResponseFromImage(string $apiKey, string $systemPrompt, string $imageBase64, string $mimeType): ?string
|
public static function generateResponseFromImage(string $apiKey, string $systemPrompt, string $imageBase64, string $mimeType): ?string
|
||||||
{
|
{
|
||||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-lite-latest:generateContent?key=' . $apiKey;
|
$url = 'https://generativelanguage.googleapis.com/v1beta/models/' . self::DEFAULT_MODEL . ':generateContent?key=' . $apiKey;
|
||||||
|
|
||||||
// Clean mimeType if it contains codec info
|
// Clean mimeType if it contains codec info
|
||||||
if (strpos($mimeType, ';') !== false) {
|
if (strpos($mimeType, ';') !== false) {
|
||||||
@@ -271,7 +273,7 @@ class GeminiService
|
|||||||
*/
|
*/
|
||||||
public static function generateOcrFromImage(string $apiKey, string $prompt, string $imageBase64, string $mimeType): ?string
|
public static function generateOcrFromImage(string $apiKey, string $prompt, string $imageBase64, string $mimeType): ?string
|
||||||
{
|
{
|
||||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-lite:generateContent?key=' . $apiKey;
|
$url = 'https://generativelanguage.googleapis.com/v1beta/models/' . self::DEFAULT_MODEL . ':generateContent?key=' . $apiKey;
|
||||||
|
|
||||||
// Clean mimeType if it contains codec info
|
// Clean mimeType if it contains codec info
|
||||||
if (strpos($mimeType, ';') !== false) {
|
if (strpos($mimeType, ';') !== false) {
|
||||||
|
|||||||
Reference in New Issue
Block a user