shim_ext_server.go 3.36 KB
Newer Older
1
2
3
4
5
6
7
//go:build !darwin

package llm

/*

#include <stdlib.h>
8
#include "dynamic_shim.h"
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

*/
import "C"
import (
	"context"
	"fmt"
	"log"
	"path/filepath"
	"sync"
	"unsafe"

	"github.com/jmorganca/ollama/api"
)

type shimExtServer struct {
24
	s       C.struct_dynamic_llama_server
25
26
27
28
29
30
31
32
	options api.Options
}

// Note: current implementation does not support concurrent instantiations
var shimMutex sync.Mutex
var llm *shimExtServer

func (llm *shimExtServer) llama_server_init(sparams *C.ext_server_params_t, err *C.ext_server_resp_t) {
33
	C.dynamic_shim_llama_server_init(llm.s, sparams, err)
34
35
}
func (llm *shimExtServer) llama_server_start() {
36
	C.dynamic_shim_llama_server_start(llm.s)
37
38
}
func (llm *shimExtServer) llama_server_stop() {
39
	C.dynamic_shim_llama_server_stop(llm.s)
40
41
42
}

func (llm *shimExtServer) llama_server_completion(json_req *C.char, resp *C.ext_server_resp_t) {
43
	C.dynamic_shim_llama_server_completion(llm.s, json_req, resp)
44
45
}
func (llm *shimExtServer) llama_server_completion_next_result(task_id C.int, resp *C.ext_server_task_result_t) {
46
	C.dynamic_shim_llama_server_completion_next_result(llm.s, task_id, resp)
47
48
}
func (llm *shimExtServer) llama_server_completion_cancel(task_id C.int, err *C.ext_server_resp_t) {
49
	C.dynamic_shim_llama_server_completion_cancel(llm.s, task_id, err)
50
51
}
func (llm *shimExtServer) llama_server_release_task_result(result *C.ext_server_task_result_t) {
52
	C.dynamic_shim_llama_server_release_task_result(llm.s, result)
53
54
55
}

func (llm *shimExtServer) llama_server_tokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
56
	C.dynamic_shim_llama_server_tokenize(llm.s, json_req, json_resp, err)
57
58
}
func (llm *shimExtServer) llama_server_detokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
59
	C.dynamic_shim_llama_server_detokenize(llm.s, json_req, json_resp, err)
60
61
}
func (llm *shimExtServer) llama_server_embedding(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
62
	C.dynamic_shim_llama_server_embedding(llm.s, json_req, json_resp, err)
63
64
}
func (llm *shimExtServer) llama_server_release_json_resp(json_resp **C.char) {
65
	C.dynamic_shim_llama_server_release_json_resp(llm.s, json_resp)
66
67
}

68
func newDynamicShimExtServer(library, model string, adapters, projectors []string, opts api.Options) (extServer, error) {
69
70
	shimMutex.Lock()
	defer shimMutex.Unlock()
71
	updatePath(filepath.Dir(library))
72
73
74
75
76
77
78
79
	libPath := C.CString(library)
	defer C.free(unsafe.Pointer(libPath))
	resp := newExtServerResp(128)
	defer freeExtServerResp(resp)
	var srv C.struct_dynamic_llama_server
	C.dynamic_shim_init(libPath, &srv, &resp)
	if resp.id < 0 {
		return nil, fmt.Errorf("Unable to load dynamic library: %s", C.GoString(resp.msg))
80
	}
81
82
83
	llm = &shimExtServer{
		s:       srv,
		options: opts,
84
	}
85
	log.Printf("Loading Dynamic Shim llm server: %s", library)
86
	return newExtServer(llm, model, adapters, projectors, opts)
87
88
89
}

func (llm *shimExtServer) Predict(ctx context.Context, pred PredictOpts, fn func(PredictResult)) error {
90
	return predict(ctx, llm, pred, fn)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
}

func (llm *shimExtServer) Encode(ctx context.Context, prompt string) ([]int, error) {
	return encode(llm, ctx, prompt)
}

func (llm *shimExtServer) Decode(ctx context.Context, tokens []int) (string, error) {
	return decode(llm, ctx, tokens)
}

func (llm *shimExtServer) Embedding(ctx context.Context, input string) ([]float64, error) {
	return embedding(llm, ctx, input)
}

func (llm *shimExtServer) Close() {
	close(llm)
}