ext_server_default.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //go:build darwin
  2. package llm
  3. /*
  4. #include <stdlib.h>
  5. #include "ext_server.h"
  6. */
  7. import "C"
  8. import (
  9. "context"
  10. "github.com/jmorganca/ollama/api"
  11. )
  12. // TODO - explore shifting Darwin to a dynamic loading pattern for consistency with Linux and Windows
  13. type llamaExtServer struct {
  14. api.Options
  15. }
  16. func (llm *llamaExtServer) llama_server_init(sparams *C.ext_server_params_t, err *C.ext_server_resp_t) {
  17. C.llama_server_init(sparams, err)
  18. }
  19. func (llm *llamaExtServer) llama_server_start() {
  20. C.llama_server_start()
  21. }
  22. func (llm *llamaExtServer) llama_server_stop() {
  23. C.llama_server_stop()
  24. }
  25. func (llm *llamaExtServer) llama_server_completion(json_req *C.char, resp *C.ext_server_resp_t) {
  26. C.llama_server_completion(json_req, resp)
  27. }
  28. func (llm *llamaExtServer) llama_server_completion_next_result(task_id C.int, resp *C.ext_server_task_result_t) {
  29. C.llama_server_completion_next_result(task_id, resp)
  30. }
  31. func (llm *llamaExtServer) llama_server_completion_cancel(task_id C.int, err *C.ext_server_resp_t) {
  32. C.llama_server_completion_cancel(task_id, err)
  33. }
  34. func (llm *llamaExtServer) llama_server_release_task_result(result *C.ext_server_task_result_t) {
  35. C.llama_server_release_task_result(result)
  36. }
  37. func (llm *llamaExtServer) llama_server_tokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  38. C.llama_server_tokenize(json_req, json_resp, err)
  39. }
  40. func (llm *llamaExtServer) llama_server_detokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  41. C.llama_server_detokenize(json_req, json_resp, err)
  42. }
  43. func (llm *llamaExtServer) llama_server_embedding(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  44. C.llama_server_embedding(json_req, json_resp, err)
  45. }
  46. func (llm *llamaExtServer) llama_server_release_json_resp(json_resp **C.char) {
  47. C.llama_server_release_json_resp(json_resp)
  48. }
  49. func newDefaultExtServer(model string, adapters, projectors []string, opts api.Options) (extServer, error) {
  50. server := &llamaExtServer{opts}
  51. return newExtServer(server, model, adapters, projectors, opts)
  52. }
  53. func (llm *llamaExtServer) Predict(ctx context.Context, pred PredictOpts, fn func(PredictResult)) error {
  54. return predict(ctx, llm, pred, fn)
  55. }
  56. func (llm *llamaExtServer) Encode(ctx context.Context, prompt string) ([]int, error) {
  57. return encode(llm, ctx, prompt)
  58. }
  59. func (llm *llamaExtServer) Decode(ctx context.Context, tokens []int) (string, error) {
  60. return decode(llm, ctx, tokens)
  61. }
  62. func (llm *llamaExtServer) Embedding(ctx context.Context, input string) ([]float64, error) {
  63. return embedding(llm, ctx, input)
  64. }
  65. func (llm *llamaExtServer) Close() {
  66. close(llm)
  67. }