ext_server_default.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //go:build !windows
  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. type llamaExtServer struct {
  13. api.Options
  14. }
  15. func (llm *llamaExtServer) llama_server_init(sparams *C.ext_server_params_t, err *C.ext_server_resp_t) {
  16. C.llama_server_init(sparams, err)
  17. }
  18. func (llm *llamaExtServer) llama_server_start() {
  19. C.llama_server_start()
  20. }
  21. func (llm *llamaExtServer) llama_server_stop() {
  22. C.llama_server_stop()
  23. }
  24. func (llm *llamaExtServer) llama_server_completion(json_req *C.char, resp *C.ext_server_resp_t) {
  25. C.llama_server_completion(json_req, resp)
  26. }
  27. func (llm *llamaExtServer) llama_server_completion_next_result(task_id C.int, resp *C.ext_server_task_result_t) {
  28. C.llama_server_completion_next_result(task_id, resp)
  29. }
  30. func (llm *llamaExtServer) llama_server_completion_cancel(task_id C.int, err *C.ext_server_resp_t) {
  31. C.llama_server_completion_cancel(task_id, err)
  32. }
  33. func (llm *llamaExtServer) llama_server_release_task_result(result *C.ext_server_task_result_t) {
  34. C.llama_server_release_task_result(result)
  35. }
  36. func (llm *llamaExtServer) llama_server_tokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  37. C.llama_server_tokenize(json_req, json_resp, err)
  38. }
  39. func (llm *llamaExtServer) llama_server_detokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  40. C.llama_server_detokenize(json_req, json_resp, err)
  41. }
  42. func (llm *llamaExtServer) llama_server_embedding(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  43. C.llama_server_embedding(json_req, json_resp, err)
  44. }
  45. func (llm *llamaExtServer) llama_server_release_json_resp(json_resp **C.char) {
  46. C.llama_server_release_json_resp(json_resp)
  47. }
  48. func newDefaultExtServer(model string, adapters, projectors []string, numLayers int64, opts api.Options) (extServer, error) {
  49. server := &llamaExtServer{opts}
  50. return newExtServer(server, model, adapters, projectors, numLayers, opts)
  51. }
  52. func (llm *llamaExtServer) Predict(ctx context.Context, pred PredictOpts, fn func(PredictResult)) error {
  53. return predict(llm, llm.Options, ctx, pred, fn)
  54. }
  55. func (llm *llamaExtServer) Encode(ctx context.Context, prompt string) ([]int, error) {
  56. return encode(llm, ctx, prompt)
  57. }
  58. func (llm *llamaExtServer) Decode(ctx context.Context, tokens []int) (string, error) {
  59. return decode(llm, ctx, tokens)
  60. }
  61. func (llm *llamaExtServer) Embedding(ctx context.Context, input string) ([]float64, error) {
  62. return embedding(llm, ctx, input)
  63. }
  64. func (llm *llamaExtServer) Close() {
  65. close(llm)
  66. }