llama.go 20.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package llm

import (
	"bufio"
	"bytes"
	"context"
	"embed"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"log"
	"math/rand"
	"net/http"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"runtime"
21
	"slices"
22
23
	"strconv"
	"strings"
24
	"sync"
25
26
27
	"time"

	"github.com/jmorganca/ollama/api"
Michael Yang's avatar
Michael Yang committed
28
	"github.com/jmorganca/ollama/format"
29
30
)

Bruce MacDonald's avatar
Bruce MacDonald committed
31
//go:embed llama.cpp/*/build/*/bin/*
32
33
var llamaCppEmbed embed.FS

34
type ModelRunner struct {
35
36
	Path        string // path to the model runner executable
	Accelerated bool
37
}
38

39
func chooseRunners(workDir, runnerType string) []ModelRunner {
40
	buildPath := path.Join("llama.cpp", runnerType, "build")
41
	var runners []ModelRunner
42

43
44
	// set the runners based on the OS
	// IMPORTANT: the order of the runners in the array is the priority order
Bruce MacDonald's avatar
Bruce MacDonald committed
45
46
	switch runtime.GOOS {
	case "darwin":
47
48
49
		runners = []ModelRunner{
			{Path: path.Join(buildPath, "metal", "bin", "ollama-runner")},
			{Path: path.Join(buildPath, "cpu", "bin", "ollama-runner")},
50
		}
51
	case "linux":
52
53
54
		runners = []ModelRunner{
			{Path: path.Join(buildPath, "cuda", "bin", "ollama-runner"), Accelerated: true},
			{Path: path.Join(buildPath, "cpu", "bin", "ollama-runner")},
55
56
57
		}
	case "windows":
		// TODO: select windows GPU runner here when available
58
59
		runners = []ModelRunner{
			{Path: path.Join(buildPath, "cpu", "bin", "Release", "ollama-runner.exe")},
60
		}
61
62
	default:
		log.Printf("unknown OS, running on CPU: %s", runtime.GOOS)
63
64
		runners = []ModelRunner{
			{Path: path.Join(buildPath, "cpu", "bin", "ollama-runner")},
65
		}
Bruce MacDonald's avatar
Bruce MacDonald committed
66
	}
67

68
69
70
	runnerAvailable := false // if no runner files are found in the embed, this flag will cause a fast fail
	for _, r := range runners {
		// find all the files in the runner's bin directory
71
		files, err := fs.Glob(llamaCppEmbed, path.Join(path.Dir(r.Path), "*"))
Bruce MacDonald's avatar
Bruce MacDonald committed
72
		if err != nil {
73
			// this is expected, ollama may be compiled without all runners packed in
Michael Yang's avatar
Michael Yang committed
74
			log.Printf("%s runner not found: %v", r.Path, err)
75
			continue
Bruce MacDonald's avatar
Bruce MacDonald committed
76
		}
77

78
		for _, f := range files {
Bruce MacDonald's avatar
Bruce MacDonald committed
79
80
			runnerAvailable = true

81
82
83
84
85
86
			srcFile, err := llamaCppEmbed.Open(f)
			if err != nil {
				log.Fatalf("read llama runner %s: %v", f, err)
			}
			defer srcFile.Close()

Bruce MacDonald's avatar
Bruce MacDonald committed
87
			// create the directory in case it does not exist, filepath.Dir() converts the file path to the OS's format
88
			destPath := filepath.Join(workDir, filepath.Dir(f))
89
90
91
			if err := os.MkdirAll(destPath, 0o755); err != nil {
				log.Fatalf("create runner temp dir %s: %v", filepath.Dir(f), err)
			}
92

Bruce MacDonald's avatar
Bruce MacDonald committed
93
			// create the path to the destination file, filepath.Base() converts the file path to the OS's format
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
			destFile := filepath.Join(destPath, filepath.Base(f))

			_, err = os.Stat(destFile)
			switch {
			case errors.Is(err, os.ErrNotExist):
				destFile, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
				if err != nil {
					log.Fatalf("write llama runner %s: %v", f, err)
				}
				defer destFile.Close()

				if _, err := io.Copy(destFile, srcFile); err != nil {
					log.Fatalf("copy llama runner %s: %v", f, err)
				}
			case err != nil:
				log.Fatalf("stat llama runner %s: %v", f, err)
110
			}
111
		}
Bruce MacDonald's avatar
Bruce MacDonald committed
112
	}
113
114
115
	if !runnerAvailable {
		log.Fatalf("%s runner not found", runnerType)
	}
116

117
118
119
	// return the runners to try in priority order
	localRunnersByPriority := []ModelRunner{}
	for _, r := range runners {
Bruce MacDonald's avatar
Bruce MacDonald committed
120
		// clean the ModelRunner paths so that they match the OS we are running on
121
122
123
124
		localRunnersByPriority = append(localRunnersByPriority, ModelRunner{
			Path:        filepath.Clean(path.Join(workDir, r.Path)),
			Accelerated: r.Accelerated,
		})
Bruce MacDonald's avatar
Bruce MacDonald committed
125
	}
126

127
	return localRunnersByPriority
128
129
130
131
132
133
}

type llamaModel struct {
	hyperparameters llamaHyperparameters
}

Michael Yang's avatar
Michael Yang committed
134
135
func (llm *llamaModel) ModelFamily() string {
	return "llama"
136
137
}

Michael Yang's avatar
Michael Yang committed
138
139
func llamaModelType(numLayer uint32) string {
	switch numLayer {
140
	case 26:
Michael Yang's avatar
Michael Yang committed
141
		return "3B"
142
	case 32:
Michael Yang's avatar
Michael Yang committed
143
		return "7B"
144
	case 40:
Michael Yang's avatar
Michael Yang committed
145
		return "13B"
146
	case 48:
Michael Yang's avatar
Michael Yang committed
147
		return "34B"
148
	case 60:
Michael Yang's avatar
Michael Yang committed
149
		return "30B"
150
	case 80:
Michael Yang's avatar
Michael Yang committed
151
152
		return "65B"
	default:
Michael Yang's avatar
Michael Yang committed
153
		return "unknown"
154
	}
Michael Yang's avatar
Michael Yang committed
155
}
156

Michael Yang's avatar
Michael Yang committed
157
158
func (llm *llamaModel) ModelType() string {
	return llamaModelType(llm.hyperparameters.NumLayer)
159
160
}

Michael Yang's avatar
Michael Yang committed
161
162
func (llm *llamaModel) FileType() string {
	return fileType(llm.hyperparameters.FileType)
163
164
}

165
166
167
168
func (llm *llamaModel) NumLayers() int64 {
	return int64(llm.hyperparameters.NumLayer)
}

169
170
171
172
173
174
175
176
177
178
179
180
181
182
type llamaHyperparameters struct {
	// NumVocab is the size of the model's vocabulary.
	NumVocab uint32

	// NumEmbd is the size of the model's embedding layer.
	NumEmbd uint32
	NumMult uint32
	NumHead uint32

	// NumLayer is the number of layers in the model.
	NumLayer uint32
	NumRot   uint32

	// FileType describes the quantization level of the model, e.g. Q4_0, Q5_K, etc.
Michael Yang's avatar
Michael Yang committed
183
	FileType uint32
184
185
186
}

type Running struct {
187
188
189
190
191
192
	Port          int
	Cmd           *exec.Cmd
	Cancel        context.CancelFunc
	exitOnce      sync.Once
	exitCh        chan error // channel to receive the exit status of the subprocess
	*StatusWriter            // captures error messages from the llama runner process
193
194
195
196
197
198
199
}

type llama struct {
	api.Options
	Running
}

200
201
var errNoGPU = errors.New("nvidia-smi command failed")

