Commit a25ac6f6 by haojie

1

parent ad62ed35
<?php
namespace App\Client;
use Orhanerday\OpenAi\OpenAi as BaseOpenAi;
class OpenAI extends BaseOpenAi
{
public function __construct($apiKey)
{
parent::__construct($apiKey);
}
public function withToken($token = '')
{
if ($token) {
$this->setHeader([1 => 'Authorization: Bearer ' . $token]);
return $this;
} else {
return $this;
}
}
}
<?php
declare(strict_types=1);
namespace App\Exceptions\OpenAI;
use InvalidArgumentException;
/**
* @internal
*/
class ApiKeyIsMissing extends InvalidArgumentException
{
/**
* Create a new exception instance.
*/
public static function create(): self
{
return new self(
'The OpenAI API Key is missing. Please publish the [openai.php] configuration file and set the [api_key].'
);
}
}
<?php
declare(strict_types=1);
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @method static \App\Client\OpenAI withToken($token = '')
* @method static string chat()
* @method static string completion()
* @method static string embeddings()
* @method static string createEdit()
* @method static string uploadFile()
* @method static string listFiles()
* @method static string retrieveFile()
* @method static string retrieveFileContent()
* @method static string deleteFile()
* @method static string createFineTune()
* @method static string listFineTunes()
* @method static string retrieveFineTune()
* @method static string cancelFineTune()
* @method static string listFineTuneEvents()
* @method static string deleteFineTune()
* @method static string image()
* @method static string imageEdit()
* @method static string createImageVariation()
* @method static string listModels()
* @method static string retrieveModel()
* @method static string moderation()
* @method static string transcribe()
* @method static string translate()
*/
class OpenAI extends Facade
{
/**
* Get the registered name of the component.
*/
protected static function getFacadeAccessor(): string
{
return 'openai';
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Http; namespace App\Http;
use App\Http\Middleware\Cors;
use Illuminate\Foundation\Http\Kernel as HttpKernel; use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel class Kernel extends HttpKernel
...@@ -21,6 +22,7 @@ class Kernel extends HttpKernel ...@@ -21,6 +22,7 @@ class Kernel extends HttpKernel
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class, \App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\Illuminate\Session\Middleware\StartSession::class,
]; ];
/** /**
...@@ -36,6 +38,9 @@ class Kernel extends HttpKernel ...@@ -36,6 +38,9 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class, \App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, \Illuminate\Routing\Middleware\SubstituteBindings::class,
# \App\Http\Middleware\HandleInertiaRequests::class,
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
Cors::class,
], ],
'api' => [ 'api' => [
......
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*');
}
}
<?php
declare(strict_types=1);
namespace App\Providers;
use App\Client\OpenAI;
use Illuminate\Support\ServiceProvider;
use App\Exceptions\OpenAI\ApiKeyIsMissing;
/**
* @internal
*/
class AiServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(OpenAI::class, static function (): OpenAI {
$apiKey = config('openai.api_key');
$baseUri = config('openai.base_uri');
$httpProxy = config('openai.http_proxy');
$organization = config('openai.organization');
if (!is_string($apiKey) || ($organization !== null && !is_string($organization))) {
throw ApiKeyIsMissing::create();
}
$openAi = new OpenAI($apiKey);
$openAi->setTimeout(60); // The default timeout time is 60s.
if ($organization !== null) {
$openAi->setORG($organization);
}
// The service proxy address has been configured.
if ($baseUri) {
$openAi->setBaseURL($baseUri);
}
// The local network proxy has been configured.
if ($httpProxy) {
$openAi->setProxy($httpProxy);
}
return $openAi;
});
$this->app->alias(OpenAi::class, 'openai');
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
}
}
...@@ -2,7 +2,10 @@ ...@@ -2,7 +2,10 @@
"name": "laravel/laravel", "name": "laravel/laravel",
"type": "project", "type": "project",
"description": "The Laravel Framework.", "description": "The Laravel Framework.",
"keywords": ["framework", "laravel"], "keywords": [
"framework",
"laravel"
],
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": "^8.0.2", "php": "^8.0.2",
...@@ -12,7 +15,9 @@ ...@@ -12,7 +15,9 @@
"laravel/sanctum": "^3.0", "laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7", "laravel/tinker": "^2.7",
"orangehill/iseed": "^3.0", "orangehill/iseed": "^3.0",
"php-open-source-saver/jwt-auth": "^2.1" "orhanerday/open-ai": "^4.7",
"php-open-source-saver/jwt-auth": "^2.1",
"ext-curl": "*"
}, },
"require-dev": { "require-dev": {
"fakerphp/faker": "^1.9.1", "fakerphp/faker": "^1.9.1",
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "c31cf328a6574f8349612ee96b872853", "content-hash": "39dc60ccbf5a3f6c0fa93559c64dd9ed",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
...@@ -2880,6 +2880,65 @@ ...@@ -2880,6 +2880,65 @@
"time": "2023-03-27T06:14:43+00:00" "time": "2023-03-27T06:14:43+00:00"
}, },
{ {
"name": "orhanerday/open-ai",
"version": "4.7.1",
"source": {
"type": "git",
"url": "https://github.com/orhanerday/open-ai.git",
"reference": "d2d96d0521ef3118cc4f33a5d54718b62aedd336"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/orhanerday/open-ai/zipball/d2d96d0521ef3118cc4f33a5d54718b62aedd336",
"reference": "d2d96d0521ef3118cc4f33a5d54718b62aedd336",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"php": ">=7.4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"pestphp/pest": "^1.20",
"spatie/ray": "^1.28"
},
"type": "library",
"autoload": {
"psr-4": {
"Orhanerday\\OpenAi\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Orhan Erday",
"email": "orhanerday@gmail.com",
"role": "Developer"
}
],
"description": "OpenAI GPT-3 Api Client in PHP",
"homepage": "https://github.com/orhanerday/open-ai",
"keywords": [
"open-ai",
"orhanerday"
],
"support": {
"issues": "https://github.com/orhanerday/open-ai/issues",
"source": "https://github.com/orhanerday/open-ai/tree/4.7.1"
},
"funding": [
{
"url": "https://github.com/orhanerday",
"type": "github"
}
],
"time": "2023-03-08T08:04:02+00:00"
},
{
"name": "php-open-source-saver/jwt-auth", "name": "php-open-source-saver/jwt-auth",
"version": "2.1.0", "version": "2.1.0",
"source": { "source": {
......
...@@ -194,6 +194,7 @@ ...@@ -194,6 +194,7 @@
// App\Providers\BroadcastServiceProvider::class, // App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class, App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
\App\Providers\AiServiceProvider::class,
], ],
......
<?php
return [
/*
|--------------------------------------------------------------------------
| OpenAI API Key and Organization
|--------------------------------------------------------------------------
|
| Here you may specify your OpenAI API Key and organization. This will be
| used to authenticate with the OpenAI API - you can find your API key
| and organization on your OpenAI dashboard, at https://openai.com.
*/
'api_key' => env('OPENAI_API_KEY'),
'base_uri' => env('OPENAI_BASE_URI', 'https://api.openai.com'),
'organization' => env('OPENAI_ORGANIZATION'),
'http_proxy' => env('HTTP_PROXY'),
];
...@@ -56,4 +56,25 @@ ...@@ -56,4 +56,25 @@
Route::get('split/status', 'TaskController@split_img_result'); Route::get('split/status', 'TaskController@split_img_result');
}); });
}); });
# GPT
Route::group([
'prefix' => '/gpt',
'namespace' => 'GPT'
], function () {
// 不需要登录
Route::group([
], function () {
// 用户登录
# Route::post('login', 'AuthController@login');
});
// 需要登录
Route::group([
'middleware' => ['auth:api'],
], function () {
// 提交对话内容
Route::post('/chat', 'ChatController@chat');
# 会话
Route::get('/stream', 'ChatController@stream');
});
});
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment