Compare commits

..

1 Commits

Author SHA1 Message Date
Grace Guo
a6df9d75cf also need to set type to omitempty to work 2025-12-17 18:55:28 -08:00
25 changed files with 42 additions and 3179 deletions

View File

@@ -337,11 +337,11 @@ func mapToTypeScriptType(jsonType string) string {
}
type ToolFunctionParameters struct {
Type string `json:"type"`
Type string `json:"type,omitempty"`
Defs any `json:"$defs,omitempty"`
Items any `json:"items,omitempty"`
Required []string `json:"required,omitempty"`
Properties map[string]ToolProperty `json:"properties"`
Properties map[string]ToolProperty `json:"properties,omitempty"`
}
func (t *ToolFunctionParameters) String() string {
@@ -352,7 +352,7 @@ func (t *ToolFunctionParameters) String() string {
type ToolFunction struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters ToolFunctionParameters `json:"parameters"`
Parameters ToolFunctionParameters `json:"parameters,omitempty"`
}
func (t *ToolFunction) String() string {
@@ -554,9 +554,6 @@ type CreateRequest struct {
Renderer string `json:"renderer,omitempty"`
Parser string `json:"parser,omitempty"`
// Requires is the minimum version of Ollama required by the model.
Requires string `json:"requires,omitempty"`
// Info is a map of additional information for the model
Info map[string]any `json:"info,omitempty"`
@@ -607,7 +604,6 @@ type ShowResponse struct {
Tensors []Tensor `json:"tensors,omitempty"`
Capabilities []model.Capability `json:"capabilities,omitempty"`
ModifiedAt time.Time `json:"modified_at,omitempty"`
Requires string `json:"requires,omitempty"`
}
// CopyRequest is the request passed to [Client.Copy].

View File

@@ -209,9 +209,6 @@ func main() {
st := &store.Store{}
// Initialize native settings with store
SetSettingsStore(st)
// Enable CORS in development mode
if devMode {
os.Setenv("OLLAMA_CORS", "1")
@@ -256,27 +253,22 @@ func main() {
done <- osrv.Run(octx)
}()
restartServer := func() {
ocancel()
<-done
octx, ocancel = context.WithCancel(ctx)
go func() {
done <- osrv.Run(octx)
}()
}
uiServer := ui.Server{
Token: token,
Restart: restartServer,
Token: token,
Restart: func() {
ocancel()
<-done
octx, ocancel = context.WithCancel(ctx)
go func() {
done <- osrv.Run(octx)
}()
},
Store: st,
ToolRegistry: toolRegistry,
Dev: devMode,
Logger: slog.Default(),
}
// Set restart callback for native settings
SetRestartCallback(restartServer)
srv := &http.Server{
Handler: uiServer.Handler(),
}

View File

@@ -1,6 +1,5 @@
#import "app_darwin.h"
#import "menu.h"
#import "settings_darwin.h"
#import "../../updater/updater_darwin.h"
#import <AppKit/AppKit.h>
#import <Cocoa/Cocoa.h>
@@ -253,7 +252,7 @@ bool firstTimeRun,startHidden; // Set in run before initialization
}
- (void)settingsUI {
openNativeSettings();
[self uiRequest:@"/settings"];
}
- (void)openUI {

View File

@@ -1,438 +0,0 @@
//go:build darwin
package main
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <stdlib.h>
#include "settings_darwin.h"
*/
import "C"
import (
"context"
"crypto/ed25519"
"crypto/rand"
"encoding/json"
"encoding/pem"
"fmt"
"log/slog"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"unsafe"
"golang.org/x/crypto/ssh"
appauth "github.com/ollama/ollama/app/auth"
"github.com/ollama/ollama/app/store"
"github.com/ollama/ollama/auth"
"github.com/ollama/ollama/envconfig"
)
// settingsStore is a reference to the app's store for settings
var settingsStore *store.Store
// SetSettingsStore sets the store reference for settings callbacks
func SetSettingsStore(s *store.Store) {
settingsStore = s
}
//export getSettingsExpose
func getSettingsExpose() C.bool {
if settingsStore == nil {
return C.bool(false)
}
settings, err := settingsStore.Settings()
if err != nil {
slog.Error("failed to get settings", "error", err)
return C.bool(false)
}
return C.bool(settings.Expose)
}
//export setSettingsExpose
func setSettingsExpose(expose C.bool) {
if settingsStore == nil {
return
}
settings, err := settingsStore.Settings()
if err != nil {
slog.Error("failed to get settings", "error", err)
return
}
settings.Expose = bool(expose)
if err := settingsStore.SetSettings(settings); err != nil {
slog.Error("failed to save settings", "error", err)
}
}
//export getSettingsBrowser
func getSettingsBrowser() C.bool {
if settingsStore == nil {
return C.bool(false)
}
settings, err := settingsStore.Settings()
if err != nil {
slog.Error("failed to get settings", "error", err)
return C.bool(false)
}
return C.bool(settings.Browser)
}
//export setSettingsBrowser
func setSettingsBrowser(browser C.bool) {
if settingsStore == nil {
return
}
settings, err := settingsStore.Settings()
if err != nil {
slog.Error("failed to get settings", "error", err)
return
}
settings.Browser = bool(browser)
if err := settingsStore.SetSettings(settings); err != nil {
slog.Error("failed to save settings", "error", err)
}
}
//export getSettingsModels
func getSettingsModels() *C.char {
if settingsStore == nil {
return C.CString(envconfig.Models())
}
settings, err := settingsStore.Settings()
if err != nil {
slog.Error("failed to get settings", "error", err)
return C.CString(envconfig.Models())
}
if settings.Models == "" {
return C.CString(envconfig.Models())
}
return C.CString(settings.Models)
}
//export setSettingsModels
func setSettingsModels(path *C.char) {
if settingsStore == nil {
return
}
settings, err := settingsStore.Settings()
if err != nil {
slog.Error("failed to get settings", "error", err)
return
}
settings.Models = C.GoString(path)
if err := settingsStore.SetSettings(settings); err != nil {
slog.Error("failed to save settings", "error", err)
}
}
//export getSettingsContextLength
func getSettingsContextLength() C.int {
if settingsStore == nil {
return C.int(4096)
}
settings, err := settingsStore.Settings()
if err != nil {
slog.Error("failed to get settings", "error", err)
return C.int(4096)
}
if settings.ContextLength <= 0 {
return C.int(4096)
}
return C.int(settings.ContextLength)
}
//export setSettingsContextLength
func setSettingsContextLength(length C.int) {
if settingsStore == nil {
return
}
settings, err := settingsStore.Settings()
if err != nil {
slog.Error("failed to get settings", "error", err)
return
}
settings.ContextLength = int(length)
if err := settingsStore.SetSettings(settings); err != nil {
slog.Error("failed to save settings", "error", err)
}
}
// restartCallback is set by the app to restart the ollama server
var restartCallback func()
// SetRestartCallback sets the function to call when settings change requires a restart
func SetRestartCallback(cb func()) {
restartCallback = cb
}
//export restartOllamaServer
func restartOllamaServer() {
if restartCallback != nil {
slog.Info("restarting ollama server due to settings change")
go restartCallback()
}
}
// hasOllamaKey checks if the user has an Ollama key file
func hasOllamaKey() bool {
home, err := os.UserHomeDir()
if err != nil {
return false
}
keyPath := filepath.Join(home, ".ollama", "id_ed25519")
_, err = os.Stat(keyPath)
return err == nil
}
// ensureKeypair generates a new keypair if one doesn't exist
func ensureKeypair() error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
privKeyPath := filepath.Join(home, ".ollama", "id_ed25519")
// Check if key already exists
if _, err := os.Stat(privKeyPath); err == nil {
return nil // Key exists
}
// Generate new keypair
slog.Info("generating new keypair for ollama account")
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return fmt.Errorf("failed to generate key: %w", err)
}
// Marshal private key
privKeyBytes, err := ssh.MarshalPrivateKey(privKey, "")
if err != nil {
return fmt.Errorf("failed to marshal private key: %w", err)
}
// Ensure directory exists
if err := os.MkdirAll(filepath.Dir(privKeyPath), 0o755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
// Write private key
if err := os.WriteFile(privKeyPath, pem.EncodeToMemory(privKeyBytes), 0o600); err != nil {
return fmt.Errorf("failed to write private key: %w", err)
}
// Write public key
sshPubKey, err := ssh.NewPublicKey(pubKey)
if err != nil {
return fmt.Errorf("failed to create ssh public key: %w", err)
}
pubKeyBytes := ssh.MarshalAuthorizedKey(sshPubKey)
pubKeyPath := filepath.Join(home, ".ollama", "id_ed25519.pub")
if err := os.WriteFile(pubKeyPath, pubKeyBytes, 0o644); err != nil {
return fmt.Errorf("failed to write public key: %w", err)
}
slog.Info("keypair generated successfully")
return nil
}
// userResponse matches the API response from ollama.com/api/me
type userResponse struct {
Name string `json:"name"`
Email string `json:"email"`
Plan string `json:"plan"`
AvatarURL string `json:"avatarurl"`
}
// fetchUserFromAPI fetches user data from ollama.com using signed request
func fetchUserFromAPI() (*userResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
signString := fmt.Sprintf("POST,/api/me?ts=%s", timestamp)
signature, err := auth.Sign(ctx, []byte(signString))
if err != nil {
return nil, fmt.Errorf("failed to sign request: %w", err)
}
endpoint := fmt.Sprintf("https://ollama.com/api/me?ts=%s", timestamp)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", signature))
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call ollama.com: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status: %d", resp.StatusCode)
}
var user userResponse
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
// Make avatar URL absolute
if user.AvatarURL != "" && !strings.HasPrefix(user.AvatarURL, "http") {
user.AvatarURL = "https://ollama.com/" + user.AvatarURL
}
// Cache the avatar URL
cachedAvatarURL = user.AvatarURL
// Cache the user data
if settingsStore != nil {
storeUser := store.User{
Name: user.Name,
Email: user.Email,
Plan: user.Plan,
}
if err := settingsStore.SetUser(storeUser); err != nil {
slog.Warn("failed to cache user", "error", err)
}
}
return &user, nil
}
//export getAccountName
func getAccountName() *C.char {
// Only return cached data - never block on network
if settingsStore == nil {
return C.CString("")
}
user, err := settingsStore.User()
if err != nil || user == nil {
return C.CString("")
}
return C.CString(user.Name)
}
// cachedAvatarURL stores the avatar URL from the last API fetch
var cachedAvatarURL string
//export getAccountAvatarURL
func getAccountAvatarURL() *C.char {
return C.CString(cachedAvatarURL)
}
//export getAccountEmail
func getAccountEmail() *C.char {
if settingsStore != nil {
user, err := settingsStore.User()
if err == nil && user != nil {
return C.CString(user.Email)
}
}
return C.CString("")
}
//export getAccountPlan
func getAccountPlan() *C.char {
if settingsStore != nil {
user, err := settingsStore.User()
if err == nil && user != nil {
return C.CString(user.Plan)
}
}
return C.CString("")
}
//export signOutAccount
func signOutAccount() {
if settingsStore != nil {
if err := settingsStore.ClearUser(); err != nil {
slog.Error("failed to clear user", "error", err)
}
}
// Also remove the key file
home, err := os.UserHomeDir()
if err != nil {
slog.Error("failed to get home dir", "error", err)
return
}
keyPath := filepath.Join(home, ".ollama", "id_ed25519")
if err := os.Remove(keyPath); err != nil && !os.IsNotExist(err) {
slog.Error("failed to remove key file", "error", err)
}
}
//export openConnectUrl
func openConnectUrl() {
// Ensure keypair exists (generate if needed)
if err := ensureKeypair(); err != nil {
slog.Error("failed to ensure keypair", "error", err)
// Fallback to basic connect page
cmd := exec.Command("open", "https://ollama.com/connect")
cmd.Start()
return
}
// Build connect URL with public key
connectURL, err := appauth.BuildConnectURL("https://ollama.com")
if err != nil {
slog.Error("failed to build connect URL", "error", err)
// Fallback to basic connect page
connectURL = "https://ollama.com/connect"
}
cmd := exec.Command("open", connectURL)
if err := cmd.Start(); err != nil {
slog.Error("failed to open connect URL", "error", err)
}
}
//export refreshAccountFromAPI
func refreshAccountFromAPI() {
if !hasOllamaKey() {
return
}
_, err := fetchUserFromAPI()
if err != nil {
slog.Debug("failed to refresh account", "error", err)
}
}
//export prefetchAccountData
func prefetchAccountData() {
// Run in background goroutine to not block app startup
go func() {
if !hasOllamaKey() {
return
}
_, err := fetchUserFromAPI()
if err != nil {
slog.Debug("failed to prefetch account data", "error", err)
} else {
slog.Debug("prefetched account data successfully")
}
}()
}
// OpenNativeSettings opens the native settings window
func OpenNativeSettings() {
C.openNativeSettings()
}
// Ensure the CString is freed (caller must free)
func freeCString(s *C.char) {
C.free(unsafe.Pointer(s))
}

View File

@@ -1,38 +0,0 @@
#import <Cocoa/Cocoa.h>
@interface SettingsWindowController : NSWindowController <NSWindowDelegate>
// General tab
@property(nonatomic, strong) NSButton *exposeCheckbox;
@property(nonatomic, strong) NSButton *browserCheckbox;
@property(nonatomic, strong) NSSlider *contextLengthSlider;
// Models tab
@property(nonatomic, strong) NSPathControl *modelsPathControl;
@property(nonatomic, strong) NSButton *modelsPathButton;
// Account tab
@property(nonatomic, strong) NSView *avatarView;
@property(nonatomic, strong) NSTextField *avatarInitialLabel;
@property(nonatomic, strong) NSImageView *avatarImageView;
@property(nonatomic, strong) NSTextField *accountNameLabel;
@property(nonatomic, strong) NSTextField *accountEmailLabel;
@property(nonatomic, strong) NSButton *manageButton;
@property(nonatomic, strong) NSButton *signOutButton;
@property(nonatomic, strong) NSButton *signInButton;
@property(nonatomic, strong) NSView *signedInContainer;
@property(nonatomic, strong) NSView *signedOutContainer;
// Plan section
@property(nonatomic, strong) NSView *planContainer;
@property(nonatomic, strong) NSTextField *planNameLabel;
@property(nonatomic, strong) NSButton *upgradeButton;
@property(nonatomic, strong) NSButton *viewUsageButton;
+ (instancetype)sharedController;
- (void)showSettings;
@end
// Go callbacks for settings
void openNativeSettings(void);

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +0,0 @@
//go:build windows
package main
import "github.com/ollama/ollama/app/store"
// SetSettingsStore sets the store reference for settings callbacks (stub for Windows)
func SetSettingsStore(s *store.Store) {
// TODO: Implement Windows native settings
}
// SetRestartCallback sets the function to call when settings change requires a restart (stub for Windows)
func SetRestartCallback(cb func()) {
// TODO: Implement Windows native settings
}

View File

@@ -943,9 +943,6 @@ func showInfo(resp *api.ShowResponse, verbose bool, w io.Writer) error {
rows = append(rows, []string{"", "parameters", resp.Details.ParameterSize})
}
rows = append(rows, []string{"", "quantization", resp.Details.QuantizationLevel})
if resp.Requires != "" {
rows = append(rows, []string{"", "requires", resp.Requires})
}
return
})

View File

@@ -291,31 +291,6 @@ Weigh anchor!
t.Errorf("unexpected output (-want +got):\n%s", diff)
}
})
t.Run("min version", func(t *testing.T) {
var b bytes.Buffer
if err := showInfo(&api.ShowResponse{
Details: api.ModelDetails{
Family: "test",
ParameterSize: "7B",
QuantizationLevel: "FP16",
},
Requires: "0.14.0",
}, false, &b); err != nil {
t.Fatal(err)
}
expect := ` Model
architecture test
parameters 7B
quantization FP16
requires 0.14.0
`
if diff := cmp.Diff(expect, b.String()); diff != "" {
t.Errorf("unexpected output (-want +got):\n%s", diff)
}
})
}
func TestDeleteHandler(t *testing.T) {

View File

@@ -49,8 +49,7 @@ func parseSentencePiece(fsys fs.FS) (*Vocabulary, error) {
tt := int32(sentencepiece.ModelProto_SentencePiece_NORMAL)
// temporary fix to handle gemma3 broken configs
// TODO(parthsareen): allow reading of tokenizer.json to allow managing special tokens when using spm
if slices.Contains([]string{"<end_of_turn>", "<start_of_turn>", "<start_function_declaration>", "<end_function_declaration>", "<start_function_call>", "<end_function_call>", "<start_function_response>", "<end_function_response>", "<escape>"}, piece.GetPiece()) {
if slices.Contains([]string{"<end_of_turn>", "<start_of_turn>"}, piece.GetPiece()) {
tt = int32(sentencepiece.ModelProto_SentencePiece_CONTROL)
}

View File

@@ -41,7 +41,6 @@ INSTRUCTION arguments
| [`ADAPTER`](#adapter) | Defines the (Q)LoRA adapters to apply to the model. |
| [`LICENSE`](#license) | Specifies the legal license. |
| [`MESSAGE`](#message) | Specify message history. |
| [`REQUIRES`](#requires) | Specify the minimum version of Ollama required by the model. |
## Examples
@@ -249,16 +248,6 @@ MESSAGE user Is Ontario in Canada?
MESSAGE assistant yes
```
### REQUIRES
The `REQUIRES` instruction allows you to specify the minimum version of Ollama required by the model.
```
REQUIRES <version>
```
The version should be a valid Ollama version (e.g. 0.14.0).
## Notes
- the **`Modelfile` is not case sensitive**. In the examples, uppercase instructions are used to make it easier to distinguish it from arguments.

15
go.mod
View File

@@ -15,8 +15,8 @@ require (
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.9.0
github.com/x448/float16 v0.8.4
golang.org/x/sync v0.17.0
golang.org/x/sys v0.37.0
golang.org/x/sync v0.12.0
golang.org/x/sys v0.36.0
)
require (
@@ -29,8 +29,7 @@ require (
github.com/pdevine/tensor v0.0.0-20240510204454-f88f4562727c
github.com/tkrajina/typescriptify-golang-structs v0.2.0
golang.org/x/image v0.22.0
golang.org/x/mod v0.30.0
golang.org/x/tools v0.38.0
golang.org/x/tools v0.30.0
gonum.org/v1/gonum v0.15.0
)
@@ -77,11 +76,11 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.43.0
golang.org/x/crypto v0.36.0
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/term v0.36.0
golang.org/x/text v0.30.0
golang.org/x/net v0.38.0 // indirect
golang.org/x/term v0.30.0
golang.org/x/text v0.23.0
google.golang.org/protobuf v1.34.1
gopkg.in/yaml.v3 v3.0.1 // indirect
)

30
go.sum
View File

@@ -224,8 +224,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -255,8 +255,6 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -269,8 +267,8 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -280,8 +278,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -297,17 +295,17 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q=
golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss=
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -321,8 +319,8 @@ golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=
golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@@ -524,13 +524,8 @@ func (s *llamaServer) Load(ctx context.Context, systemInfo ml.SystemInfo, system
// Use the size of one layer as a buffer
layers := s.ggml.Tensors().GroupLayers()
if blk0, ok := layers["blk.0"]; ok {
buffer := blk0.Size() + kv[0]
for i := range gpus {
if gpus[i].FreeMemory > buffer {
gpus[i].FreeMemory -= buffer
} else {
gpus[i].FreeMemory = 0
}
gpus[i].FreeMemory -= blk0.Size() + kv[0]
}
} else {
slog.Warn("model missing blk.0 layer size")
@@ -580,11 +575,7 @@ func (s *llamaServer) Load(ctx context.Context, systemInfo ml.SystemInfo, system
projectorGPU = firstIntegrated
}
if gpus[projectorGPU].FreeMemory > projectorWeights {
gpus[projectorGPU].FreeMemory -= projectorWeights
} else {
gpus[projectorGPU].FreeMemory = 0
}
gpus[projectorGPU].FreeMemory -= projectorWeights
}
var kvTotal uint64

View File

@@ -1,323 +0,0 @@
package parsers
import (
"fmt"
"regexp"
"strings"
"github.com/ollama/ollama/api"
)
type FunctionGemmaParserState int
const (
FunctionGemmaCollectingContent FunctionGemmaParserState = iota
FunctionGemmaCollectingToolCalls
)
const (
functionGemmaFunctionCallOpen = "<start_function_call>"
functionGemmaFunctionCallClose = "<end_function_call>"
)
// This format uses <start_function_call>call:name{args}<end_function_call> for tool calls.
type FunctionGemmaParser struct {
state FunctionGemmaParserState
buffer strings.Builder
tools []api.Tool
}
func (p *FunctionGemmaParser) HasToolSupport() bool { return true }
func (p *FunctionGemmaParser) HasThinkingSupport() bool { return false }
func (p *FunctionGemmaParser) Init(tools []api.Tool, lastMessage *api.Message, thinkValue *api.ThinkValue) []api.Tool {
p.tools = tools
p.state = FunctionGemmaCollectingContent
return tools
}
type functionGemmaEvent interface {
isFunctionGemmaEvent()
}
type FunctionGemmaEventContent struct {
content string
}
type functionGemmaEventToolCall struct {
toolCall api.ToolCall
}
func (FunctionGemmaEventContent) isFunctionGemmaEvent() {}
func (functionGemmaEventToolCall) isFunctionGemmaEvent() {}
func (p *FunctionGemmaParser) Add(s string, done bool) (content string, thinking string, calls []api.ToolCall, err error) {
p.buffer.WriteString(s)
events := p.parseEvents()
var toolCalls []api.ToolCall
var contentSb strings.Builder
for _, event := range events {
switch event := event.(type) {
case functionGemmaEventToolCall:
toolCalls = append(toolCalls, event.toolCall)
case FunctionGemmaEventContent:
contentSb.WriteString(event.content)
}
}
return contentSb.String(), "", toolCalls, nil
}
func (p *FunctionGemmaParser) parseEvents() []functionGemmaEvent {
var all []functionGemmaEvent
keepLooping := true
for keepLooping {
var events []functionGemmaEvent
events, keepLooping = p.eat()
if len(events) > 0 {
all = append(all, events...)
}
}
return all
}
// emitWithPartialCheck extracts unambiguous content before a potential partial tag
func (p *FunctionGemmaParser) emitWithPartialCheck(bufStr, tag string) (unambiguous, ambiguous string) {
if overlapLen := overlap(bufStr, tag); overlapLen > 0 {
beforePartialTag := bufStr[:len(bufStr)-overlapLen]
return beforePartialTag, bufStr[len(beforePartialTag):]
}
return bufStr, ""
}
func (p *FunctionGemmaParser) eat() ([]functionGemmaEvent, bool) {
bufStr := p.buffer.String()
if bufStr == "" {
return nil, false
}
switch p.state {
case FunctionGemmaCollectingContent:
if strings.Contains(bufStr, functionGemmaFunctionCallOpen) {
split := strings.SplitN(bufStr, functionGemmaFunctionCallOpen, 2)
content := split[0]
p.buffer.Reset()
p.buffer.WriteString(split[1])
p.state = FunctionGemmaCollectingToolCalls
if content != "" {
return []functionGemmaEvent{FunctionGemmaEventContent{content: content}}, true
}
return nil, true
}
unambig, ambig := p.emitWithPartialCheck(bufStr, functionGemmaFunctionCallOpen)
p.buffer.Reset()
p.buffer.WriteString(ambig)
if unambig != "" {
return []functionGemmaEvent{FunctionGemmaEventContent{content: unambig}}, false
}
return nil, false
case FunctionGemmaCollectingToolCalls:
if strings.Contains(bufStr, functionGemmaFunctionCallClose) {
split := strings.SplitN(bufStr, functionGemmaFunctionCallClose, 2)
remaining := split[1]
p.buffer.Reset()
p.buffer.WriteString(remaining)
var events []functionGemmaEvent
if tc, err := p.parseToolCall(split[0]); err == nil {
events = append(events, functionGemmaEventToolCall{toolCall: tc})
}
if !strings.Contains(remaining, functionGemmaFunctionCallOpen) {
p.state = FunctionGemmaCollectingContent
}
return events, true
}
return nil, false
}
return nil, false
}
// Matches call:function_name{args}
var functionGemmaCallRegex = regexp.MustCompile(`call:([^{]+)\{(.*)\}`)
func (p *FunctionGemmaParser) parseToolCall(content string) (api.ToolCall, error) {
toolCall := api.ToolCall{}
// Extract function name and arguments
match := functionGemmaCallRegex.FindStringSubmatch(content)
if len(match) < 3 {
return toolCall, nil
}
toolCall.Function.Name = match[1]
argsStr := match[2]
// Parse arguments
toolCall.Function.Arguments = p.parseArguments(argsStr)
return toolCall, nil
}
// parseArguments parses the key:value,key:value format
func (p *FunctionGemmaParser) parseArguments(argsStr string) api.ToolCallFunctionArguments {
args := make(api.ToolCallFunctionArguments)
if argsStr == "" {
return args
}
// Split by comma, but handle nested structures
parts := p.splitArguments(argsStr)
for _, part := range parts {
// Find the first colon to split key:value
colonIdx := strings.Index(part, ":")
if colonIdx == -1 {
continue
}
key := part[:colonIdx]
value := part[colonIdx+1:]
// Parse the value
args[key] = p.parseValue(value)
}
return args
}
// splitArguments splits arguments by comma, respecting nested structures
func (p *FunctionGemmaParser) splitArguments(argsStr string) []string {
var parts []string
var current strings.Builder
depth := 0
inEscape := false
for i := 0; i < len(argsStr); i++ {
ch := argsStr[i]
// Check for <escape> tags
if i+8 <= len(argsStr) && argsStr[i:i+8] == "<escape>" {
inEscape = !inEscape
current.WriteString("<escape>")
i += 7 // Skip the rest of <escape>
continue
}
if !inEscape {
switch ch {
case '{', '[':
depth++
current.WriteByte(ch)
case '}', ']':
depth--
current.WriteByte(ch)
case ',':
if depth == 0 {
if current.Len() > 0 {
parts = append(parts, current.String())
current.Reset()
}
continue
}
current.WriteByte(ch)
default:
current.WriteByte(ch)
}
} else {
current.WriteByte(ch)
}
}
if current.Len() > 0 {
parts = append(parts, current.String())
}
return parts
}
// parseValue parses a single value from the FunctionGemma format
func (p *FunctionGemmaParser) parseValue(value string) any {
// Check for escaped string
if strings.HasPrefix(value, "<escape>") && strings.HasSuffix(value, "<escape>") {
// Remove the escape tags
return value[8 : len(value)-8]
}
// Check for boolean
if value == "true" {
return true
}
if value == "false" {
return false
}
// Check for number
if num, ok := parseNumber(value); ok {
return num
}
// Check for array
if strings.HasPrefix(value, "[") && strings.HasSuffix(value, "]") {
return p.parseArray(value[1 : len(value)-1])
}
// Check for object
if strings.HasPrefix(value, "{") && strings.HasSuffix(value, "}") {
return p.parseObject(value[1 : len(value)-1])
}
// Default to string
return value
}
// parseArray parses an array value
func (p *FunctionGemmaParser) parseArray(content string) []any {
var result []any
parts := p.splitArguments(content)
for _, part := range parts {
result = append(result, p.parseValue(part))
}
return result
}
// parseObject parses an object value
func (p *FunctionGemmaParser) parseObject(content string) map[string]any {
result := make(map[string]any)
parts := p.splitArguments(content)
for _, part := range parts {
colonIdx := strings.Index(part, ":")
if colonIdx == -1 {
continue
}
key := part[:colonIdx]
value := part[colonIdx+1:]
result[key] = p.parseValue(value)
}
return result
}
// parseNumber tries to parse a string as a number
func parseNumber(s string) (any, bool) {
// Try integer first
var intVal int64
if _, err := fmt.Sscanf(s, "%d", &intVal); err == nil {
// Check if the entire string was consumed
if fmt.Sprintf("%d", intVal) == s {
return intVal, true
}
}
// Try float
var floatVal float64
if _, err := fmt.Sscanf(s, "%f", &floatVal); err == nil {
return floatVal, true
}
return nil, false
}

View File

@@ -1,423 +0,0 @@
package parsers
import (
"testing"
"github.com/ollama/ollama/api"
"github.com/stretchr/testify/assert"
)
func TestFunctionGemmaParser(t *testing.T) {
tests := []struct {
name string
chunks []string
tools []api.Tool
expectedCalls []api.ToolCall
expectedText string
}{
{
name: "plain_content",
chunks: []string{"H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"},
expectedCalls: nil,
expectedText: "Hello, world!",
},
{
name: "simple_tool_call",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "get", "_", "weather", "{",
"city", ":", "<", "escape", ">", "Paris", "<", "escape", ">",
"}", "<", "end", "_", "function", "_", "call", ">",
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}},
},
},
},
},
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "Paris"},
},
},
},
expectedText: "",
},
{
name: "content_before_tool_call",
chunks: []string{
"L", "et", " ", "me", " ", "check", ".",
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "get", "_", "weather", "{",
"city", ":", "<", "escape", ">", "Paris", "<", "escape", ">",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "Paris"},
},
},
},
expectedText: "Let me check.",
},
{
name: "numeric_arguments",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "add", "{",
"a", ":", "1", ",", "b", ":", "2",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "add",
Arguments: api.ToolCallFunctionArguments{"a": int64(1), "b": int64(2)},
},
},
},
expectedText: "",
},
{
name: "boolean_arguments",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "set", "_", "flag", "{",
"enabled", ":", "true", ",", "verbose", ":", "false",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "set_flag",
Arguments: api.ToolCallFunctionArguments{"enabled": true, "verbose": false},
},
},
},
expectedText: "",
},
{
name: "multiple_tool_calls",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "get", "_", "weather", "{",
"city", ":", "<", "escape", ">", "Paris", "<", "escape", ">",
"}", "<", "end", "_", "function", "_", "call", ">",
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "get", "_", "weather", "{",
"city", ":", "<", "escape", ">", "London", "<", "escape", ">",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "Paris"},
},
},
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "London"},
},
},
},
expectedText: "",
},
{
name: "array_argument",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "process", "{",
"items", ":", "[",
"<", "escape", ">", "a", "<", "escape", ">", ",",
"<", "escape", ">", "b", "<", "escape", ">", ",",
"<", "escape", ">", "c", "<", "escape", ">",
"]",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "process",
Arguments: api.ToolCallFunctionArguments{"items": []any{"a", "b", "c"}},
},
},
},
expectedText: "",
},
{
name: "object_argument",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "update", "{",
"data", ":", "{",
"name", ":", "<", "escape", ">", "test", "<", "escape", ">", ",",
"value", ":", "42",
"}",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "update",
Arguments: api.ToolCallFunctionArguments{
"data": map[string]any{"name": "test", "value": int64(42)},
},
},
},
},
expectedText: "",
},
{
name: "empty_input",
chunks: []string{},
expectedCalls: nil,
expectedText: "",
},
{
name: "tool_call_with_no_arguments",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "get", "_", "time", "{", "}",
"<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_time",
Arguments: api.ToolCallFunctionArguments{},
},
},
},
expectedText: "",
},
{
name: "content_with_angle_brackets",
chunks: []string{
"The", " ", "result", " ", "is", " ", "a", " ", "<", "value", ">", " ", "tag",
},
expectedCalls: nil,
expectedText: "The result is a <value> tag",
},
{
name: "float_argument",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "set", "_", "temp", "{",
"value", ":", "3", ".", "14",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "set_temp",
Arguments: api.ToolCallFunctionArguments{"value": 3.14},
},
},
},
expectedText: "",
},
{
name: "content_after_tool_call",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "test", "{", "}",
"<", "end", "_", "function", "_", "call", ">",
"Done", "!",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "test",
Arguments: api.ToolCallFunctionArguments{},
},
},
},
expectedText: "Done!",
},
{
name: "unicode_content_and_arguments",
chunks: []string{
"こんにちは", " ",
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "greet", "{",
"name", ":", "<", "escape", ">", "日本語", "<", "escape", ">",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "greet",
Arguments: api.ToolCallFunctionArguments{"name": "日本語"},
},
},
},
expectedText: "こんにちは ",
},
{
name: "multiple_params_sorted",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "search", "{",
"query", ":", "<", "escape", ">", "test", "<", "escape", ">", ",",
"limit", ":", "10", ",",
"offset", ":", "0",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "search",
Arguments: api.ToolCallFunctionArguments{
"query": "test",
"limit": int64(10),
"offset": int64(0),
},
},
},
},
expectedText: "",
},
{
name: "nested_object_argument",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "create", "{",
"config", ":", "{",
"settings", ":", "{",
"enabled", ":", "true", ",",
"name", ":", "<", "escape", ">", "test", "<", "escape", ">",
"}",
"}",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "create",
Arguments: api.ToolCallFunctionArguments{
"config": map[string]any{
"settings": map[string]any{
"enabled": true,
"name": "test",
},
},
},
},
},
},
expectedText: "",
},
{
name: "partial_start_tag_in_content",
chunks: []string{
"Hello", " ", "<", "start", " ", "world",
},
expectedCalls: nil,
expectedText: "Hello <start world",
},
{
name: "parallel_tool_calls",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "get", "_", "weather", "{",
"city", ":", "<", "escape", ">", "Paris", "<", "escape", ">",
"}", "<", "end", "_", "function", "_", "call", ">",
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "get", "_", "time", "{",
"timezone", ":", "<", "escape", ">", "UTC", "<", "escape", ">",
"}", "<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "Paris"},
},
},
{
Function: api.ToolCallFunction{
Name: "get_time",
Arguments: api.ToolCallFunctionArguments{"timezone": "UTC"},
},
},
},
expectedText: "",
},
{
name: "content_between_tool_calls",
chunks: []string{
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "first", "{", "}",
"<", "end", "_", "function", "_", "call", ">",
"Some", " ", "text", " ", "here",
"<", "start", "_", "function", "_", "call", ">",
"call", ":", "second", "{", "}",
"<", "end", "_", "function", "_", "call", ">",
},
expectedCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "first",
Arguments: api.ToolCallFunctionArguments{},
},
},
{
Function: api.ToolCallFunction{
Name: "second",
Arguments: api.ToolCallFunctionArguments{},
},
},
},
expectedText: "Some text here",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := &FunctionGemmaParser{}
parser.Init(tt.tools, nil, nil)
var allContent string
var allCalls []api.ToolCall
for i, chunk := range tt.chunks {
done := i == len(tt.chunks)-1
content, _, calls, err := parser.Add(chunk, done)
assert.NoError(t, err)
allContent += content
allCalls = append(allCalls, calls...)
}
// Handle empty chunks case
if len(tt.chunks) == 0 {
content, _, calls, err := parser.Add("", true)
assert.NoError(t, err)
allContent = content
allCalls = calls
}
assert.Equal(t, tt.expectedText, allContent)
assert.Equal(t, tt.expectedCalls, allCalls)
})
}
}
func TestFunctionGemmaParser_HasSupport(t *testing.T) {
parser := &FunctionGemmaParser{}
assert.True(t, parser.HasToolSupport())
assert.False(t, parser.HasThinkingSupport())
}

View File

@@ -66,8 +66,6 @@ func ParserForName(name string) Parser {
return &Olmo3ThinkParser{}
case "nemotron-3-nano":
return &Nemotron3NanoParser{}
case "functiongemma":
return &FunctionGemmaParser{}
default:
return nil
}

View File

@@ -1,287 +0,0 @@
package renderers
import (
"fmt"
"sort"
"strings"
"github.com/ollama/ollama/api"
)
type FunctionGemmaRenderer struct{}
const defaultSystemMessage = "You can do function calling with the following functions:"
func (r *FunctionGemmaRenderer) Render(messages []api.Message, tools []api.Tool, thinkValue *api.ThinkValue) (string, error) {
var sb strings.Builder
sb.WriteString("<bos>")
var systemMessage string
var loopMessages []api.Message
if len(messages) > 0 && (messages[0].Role == "system" || messages[0].Role == "developer") {
systemMessage = messages[0].Content
loopMessages = messages[1:]
} else {
loopMessages = messages
}
if systemMessage != "" || len(tools) > 0 {
sb.WriteString("<start_of_turn>developer\n")
if systemMessage != "" {
sb.WriteString(strings.TrimSpace(systemMessage))
}
if len(tools) > 0 {
if systemMessage != "" {
sb.WriteString("\n")
}
if strings.TrimSpace(systemMessage) != defaultSystemMessage {
// Only add default message if user does not provide it
sb.WriteString(defaultSystemMessage)
}
}
for _, tool := range tools {
sb.WriteString(r.renderToolDeclaration(tool))
}
sb.WriteString("<end_of_turn>\n")
}
// Track previous message type for tool response handling
prevMessageType := ""
for i, message := range loopMessages {
switch message.Role {
case "assistant":
if prevMessageType != "tool_response" {
sb.WriteString("<start_of_turn>model\n")
}
prevMessageType = ""
if message.Content != "" {
sb.WriteString(strings.TrimSpace(message.Content))
}
if len(message.ToolCalls) > 0 {
for _, tc := range message.ToolCalls {
sb.WriteString(r.formatToolCall(tc))
}
// After tool calls, expect tool responses
if i+1 < len(loopMessages) && loopMessages[i+1].Role == "tool" {
sb.WriteString("<start_function_response>")
prevMessageType = "tool_call"
} else {
sb.WriteString("<end_of_turn>\n")
}
} else {
sb.WriteString("<end_of_turn>\n")
}
case "user":
if prevMessageType != "tool_response" {
sb.WriteString("<start_of_turn>user\n")
}
prevMessageType = ""
sb.WriteString(strings.TrimSpace(message.Content))
sb.WriteString("<end_of_turn>\n")
case "tool":
toolName := ""
// Find the tool name from the previous assistant's tool call
for j := i - 1; j >= 0; j-- {
if loopMessages[j].Role == "assistant" && len(loopMessages[j].ToolCalls) > 0 {
// Count how many tool messages came before this one
toolIdx := 0
for k := j + 1; k < i; k++ {
if loopMessages[k].Role == "tool" {
toolIdx++
}
}
if toolIdx < len(loopMessages[j].ToolCalls) {
toolName = loopMessages[j].ToolCalls[toolIdx].Function.Name
}
break
}
}
if prevMessageType != "tool_call" {
sb.WriteString("<start_function_response>")
}
sb.WriteString("response:" + toolName + "{" + r.formatArgValue(message.Content) + "}<end_function_response>")
prevMessageType = "tool_response"
default:
sb.WriteString("<start_of_turn>" + message.Role + "\n")
sb.WriteString(strings.TrimSpace(message.Content))
sb.WriteString("<end_of_turn>\n")
}
}
if prevMessageType != "tool_response" {
sb.WriteString("<start_of_turn>model\n")
}
return sb.String(), nil
}
func (r *FunctionGemmaRenderer) renderToolDeclaration(tool api.Tool) string {
var sb strings.Builder
fn := tool.Function
sb.WriteString("<start_function_declaration>declaration:" + fn.Name + "{")
sb.WriteString("description:<escape>" + fn.Description + "<escape>")
if fn.Parameters.Properties != nil || fn.Parameters.Type != "" {
sb.WriteString(",parameters:{")
needsComma := false
// Only include properties:{} if there are actual properties
if len(fn.Parameters.Properties) > 0 {
sb.WriteString("properties:{")
r.writeProperties(&sb, fn.Parameters.Properties)
sb.WriteString("}")
needsComma = true
}
if len(fn.Parameters.Required) > 0 {
if needsComma {
sb.WriteString(",")
}
sb.WriteString("required:[")
for i, req := range fn.Parameters.Required {
if i > 0 {
sb.WriteString(",")
}
sb.WriteString("<escape>" + req + "<escape>")
}
sb.WriteString("]")
needsComma = true
}
if fn.Parameters.Type != "" {
if needsComma {
sb.WriteString(",")
}
sb.WriteString("type:<escape>" + strings.ToUpper(fn.Parameters.Type) + "<escape>")
}
sb.WriteString("}")
}
sb.WriteString("}<end_function_declaration>")
return sb.String()
}
func (r *FunctionGemmaRenderer) writeProperties(sb *strings.Builder, props map[string]api.ToolProperty) {
keys := make([]string, 0, len(props))
for k := range props {
keys = append(keys, k)
}
sort.Strings(keys)
first := true
for _, name := range keys {
prop := props[name]
if !first {
sb.WriteString(",")
}
first = false
sb.WriteString(name + ":{description:<escape>")
sb.WriteString(prop.Description)
sb.WriteString("<escape>")
if len(prop.Type) > 0 {
sb.WriteString(",type:<escape>" + strings.ToUpper(prop.Type[0]) + "<escape>")
}
sb.WriteString("}")
}
}
func (r *FunctionGemmaRenderer) formatToolCall(tc api.ToolCall) string {
var sb strings.Builder
sb.WriteString("<start_function_call>call:" + tc.Function.Name + "{")
keys := make([]string, 0, len(tc.Function.Arguments))
for k := range tc.Function.Arguments {
keys = append(keys, k)
}
sort.Strings(keys)
first := true
for _, key := range keys {
value := tc.Function.Arguments[key]
if !first {
sb.WriteString(",")
}
first = false
sb.WriteString(key + ":" + r.formatArgValue(value))
}
sb.WriteString("}<end_function_call>")
return sb.String()
}
func (r *FunctionGemmaRenderer) formatArgValue(value any) string {
switch v := value.(type) {
case string:
return "<escape>" + v + "<escape>"
case bool:
if v {
return "true"
}
return "false"
case float64:
if v == float64(int64(v)) {
return fmt.Sprintf("%d", int64(v))
}
return fmt.Sprintf("%v", v)
case int, int64, int32:
return fmt.Sprintf("%d", v)
case map[string]any:
return r.formatMapValue(v)
case []any:
return r.formatArrayValue(v)
default:
return fmt.Sprintf("%v", v)
}
}
func (r *FunctionGemmaRenderer) formatMapValue(m map[string]any) string {
var sb strings.Builder
sb.WriteString("{")
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
first := true
for _, key := range keys {
if !first {
sb.WriteString(",")
}
first = false
sb.WriteString(key + ":" + r.formatArgValue(m[key]))
}
sb.WriteString("}")
return sb.String()
}
func (r *FunctionGemmaRenderer) formatArrayValue(arr []any) string {
var sb strings.Builder
sb.WriteString("[")
for i, item := range arr {
if i > 0 {
sb.WriteString(",")
}
sb.WriteString(r.formatArgValue(item))
}
sb.WriteString("]")
return sb.String()
}

View File

@@ -1,514 +0,0 @@
package renderers
import (
"testing"
"github.com/ollama/ollama/api"
"github.com/stretchr/testify/assert"
)
func TestFunctionGemmaRenderer(t *testing.T) {
tests := []struct {
name string
messages []api.Message
tools []api.Tool
expected string
}{
{
name: "basic_user_message",
messages: []api.Message{
{Role: "user", Content: "Hello!"},
},
expected: "<bos><start_of_turn>user\nHello!<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "with_system_message",
messages: []api.Message{
{Role: "system", Content: "You are helpful"},
{Role: "user", Content: "Hello!"},
},
expected: "<bos><start_of_turn>developer\nYou are helpful<end_of_turn>\n<start_of_turn>user\nHello!<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "with_developer_role",
messages: []api.Message{
{Role: "developer", Content: "You are a coding assistant"},
{Role: "user", Content: "Hello!"},
},
expected: "<bos><start_of_turn>developer\nYou are a coding assistant<end_of_turn>\n<start_of_turn>user\nHello!<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "custom_system_message_with_tools",
messages: []api.Message{
{Role: "system", Content: "You are a weather expert."},
{Role: "user", Content: "Weather?"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City"},
},
},
},
},
},
// Custom system message is preserved, tools are appended
expected: "<bos><start_of_turn>developer\nYou are a weather expert.\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Get weather<escape>,parameters:{properties:{city:{description:<escape>City<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather?<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "developer_role_with_tools",
messages: []api.Message{
{Role: "developer", Content: "Be concise."},
{Role: "user", Content: "Weather?"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City"},
},
},
},
},
},
// Developer role message is preserved, tools are appended
expected: "<bos><start_of_turn>developer\nBe concise.\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Get weather<escape>,parameters:{properties:{city:{description:<escape>City<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather?<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "multi_turn",
messages: []api.Message{
{Role: "user", Content: "Hi"},
{Role: "assistant", Content: "Hello!"},
{Role: "user", Content: "More"},
},
expected: "<bos><start_of_turn>user\nHi<end_of_turn>\n<start_of_turn>model\nHello!<end_of_turn>\n<start_of_turn>user\nMore<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "with_tools",
messages: []api.Message{
{Role: "user", Content: "Weather?"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City"},
},
},
},
},
},
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Get weather<escape>,parameters:{properties:{city:{description:<escape>City<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather?<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "tool_call",
messages: []api.Message{
{Role: "user", Content: "Weather?"},
{
Role: "assistant",
ToolCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "Paris"},
},
},
},
},
{Role: "tool", Content: "Sunny"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City"},
},
},
},
},
},
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Get weather<escape>,parameters:{properties:{city:{description:<escape>City<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather?<end_of_turn>\n<start_of_turn>model\n<start_function_call>call:get_weather{city:<escape>Paris<escape>}<end_function_call><start_function_response>response:get_weather{<escape>Sunny<escape>}<end_function_response>",
},
{
name: "assistant_content_with_tool_call",
messages: []api.Message{
{Role: "user", Content: "Weather?"},
{
Role: "assistant",
Content: "Let me check.",
ToolCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "Paris"},
},
},
},
},
{Role: "tool", Content: "Sunny"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City"},
},
},
},
},
},
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Get weather<escape>,parameters:{properties:{city:{description:<escape>City<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather?<end_of_turn>\n<start_of_turn>model\nLet me check.<start_function_call>call:get_weather{city:<escape>Paris<escape>}<end_function_call><start_function_response>response:get_weather{<escape>Sunny<escape>}<end_function_response>",
},
{
name: "numeric_arguments",
messages: []api.Message{
{Role: "user", Content: "Add"},
{
Role: "assistant",
ToolCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "add",
Arguments: api.ToolCallFunctionArguments{"a": float64(1), "b": float64(2)},
},
},
},
},
{Role: "tool", Content: "3"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "add",
Description: "Add numbers",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"a": {Type: api.PropertyType{"number"}},
"b": {Type: api.PropertyType{"number"}},
},
},
},
},
},
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:add{description:<escape>Add numbers<escape>,parameters:{properties:{a:{description:<escape><escape>,type:<escape>NUMBER<escape>},b:{description:<escape><escape>,type:<escape>NUMBER<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nAdd<end_of_turn>\n<start_of_turn>model\n<start_function_call>call:add{a:1,b:2}<end_function_call><start_function_response>response:add{<escape>3<escape>}<end_function_response>",
},
{
name: "empty_messages",
messages: []api.Message{},
expected: "<bos><start_of_turn>model\n",
},
{
name: "tool_with_required_params",
messages: []api.Message{
{Role: "user", Content: "Weather?"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Gets the weather for a given city",
Parameters: api.ToolFunctionParameters{
Type: "object",
Required: []string{"city"},
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City Name"},
"country": {Type: api.PropertyType{"string"}, Description: "Country Name"},
},
},
},
},
},
// Required params are escaped: required:[<escape>city<escape>]
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Gets the weather for a given city<escape>,parameters:{properties:{city:{description:<escape>City Name<escape>,type:<escape>STRING<escape>},country:{description:<escape>Country Name<escape>,type:<escape>STRING<escape>}},required:[<escape>city<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather?<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "multiple_tools",
messages: []api.Message{
{Role: "user", Content: "Weather and time?"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City"},
},
},
},
},
{
Type: "function",
Function: api.ToolFunction{
Name: "get_time",
Description: "Get current time",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"timezone": {Type: api.PropertyType{"string"}, Description: "Timezone"},
},
},
},
},
},
// Multiple tool declarations are consecutive
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Get weather<escape>,parameters:{properties:{city:{description:<escape>City<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:get_time{description:<escape>Get current time<escape>,parameters:{properties:{timezone:{description:<escape>Timezone<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather and time?<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "parallel_tool_calls",
messages: []api.Message{
{Role: "user", Content: "Weather and time?"},
{
Role: "assistant",
ToolCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "Paris"},
},
},
{
Function: api.ToolCallFunction{
Name: "get_time",
Arguments: api.ToolCallFunctionArguments{"timezone": "UTC"},
},
},
},
},
{Role: "tool", Content: "Sunny"},
{Role: "tool", Content: "12:00"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City"},
},
},
},
},
{
Type: "function",
Function: api.ToolFunction{
Name: "get_time",
Description: "Get current time",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"timezone": {Type: api.PropertyType{"string"}, Description: "Timezone"},
},
},
},
},
},
// Multiple tool calls and responses are consecutive
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Get weather<escape>,parameters:{properties:{city:{description:<escape>City<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><start_function_declaration>declaration:get_time{description:<escape>Get current time<escape>,parameters:{properties:{timezone:{description:<escape>Timezone<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather and time?<end_of_turn>\n<start_of_turn>model\n<start_function_call>call:get_weather{city:<escape>Paris<escape>}<end_function_call><start_function_call>call:get_time{timezone:<escape>UTC<escape>}<end_function_call><start_function_response>response:get_weather{<escape>Sunny<escape>}<end_function_response><start_function_response>response:get_time{<escape>12:00<escape>}<end_function_response>",
},
{
name: "user_after_tool_response",
messages: []api.Message{
{Role: "user", Content: "Weather?"},
{
Role: "assistant",
ToolCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{"city": "Paris"},
},
},
},
},
{Role: "tool", Content: "Sunny"},
{Role: "user", Content: "Thanks! What about London?"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "get_weather",
Description: "Get weather",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"city": {Type: api.PropertyType{"string"}, Description: "City"},
},
},
},
},
},
// User message after tool response gets concatenated (user reverted to this behavior)
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:get_weather{description:<escape>Get weather<escape>,parameters:{properties:{city:{description:<escape>City<escape>,type:<escape>STRING<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nWeather?<end_of_turn>\n<start_of_turn>model\n<start_function_call>call:get_weather{city:<escape>Paris<escape>}<end_function_call><start_function_response>response:get_weather{<escape>Sunny<escape>}<end_function_response>Thanks! What about London?<end_of_turn>\n<start_of_turn>model\n",
},
// Edge cases
{
name: "tool_empty_properties",
messages: []api.Message{
{Role: "user", Content: "Test"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "test_fn",
Description: "",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{},
},
},
},
},
// Empty properties are omitted
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:test_fn{description:<escape><escape>,parameters:{type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nTest<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "unicode_content",
messages: []api.Message{
{Role: "user", Content: "こんにちは 🎉"},
},
expected: "<bos><start_of_turn>user\nこんにちは 🎉<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "newlines_in_content",
messages: []api.Message{
{Role: "user", Content: "Line 1\nLine 2\nLine 3"},
},
expected: "<bos><start_of_turn>user\nLine 1\nLine 2\nLine 3<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "special_chars_in_content",
messages: []api.Message{
{Role: "user", Content: "Test <tag> & \"quotes\" chars"},
},
expected: "<bos><start_of_turn>user\nTest <tag> & \"quotes\" chars<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "boolean_argument",
messages: []api.Message{
{Role: "user", Content: "Set flag"},
{
Role: "assistant",
ToolCalls: []api.ToolCall{
{
Function: api.ToolCallFunction{
Name: "set_flag",
Arguments: api.ToolCallFunctionArguments{"enabled": true},
},
},
},
},
{Role: "tool", Content: "done"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "set_flag",
Description: "Set a flag",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"enabled": {Type: api.PropertyType{"boolean"}, Description: "Flag value"},
},
},
},
},
},
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:set_flag{description:<escape>Set a flag<escape>,parameters:{properties:{enabled:{description:<escape>Flag value<escape>,type:<escape>BOOLEAN<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nSet flag<end_of_turn>\n<start_of_turn>model\n<start_function_call>call:set_flag{enabled:true}<end_function_call><start_function_response>response:set_flag{<escape>done<escape>}<end_function_response>",
},
{
name: "multiple_required_params",
messages: []api.Message{
{Role: "user", Content: "Test"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "test",
Description: "Test",
Parameters: api.ToolFunctionParameters{
Type: "object",
Required: []string{"a", "b", "c"},
Properties: map[string]api.ToolProperty{
"a": {Type: api.PropertyType{"string"}, Description: "A"},
"b": {Type: api.PropertyType{"string"}, Description: "B"},
"c": {Type: api.PropertyType{"string"}, Description: "C"},
},
},
},
},
},
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:test{description:<escape>Test<escape>,parameters:{properties:{a:{description:<escape>A<escape>,type:<escape>STRING<escape>},b:{description:<escape>B<escape>,type:<escape>STRING<escape>},c:{description:<escape>C<escape>,type:<escape>STRING<escape>}},required:[<escape>a<escape>,<escape>b<escape>,<escape>c<escape>],type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nTest<end_of_turn>\n<start_of_turn>model\n",
},
{
name: "array_type_param",
messages: []api.Message{
{Role: "user", Content: "Test"},
},
tools: []api.Tool{
{
Type: "function",
Function: api.ToolFunction{
Name: "test",
Description: "Test",
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: map[string]api.ToolProperty{
"items": {Type: api.PropertyType{"array"}, Description: "List of items"},
},
},
},
},
},
expected: "<bos><start_of_turn>developer\nYou can do function calling with the following functions:<start_function_declaration>declaration:test{description:<escape>Test<escape>,parameters:{properties:{items:{description:<escape>List of items<escape>,type:<escape>ARRAY<escape>}},type:<escape>OBJECT<escape>}}<end_function_declaration><end_of_turn>\n<start_of_turn>user\nTest<end_of_turn>\n<start_of_turn>model\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
renderer := &FunctionGemmaRenderer{}
result, err := renderer.Render(tt.messages, tt.tools, nil)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}

View File

@@ -78,8 +78,6 @@ func rendererForName(name string) Renderer {
return renderer
case "nemotron-3-nano":
return &Nemotron3NanoRenderer{}
case "functiongemma":
return &FunctionGemmaRenderer{}
default:
return nil
}

View File

@@ -17,7 +17,6 @@ import (
"strings"
"sync"
"golang.org/x/mod/semver"
"golang.org/x/sync/errgroup"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
@@ -105,16 +104,6 @@ func (f Modelfile) CreateRequest(relativeDir string) (*api.CreateRequest, error)
req.Renderer = c.Args
case "parser":
req.Parser = c.Args
case "requires":
// golang.org/x/mod/semver requires "v" prefix
requires := c.Args
if !strings.HasPrefix(requires, "v") {
requires = "v" + requires
}
if !semver.IsValid(requires) {
return nil, fmt.Errorf("requires must be a valid semver (e.g. 0.14.0)")
}
req.Requires = strings.TrimPrefix(requires, "v")
case "message":
role, msg, _ := strings.Cut(c.Args, ": ")
messages = append(messages, api.Message{Role: role, Content: msg})
@@ -333,7 +322,7 @@ func (c Command) String() string {
switch c.Name {
case "model":
fmt.Fprintf(&sb, "FROM %s", c.Args)
case "license", "template", "system", "adapter", "renderer", "parser", "requires":
case "license", "template", "system", "adapter", "renderer", "parser":
fmt.Fprintf(&sb, "%s %s", strings.ToUpper(c.Name), quote(c.Args))
case "message":
role, message, _ := strings.Cut(c.Args, ": ")
@@ -359,7 +348,7 @@ const (
var (
errMissingFrom = errors.New("no FROM line")
errInvalidMessageRole = errors.New("message role must be one of \"system\", \"user\", or \"assistant\"")
errInvalidCommand = errors.New("command must be one of \"from\", \"license\", \"template\", \"system\", \"adapter\", \"renderer\", \"parser\", \"parameter\", \"message\", or \"requires\"")
errInvalidCommand = errors.New("command must be one of \"from\", \"license\", \"template\", \"system\", \"adapter\", \"renderer\", \"parser\", \"parameter\", or \"message\"")
)
type ParserError struct {
@@ -619,7 +608,7 @@ func isValidMessageRole(role string) bool {
func isValidCommand(cmd string) bool {
switch strings.ToLower(cmd) {
case "from", "license", "template", "system", "adapter", "renderer", "parser", "parameter", "message", "requires":
case "from", "license", "template", "system", "adapter", "renderer", "parser", "parameter", "message":
return true
default:
return false

View File

@@ -61,7 +61,6 @@ func (s *Server) CreateHandler(c *gin.Context) {
config.Renderer = r.Renderer
config.Parser = r.Parser
config.Requires = r.Requires
for v := range r.Files {
if !fs.ValidPath(v) {
@@ -121,7 +120,7 @@ func (s *Server) CreateHandler(c *gin.Context) {
ch <- gin.H{"error": err.Error()}
}
if err == nil && !remote && (config.Renderer == "" || config.Parser == "" || config.Requires == "") {
if err == nil && !remote && (config.Renderer == "" || config.Parser == "") {
manifest, mErr := ParseNamedManifest(fromName)
if mErr == nil && manifest.Config.Digest != "" {
configPath, pErr := GetBlobsPath(manifest.Config.Digest)
@@ -135,9 +134,6 @@ func (s *Server) CreateHandler(c *gin.Context) {
if config.Parser == "" {
config.Parser = baseConfig.Parser
}
if config.Requires == "" {
config.Requires = baseConfig.Requires
}
}
cfgFile.Close()
}

View File

@@ -1106,7 +1106,6 @@ func GetModelInfo(req api.ShowRequest) (*api.ShowResponse, error) {
Messages: msgs,
Capabilities: m.Capabilities(),
ModifiedAt: manifest.fi.ModTime(),
Requires: m.Config.Requires,
}
if m.Config.RemoteHost != "" {

View File

@@ -363,7 +363,7 @@ func TestChatDebugRenderOnly(t *testing.T) {
DebugRenderOnly: true,
},
expectDebug: true,
expectTemplate: "[{\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"description\":\"Get weather information\",\"parameters\":{\"type\":\"\",\"properties\":null}}}]user: Get the weather\n",
expectTemplate: "[{\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"description\":\"Get weather information\",\"parameters\":{\"type\":\"\"}}}]user: Get the weather\n",
},
}

View File

@@ -9,7 +9,6 @@ type ConfigV2 struct {
FileType string `json:"file_type"` // shown as Quantization Level
Renderer string `json:"renderer,omitempty"`
Parser string `json:"parser,omitempty"`
Requires string `json:"requires,omitempty"`
RemoteHost string `json:"remote_host,omitempty"`
RemoteModel string `json:"remote_model,omitempty"`