Skip to content

Commit

Permalink
Added get_models function designed for both Ollama and Openai
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed May 4, 2024
1 parent 7594926 commit 5607b87
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions classes/ai/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ private function get_prompt_data($prompttext) : array {
];
return $data;
}
public function get_models() : \stdClass {
$url = new \moodle_url($this->endpoint);
$ollama = true;
// Ollama url.
$modelquery = '/api/tags';
if ($url->get_host() == 'api.openai.com') {
$modelquery = '/v1/models';
$ollama = false;
}

$modelsurl = $url->get_scheme().'://'.$url->get_host().':'.$url->get_port().$modelquery;
$curl = new curl();
$options['CURLOPT_HTTPHEADER'] = ["Authorization: Bearer $this->apikey"];

$modeldata = json_decode($curl->get($modelsurl, null, $options));
if (!$ollama) {
$modeldata->models = $modeldata->data;
foreach ($modeldata->models as $model) {
$model->name = $model->id;
}
}

return $modeldata;
}

}

0 comments on commit 5607b87

Please sign in to comment.