-
Notifications
You must be signed in to change notification settings - Fork 105
Add conversational search #774
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
Open
Strift
wants to merge
21
commits into
main
Choose a base branch
from
feat/add-chat-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7604609
Add method to update workspace settings
Strift 4897fbf
add CRUD methods
Strift 51f4400
Refactor
Strift cd2b8df
Add chat completion streaming
Strift e52759c
Lint
Strift 1395b8d
Run phpstan
Strift f87bde4
Fix doc types
Strift b063d4b
Add get/update methods for chat settings
Strift b09585f
Fix incorrect exception catch block
Strift 0cc3972
lint
Strift 2b98215
Update src/Http/Client.php
Strift bde19cc
Update src/Http/Client.php
Strift c2ee6ca
Merge branch 'main' into feat/add-chat-api
Strift cb6c0bb
Improve chat workspaces type hints
Strift b6ae3bb
Make workspaceName non-empty-string
Strift c91b873
Throw exception if token is not available
Strift dd6aef9
Update types and tests
Strift 340b57f
Merge branch 'main' into feat/add-chat-api
Strift eafe1f2
Merge branch 'main' into feat/add-chat-api
Strift d78323d
Update src/Contracts/ChatWorkspacesResults.php
Strift 4dce531
Update src/Contracts/ChatWorkspacesResults.php
Strift File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Contracts; | ||
|
||
class ChatWorkspaceSettings extends Data | ||
{ | ||
private ?string $source; | ||
private ?string $orgId; | ||
private ?string $projectId; | ||
private ?string $apiVersion; | ||
private ?string $deploymentId; | ||
private ?string $baseUrl; | ||
private ?string $apiKey; | ||
private array $prompts; | ||
|
||
public function __construct(array $params) | ||
{ | ||
parent::__construct($params); | ||
|
||
$this->source = $params['source'] ?? null; | ||
$this->orgId = $params['orgId'] ?? null; | ||
$this->projectId = $params['projectId'] ?? null; | ||
$this->apiVersion = $params['apiVersion'] ?? null; | ||
$this->deploymentId = $params['deploymentId'] ?? null; | ||
$this->baseUrl = $params['baseUrl'] ?? null; | ||
$this->apiKey = $params['apiKey'] ?? null; | ||
$this->prompts = $params['prompts'] ?? []; | ||
} | ||
|
||
public function getSource(): ?string | ||
{ | ||
return $this->source; | ||
} | ||
|
||
public function getOrgId(): ?string | ||
{ | ||
return $this->orgId; | ||
} | ||
|
||
public function getProjectId(): ?string | ||
{ | ||
return $this->projectId; | ||
} | ||
|
||
public function getApiVersion(): ?string | ||
{ | ||
return $this->apiVersion; | ||
} | ||
|
||
public function getDeploymentId(): ?string | ||
{ | ||
return $this->deploymentId; | ||
} | ||
|
||
public function getBaseUrl(): ?string | ||
{ | ||
return $this->baseUrl; | ||
} | ||
|
||
public function getApiKey(): ?string | ||
{ | ||
return $this->apiKey; | ||
} | ||
|
||
/** | ||
* @return array{system?: string, searchDescription?: string, searchQParam?: string, searchIndexUidParam?: string} | ||
*/ | ||
public function getPrompts(): array | ||
{ | ||
return $this->prompts; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'source' => $this->source, | ||
'orgId' => $this->orgId, | ||
'projectId' => $this->projectId, | ||
'apiVersion' => $this->apiVersion, | ||
'deploymentId' => $this->deploymentId, | ||
'baseUrl' => $this->baseUrl, | ||
'apiKey' => $this->apiKey, | ||
'prompts' => $this->prompts, | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Contracts; | ||
|
||
class ChatWorkspacesResults extends Data | ||
{ | ||
private int $offset; | ||
private int $limit; | ||
private int $total; | ||
Strift marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function __construct(array $params) | ||
{ | ||
parent::__construct($params['results'] ?? []); | ||
Strift marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$this->offset = $params['offset']; | ||
$this->limit = $params['limit']; | ||
$this->total = $params['total'] ?? 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can api really return data without |
||
} | ||
|
||
/** | ||
* @return array<int, array{uid: string}> | ||
*/ | ||
public function getResults(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function getOffset(): int | ||
{ | ||
return $this->offset; | ||
} | ||
|
||
public function getLimit(): int | ||
{ | ||
return $this->limit; | ||
} | ||
|
||
public function getTotal(): int | ||
{ | ||
return $this->total; | ||
} | ||
Strift marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'results' => $this->data, | ||
'offset' => $this->offset, | ||
'limit' => $this->limit, | ||
'total' => $this->total, | ||
]; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return \count($this->data); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,10 @@ public function patch(string $path, $body = null, array $query = []); | |
* @throws JsonDecodingException | ||
*/ | ||
public function delete(string $path, array $query = []); | ||
|
||
/** | ||
* @throws ApiException | ||
* @throws JsonEncodingException | ||
*/ | ||
public function postStream(string $path, $body = null, array $query = []): \Psr\Http\Message\StreamInterface; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This again - misses import statement and using only class name as per coding standards |
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Endpoints; | ||
|
||
use Meilisearch\Contracts\ChatWorkspacesResults; | ||
use Meilisearch\Contracts\Endpoint; | ||
use Meilisearch\Contracts\Http; | ||
use Meilisearch\Endpoints\Delegates\HandlesChatWorkspaceSettings; | ||
|
||
class ChatWorkspaces extends Endpoint | ||
{ | ||
use HandlesChatWorkspaceSettings; | ||
|
||
protected const PATH = '/chats'; | ||
|
||
private ?string $workspaceName; | ||
Strift marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function __construct(Http $http, ?string $workspaceName = null) | ||
{ | ||
$this->workspaceName = $workspaceName; | ||
parent::__construct($http); | ||
} | ||
|
||
public function listWorkspaces(): ChatWorkspacesResults | ||
{ | ||
$response = $this->http->get(self::PATH); | ||
|
||
return new ChatWorkspacesResults($response); | ||
} | ||
|
||
public function workspace(string $workspaceName): self | ||
{ | ||
return new self($this->http, $workspaceName); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Endpoints\Delegates; | ||
|
||
use Meilisearch\Contracts\ChatWorkspaceSettings; | ||
|
||
trait HandlesChatWorkspaceSettings | ||
{ | ||
/** | ||
* Get the current settings for this chat workspace. | ||
*/ | ||
public function getSettings(): ChatWorkspaceSettings | ||
{ | ||
if (null === $this->workspaceName) { | ||
throw new \InvalidArgumentException('Workspace name is required to get settings'); | ||
} | ||
|
||
$response = $this->http->get('/chats/'.$this->workspaceName.'/settings'); | ||
|
||
return new ChatWorkspaceSettings($response); | ||
} | ||
|
||
/** | ||
* Update the settings for this chat workspace. | ||
* | ||
* @param array{ | ||
* source?: 'openAi'|'azureOpenAi'|'mistral'|'gemini'|'vLlm', | ||
* orgId?: string, | ||
* projectId?: string, | ||
* apiVersion?: string, | ||
* deploymentId?: string, | ||
* baseUrl?: string, | ||
* apiKey?: string, | ||
* prompts?: array<string, string> | ||
* } $settings | ||
*/ | ||
public function updateSettings(array $settings): ChatWorkspaceSettings | ||
{ | ||
if (null === $this->workspaceName) { | ||
throw new \InvalidArgumentException('Workspace name is required to update settings'); | ||
} | ||
|
||
$response = $this->http->patch('/chats/'.$this->workspaceName.'/settings', $settings); | ||
|
||
return new ChatWorkspaceSettings($response); | ||
} | ||
|
||
/** | ||
* Reset the settings for this chat workspace to default values. | ||
*/ | ||
public function resetSettings(): ChatWorkspaceSettings | ||
{ | ||
if (null === $this->workspaceName) { | ||
throw new \InvalidArgumentException('Workspace name is required to reset settings'); | ||
} | ||
|
||
$response = $this->http->delete('/chats/'.$this->workspaceName.'/settings'); | ||
|
||
return new ChatWorkspaceSettings($response); | ||
} | ||
|
||
/** | ||
* Create a streaming chat completion using OpenAI-compatible API. | ||
* | ||
* @param array{ | ||
* model: string, | ||
* messages: array<array{role: string, content: string}>, | ||
* stream: bool | ||
* } $options The request body for the chat completion | ||
*/ | ||
public function streamCompletion(array $options): \Psr\Http\Message\StreamInterface | ||
{ | ||
if (null === $this->workspaceName) { | ||
throw new \InvalidArgumentException('Workspace name is required for chat completion'); | ||
} | ||
|
||
return $this->http->postStream('/chats/'.$this->workspaceName.'/chat/completions', $options); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Endpoints\Delegates; | ||
|
||
use Meilisearch\Contracts\ChatWorkspacesResults; | ||
use Meilisearch\Endpoints\ChatWorkspaces; | ||
|
||
trait HandlesChatWorkspaces | ||
{ | ||
public ChatWorkspaces $chats; | ||
|
||
/** | ||
* List all chat workspaces. | ||
*/ | ||
public function getChatWorkspaces(): ChatWorkspacesResults | ||
{ | ||
return $this->chats->listWorkspaces(); | ||
} | ||
|
||
/** | ||
* Get a specific chat workspace instance. | ||
*/ | ||
public function chatWorkspace(string $workspaceName): ChatWorkspaces | ||
{ | ||
return $this->chats->workspace($workspaceName); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this misses many typehints
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated 🙌