137 lines
3.5 KiB
Go
137 lines
3.5 KiB
Go
package ollama
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type CreateModelRequest struct {
|
|
Model string `json:"model"`
|
|
From string `json:"from,omitempty"`
|
|
Template string `json:"template,omitempty"`
|
|
License string `json:"license,omitempty"`
|
|
System string `json:"system,omitempty"`
|
|
Parameters map[string]any `json:"parameters,omitempty"`
|
|
Messages []CreateModelRequestMessage `json:"messages,omitempty"`
|
|
Quantize string `json:"quantize,omitempty"`
|
|
Stream *bool `json:"stream,omitempty"`
|
|
}
|
|
|
|
type CreateModelRequestMessage struct {
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
Images []string `json:"images,omitempty"`
|
|
ToolCalls []CreateModelRequestMessageToolCall `json:"tool_calls,omitempty"`
|
|
}
|
|
|
|
type CreateModelRequestMessageToolCall struct {
|
|
Function *CreateModelRequestMessageToolCallFunction `json:"function,omitempty"`
|
|
}
|
|
|
|
type CreateModelRequestMessageToolCallFunction struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description,omitempty"`
|
|
Arguments map[string]any `json:"arguments,omitempty"`
|
|
}
|
|
|
|
type CreateModelResponse struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
type CreateModelResponseStream struct {
|
|
Status string `json:"status"`
|
|
Digest string `json:"digest"`
|
|
Total int `json:"total"`
|
|
Completed int `json:"completed"`
|
|
}
|
|
|
|
func (o Ollama) CreateModel(reqBody CreateModelRequest) (CreateModelResponse, error) {
|
|
reqBody.Stream = PtrOf(false)
|
|
|
|
reqBodyBytes, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return CreateModelResponse{}, err
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/create", o.baseUrl), bytes.NewReader(reqBodyBytes))
|
|
if err != nil {
|
|
return CreateModelResponse{}, err
|
|
}
|
|
|
|
for key, val := range o.customHeaders {
|
|
req.Header.Set(key, val)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return CreateModelResponse{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return CreateModelResponse{}, errors.New("status code is not 200")
|
|
}
|
|
|
|
var respBody CreateModelResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
|
|
return CreateModelResponse{}, err
|
|
}
|
|
return respBody, nil
|
|
}
|
|
|
|
func (o Ollama) CreateModelStream(reqBody CreateModelRequest, onChunk func(chunk CreateModelResponseStream)) error {
|
|
reqBody.Stream = PtrOf(true)
|
|
|
|
reqBodyBytes, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/create", o.baseUrl), bytes.NewReader(reqBodyBytes))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for key, val := range o.customHeaders {
|
|
req.Header.Set(key, val)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return errors.New("status code is not 200")
|
|
}
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
for scanner.Scan() {
|
|
line := bytes.TrimSpace(scanner.Bytes())
|
|
|
|
var chunk CreateModelResponseStream
|
|
if err := json.Unmarshal(line, &chunk); err != nil {
|
|
return err
|
|
}
|
|
|
|
onChunk(chunk)
|
|
if chunk.Status == "success" {
|
|
break
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|