Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for scoped context in query #15

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint-php-cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

strategy:
matrix:
php-versions: [ "8.1", "8.2" ]
php-versions: [ "8.0", "8.1", "8.2" ]

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:

strategy:
matrix:
php-versions: [ "8.1", "8.2" ]
php-versions: [ "8.0", "8.1", "8.2" ]

name: php-lint

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
# do not stop on another job's failure
fail-fast: false
matrix:
php-versions: [ "8.1", "8.2" ]
php-versions: [ '8.0', '8.1', '8.2' ]
databases: ['sqlite']
server-versions: ['master']

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

strategy:
matrix:
php-versions: [ '8.1', '8.2' ]
php-versions: [ '8.0', '8.1', '8.2' ]
server-versions: [ 'dev-master', 'dev-stable28' ]
fail-fast: false

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "^8.1 || ^8.2"
"php": "^8.0 || ^8.1 || ^8.2"
},
"require-dev": {
"nextcloud/coding-standard": "^1.1",
Expand All @@ -32,7 +32,7 @@
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
"cs:check": "php-cs-fixer fix --dry-run --diff",
"cs:fix": "php-cs-fixer fix",
"psalm": "psalm.phar --threads=1",
"psalm": "psalm.phar --threads=1 --no-cache",
"psalm:update-baseline": "psalm.phar --threads=1 --update-baseline",
"psalm:update-baseline:force": "psalm.phar --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
"psalm:clear": "psalm.phar --clear-cache && psalm.phar --clear-global-cache",
Expand All @@ -43,7 +43,7 @@
"optimize-autoloader": true,
"classmap-authoritative": true,
"platform": {
"php": "8.1"
"php": "8.0"
}
},
"autoload": {
Expand Down
194 changes: 101 additions & 93 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\ContextChat\Listener\FileListener;
use OCA\ContextChat\TextProcessing\ContextChatProvider;
use OCA\ContextChat\TextProcessing\FreePromptProvider;
use OCA\ContextChat\TextProcessing\ScopedContextChatProvider;
use OCP\App\Events\AppDisableEvent;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand Down Expand Up @@ -72,6 +73,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(AppDisableEvent::class, AppDisableListener::class);
$context->registerTextProcessingProvider(ContextChatProvider::class);
$context->registerTextProcessingProvider(FreePromptProvider::class);
$context->registerTextProcessingProvider(ScopedContextChatProvider::class);
}

public function boot(IBootContext $context): void {
Expand Down
47 changes: 46 additions & 1 deletion lib/Command/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace OCA\ContextChat\Command;

use OCA\ContextChat\TextProcessing\ContextChatTaskType;
use OCA\ContextChat\TextProcessing\ScopedContextChatTaskType;
use OCA\ContextChat\Type\ScopeType;
use OCP\TextProcessing\FreePromptTaskType;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\Task;
Expand Down Expand Up @@ -43,16 +45,59 @@ protected function configure() {
InputArgument::REQUIRED,
'The prompt'
)
->addOption('no-context', null, InputOption::VALUE_NONE, 'Do not use context');
->addOption(
'no-context',
null,
InputOption::VALUE_NONE,
'Do not use context'
)
->addOption(
'context-sources',
null,
InputOption::VALUE_REQUIRED,
'Context sources to use (as a comma-separated list without brackets)',
)
->addOption(
'context-providers',
null,
InputOption::VALUE_REQUIRED,
'Context providers to use (as a comma-separated list without brackets)',
);
}

