Skip to content

Commit

Permalink
fix: enrich sources for no scope requests too
Browse files Browse the repository at this point in the history
Signed-off-by: Anupam Kumar <[email protected]>
  • Loading branch information
kyteinsky committed Sep 18, 2024
1 parent 96fc5a1 commit a48c7e1
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/TaskProcessing/ContextChatProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public function getOptionalOutputShapeEnumValues(): array {

/**
* @inheritDoc
* @return array{output: string, sources: list<string>}
* @throws \RuntimeException
*/
public function process(?string $userId, array $input, callable $reportProgress): array {
if ($userId === null) {
Expand Down Expand Up @@ -112,7 +114,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
if (isset($response['error'])) {
throw new \RuntimeException('No result in ContextChat response. ' . $response['error']);
}
return $response;
return $this->processResponse($userId, $response);
}

// scoped query
Expand Down Expand Up @@ -142,23 +144,37 @@ public function process(?string $userId, array $input, callable $reportProgress)
$processedScopes,
);

return $this->processResponse($userId, $response);
}

/**
* Validate and enrich sources JSON strings of the response
*
* @param string $userId
* @param array $response
* @return array{output: string, sources: list<string>}
* @throws \RuntimeException
*/
private function processResponse(string $userId, array $response): array {
if (isset($response['error'])) {
throw new \RuntimeException('No result in ContextChat response: ' . $response['error']);
}
if (!isset($response['output']) || !is_string($response['output'])
|| !isset($response['sources']) || !is_array($response['sources'])) {
throw new \RuntimeException('Invalid response from ContextChat, expected "output" and "sources" keys');
throw new \RuntimeException('Invalid response from ContextChat, expected "output" and "sources" keys: ' . json_encode($response));
}

$jsonSources = array_map(
$jsonSources = array_filter(array_map(
fn ($source) => json_encode($source),
$this->metadataService->getEnrichedSources($userId, ...$response['sources'] ?? [])
);
if (in_array(false, $jsonSources, true)) {
throw new \RuntimeException('Could not encode sources to JSON');
$this->metadataService->getEnrichedSources($userId, ...$response['sources'] ?? []),
), fn ($json) => is_string($json));

if (count($jsonSources) === 0) {
$this->logger->warning('No sources could be enriched', ['sources' => $response['sources']]);
} elseif (count($jsonSources) !== count($response['sources'] ?? [])) {
$this->logger->warning('Some sources could not be enriched', ['sources' => $response['sources'], 'jsonSources' => $jsonSources]);
}

/** @psalm-suppress InvalidReturnStatement */
return [
'output' => $response['output'] ?? '',
'sources' => $jsonSources,
Expand Down

0 comments on commit a48c7e1

Please sign in to comment.