llama.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. package llm
  2. import (
  3. "bufio"
  4. "bytes"
  5. "context"
  6. "embed"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "io/fs"
  12. "log"
  13. "math/rand"
  14. "net/http"
  15. "os"
  16. "os/exec"
  17. "path"
  18. "path/filepath"
  19. "runtime"
  20. "strconv"
  21. "strings"
  22. "time"
  23. "github.com/jmorganca/ollama/api"
  24. )
  25. //go:embed llama.cpp/*/build/*/bin/*
  26. var llamaCppEmbed embed.FS
  27. type ModelRunner struct {
  28. Path string // path to the model runner executable
  29. }
  30. func chooseRunners(workDir, runnerType string) []ModelRunner {
  31. buildPath := path.Join("llama.cpp", runnerType, "build")
  32. var runners []string
  33. // set the runners based on the OS
  34. // IMPORTANT: the order of the runners in the array is the priority order
  35. switch runtime.GOOS {
  36. case "darwin":
  37. runners = []string{
  38. path.Join(buildPath, "metal", "bin", "server"),
  39. path.Join(buildPath, "cpu", "bin", "server"),
  40. }
  41. case "linux":
  42. runners = []string{
  43. path.Join(buildPath, "cuda", "bin", "server"),
  44. path.Join(buildPath, "cpu", "bin", "server"),
  45. }
  46. case "windows":
  47. // TODO: select windows GPU runner here when available
  48. runners = []string{
  49. path.Join(buildPath, "cpu", "bin", "Release", "server.exe"),
  50. }
  51. default:
  52. log.Printf("unknown OS, running on CPU: %s", runtime.GOOS)
  53. runners = []string{
  54. path.Join(buildPath, "cpu", "bin", "server"),
  55. }
  56. }
  57. runnerAvailable := false // if no runner files are found in the embed, this flag will cause a fast fail
  58. for _, r := range runners {
  59. // find all the files in the runner's bin directory
  60. files, err := fs.Glob(llamaCppEmbed, path.Join(path.Dir(r), "*"))
  61. if err != nil {
  62. // this is expected, ollama may be compiled without all runners packed in
  63. log.Printf("%s runner not found: %v", r, err)
  64. continue
  65. }
  66. for _, f := range files {
  67. runnerAvailable = true
  68. srcFile, err := llamaCppEmbed.Open(f)
  69. if err != nil {
  70. log.Fatalf("read llama runner %s: %v", f, err)
  71. }
  72. defer srcFile.Close()
  73. // create the directory in case it does not exist, filepath.Dir() converts the file path to the OS's format
  74. destPath := filepath.Join(workDir, filepath.Dir(f))
  75. if err := os.MkdirAll(destPath, 0o755); err != nil {
  76. log.Fatalf("create runner temp dir %s: %v", filepath.Dir(f), err)
  77. }
  78. // create the path to the destination file, filepath.Base() converts the file path to the OS's format
  79. destFile := filepath.Join(destPath, filepath.Base(f))
  80. _, err = os.Stat(destFile)
  81. switch {
  82. case errors.Is(err, os.ErrNotExist):
  83. destFile, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
  84. if err != nil {
  85. log.Fatalf("write llama runner %s: %v", f, err)
  86. }
  87. defer destFile.Close()
  88. if _, err := io.Copy(destFile, srcFile); err != nil {
  89. log.Fatalf("copy llama runner %s: %v", f, err)
  90. }
  91. case err != nil:
  92. log.Fatalf("stat llama runner %s: %v", f, err)
  93. }
  94. }
  95. }
  96. if !runnerAvailable {
  97. log.Fatalf("%s runner not found", runnerType)
  98. }
  99. // return the runners to try in priority order
  100. localRunnersByPriority := []ModelRunner{}
  101. for _, r := range runners {
  102. // clean the ModelRunner paths so that they match the OS we are running on
  103. localRunnersByPriority = append(localRunnersByPriority, ModelRunner{Path: filepath.Clean(path.Join(workDir, r))})
  104. }
  105. return localRunnersByPriority
  106. }
  107. type llamaModel struct {
  108. hyperparameters llamaHyperparameters
  109. }
  110. func (llm *llamaModel) ModelFamily() string {
  111. return "llama"
  112. }
  113. func llamaModelType(numLayer uint32) string {
  114. switch numLayer {
  115. case 26:
  116. return "3B"
  117. case 32:
  118. return "7B"
  119. case 40:
  120. return "13B"
  121. case 48:
  122. return "34B"
  123. case 60:
  124. return "30B"
  125. case 80:
  126. return "65B"
  127. default:
  128. return "unknown"
  129. }
  130. }
  131. func (llm *llamaModel) ModelType() string {
  132. return llamaModelType(llm.hyperparameters.NumLayer)
  133. }
  134. func (llm *llamaModel) FileType() string {
  135. return fileType(llm.hyperparameters.FileType)
  136. }
  137. func (llm *llamaModel) NumLayers() int64 {
  138. return int64(llm.hyperparameters.NumLayer)
  139. }
  140. type llamaHyperparameters struct {
  141. // NumVocab is the size of the model's vocabulary.
  142. NumVocab uint32
  143. // NumEmbd is the size of the model's embedding layer.
  144. NumEmbd uint32
  145. NumMult uint32
  146. NumHead uint32
  147. // NumLayer is the number of layers in the model.
  148. NumLayer uint32
  149. NumRot uint32
  150. // FileType describes the quantization level of the model, e.g. Q4_0, Q5_K, etc.
  151. FileType uint32
  152. }
  153. type Running struct {
  154. Port int
  155. Cmd *exec.Cmd
  156. Cancel context.CancelFunc
  157. }
  158. type llama struct {
  159. api.Options
  160. Running
  161. }
  162. var errNoGPU = errors.New("nvidia-smi command failed")
  163. // CheckVRAM returns the available VRAM in MiB on Linux machines with NVIDIA GPUs
  164. func CheckVRAM() (int64, error) {
  165. cmd := exec.Command("nvidia-smi", "--query-gpu=memory.total", "--format=csv,noheader,nounits")
  166. var stdout bytes.Buffer
  167. cmd.Stdout = &stdout
  168. err := cmd.Run()
  169. if err != nil {
  170. return 0, errNoGPU
  171. }
  172. var total int64
  173. scanner := bufio.NewScanner(&stdout)
  174. for scanner.Scan() {
  175. line := scanner.Text()
  176. vram, err := strconv.ParseInt(strings.TrimSpace(line), 10, 64)
  177. if err != nil {
  178. return 0, fmt.Errorf("failed to parse available VRAM: %v", err)
  179. }
  180. total += vram
  181. }
  182. return total, nil
  183. }
  184. func NumGPU(numLayer, fileSizeBytes int64, opts api.Options) int {
  185. if opts.NumGPU != -1 {
  186. return opts.NumGPU
  187. }
  188. if runtime.GOOS == "linux" {
  189. vramMib, err := CheckVRAM()
  190. if err != nil {
  191. if err.Error() != "nvidia-smi command failed" {
  192. log.Print(err.Error())
  193. }
  194. // nvidia driver not installed or no nvidia GPU found
  195. return 0
  196. }
  197. totalVramBytes := int64(vramMib) * 1024 * 1024 // 1 MiB = 1024^2 bytes
  198. // Calculate bytes per layer
  199. // TODO: this is a rough heuristic, better would be to calculate this based on number of layers and context size
  200. bytesPerLayer := fileSizeBytes / numLayer
  201. // max number of layers we can fit in VRAM
  202. layers := int(totalVramBytes / bytesPerLayer)
  203. log.Printf("%d MiB VRAM available, loading up to %d GPU layers", vramMib, layers)
  204. return layers
  205. }
  206. // default to enable metal on macOS
  207. return 1
  208. }
  209. func newLlama(model string, adapters []string, runners []ModelRunner, numLayers int64, opts api.Options) (*llama, error) {
  210. fileInfo, err := os.Stat(model)
  211. if err != nil {
  212. return nil, err
  213. }
  214. if len(adapters) > 1 {
  215. return nil, errors.New("ollama supports only one lora adapter, but multiple were provided")
  216. }
  217. params := []string{
  218. "--model", model,
  219. "--ctx-size", fmt.Sprintf("%d", opts.NumCtx),
  220. "--rope-freq-base", fmt.Sprintf("%f", opts.RopeFrequencyBase),
  221. "--rope-freq-scale", fmt.Sprintf("%f", opts.RopeFrequencyScale),
  222. "--batch-size", fmt.Sprintf("%d", opts.NumBatch),
  223. "--n-gpu-layers", fmt.Sprintf("%d", NumGPU(numLayers, fileInfo.Size(), opts)),
  224. "--embedding",
  225. }
  226. if opts.NumGQA > 0 {
  227. params = append(params, "--gqa", fmt.Sprintf("%d", opts.NumGQA))
  228. }
  229. if len(adapters) > 0 {
  230. // TODO: applying multiple adapters is not supported by the llama.cpp server yet
  231. params = append(params, "--lora", adapters[0])
  232. }
  233. if opts.NumThread > 0 {
  234. params = append(params, "--threads", fmt.Sprintf("%d", opts.NumThread))
  235. }
  236. if !opts.F16KV {
  237. params = append(params, "--memory-f32")
  238. }
  239. if opts.UseMLock {
  240. params = append(params, "--mlock")
  241. }
  242. if !opts.UseMMap {
  243. params = append(params, "--no-mmap")
  244. }
  245. if opts.UseNUMA {
  246. params = append(params, "--numa")
  247. }
  248. // start the llama.cpp server with a retry in case the port is already in use
  249. for _, runner := range runners {
  250. if _, err := os.Stat(runner.Path); err != nil {
  251. log.Printf("llama runner not found: %v", err)
  252. continue
  253. }
  254. port := rand.Intn(65535-49152) + 49152 // get a random port in the ephemeral range
  255. ctx, cancel := context.WithCancel(context.Background())
  256. cmd := exec.CommandContext(
  257. ctx,
  258. runner.Path,
  259. append(params, "--port", strconv.Itoa(port))...,
  260. )
  261. cmd.Env = append(os.Environ(), fmt.Sprintf("LD_LIBRARY_PATH=%s", filepath.Dir(runner.Path)))
  262. cmd.Stdout = os.Stderr
  263. cmd.Stderr = os.Stderr
  264. llm := &llama{Options: opts, Running: Running{Port: port, Cmd: cmd, Cancel: cancel}}
  265. log.Print("starting llama runner")
  266. if err := llm.Cmd.Start(); err != nil {
  267. log.Printf("error starting the external llama runner: %v", err)
  268. continue
  269. }
  270. // monitor the command, it is blocking, so if it exits we need to capture that
  271. go func() {
  272. err := llm.Cmd.Wait() // this will block until the command exits
  273. if err != nil {
  274. log.Printf("llama runner exited with error: %v", err)
  275. } else {
  276. log.Printf("llama runner exited")
  277. }
  278. }()
  279. if err := waitForServer(llm); err != nil {
  280. log.Printf("error starting llama runner: %v", err)
  281. llm.Close()
  282. // try again
  283. continue
  284. }
  285. // server started successfully
  286. return llm, nil
  287. }
  288. return nil, fmt.Errorf("failed to start a llama runner")
  289. }
  290. func waitForServer(llm *llama) error {
  291. // wait for the server to start responding
  292. start := time.Now()
  293. expiresAt := time.Now().Add(2 * time.Minute) // be generous with timeout, large models can take a while to load
  294. ticker := time.NewTicker(200 * time.Millisecond)
  295. log.Print("waiting for llama runner to start responding")
  296. for range ticker.C {
  297. if time.Now().After(expiresAt) {
  298. return fmt.Errorf("llama runner did not start within alloted time, retrying")
  299. }
  300. // check if the server process has terminated
  301. if llm.Cmd.ProcessState != nil && llm.Cmd.ProcessState.Exited() {
  302. return fmt.Errorf("llama runner process has terminated")
  303. }
  304. if err := llm.Ping(context.Background()); err == nil {
  305. break
  306. }
  307. }
  308. log.Printf("llama runner started in %f seconds", time.Since(start).Seconds())
  309. return nil
  310. }
  311. func (llm *llama) Close() {
  312. llm.Cancel()
  313. }
  314. func (llm *llama) SetOptions(opts api.Options) {
  315. llm.Options = opts
  316. }
  317. type GenerationSettings struct {
  318. FrequencyPenalty float64 `json:"frequency_penalty"`
  319. IgnoreEOS bool `json:"ignore_eos"`
  320. LogitBias []interface{} `json:"logit_bias"`
  321. Mirostat int `json:"mirostat"`
  322. MirostatEta float64 `json:"mirostat_eta"`
  323. MirostatTau float64 `json:"mirostat_tau"`
  324. Model string `json:"model"`
  325. NCtx int `json:"n_ctx"`
  326. NKeep int `json:"n_keep"`
  327. NPredict int `json:"n_predict"`
  328. NProbs int `json:"n_probs"`
  329. PenalizeNl bool `json:"penalize_nl"`
  330. PresencePenalty float64 `json:"presence_penalty"`
  331. RepeatLastN int `json:"repeat_last_n"`
  332. RepeatPenalty float64 `json:"repeat_penalty"`
  333. Seed uint32 `json:"seed"`
  334. Stop []string `json:"stop"`
  335. Stream bool `json:"stream"`
  336. Temp float64 `json:"temp"`
  337. TfsZ float64 `json:"tfs_z"`
  338. TopK int `json:"top_k"`
  339. TopP float64 `json:"top_p"`
  340. TypicalP float64 `json:"typical_p"`
  341. }
  342. type Timings struct {
  343. PredictedN int `json:"predicted_n"`
  344. PredictedMS float64 `json:"predicted_ms"`
  345. PromptN int `json:"prompt_n"`
  346. PromptMS float64 `json:"prompt_ms"`
  347. }
  348. type Prediction struct {
  349. Content string `json:"content"`
  350. Model string `json:"model"`
  351. Prompt string `json:"prompt"`
  352. Stop bool `json:"stop"`
  353. Timings `json:"timings"`
  354. }
  355. type PredictRequest struct {
  356. Prompt string `json:"prompt"`
  357. Stream bool `json:"stream"`
  358. NPredict int `json:"n_predict"`
  359. NKeep int `json:"n_keep"`
  360. Temperature float32 `json:"temperature"`
  361. TopK int `json:"top_k"`
  362. TopP float32 `json:"top_p"`
  363. TfsZ float32 `json:"tfs_z"`
  364. TypicalP float32 `json:"typical_p"`
  365. RepeatLastN int `json:"repeat_last_n"`
  366. RepeatPenalty float32 `json:"repeat_penalty"`
  367. PresencePenalty float32 `json:"presence_penalty"`
  368. FrequencyPenalty float32 `json:"frequency_penalty"`
  369. Mirostat int `json:"mirostat"`
  370. MirostatTau float32 `json:"mirostat_tau"`
  371. MirostatEta float32 `json:"mirostat_eta"`
  372. PenalizeNl bool `json:"penalize_nl"`
  373. Seed int `json:"seed"`
  374. Stop []string `json:"stop,omitempty"`
  375. }
  376. const maxBufferSize = 512 * 1024 // 512KB
  377. func (llm *llama) Predict(ctx context.Context, prevContext []int, prompt string, fn func(api.GenerateResponse)) error {
  378. prevConvo, err := llm.Decode(ctx, prevContext)
  379. if err != nil {
  380. return err
  381. }
  382. var nextContext strings.Builder
  383. nextContext.WriteString(prevConvo)
  384. nextContext.WriteString(prompt)
  385. endpoint := fmt.Sprintf("http://127.0.0.1:%d/completion", llm.Port)
  386. predReq := PredictRequest{
  387. Prompt: nextContext.String(),
  388. Stream: true,
  389. NPredict: llm.NumPredict,
  390. NKeep: llm.NumKeep,
  391. Temperature: llm.Temperature,
  392. TopK: llm.TopK,
  393. TopP: llm.TopP,
  394. TfsZ: llm.TFSZ,
  395. TypicalP: llm.TypicalP,
  396. RepeatLastN: llm.RepeatLastN,
  397. RepeatPenalty: llm.RepeatPenalty,
  398. PresencePenalty: llm.PresencePenalty,
  399. FrequencyPenalty: llm.FrequencyPenalty,
  400. Mirostat: llm.Mirostat,
  401. MirostatTau: llm.MirostatTau,
  402. MirostatEta: llm.MirostatEta,
  403. PenalizeNl: llm.PenalizeNewline,
  404. Seed: llm.Seed,
  405. Stop: llm.Stop,
  406. }
  407. data, err := json.Marshal(predReq)
  408. if err != nil {
  409. return fmt.Errorf("error marshaling data: %v", err)
  410. }
  411. req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(data))
  412. if err != nil {
  413. return fmt.Errorf("error creating POST request: %v", err)
  414. }
  415. req.Header.Set("Content-Type", "application/json")
  416. resp, err := http.DefaultClient.Do(req)
  417. if err != nil {
  418. return fmt.Errorf("POST predict: %v", err)
  419. }
  420. defer resp.Body.Close()
  421. if resp.StatusCode >= 400 {
  422. bodyBytes, err := io.ReadAll(resp.Body)
  423. if err != nil {
  424. return fmt.Errorf("failed reading llm error response: %w", err)
  425. }
  426. log.Printf("llm predict error: %s", bodyBytes)
  427. return fmt.Errorf("%s", bodyBytes)
  428. }
  429. scanner := bufio.NewScanner(resp.Body)
  430. // increase the buffer size to avoid running out of space
  431. buf := make([]byte, 0, maxBufferSize)
  432. scanner.Buffer(buf, maxBufferSize)
  433. for scanner.Scan() {
  434. select {
  435. case <-ctx.Done():
  436. // This handles the request cancellation
  437. return ctx.Err()
  438. default:
  439. line := scanner.Text()
  440. if line == "" {
  441. continue
  442. }
  443. // Read data from the server-side event stream
  444. if strings.HasPrefix(line, "data: ") {
  445. evt := line[6:]
  446. var p Prediction
  447. if err := json.Unmarshal([]byte(evt), &p); err != nil {
  448. return fmt.Errorf("error unmarshaling llm prediction response: %v", err)
  449. }
  450. if p.Content != "" {
  451. fn(api.GenerateResponse{Response: p.Content})
  452. nextContext.WriteString(p.Content)
  453. }
  454. if p.Stop {
  455. embd, err := llm.Encode(ctx, nextContext.String())
  456. if err != nil {
  457. return fmt.Errorf("encoding context: %v", err)
  458. }
  459. fn(api.GenerateResponse{
  460. Done: true,
  461. Context: embd,
  462. PromptEvalCount: p.PromptN,
  463. PromptEvalDuration: parseDurationMs(p.PromptMS),
  464. EvalCount: p.PredictedN,
  465. EvalDuration: parseDurationMs(p.PredictedMS),
  466. })
  467. return nil
  468. }
  469. }
  470. }
  471. }
  472. if err := scanner.Err(); err != nil {
  473. return fmt.Errorf("error reading llm response: %v", err)
  474. }
  475. return nil
  476. }
  477. type TokenizeRequest struct {
  478. Content string `json:"content"`
  479. }
  480. type TokenizeResponse struct {
  481. Tokens []int `json:"tokens"`
  482. }
  483. func (llm *llama) Encode(ctx context.Context, prompt string) ([]int, error) {
  484. endpoint := fmt.Sprintf("http://127.0.0.1:%d/tokenize", llm.Port)
  485. data, err := json.Marshal(TokenizeRequest{Content: prompt})
  486. if err != nil {
  487. return nil, fmt.Errorf("marshaling encode data: %w", err)
  488. }
  489. req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(data))
  490. if err != nil {
  491. return nil, fmt.Errorf("encode request: %w", err)
  492. }
  493. req.Header.Set("Content-Type", "application/json")
  494. resp, err := http.DefaultClient.Do(req)
  495. if err != nil {
  496. return nil, fmt.Errorf("do encode request: %w", err)
  497. }
  498. defer resp.Body.Close()
  499. body, err := io.ReadAll(resp.Body)
  500. if err != nil {
  501. return nil, fmt.Errorf("read encode request: %w", err)
  502. }
  503. if resp.StatusCode >= 400 {
  504. log.Printf("llm encode error: %s", body)
  505. return nil, fmt.Errorf("%s", body)
  506. }
  507. var encoded TokenizeResponse
  508. if err := json.Unmarshal(body, &encoded); err != nil {
  509. return nil, fmt.Errorf("unmarshal encode response: %w", err)
  510. }
  511. return encoded.Tokens, nil
  512. }
  513. type DetokenizeRequest struct {
  514. Tokens []int `json:"tokens"`
  515. }
  516. type DetokenizeResponse struct {
  517. Content string `json:"content"`
  518. }
  519. func (llm *llama) Decode(ctx context.Context, tokens []int) (string, error) {
  520. if len(tokens) == 0 {
  521. return "", nil
  522. }
  523. endpoint := fmt.Sprintf("http://127.0.0.1:%d/detokenize", llm.Port)
  524. data, err := json.Marshal(DetokenizeRequest{Tokens: tokens})
  525. if err != nil {
  526. return "", fmt.Errorf("marshaling decode data: %w", err)
  527. }
  528. req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(data))
  529. if err != nil {
  530. return "", fmt.Errorf("decode request: %w", err)
  531. }
  532. req.Header.Set("Content-Type", "application/json")
  533. resp, err := http.DefaultClient.Do(req)
  534. if err != nil {
  535. return "", fmt.Errorf("do decode request: %w", err)
  536. }
  537. defer resp.Body.Close()
  538. body, err := io.ReadAll(resp.Body)
  539. if err != nil {
  540. return "", fmt.Errorf("read decode request: %w", err)
  541. }
  542. if resp.StatusCode >= 400 {
  543. log.Printf("llm decode error: %s", body)
  544. return "", fmt.Errorf("%s", body)
  545. }
  546. var decoded DetokenizeResponse
  547. if err := json.Unmarshal(body, &decoded); err != nil {
  548. return "", fmt.Errorf("unmarshal encode response: %w", err)
  549. }
  550. // decoded content contains a leading whitespace
  551. decoded.Content, _ = strings.CutPrefix(decoded.Content, "")
  552. return decoded.Content, nil
  553. }
  554. type EmbeddingRequest struct {
  555. Content string `json:"content"`
  556. }
  557. type EmbeddingResponse struct {
  558. Embedding []float64 `json:"embedding"`
  559. }
  560. func (llm *llama) Embedding(ctx context.Context, input string) ([]float64, error) {
  561. endpoint := fmt.Sprintf("http://127.0.0.1:%d/embedding", llm.Port)
  562. data, err := json.Marshal(TokenizeRequest{Content: input})
  563. if err != nil {
  564. return nil, fmt.Errorf("error marshaling embed data: %w", err)
  565. }
  566. req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(data))
  567. if err != nil {
  568. return nil, fmt.Errorf("error creating embed request: %w", err)
  569. }
  570. req.Header.Set("Content-Type", "application/json")
  571. resp, err := http.DefaultClient.Do(req)
  572. if err != nil {
  573. return nil, fmt.Errorf("POST embedding: %w", err)
  574. }
  575. defer resp.Body.Close()
  576. body, err := io.ReadAll(resp.Body)
  577. if err != nil {
  578. return nil, fmt.Errorf("error reading embed response: %w", err)
  579. }
  580. if resp.StatusCode >= 400 {
  581. log.Printf("llm encode error: %s", body)
  582. return nil, fmt.Errorf("%s", body)
  583. }
  584. var embedding EmbeddingResponse
  585. if err := json.Unmarshal(body, &embedding); err != nil {
  586. return nil, fmt.Errorf("unmarshal tokenize response: %w", err)
  587. }
  588. return embedding.Embedding, nil
  589. }
  590. // Ping checks that the server subprocess is still running and responding to requests
  591. func (llm *llama) Ping(ctx context.Context) error {
  592. resp, err := http.Head(fmt.Sprintf("http://127.0.0.1:%d", llm.Port))
  593. if err != nil {
  594. return fmt.Errorf("ping resp: %w", err)
  595. }
  596. if resp.StatusCode != http.StatusOK {
  597. return fmt.Errorf("unexpected ping status: %s", resp.Status)
  598. }
  599. return nil
  600. }