Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ai
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
ai
Commits
60236fc6
Verified
Commit
60236fc6
authored
3 weeks ago
by
Rob Loach
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3525015
: Add Drush Command
parent
1035620f
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!609
Issue #3525015: Add Drush Command
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Drush/Commands/AiCommands.php
+73
-0
73 additions, 0 deletions
src/Drush/Commands/AiCommands.php
with
73 additions
and
0 deletions
src/Drush/Commands/AiCommands.php
0 → 100644
+
73
−
0
View file @
60236fc6
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\ai\Drush\Commands
;
use
Drush\Attributes
as
CLI
;
use
Drush\Commands\DrushCommands
;
use
Drush\Utils\StringUtils
;
use
Symfony\Component\Console\Input\InputInterface
;
use
Drush\Boot\DrupalBootLevels
;
use
Drupal\ai\OperationType\Chat\ChatInput
;
use
Drupal\ai\OperationType\Chat\ChatMessage
;
class
AiCommands
extends
DrushCommands
{
const
DEFAULT_SYSTEM_PROMPT
=
<<<EOD
You are a Drupal assistant, capable of answer questions about Drupal and Drush. When responding, make sure...
1. To keep all answers brief, and in ANSI
2. When highlighting text, wrap it in <info></info> tags
3. When output errors, use <error></error> tags
4. When writing optional text, use <comment></comment> tags
5. When posing questions, use <question></question> tags
6. When outputing a link, write the link in this format: <href=https://drupal.org>Drupal</>
EOD;
/**
* Send a message through to an AI provider.
*/
#[CLI\Command(name: 'ai:chat', aliases: ['chat', 'ai'])]
#[CLI\Argument(name: 'input', description: 'The message to send to the chat.')]
#[CLI\Option(name: 'provider', description: 'Indicates a provider to use other than the default.')]
#[CLI\Option(name: 'model', description: 'Indicates which model to use, other than the default.')]
#[CLI\Option(name: 'system', description: 'Indicates the system message to use, other than the default.')]
#[CLI\Usage(name: 'drush ai:chat "Hello, how are you?"', description: 'Sends a message to your chat provider.')]
#[CLI\Bootstrap(level: DrupalBootLevels::FULL)]
public
function
chat
(
string
$input
,
array
$options
=
[
'provider'
=>
self
::
OPT
,
'model'
=>
self
::
OPT
,
'system'
=>
self
::
OPT
])
:
void
{
// Make sure an AI Provider exists.
if
(
!
\Drupal
::
hasService
(
'ai.provider'
))
{
$this
->
logger
()
->
error
(
dt
(
'No AI Provider Service found'
));
return
;
}
// Retrieve the AI Provider and the options.
$service
=
\Drupal
::
service
(
'ai.provider'
);
$defaults
=
$service
->
getDefaultProviderForOperationType
(
'chat'
);
$provider_id
=
empty
(
$options
[
'provider'
])
?
$defaults
[
'provider_id'
]
:
$options
[
'provider'
];
$model_id
=
empty
(
$options
[
'model'
])
?
$defaults
[
'model_id'
]
:
$options
[
'model'
];
$system_message
=
!
empty
(
$optionts
[
'system'
])
?
$option
[
'system'
]
:
self
::
DEFAULT_SYSTEM_PROMPT
;
// Validate the provider ID and model ID.
if
(
empty
(
$provider_id
)
||
empty
(
$model_id
))
{
$this
->
logger
()
->
error
(
dt
(
'No default AI provider or model set'
));
return
;
}
// Build the provider and the chat message.
$provider
=
$service
->
createInstance
(
$provider_id
);
$messages
=
new
ChatInput
([
new
chatMessage
(
'system'
,
$system_message
),
new
chatMessage
(
'user'
,
$input
),
]);
// Output the message.
$message
=
$provider
->
chat
(
$messages
,
$model_id
)
->
getNormalized
();
$lines
=
explode
(
"
\n
"
,
$message
->
getText
());
foreach
(
$lines
as
$line
)
{
$this
->
output
()
->
writeln
(
$line
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment