diff --git a/app/config/services.php b/app/config/services.php index 8383b44c8..086acd185 100644 --- a/app/config/services.php +++ b/app/config/services.php @@ -199,4 +199,17 @@ return [ 'optional' => false, 'icon' => '', ], + 'assistant' => [ + 'key' => 'assistant', + 'name' => 'Assistant', + 'subtitle' => 'The Appwrite assistant service allows you to interact with appwrite assistant.', + 'description' => '', + 'controller' => 'api/assistant.php', + 'sdk' => true, + 'docs' => true, + 'docsUrl' => '', + 'tests' => false, + 'optional' => false, + 'icon' => '', + ], ]; diff --git a/app/controllers/api/assistant.php b/app/controllers/api/assistant.php new file mode 100644 index 000000000..7ea9da87a --- /dev/null +++ b/app/controllers/api/assistant.php @@ -0,0 +1,74 @@ +groups(['console']) + ->inject('project') + ->action(function (Document $project) { + if ($project->getId() !== 'console') { + throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN); + } + }); + + +App::post('/v1/assistant/chat') + ->desc('Ask Query') + ->groups(['api', 'assistant']) + ->label('scope', 'public') + // ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) + ->label('sdk.namespace', 'assistant') + ->label('sdk.method', 'chat') + ->label('sdk.description', '/docs/references/assistant/chat.md') + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_CONSOLE_VARIABLES) + ->param('query', '', new Text(2000), 'Query') + ->inject('response') + ->action(function (string $query, Response $response) { + $ch = curl_init('http://appwrite-assistant:3003/'); + $responseHeaders = []; + $responseStatus = -1; + $responseBody = ''; + $responseType = ''; + $query = json_encode(['prompt' => $query]); + + $headers = ['accept: text/event-stream']; + $handleEvent = function($ch, $data) { + var_dump($data); + return \strlen($data); + }; + + curl_setopt($ch, CURLOPT_WRITEFUNCTION, $handleEvent); + + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); + curl_setopt($ch, CURLOPT_TIMEOUT, 9000); + curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) { + $len = strlen($header); + $header = explode(':', $header, 2); + + if (count($header) < 2) { // ignore invalid headers + return $len; + } + + $responseHeaders[strtolower(trim($header[0]))] = trim($header[1]); + + return $len; + }); + + curl_setopt($ch, CURLOPT_POSTFIELDS, $query); + + $responseBody = curl_exec($ch); + + curl_close($ch); + + $response->send('Response ended'); + + }); diff --git a/docker-compose.yml b/docker-compose.yml index 560c42ae3..f72458560 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -659,6 +659,15 @@ services: - _APP_REDIS_PORT - _APP_REDIS_USER - _APP_REDIS_PASS + + appwrite-assistant: + container_name: appwrite-assistant + image: dlohani/appwrite_ask + networks: + - appwrite + environment: + - OPENAI_API_KEY + mariadb: image: mariadb:10.7 # fix issues when upgrading using: mysql_upgrade -u root -p