protected function execute(InputInterface $input, OutputInterface $output) {
$userId = $input->getArgument('uid');
$prompt = $input->getArgument('prompt');
$noContext = $input->getOption('no-context');
$contextSources = $input->getOption('context-sources');
$contextProviders = $input->getOption('context-providers');

if ($noContext && (!empty($contextSources) || !empty($contextProviders))) {
throw new \InvalidArgumentException('Cannot use --no-context with --context-sources or --context-provider');
}

if (!empty($contextSources) && !empty($contextProviders)) {
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
throw new \InvalidArgumentException('Cannot use --context-sources with --context-provider');
}

if ($noContext) {
$task = new Task(FreePromptTaskType::class, $prompt, 'context_chat', $userId);
} elseif (!empty($contextSources)) {
$contextSources = preg_replace('/\s*,+\s*/', ',', $contextSources);
$contextSourcesArray = array_filter(explode(',', $contextSources), fn ($source) => !empty($source));
$task = new Task(ScopedContextChatTaskType::class, json_encode([
'scopeType' => ScopeType::SOURCE,
'scopeList' => $contextSourcesArray,
'prompt' => $prompt,
]), 'context_chat', $userId);
} elseif (!empty($contextProviders)) {
$contextProviders = preg_replace('/\s*,+\s*/', ',', $contextProviders);
$contextProvidersArray = array_filter(explode(',', $contextProviders), fn ($source) => !empty($source));
$task = new Task(ScopedContextChatTaskType::class, json_encode([
'scopeType' => ScopeType::PROVIDER,
'scopeList' => $contextProvidersArray,
'prompt' => $prompt,
]), 'context_chat', $userId);
} else {
$task = new Task(ContextChatTaskType::class, $prompt, 'context_chat', $userId);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/ScanFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function configure() {
InputArgument::REQUIRED,
'The user ID to scan the storage of'
)
->addOption('mimetype', 'm', InputOption::VALUE_OPTIONAL, 'The mime type filter');
->addOption('mimetype', 'm', InputOption::VALUE_REQUIRED, 'The mime type filter');
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
}

protected function execute(InputInterface $input, OutputInterface $output) {
Expand Down
19 changes: 19 additions & 0 deletions lib/Service/LangRopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,25 @@ public function query(string $userId, string $prompt, bool $useContext = true):
return ['message' => $this->getWithPresentableSources($response['output'] ?? '', ...($response['sources'] ?? []))];
}

/**
* @param string $userId
* @param string $prompt
* @param string $scopeType
* @param array<string> $scopeList
* @return array
*/
public function scopedQuery(string $userId, string $prompt, string $scopeType, array $scopeList): array {
$params = [
'query' => $prompt,
'userId' => $userId,
'scopeType' => $scopeType,
'scopeList' => $scopeList,
];

$response = $this->requestToExApp('/scopedQuery', 'POST', $params);
return ['message' => $this->getWithPresentableSources($response['output'] ?? '', ...($response['sources'] ?? []))];
}

public function getWithPresentableSources(string $llmResponse, string ...$sourceRefs): string {
if (count($sourceRefs) === 0) {
return $llmResponse;
Expand Down
6 changes: 5 additions & 1 deletion lib/TextProcessing/ContextChatProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ public function __construct(
}

public function getName(): string {
return $this->l10n->t('Nextcloud Assistant Context Chat provider');
return $this->l10n->t('Nextcloud Assistant Context Chat Provider');
}

public function process(string $prompt): string {
if ($this->userId === null) {
throw new \RuntimeException('User ID is required to process the prompt.');
marcelklehr marked this conversation as resolved.
Show resolved Hide resolved
}

$response = $this->langRopeService->query($this->userId, $prompt);
if (isset($response['error'])) {
throw new \RuntimeException('No result in ContextChat response. ' . $response['error']);
Expand Down
4 changes: 4 additions & 0 deletions lib/TextProcessing/FreePromptProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function getName(): string {
}

public function process(string $prompt): string {
if ($this->userId === null) {
throw new \RuntimeException('User ID is required to process the prompt.');
}

$response = $this->langRopeService->query($this->userId, $prompt, false);
if (isset($response['error'])) {
throw new \RuntimeException('No result in ContextChat response. ' . $response['error']);
Expand Down
93 changes: 93 additions & 0 deletions lib/TextProcessing/ScopedContextChatProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);
namespace OCA\ContextChat\TextProcessing;

use OCA\ContextChat\Service\LangRopeService;
use OCA\ContextChat\Type\ScopeType;
use OCP\IL10N;
use OCP\TextProcessing\IProvider;
use OCP\TextProcessing\IProviderWithUserId;

/**
* @template-implements IProviderWithUserId<ScopedContextChatTaskType>
* @template-implements IProvider<ScopedContextChatTaskType>
*/
class ScopedContextChatProvider implements IProvider, IProviderWithUserId {

private ?string $userId = null;

public function __construct(
private LangRopeService $langRopeService,
private IL10N $l10n,
) {
}

public function getName(): string {
return $this->l10n->t('Nextcloud Assistant Scoped Context Chat Provider');
}

/**
* @param string $prompt JSON string with the following structure:
* {
* "scopeType": string,
* "scopeList": list[string],
* "prompt": string,
* }
*
* @return string
*/
public function process(string $prompt): string {
if ($this->userId === null) {
throw new \RuntimeException('User ID is required to process the prompt.');
}

try {
$parsedData = json_decode($prompt, true, flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE);
} catch (\JsonException $e) {
throw new \RuntimeException(
'Invalid JSON string, expected { "scopeType": string, "scopeList": list[string], "prompt": string }',
intval($e->getCode()), $e,
);
}

if (
!is_array($parsedData)
|| !isset($parsedData['scopeType'])
|| !is_string($parsedData['scopeType'])
|| !isset($parsedData['scopeList'])
|| !is_array($parsedData['scopeList'])
|| !isset($parsedData['prompt'])
|| !is_string($parsedData['prompt'])
) {
throw new \RuntimeException('Invalid JSON string, expected { "scopeType": string, "scopeList": list[string], "prompt": string }');
}

try {
ScopeType::validate($parsedData['scopeType']);
} catch (\InvalidArgumentException $e) {
throw new \RuntimeException($e->getMessage(), intval($e->getCode()), $e);
}

$response = $this->langRopeService->scopedQuery(
$this->userId,
$parsedData['prompt'],
$parsedData['scopeType'],
$parsedData['scopeList'],
);

if (isset($response['error'])) {
throw new \RuntimeException('No result in ContextChat response. ' . $response['error']);
}

return $response['message'] ?? '';
}

public function getTaskType(): string {
return ScopedContextChatTaskType::class;
}

public function setUserId(?string $userId): void {
$this->userId = $userId;
}
}
52 changes: 52 additions & 0 deletions lib/TextProcessing/ScopedContextChatTaskType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Julien Veyssier <[email protected]>
*
* @author Julien Veyssier <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\ContextChat\TextProcessing;

use OCP\IL10N;
use OCP\TextProcessing\ITaskType;

class ScopedContextChatTaskType implements ITaskType {
public function __construct(
private IL10N $l,
) {
}

/**
* @inheritDoc
* @since 27.1.0
*/
public function getName(): string {
return $this->l->t('Scoped Context Chat');
}

/**
* @inheritDoc
* @since 27.1.0
*/
public function getDescription(): string {
return $this->l->t('Ask a question about the data selected by you.');
}
}
28 changes: 28 additions & 0 deletions lib/Type/ScopeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Nextcloud - ContextChat
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Anupam Kumar <[email protected]>
* @copyright Anupam Kumar 2023
*/

declare(strict_types=1);

namespace OCA\ContextChat\Type;

class ScopeType {
public const PROVIDER = 'provider';
public const SOURCE = 'source';

public static function validate(string $scopeType): void {
$relection = new \ReflectionClass(self::class);
if (!in_array($scopeType, $relection->getConstants())) {
throw new \InvalidArgumentException(
"Invalid scope type: {$scopeType}, should be one of: [" . implode(', ', $relection->getConstants()) . ']'
);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smooth! Very nice :)

}
4 changes: 2 additions & 2 deletions stubs/appapi-public-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace OCA\AppAPI {
class PublicFunctions {
public function __construct(
private readonly \OCA\AppAPI\Service\ExAppService $exAppService,
private readonly \OCA\AppAPI\Service\AppAPIService $service,
private \OCA\AppAPI\Service\ExAppService $exAppService,
private \OCA\AppAPI\Service\AppAPIService $service,
) {
}

Expand Down
Loading
Loading