Michael Yang's avatar
Michael Yang committed
202
// CheckVRAM returns the free VRAM in bytes on Linux machines with NVIDIA GPUs
Michael Yang's avatar
Michael Yang committed
203
func CheckVRAM() (int64, error) {
204
	cmd := exec.Command("nvidia-smi", "--query-gpu=memory.free", "--format=csv,noheader,nounits")
205
206
207
208
209
210
211
	var stdout bytes.Buffer
	cmd.Stdout = &stdout
	err := cmd.Run()
	if err != nil {
		return 0, errNoGPU
	}

Michael Yang's avatar
Michael Yang committed
212
	var freeMiB int64
213
214
215
	scanner := bufio.NewScanner(&stdout)
	for scanner.Scan() {
		line := scanner.Text()
Michael Yang's avatar
Michael Yang committed
216
		vram, err := strconv.ParseInt(strings.TrimSpace(line), 10, 64)
217
218
219
220
		if err != nil {
			return 0, fmt.Errorf("failed to parse available VRAM: %v", err)
		}

Michael Yang's avatar
Michael Yang committed
221
		freeMiB += vram
222
223
	}

Michael Yang's avatar
Michael Yang committed
224
225
	freeBytes := freeMiB * 1024 * 1024
	if freeBytes < 2*format.GigaByte {
Michael Yang's avatar
Michael Yang committed
226
		log.Printf("less than 2 GB VRAM available, falling back to CPU only")
Michael Yang's avatar
Michael Yang committed
227
		freeMiB = 0
Michael Yang's avatar
Michael Yang committed
228
229
	}

Michael Yang's avatar
Michael Yang committed
230
	return freeBytes, nil
231
232
}

233
func NumGPU(numLayer, fileSizeBytes int64, opts api.Options) int {
234
235
236
237
	if opts.NumGPU != -1 {
		return opts.NumGPU
	}
	if runtime.GOOS == "linux" {
Michael Yang's avatar
Michael Yang committed
238
		freeBytes, err := CheckVRAM()
239
240
241
242
243
244
245
		if err != nil {
			if err.Error() != "nvidia-smi command failed" {
				log.Print(err.Error())
			}
			// nvidia driver not installed or no nvidia GPU found
			return 0
		}
246
247
248
249
250

		// Calculate bytes per layer
		// TODO: this is a rough heuristic, better would be to calculate this based on number of layers and context size
		bytesPerLayer := fileSizeBytes / numLayer

251
		// max number of layers we can fit in VRAM, subtract 8% to prevent consuming all available VRAM and running out of memory
Michael Yang's avatar
Michael Yang committed
252
		layers := int(freeBytes/bytesPerLayer) * 92 / 100
253
		log.Printf("%d MB VRAM available, loading up to %d GPU layers", freeBytes/(1024*1024), layers)
254

255
		return layers
256
	}
257
258
	// default to enable metal on macOS
	return 1
259
260
}

261
262
// StatusWriter is a writer that captures error messages from the llama runner process
type StatusWriter struct {
263
264
	ErrCh      chan error
	LastErrMsg string
265
266
267
268
269
270
271
272
273
}

func NewStatusWriter() *StatusWriter {
	return &StatusWriter{
		ErrCh: make(chan error, 1),
	}
}

func (w *StatusWriter) Write(b []byte) (int, error) {
274
	var errMsg string
275
	if _, after, ok := bytes.Cut(b, []byte("error:")); ok {
276
277
278
		errMsg = string(bytes.TrimSpace(after))
	} else if _, after, ok := bytes.Cut(b, []byte("CUDA error")); ok {
		errMsg = string(bytes.TrimSpace(after))
279
	}
280
281
282
283
284
285

	if errMsg != "" {
		w.LastErrMsg = errMsg
		w.ErrCh <- fmt.Errorf("llama runner: %s", errMsg)
	}

286
287
288
	return os.Stderr.Write(b)
}

289
func newLlama(model string, adapters []string, runners []ModelRunner, ggml *GGML, opts api.Options) (*llama, error) {
290
291
	fileInfo, err := os.Stat(model)
	if err != nil {
292
293
294
295
296
297
298
		return nil, err
	}

	if len(adapters) > 1 {
		return nil, errors.New("ollama supports only one lora adapter, but multiple were provided")
	}

299
	numGPU := NumGPU(ggml.NumLayers(), fileInfo.Size(), opts)
300
301
302
303
304
305
	params := []string{
		"--model", model,
		"--ctx-size", fmt.Sprintf("%d", opts.NumCtx),
		"--rope-freq-base", fmt.Sprintf("%f", opts.RopeFrequencyBase),
		"--rope-freq-scale", fmt.Sprintf("%f", opts.RopeFrequencyScale),
		"--batch-size", fmt.Sprintf("%d", opts.NumBatch),
306
		"--n-gpu-layers", fmt.Sprintf("%d", numGPU),
307
308
309
		"--embedding",
	}

Bruce MacDonald's avatar
Bruce MacDonald committed
310
311
312
313
	if opts.NumGQA > 0 {
		params = append(params, "--gqa", fmt.Sprintf("%d", opts.NumGQA))
	}

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
	if len(adapters) > 0 {
		// TODO: applying multiple adapters is not supported by the llama.cpp server yet
		params = append(params, "--lora", adapters[0])
	}

	if opts.NumThread > 0 {
		params = append(params, "--threads", fmt.Sprintf("%d", opts.NumThread))
	}

	if !opts.F16KV {
		params = append(params, "--memory-f32")
	}
	if opts.UseMLock {
		params = append(params, "--mlock")
	}
	if !opts.UseMMap {
		params = append(params, "--no-mmap")
	}
	if opts.UseNUMA {
		params = append(params, "--numa")
	}

336
337
	var runnerErr error

338
	// start the llama.cpp server with a retry in case the port is already in use
339
	for _, runner := range runners {
340
341
342
343
344
		if runner.Accelerated && numGPU == 0 {
			log.Printf("skipping accelerated runner because num_gpu=0")
			continue
		}

345
346
347
348
349
		if _, err := os.Stat(runner.Path); err != nil {
			log.Printf("llama runner not found: %v", err)
			continue
		}

350
351
352
353
354
355
356
		port := rand.Intn(65535-49152) + 49152 // get a random port in the ephemeral range
		ctx, cancel := context.WithCancel(context.Background())
		cmd := exec.CommandContext(
			ctx,
			runner.Path,
			append(params, "--port", strconv.Itoa(port))...,
		)
Bruce MacDonald's avatar
Bruce MacDonald committed
357
		cmd.Env = append(os.Environ(), fmt.Sprintf("LD_LIBRARY_PATH=%s", filepath.Dir(runner.Path)))
358
		cmd.Stdout = os.Stderr
359
360
		statusWriter := NewStatusWriter()
		cmd.Stderr = statusWriter
361

362
		llm := &llama{Options: opts, Running: Running{Port: port, Cmd: cmd, Cancel: cancel, exitCh: make(chan error)}}
363

364
		log.Print("starting llama runner")
Bruce MacDonald's avatar
Bruce MacDonald committed
365
		if err := llm.Cmd.Start(); err != nil {
366
			log.Printf("error starting the external llama runner: %v", err)
Bruce MacDonald's avatar
Bruce MacDonald committed
367
368
369
			continue
		}

370
		// monitor the llama runner process and signal when it exits
371
		go func() {
372
			err := llm.Cmd.Wait()
373
374
375
376
377
378
379
			// default to printing the exit message of the command process, it will probably just say 'exit staus 1'
			errMsg := err.Error()
			// try to set a better error message if llama runner logs captured an error
			if statusWriter.LastErrMsg != "" {
				errMsg = statusWriter.LastErrMsg
			}
			log.Println(errMsg)
380
381
382
383
			// llm.Cmd.Wait() can only be called once, use this exit channel to signal that the process has exited
			llm.exitOnce.Do(func() {
				close(llm.exitCh)
			})
384
385
		}()

386
		if err := waitForServer(llm); err != nil {
387
			log.Printf("error starting llama runner: %v", err)
388
			llm.Close()
389
390
391
392
393
394
395
396
397
398
399

			// default the runnerErr to the error returned by the most recent llama runner process
			runnerErr = err

			// capture the error directly from the runner process, if any
			select {
			case runnerErr = <-statusWriter.ErrCh:
			default:
				// the runner process probably timed out
			}

400
401
402
			// try again
			continue
		}
Bruce MacDonald's avatar
Bruce MacDonald committed
403

404
405
406
407
		// server started successfully
		return llm, nil
	}

408
409
	if runnerErr != nil {
		// this is the error returned from the llama runner process that failed most recently
410
411
412
413
414
415
416

		// falcon and starcoder model families are not compatible with older versions of llama.cpp
		families := []string{"falcon", "starcoder"}
		if strings.Contains(runnerErr.Error(), "failed to load model") && slices.Contains(families, ggml.ModelFamily()) {
			return nil, fmt.Errorf("%v: %s", runnerErr, "this model may be incompatible with your version of Ollama. Please run `ollama pull` to get the latest version of this model.")
		}

417
418
419
		return nil, runnerErr
	}

420
	return nil, fmt.Errorf("failed to start a llama runner")
421
422
423
424
}

func waitForServer(llm *llama) error {
	start := time.Now()
425
	expiresAt := time.Now().Add(3 * time.Minute) // be generous with timeout, large models can take a while to load
Bruce MacDonald's avatar
Bruce MacDonald committed
426
	ticker := time.NewTicker(200 * time.Millisecond)
427
	defer ticker.Stop()
428

429
	log.Print("waiting for llama runner to start responding")
430
431
432
433
	for {
		select {
		case <-llm.exitCh:
			// failed to start subprocess
434
			return fmt.Errorf("llama runner process has terminated")
435
436
437
		case <-ticker.C:
			if time.Now().After(expiresAt) {
				// timeout
438
				return fmt.Errorf("timed out waiting for llama runner to start")
439
			}
440

441
442
443
444
445
			if err := llm.Ping(context.Background()); err == nil {
				// success
				log.Printf("llama runner started in %f seconds", time.Since(start).Seconds())
				return nil
			}
446
447
448
449
450
		}
	}
}

func (llm *llama) Close() {
451
	// signal the sub-process to terminate
Bruce MacDonald's avatar
Bruce MacDonald committed
452
	llm.Cancel()
453
454

	// wait for the command to exit to prevent race conditions with the next run
455
456
	<-llm.exitCh

457
458
	if llm.StatusWriter != nil && llm.StatusWriter.LastErrMsg != "" {
		log.Printf("llama runner stopped with error: %v", llm.StatusWriter.LastErrMsg)
459
460
	} else {
		log.Print("llama runner stopped successfully")
461
	}
462
463
464
465
466
467
}

func (llm *llama) SetOptions(opts api.Options) {
	llm.Options = opts
}

Michael Yang's avatar
Michael Yang committed
468
type prediction struct {
Michael Yang's avatar
Michael Yang committed
469
470
471
472
473
	Content string `json:"content"`
	Model   string `json:"model"`
	Prompt  string `json:"prompt"`
	Stop    bool   `json:"stop"`

Michael Yang's avatar
Michael Yang committed
474
475
476
477
478
479
	Timings struct {
		PredictedN  int     `json:"predicted_n"`
		PredictedMS float64 `json:"predicted_ms"`
		PromptN     int     `json:"prompt_n"`
		PromptMS    float64 `json:"prompt_ms"`
	}
480
481
}

Michael Yang's avatar
Michael Yang committed
482
const maxBufferSize = 512 * format.KiloByte
483

