Skip to content

Commit

Permalink
fix: better error handling and some fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Anupam Kumar <[email protected]>
  • Loading branch information
kyteinsky committed Oct 1, 2024
1 parent 661a6f1 commit bcef683
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
29 changes: 20 additions & 9 deletions lib/Service/LangRopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ private function requestToExApp(
$enabledResponse = $appApiFunctions->exAppRequest('context_chat_backend', '/enabled', $this->userId, 'GET');

if (is_array($enabledResponse) && isset($enabledResponse['error'])) {
throw new RuntimeException('Error during request to ExApp (context_chat_backend): ' . $enabledResponse['error']);
throw new RuntimeException('Error during request to Context Chat Backend (ExApp): ' . $enabledResponse['error']);
}

$enabledResponse = $enabledResponse->getBody();

if (!is_string($enabledResponse)) {
$this->logger->error('Error during request to ExApp (context_chat_backend): response body is not a string', ['response' => $enabledResponse]);
throw new RuntimeException('Error during request to ExApp (context_chat_backend): response body is not a string');
$this->logger->error('Error during request to Context Chat Backend (ExApp): response body is not a string', ['response' => $enabledResponse]);
throw new RuntimeException('Error during request to Context Chat Backend (ExApp): response body is not a string');
}

$enabledResponse = json_decode($enabledResponse, true);
if ($enabledResponse === null) {
$this->logger->error('Error during request to ExApp (context_chat_backend): response body is not a valid JSON', ['response' => $enabledResponse]);
throw new RuntimeException('Error during request to ExApp (context_chat_backend): response body is not a valid JSON');
$this->logger->error('Error during request to Context Chat Backend (ExApp): response body is not a valid JSON', ['response' => $enabledResponse]);
throw new RuntimeException('Error during request to Context Chat Backend (ExApp): response body is not a valid JSON');
}

if (isset($enabledResponse['enabled']) && $enabledResponse['enabled'] === true) {
Expand Down Expand Up @@ -134,15 +134,26 @@ private function requestToExApp(
$options,
);
if (is_array($response) && isset($response['error'])) {
throw new RuntimeException('Error during request to ExApp (context_chat_backend): ' . $response['error']);
throw new RuntimeException('Error during request to Context Chat Backend (ExApp): ' . $response['error']);
}
if (is_array($response)) {
// this should never happen since app_api only returns errors in an array
throw new RuntimeException('Error during request to Context Chat Backend (ExApp): response is not a valid response object');
}
if (intval($response->getStatusCode() / 100) !== 2) {
$this->logger->error('Error during request to Context Chat Backend (ExApp)', [
'code' => $response->getStatusCode(),
'response' => $response->getBody()
]);
throw new RuntimeException('Error during request to Context Chat Backend (ExApp)');
}

$resContentType = $response->getHeader('Content-Type');
if (strpos($resContentType, 'application/json') !== false) {
$body = $response->getBody();
if (!is_string($body)) {
$this->logger->error('Error during request to ExApp (context_chat_backend): response body is not a string, but content type is application/json', ['response' => $response]);
throw new RuntimeException('Error during request to ExApp (context_chat_backend): response body is not a string, but content type is application/json');
$this->logger->error('Error during request to Context Chat Backend (ExApp): response body is not a string, but content type is application/json', ['response' => $response]);
throw new RuntimeException('Error during request to Context Chat Backend (ExApp): response body is not a string, but content type is application/json');
}

return json_decode($body, true);
Expand Down Expand Up @@ -225,7 +236,7 @@ public function indexSources(array $sources): void {
* @return array
* @throws RuntimeException
*/
public function query(string $userId, string $prompt, bool $useContext = true, ?string $scopeType = null, ?array $scopeList = null): array {
public function query(string $userId, string $prompt, bool $useContext = false, ?string $scopeType = null, ?array $scopeList = null): array {
$params = [
'query' => $prompt,
'userId' => $userId,
Expand Down
14 changes: 13 additions & 1 deletion lib/TaskProcessing/ContextChatProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
// scoped query
$scopeList = array_unique($input['scopeList']);
if (count($scopeList) === 0) {
throw new \RuntimeException('No sources found');
throw new \RuntimeException('Empty scope list provided, use unscoped query instead');
}

// index sources before the query, not needed for providers
Expand All @@ -136,6 +136,10 @@ public function process(?string $userId, array $input, callable $reportProgress)
throw new \InvalidArgumentException('Invalid scope type');
}

if (count($processedScopes) === 0) {
throw new \RuntimeException('No supported sources found in the scope list, extend the list or use unscoped query instead');
}

$response = $this->langRopeService->query(
$userId,
$input['prompt'],
Expand Down Expand Up @@ -164,6 +168,14 @@ private function processResponse(string $userId, array $response): array {
throw new \RuntimeException('Invalid response from ContextChat, expected "output" and "sources" keys: ' . json_encode($response));
}

if (count($response['sources']) === 0) {
$this->logger->info('No sources found in the response', ['response' => $response]);
return [
'output' => $response['output'] ?? '',
'sources' => [],
];
}

$jsonSources = array_filter(array_map(
fn ($source) => json_encode($source),
$this->metadataService->getEnrichedSources($userId, ...$response['sources'] ?? []),
Expand Down

0 comments on commit bcef683

Please sign in to comment.