Llm.php 934 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace app\api\controller\ps;
  3. use app\common\controller\Api;
  4. use app\common\library\ps\LlmClient;
  5. use think\Config;
  6. /**
  7. * 大模型连通测试
  8. */
  9. class Llm extends Api
  10. {
  11. protected $noNeedRight = '*';
  12. /**
  13. * 探测本地 Ollama;顺带返回线上是否已启用(不含 Key)
  14. */
  15. public function ping()
  16. {
  17. $cfg = Config::get('llm') ?: [];
  18. unset($cfg['api_key'], $cfg['online_api_key']);
  19. $online = LlmClient::online();
  20. $out = [
  21. 'config' => $cfg,
  22. 'online_enabled' => $online->enabled(),
  23. 'online_vision' => $online->enabled() ? $online->visionModel() : '',
  24. ];
  25. try {
  26. $models = LlmClient::local()->listModels();
  27. $this->success('Ollama 已连通', $out + ['models' => $models]);
  28. } catch (\Exception $e) {
  29. $this->error($e->getMessage(), $out);
  30. }
  31. }
  32. }