484
485
func (llm *llama) Predict(ctx context.Context, prevContext []int, prompt string, fn func(api.GenerateResponse)) error {
	prevConvo, err := llm.Decode(ctx, prevContext)
486
	if err != nil {
487
		return err
488
	}
489

490
	// Remove leading spaces from prevConvo if present
491
	prevConvo = strings.TrimPrefix(prevConvo, " ")
492

493
494
495
496
	var nextContext strings.Builder
	nextContext.WriteString(prevConvo)
	nextContext.WriteString(prompt)

Michael Yang's avatar
Michael Yang committed
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
	request := map[string]any{
		"prompt":            nextContext.String(),
		"stream":            true,
		"n_predict":         llm.NumPredict,
		"n_keep":            llm.NumKeep,
		"temperature":       llm.Temperature,
		"top_k":             llm.TopK,
		"top_p":             llm.TopP,
		"tfs_z":             llm.TFSZ,
		"typical_p":         llm.TypicalP,
		"repeat_last_n":     llm.RepeatLastN,
		"repeat_penalty":    llm.RepeatPenalty,
		"presence_penalty":  llm.PresencePenalty,
		"frequency_penalty": llm.FrequencyPenalty,
		"mirostat":          llm.Mirostat,
		"mirostat_tau":      llm.MirostatTau,
		"mirostat_eta":      llm.MirostatEta,
		"penalize_nl":       llm.PenalizeNewline,
		"seed":              llm.Seed,
		"stop":              llm.Stop,
517
	}
518

519
	// Handling JSON marshaling with special characters unescaped.
520
521
	buffer := &bytes.Buffer{}
	enc := json.NewEncoder(buffer)
522
523
	enc.SetEscapeHTML(false)

Michael Yang's avatar
Michael Yang committed
524
	if err := enc.Encode(request); err != nil {
525
		return fmt.Errorf("failed to marshal data: %v", err)
526
527
	}

Michael Yang's avatar
Michael Yang committed
528
	endpoint := fmt.Sprintf("http://127.0.0.1:%d/completion", llm.Port)
529
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, buffer)
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
	if err != nil {
		return fmt.Errorf("error creating POST request: %v", err)
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return fmt.Errorf("POST predict: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode >= 400 {
		bodyBytes, err := io.ReadAll(resp.Body)
		if err != nil {
			return fmt.Errorf("failed reading llm error response: %w", err)
		}
		log.Printf("llm predict error: %s", bodyBytes)
		return fmt.Errorf("%s", bodyBytes)
	}

	scanner := bufio.NewScanner(resp.Body)
551
552
553
	// increase the buffer size to avoid running out of space
	buf := make([]byte, 0, maxBufferSize)
	scanner.Buffer(buf, maxBufferSize)
554
555
556
557
558
559
	for scanner.Scan() {
		select {
		case <-ctx.Done():
			// This handles the request cancellation
			return ctx.Err()
		default:
Michael Yang's avatar
Michael Yang committed
560
561
			line := scanner.Bytes()
			if len(line) == 0 {
562
563
564
				continue
			}

Michael Yang's avatar
Michael Yang committed
565
			if evt, ok := bytes.CutPrefix(line, []byte("data: ")); ok {
Michael Yang's avatar
Michael Yang committed
566
				var p prediction
Michael Yang's avatar
Michael Yang committed
567
				if err := json.Unmarshal(evt, &p); err != nil {
Michael Yang's avatar
Michael Yang committed
568
					return fmt.Errorf("error unmarshaling llm prediction response: %v", err)
569
570
				}

Michael Yang's avatar
Michael Yang committed
571
572
573
574
				if p.Content != "" {
					fn(api.GenerateResponse{Response: p.Content})
					nextContext.WriteString(p.Content)
				}
Michael Yang's avatar
Michael Yang committed
575
576

				if p.Stop {
577
					embd, err := llm.Encode(ctx, nextContext.String())
578
579
580
					if err != nil {
						return fmt.Errorf("encoding context: %v", err)
					}
581

582
583
584
					fn(api.GenerateResponse{
						Done:               true,
						Context:            embd,
Michael Yang's avatar
Michael Yang committed
585
586
587
588
						PromptEvalCount:    p.Timings.PromptN,
						PromptEvalDuration: parseDurationMs(p.Timings.PromptMS),
						EvalCount:          p.Timings.PredictedN,
						EvalDuration:       parseDurationMs(p.Timings.PredictedMS),
589
590
					})

Michael Yang's avatar
Michael Yang committed
591
					return nil
592
593
594
595
596
597
				}
			}
		}
	}

	if err := scanner.Err(); err != nil {
598
599
600
601
602
603
604
605
		if strings.Contains(err.Error(), "unexpected EOF") {
			// this means the llama runner subprocess crashed
			llm.Close()
			if llm.StatusWriter != nil && llm.StatusWriter.LastErrMsg != "" {
				return fmt.Errorf("llama runner exited: %v", llm.StatusWriter.LastErrMsg)
			}
			return fmt.Errorf("llama runner exited, you may not have enough available memory to run this model")
		}
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
		return fmt.Errorf("error reading llm response: %v", err)
	}

	return nil
}

