|
| 1 | +import uuid |
| 2 | +import time |
| 3 | +import multiprocessing |
| 4 | +from typing import List, Optional |
| 5 | + |
| 6 | +from . import llama_cpp |
| 7 | + |
| 8 | +class Llama: |
| 9 | + def __init__( |
| 10 | + self, |
| 11 | + model_path: str, |
| 12 | + n_ctx: int = 512, |
| 13 | + n_parts: int = -1, |
| 14 | + seed: int = 1337, |
| 15 | + f16_kv: bool = False, |
| 16 | + logits_all: bool = False, |
| 17 | + vocab_only: bool = False, |
| 18 | + n_threads: Optional[int] = None, |
| 19 | + model_name: Optional[str]=None, |
| 20 | + ): |
| 21 | + self.model_path = model_path |
| 22 | + self.model = model_name or model_path |
| 23 | + |
| 24 | + self.params = llama_cpp.llama_context_default_params() |
| 25 | + self.params.n_ctx = n_ctx |
| 26 | + self.params.n_parts = n_parts |
| 27 | + self.params.seed = seed |
| 28 | + self.params.f16_kv = f16_kv |
| 29 | + self.params.logits_all = logits_all |
| 30 | + self.params.vocab_only = vocab_only |
| 31 | + |
| 32 | + self.n_threads = n_threads or multiprocessing.cpu_count() |
| 33 | + |
| 34 | + self.tokens = (llama_cpp.llama_token * self.params.n_ctx)() |
| 35 | + |
| 36 | + self.ctx = llama_cpp.llama_init_from_file( |
| 37 | + self.model_path.encode("utf-8"), self.params |
| 38 | + ) |
| 39 | + |
| 40 | + def __call__( |
| 41 | + self, |
| 42 | + prompt: str, |
| 43 | + suffix: Optional[str] = None, |
| 44 | + max_tokens: int = 16, |
| 45 | + temperature: float = 0.8, |
| 46 | + top_p: float = 0.95, |
| 47 | + echo: bool = False, |
| 48 | + stop: List[str] = [], |
| 49 | + repeat_penalty: float = 1.1, |
| 50 | + top_k: int = 40, |
| 51 | + ): |
| 52 | + text = "" |
| 53 | + finish_reason = "length" |
| 54 | + completion_tokens = 0 |
| 55 | + |
| 56 | + prompt_tokens = llama_cpp.llama_tokenize( |
| 57 | + self.ctx, prompt.encode("utf-8"), self.tokens, self.params.n_ctx, True |
| 58 | + ) |
| 59 | + |
| 60 | + if prompt_tokens + max_tokens > self.params.n_ctx: |
| 61 | + raise ValueError( |
| 62 | + f"Requested tokens exceed context window of {self.params.n_ctx}" |
| 63 | + ) |
| 64 | + |
| 65 | + for i in range(prompt_tokens): |
| 66 | + llama_cpp.llama_eval( |
| 67 | + self.ctx, (llama_cpp.c_int * 1)(self.tokens[i]), 1, i, self.n_threads |
| 68 | + ) |
| 69 | + |
| 70 | + for i in range(max_tokens): |
| 71 | + token = llama_cpp.llama_sample_top_p_top_k( |
| 72 | + self.ctx, |
| 73 | + self.tokens, |
| 74 | + prompt_tokens + completion_tokens, |
| 75 | + top_k=top_k, |
| 76 | + top_p=top_p, |
| 77 | + temp=temperature, |
| 78 | + repeat_penalty=repeat_penalty, |
| 79 | + ) |
| 80 | + if token == llama_cpp.llama_token_eos(): |
| 81 | + finish_reason = "stop" |
| 82 | + break |
| 83 | + text += llama_cpp.llama_token_to_str(self.ctx, token).decode("utf-8") |
| 84 | + self.tokens[prompt_tokens + i] = token |
| 85 | + completion_tokens += 1 |
| 86 | + |
| 87 | + any_stop = [s for s in stop if s in text] |
| 88 | + if len(any_stop) > 0: |
| 89 | + first_stop = any_stop[0] |
| 90 | + text = text[: text.index(first_stop)] |
| 91 | + finish_reason = "stop" |
| 92 | + break |
| 93 | + |
| 94 | + llama_cpp.llama_eval( |
| 95 | + self.ctx, |
| 96 | + (llama_cpp.c_int * 1)(self.tokens[prompt_tokens + i]), |
| 97 | + 1, |
| 98 | + prompt_tokens + completion_tokens, |
| 99 | + self.n_threads, |
| 100 | + ) |
| 101 | + |
| 102 | + if echo: |
| 103 | + text = prompt + text |
| 104 | + |
| 105 | + if suffix is not None: |
| 106 | + text = text + suffix |
| 107 | + |
| 108 | + return { |
| 109 | + "id": f"cmpl-{str(uuid.uuid4())}", # Likely to change |
| 110 | + "object": "text_completion", |
| 111 | + "created": int(time.time()), |
| 112 | + "model": self.model, # Likely to change |
| 113 | + "choices": [ |
| 114 | + { |
| 115 | + "text": text, |
| 116 | + "index": 0, |
| 117 | + "logprobs": None, |
| 118 | + "finish_reason": finish_reason, |
| 119 | + } |
| 120 | + ], |
| 121 | + "usage": { |
| 122 | + "prompt_tokens": prompt_tokens, |
| 123 | + "completion_tokens": completion_tokens, |
| 124 | + "total_tokens": prompt_tokens + completion_tokens, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + def __del__(self): |
| 129 | + llama_cpp.llama_free(self.ctx) |
| 130 | + |
| 131 | + |
0 commit comments