|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\Multilingual; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A simple ollama client to query towerinstruct model |
|
9
|
|
|
* @link https://ollama.com/thinkverse/towerinstruct |
|
10
|
|
|
* @link https://huggingface.co/Unbabel/TowerInstruct-7B-v0.2 |
|
11
|
|
|
*/ |
|
12
|
|
|
class OllamaTowerInstruct |
|
13
|
|
|
{ |
|
14
|
|
|
final public const BASE_URL = 'http://localhost:11434'; |
|
15
|
|
|
final public const BASE_MODEL = 'thinkverse/towerinstruct'; |
|
16
|
|
|
|
|
17
|
|
|
protected ?string $model; |
|
18
|
|
|
protected ?string $url; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(?string $model = null, ?string $url = null) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->model = $model ?? self::BASE_MODEL; |
|
23
|
|
|
$this->url = $url ?? self::BASE_URL; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function translate(?string $string, string $to, string $from) |
|
27
|
|
|
{ |
|
28
|
|
|
/* |
|
29
|
|
|
messages = [ |
|
30
|
|
|
{"role": "user", "content": "Translate the following text from Portuguese into English.\nPortuguese: Um grupo de investigadores lançou um novo modelo para tarefas relacionadas com tradução.\nEnglish:"}, |
|
31
|
|
|
] |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
$prompt = "Translate the following text from $from into $to and keep variables between {} as is.\n$from: $string\n$to:"; |
|
35
|
|
|
|
|
36
|
|
|
$result = $this->generate($prompt); |
|
37
|
|
|
|
|
38
|
|
|
$response = $result['response'] ?? ''; |
|
39
|
|
|
|
|
40
|
|
|
return trim($response); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param null|array<int> $context |
|
45
|
|
|
* @return array{model:string,created_at:string,response:string,done:bool,done_reason:string,context:array<int>,total_duration:int,load_duration:int,prompt_eval_count:int,prompt_eval_duration:int,eval_count:int,eval_duration:int} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function generate(string $prompt, ?array $context = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$data = [ |
|
50
|
|
|
'model' => $this->model, |
|
51
|
|
|
'prompt' => $prompt, |
|
52
|
|
|
'stream' => false, |
|
53
|
|
|
]; |
|
54
|
|
|
if (!empty($context)) { |
|
55
|
|
|
$data['context'] = $context; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$url = $this->url . '/api/generate'; |
|
59
|
|
|
$ch = curl_init(); |
|
60
|
|
|
|
|
61
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
62
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
|
63
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
|
64
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
65
|
|
|
|
|
66
|
|
|
$output = curl_exec($ch); |
|
67
|
|
|
|
|
68
|
|
|
curl_close($ch); |
|
69
|
|
|
|
|
70
|
|
|
$decoded = json_decode($output, true); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if (!$decoded) { |
|
73
|
|
|
throw new Exception("Failed to decode json: " . json_last_error_msg()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
//@phpstan-ignore-next-line |
|
77
|
|
|
return $decoded; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|