type TokenizeRequest struct {
	Content string `json:"content"`
}

type TokenizeResponse struct {
	Tokens []int `json:"tokens"`
}

func (llm *llama) Encode(ctx context.Context, prompt string) ([]int, error) {
	endpoint := fmt.Sprintf("http://127.0.0.1:%d/tokenize", llm.Port)
	data, err := json.Marshal(TokenizeRequest{Content: prompt})
	if err != nil {
		return nil, fmt.Errorf("marshaling encode data: %w", err)
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(data))
	if err != nil {
		return nil, fmt.Errorf("encode request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("do encode request: %w", err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("read encode request: %w", err)
	}

	if resp.StatusCode >= 400 {
		log.Printf("llm encode error: %s", body)
		return nil, fmt.Errorf("%s", body)
	}

	var encoded TokenizeResponse
	if err := json.Unmarshal(body, &encoded); err != nil {
		return nil, fmt.Errorf("unmarshal encode response: %w", err)
	}

	return encoded.Tokens, nil
}

type DetokenizeRequest struct {
	Tokens []int `json:"tokens"`
}

type DetokenizeResponse struct {
	Content string `json:"content"`
}

func (llm *llama) Decode(ctx context.Context, tokens []int) (string, error) {
	if len(tokens) == 0 {
		return "", nil
	}
	endpoint := fmt.Sprintf("http://127.0.0.1:%d/detokenize", llm.Port)
	data, err := json.Marshal(DetokenizeRequest{Tokens: tokens})
	if err != nil {
		return "", fmt.Errorf("marshaling decode data: %w", err)
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(data))
	if err != nil {
		return "", fmt.Errorf("decode request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", fmt.Errorf("do decode request: %w", err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("read decode request: %w", err)
	}

	if resp.StatusCode >= 400 {
		log.Printf("llm decode error: %s", body)
		return "", fmt.Errorf("%s", body)
	}

	var decoded DetokenizeResponse
	if err := json.Unmarshal(body, &decoded); err != nil {
		return "", fmt.Errorf("unmarshal encode response: %w", err)
	}

	return decoded.Content, nil
}

type EmbeddingRequest struct {
	Content string `json:"content"`
}

type EmbeddingResponse struct {
	Embedding []float64 `json:"embedding"`
}

func (llm *llama) Embedding(ctx context.Context, input string) ([]float64, error) {
	endpoint := fmt.Sprintf("http://127.0.0.1:%d/embedding", llm.Port)
	data, err := json.Marshal(TokenizeRequest{Content: input})
	if err != nil {
		return nil, fmt.Errorf("error marshaling embed data: %w", err)
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(data))
	if err != nil {
		return nil, fmt.Errorf("error creating embed request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("POST embedding: %w", err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("error reading embed response: %w", err)
	}

	if resp.StatusCode >= 400 {
		log.Printf("llm encode error: %s", body)
		return nil, fmt.Errorf("%s", body)
	}

	var embedding EmbeddingResponse
	if err := json.Unmarshal(body, &embedding); err != nil {
		return nil, fmt.Errorf("unmarshal tokenize response: %w", err)
	}

	return embedding.Embedding, nil
}

// Ping checks that the server subprocess is still running and responding to requests
func (llm *llama) Ping(ctx context.Context) error {
Bruce MacDonald's avatar
Bruce MacDonald committed
752
	resp, err := http.Head(fmt.Sprintf("http://127.0.0.1:%d", llm.Port))
753
754
755
756
757
758
759
760
	if err != nil {
		return fmt.Errorf("ping resp: %w", err)
	}
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("unexpected ping status: %s", resp.Status)
	}
	return nil
}