api.tinyollama.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Minimalist ollama REST API implementation
  4. * https://github.com/ollama/ollama/blob/main/docs/api.md
  5. */
  6. class TinyOllama {
  7. /**
  8. * HTTP API abstraction layer
  9. *
  10. * @var object
  11. */
  12. protected $api = '';
  13. /**
  14. * Remote API interraction protocol
  15. *
  16. * @var string
  17. */
  18. protected $proto = 'http';
  19. /**
  20. * Remote API interaction host
  21. *
  22. * @var string
  23. */
  24. protected $host = 'localhost';
  25. /**
  26. * Remote API interaction port
  27. *
  28. * @var string
  29. */
  30. protected $port = 11434;
  31. /**
  32. * Response live-streaming flag
  33. *
  34. * @var bool
  35. */
  36. protected $streamingFlag = false;
  37. /**
  38. * Request model name
  39. *
  40. * @var string
  41. */
  42. protected $model = '';
  43. /**
  44. * Base URL which will be used as endpoint prefix
  45. *
  46. * @var string
  47. */
  48. protected $baseUrl = '';
  49. /**
  50. * Request temperature
  51. *
  52. * @var bool|int
  53. */
  54. protected $temperature = false;
  55. /**
  56. * Creates new TinyOllama instance
  57. *
  58. * @param string $host
  59. * @param int $port
  60. * @param string $model
  61. */
  62. public function __construct($host = 'localhost', $port = 11434, $model = '') {
  63. $this->setHost($host);
  64. $this->setPort($port);
  65. if (!empty($model)) {
  66. $this->setModel($model);
  67. }
  68. $this->initApi();
  69. $this->setBaseUrl();
  70. }
  71. /**
  72. * Initializes the API by creating a new instance of the OmaeUrl class
  73. *
  74. * @return void
  75. */
  76. protected function initApi() {
  77. $this->api = new OmaeUrl();
  78. }
  79. /**
  80. * Set the value of host
  81. *
  82. * @param string $host
  83. *
  84. * @return void
  85. */
  86. public function setHost($host) {
  87. $this->host = $host;
  88. }
  89. /**
  90. * Set the value of port
  91. *
  92. * @param int $port
  93. *
  94. * @return void
  95. */
  96. public function setPort($port) {
  97. $this->port = $port;
  98. }
  99. /**
  100. * Set the value of model name for instance
  101. *
  102. * @param string $model
  103. *
  104. * @return void
  105. */
  106. public function setModel($model) {
  107. $this->model = $model;
  108. }
  109. /**
  110. * Set the value for baseUrl
  111. *
  112. * @return void
  113. */
  114. public function setBaseUrl() {
  115. $baseUrl = $this->proto . '://' . $this->host . ':' . $this->port . '/api/';
  116. $this->baseUrl = $baseUrl;
  117. }
  118. /**
  119. * Set the value of the instance generation temperature
  120. *
  121. * @param float $temperature
  122. *
  123. * @return void
  124. */
  125. public function setTemperature($temperature) {
  126. $this->temperature = $temperature;
  127. }
  128. /**
  129. * Returns list of running models
  130. *
  131. * @return array
  132. */
  133. public function ps() {
  134. $result = array();
  135. $endPoint = 'ps';
  136. $rawReply = $this->api->response($this->baseUrl . $endPoint);
  137. if ($rawReply) {
  138. $result = json_decode($rawReply, true);
  139. }
  140. return ($result);
  141. }
  142. /**
  143. * Returns list of available local models
  144. *
  145. * @return array
  146. */
  147. public function list() {
  148. $result = array();
  149. $endPoint = 'tags';
  150. $rawReply = $this->api->response($this->baseUrl . $endPoint);
  151. if ($rawReply) {
  152. $result = json_decode($rawReply, true);
  153. }
  154. return ($result);
  155. }
  156. /**
  157. * Generates a response based on the given prompt.
  158. *
  159. * @param string $prompt The prompt for generating the response.
  160. * @param array $context the context parameter returned from a previous request,
  161. * this can be used to keep a short conversational memory
  162. *
  163. * @return array|string The generated response. Contains [response] data key
  164. * @throws Exception If the model or prompt is empty.
  165. */
  166. public function generate($prompt, $context = array()) {
  167. $result = array();
  168. $request = array();
  169. $endPoint = 'generate';
  170. if (!empty($this->model)) {
  171. $request['model'] = $this->model;
  172. if (!empty($prompt)) {
  173. $request['prompt'] = $prompt;
  174. $request['stream'] = $this->streamingFlag;
  175. if ($this->temperature !== false) {
  176. $request['temperature'] = $this->temperature;
  177. }
  178. if (!empty($context)) {
  179. $request['context'] = $context;
  180. }
  181. $requestBody = json_encode($request);
  182. $this->api->dataPostRaw($requestBody);
  183. $rawReply = $this->api->response($this->baseUrl . $endPoint);
  184. if ($rawReply) {
  185. if ($this->streamingFlag) {
  186. $result = $rawReply;
  187. } else {
  188. $result = json_decode($rawReply, true);
  189. }
  190. }
  191. } else {
  192. throw new Exception('EX_EMPTY_POMPT');
  193. }
  194. } else {
  195. throw new Exception('EX_EMPTY_MODEL');
  196. }
  197. return ($result);
  198. }
  199. /**
  200. * Sends a chat message to the API.
  201. *
  202. * @param string $message The message content.
  203. * @param string $role The role of the message sender. Default is 'user'. Possible: system, user, assistant, or tool
  204. * @param array $allMessages An array of all previous messages. Default is an empty array.
  205. * @return array The API response. Contains [message]=>[role]+[content] data keys
  206. *
  207. * @throws Exception If the model is empty or if the message is empty.
  208. */
  209. public function chat($message, $role = 'user', $allMessages = array()) {
  210. $result = array();
  211. $request = array();
  212. $messages = $allMessages;
  213. $endPoint = 'chat';
  214. if (!empty($this->model)) {
  215. $request['model'] = $this->model;
  216. if (!empty($message)) {
  217. $messages[] = array(
  218. 'role' => $role,
  219. 'content' => $message
  220. );
  221. $request['messages'] = $messages;
  222. $request['stream'] = $this->streamingFlag;
  223. $requestBody = json_encode($request);
  224. $this->api->dataPostRaw($requestBody);
  225. $rawReply = $this->api->response($this->baseUrl . $endPoint);
  226. if ($rawReply) {
  227. if ($this->streamingFlag) {
  228. $result = $rawReply;
  229. } else {
  230. $result = json_decode($rawReply, true);
  231. }
  232. }
  233. } else {
  234. throw new Exception('EX_EMPTY_MESSAGE');
  235. }
  236. } else {
  237. throw new Exception('EX_EMPTY_MODEL');
  238. }
  239. return ($result);
  240. }
  241. }