modelPath = $options['model_path'] ?? $this->modelPath; $this->nCtx = $options['n_ctx'] ?? $this->nCtx; $this->nParts = $options['n_parts'] ?? $this->nParts; $this->seed = $options['seed'] ?? $this->seed; $this->f16Kv = $options['f16_kv'] ?? $this->f16Kv; $this->logitsAll = $options['logits_all'] ?? $this->logitsAll; $this->vocabOnly = $options['vocab_only'] ?? $this->vocabOnly; $this->useMlock = $options['use_mlock'] ?? $this->useMlock; $this->nThreads = $options['n_threads'] ?? $this->nThreads; $this->nBatch = $options['n_batch'] ?? $this->nBatch; $this->suffix = $options['suffix'] ?? $this->suffix; $this->maxTokens = $options['max_tokens'] ?? $this->maxTokens; $this->temperature = $options['temperature'] ?? $this->temperature; $this->topP = $options['top_p'] ?? $this->topP; $this->logprobs = $options['logprobs'] ?? $this->logprobs; $this->echo = $options['echo'] ?? $this->echo; $this->stop = $options['stop'] ?? $this->stop; $this->repeatPenalty = $options['repeat_penalty'] ?? $this->repeatPenalty; $this->topK = $options['top_k'] ?? $this->topK; $this->lastNTokensSize = $options['last_n_tokens_size'] ?? $this->lastNTokensSize; parent::__construct($options, $callbackManager, $cache); if ($adapter === null) { $adapter = LLamaCPPLangchainAdapter::create( [ 'model_path' => $this->modelPath, 'n_ctx' => $this->nCtx, 'n_parts' => $this->nParts, 'seed' => $this->seed, 'f16_kv' => $this->f16Kv, 'logits_all' => $this->logitsAll, 'vocab_only' => $this->vocabOnly, 'use_mlock' => $this->useMlock, 'n_threads' => $this->nThreads, ] ); } $this->adapter = $adapter; } /** * Returns the default parameters for the model. * @return array */ private function defaultParams() { return [ 'suffix' => $this->suffix, 'max_tokens' => $this->maxTokens, 'temperature' => $this->temperature, 'top_p' => $this->topP, 'logprobs' => $this->logprobs, 'echo' => $this->echo, 'stop_sequences' => $this->stop, 'repeat_penalty' => $this->repeatPenalty, 'top_k' => $this->topK, ]; } /** * Call the Llama model and return the output. * * @param string $prompt The prompt to use for generation. * @param mixed $stop A list of strings to stop generation when encountered. * * @return string The generated text. */ public function call(string $prompt, $stop = null): string { $params = $this->defaultParams(); if ($this->stop && $stop !== null) { throw new InvalidArgumentException('`stop` found in both the input and default params.'); } elseif ($this->stop) { $params['stop_sequences'] = $this->stop; } elseif ($stop !== null) { $params['stop_sequences'] = $stop; } else { $params['stop_sequences'] = []; } $text = $this->adapter->predict($prompt, $params); return $text; } public function llmType(): string { return 'llama.cpp'; } public function toArray(): array { return $this->getIdentifyingParams(); } public function getIdentifyingParams(): array { return array_merge(['model_path' => $this->modelPath], $this->defaultParams()); } }