1
0
Fork 0
mirror of synced 2024-06-27 18:50:47 +12:00

setup appwrite assistant for test

This commit is contained in:
Damodar Lohani 2023-08-03 10:29:36 +00:00
parent 8ed4029169
commit 2a7ec490af
3 changed files with 96 additions and 0 deletions

View file

@ -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' => '',
],
];

View file

@ -0,0 +1,74 @@
<?php
use Appwrite\Extend\Exception;
use Appwrite\Utopia\Response;
use Utopia\App;
use Utopia\Database\Document;
use Utopia\Validator\Text;
App::init()
->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');
});

View file

@ -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