shim_ext_server.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //go:build !darwin
  2. package llm
  3. /*
  4. #include <stdlib.h>
  5. #include "dynamic_shim.h"
  6. */
  7. import "C"
  8. import (
  9. "context"
  10. "fmt"
  11. "log"
  12. "path/filepath"
  13. "sync"
  14. "unsafe"
  15. "github.com/jmorganca/ollama/api"
  16. )
  17. type shimExtServer struct {
  18. s C.struct_dynamic_llama_server
  19. options api.Options
  20. }
  21. // Note: current implementation does not support concurrent instantiations
  22. var shimMutex sync.Mutex
  23. var llm *shimExtServer
  24. func (llm *shimExtServer) llama_server_init(sparams *C.ext_server_params_t, err *C.ext_server_resp_t) {
  25. C.dynamic_shim_llama_server_init(llm.s, sparams, err)
  26. }
  27. func (llm *shimExtServer) llama_server_start() {
  28. C.dynamic_shim_llama_server_start(llm.s)
  29. }
  30. func (llm *shimExtServer) llama_server_stop() {
  31. C.dynamic_shim_llama_server_stop(llm.s)
  32. }
  33. func (llm *shimExtServer) llama_server_completion(json_req *C.char, resp *C.ext_server_resp_t) {
  34. C.dynamic_shim_llama_server_completion(llm.s, json_req, resp)
  35. }
  36. func (llm *shimExtServer) llama_server_completion_next_result(task_id C.int, resp *C.ext_server_task_result_t) {
  37. C.dynamic_shim_llama_server_completion_next_result(llm.s, task_id, resp)
  38. }
  39. func (llm *shimExtServer) llama_server_completion_cancel(task_id C.int, err *C.ext_server_resp_t) {
  40. C.dynamic_shim_llama_server_completion_cancel(llm.s, task_id, err)
  41. }
  42. func (llm *shimExtServer) llama_server_release_task_result(result *C.ext_server_task_result_t) {
  43. C.dynamic_shim_llama_server_release_task_result(llm.s, result)
  44. }
  45. func (llm *shimExtServer) llama_server_tokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  46. C.dynamic_shim_llama_server_tokenize(llm.s, json_req, json_resp, err)
  47. }
  48. func (llm *shimExtServer) llama_server_detokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  49. C.dynamic_shim_llama_server_detokenize(llm.s, json_req, json_resp, err)
  50. }
  51. func (llm *shimExtServer) llama_server_embedding(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  52. C.dynamic_shim_llama_server_embedding(llm.s, json_req, json_resp, err)
  53. }
  54. func (llm *shimExtServer) llama_server_release_json_resp(json_resp **C.char) {
  55. C.dynamic_shim_llama_server_release_json_resp(llm.s, json_resp)
  56. }
  57. func newDynamicShimExtServer(library, model string, adapters, projectors []string, opts api.Options) (extServer, error) {
  58. shimMutex.Lock()
  59. defer shimMutex.Unlock()
  60. updatePath(filepath.Dir(library))
  61. libPath := C.CString(library)
  62. defer C.free(unsafe.Pointer(libPath))
  63. resp := newExtServerResp(128)
  64. defer freeExtServerResp(resp)
  65. var srv C.struct_dynamic_llama_server
  66. C.dynamic_shim_init(libPath, &srv, &resp)
  67. if resp.id < 0 {
  68. return nil, fmt.Errorf("Unable to load dynamic library: %s", C.GoString(resp.msg))
  69. }
  70. llm = &shimExtServer{
  71. s: srv,
  72. options: opts,
  73. }
  74. log.Printf("Loading Dynamic Shim llm server: %s", library)
  75. return newExtServer(llm, model, adapters, projectors, opts)
  76. }
  77. func (llm *shimExtServer) Predict(ctx context.Context, pred PredictOpts, fn func(PredictResult)) error {
  78. return predict(ctx, llm, pred, fn)
  79. }
  80. func (llm *shimExtServer) Encode(ctx context.Context, prompt string) ([]int, error) {
  81. return encode(llm, ctx, prompt)
  82. }
  83. func (llm *shimExtServer) Decode(ctx context.Context, tokens []int) (string, error) {
  84. return decode(llm, ctx, tokens)
  85. }
  86. func (llm *shimExtServer) Embedding(ctx context.Context, input string) ([]float64, error) {
  87. return embedding(llm, ctx, input)
  88. }
  89. func (llm *shimExtServer) Close() {
  90. close(llm)
  91. }