Skip to content

Commit

Permalink
1452: Added message classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed May 22, 2024
1 parent de55b26 commit a0e3f14
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Client/Ollama.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Client to communicate with Ollama.
*/
class Ollama {
readonly class Ollama {

/**
* Default constructor.
Expand All @@ -18,8 +18,8 @@ class Ollama {
* The port that Ollama is listing on.
*/
public function __construct(
private readonly string $url,
private readonly int $port,
private string $url,
private int $port,
) {
}

Expand Down Expand Up @@ -49,6 +49,19 @@ public function listLocalModels(): array {
return $models;
}

/**
* Install/update model in Ollama.
*
* @param string $modelName
* Name of the model
*
* @return string
*
* @see https://ollama.com/library
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \JsonException
*/
public function install(string $modelName): string {
$this->call(method: 'post', uri: '/api/pull', options: [
'json' => [
Expand All @@ -62,7 +75,8 @@ public function install(string $modelName): string {
RequestOptions::TIMEOUT => 300,
]);

return 'dfs';
// @todo: change to stream and return status.
return '';
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Model/ChatMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Drupal\llm_services\Model;

class ChatMessage {

/**
* The role of this message.
*
* @var \Drupal\llm_services\Model\MessageRoles
*/
public MessageRoles $role;

/**
* The message content.
*
* @var string
*/
public string $content;

/**
* Images base64 encoded.
*
* Used for multimodal models such as llava. Which can describe the content of
* the image.
*
* @var array<string>
*/
public array $images;
}
31 changes: 31 additions & 0 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Drupal\llm_services\Model;

class Message {

/**
* Name of the model to use.
*
* @var string
*/
public string $model;

/**
* Message(s) to send
*
* @see https://github.com/ollama/ollama/blob/main/docs/api.md#parameters-1
*
* @var array<\Drupal\llm_services\Model\ChatMessage>
*/
public array $messages;

/**
* Additional model parameters.
*
* @see https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values
*
* @var array<string, string>
*/
public array $options;
}
9 changes: 9 additions & 0 deletions src/Model/MessageRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Drupal\llm_services\Model;

enum MessageRoles: string {
case Assistant = 'assistant';
case System = "system";
case User = 'user';
}

0 comments on commit a0e3f14

Please sign in to comment.