|
| 1 | +package ollama_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + // Packages |
| 10 | + |
| 11 | + llm "github.com/mutablelogic/go-llm" |
| 12 | + ollama "github.com/mutablelogic/go-llm/pkg/ollama" |
| 13 | + tool "github.com/mutablelogic/go-llm/pkg/tool" |
| 14 | + assert "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_completion_001(t *testing.T) { |
| 18 | + assert := assert.New(t) |
| 19 | + |
| 20 | + // Pull the model |
| 21 | + model, err := client.PullModel(context.TODO(), "qwen:0.5b", ollama.WithPullStatus(func(status *ollama.PullStatus) { |
| 22 | + t.Log(status) |
| 23 | + })) |
| 24 | + if err != nil { |
| 25 | + t.FailNow() |
| 26 | + } |
| 27 | + |
| 28 | + // Get a completion |
| 29 | + response, err := model.Completion(context.TODO(), "Hello, how are you?") |
| 30 | + if assert.NoError(err) { |
| 31 | + assert.NotEmpty(response) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func Test_completion_002(t *testing.T) { |
| 36 | + assert := assert.New(t) |
| 37 | + |
| 38 | + // Pull the model |
| 39 | + model, err := client.PullModel(context.TODO(), "qwen:0.5b", ollama.WithPullStatus(func(status *ollama.PullStatus) { |
| 40 | + t.Log(status) |
| 41 | + })) |
| 42 | + if err != nil { |
| 43 | + t.FailNow() |
| 44 | + } |
| 45 | + |
| 46 | + t.Run("Temperature", func(t *testing.T) { |
| 47 | + response, err := model.Completion(context.TODO(), "Tell me in less than five words why the sky is blue?", llm.WithTemperature(0.5)) |
| 48 | + if assert.NoError(err) { |
| 49 | + t.Log(response) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("TopP", func(t *testing.T) { |
| 54 | + response, err := model.Completion(context.TODO(), "Tell me in less than five words why the sky is blue?", llm.WithTopP(0.5)) |
| 55 | + if assert.NoError(err) { |
| 56 | + t.Log(response) |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("TopK", func(t *testing.T) { |
| 61 | + response, err := model.Completion(context.TODO(), "Tell me in less than five words why the sky is blue?", llm.WithTopK(50)) |
| 62 | + if assert.NoError(err) { |
| 63 | + t.Log(response) |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("Stop", func(t *testing.T) { |
| 68 | + response, err := model.Completion(context.TODO(), "Tell me in less than five words why the sky is blue?", llm.WithStopSequence("sky")) |
| 69 | + if assert.NoError(err) { |
| 70 | + t.Log(response) |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("System", func(t *testing.T) { |
| 75 | + response, err := model.Completion(context.TODO(), "Tell me in less than five words why the sky is blue?", llm.WithSystemPrompt("reply as if you are shakespeare")) |
| 76 | + if assert.NoError(err) { |
| 77 | + t.Log(response) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("Seed", func(t *testing.T) { |
| 82 | + response, err := model.Completion(context.TODO(), "Tell me in less than five words why the sky is blue?", llm.WithSeed(123)) |
| 83 | + if assert.NoError(err) { |
| 84 | + t.Log(response) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("Format", func(t *testing.T) { |
| 89 | + response, err := model.Completion(context.TODO(), "Why the sky is blue? Reply in JSON format", llm.WithFormat("json")) |
| 90 | + if assert.NoError(err) { |
| 91 | + t.Log(response) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("FrequencyPenalty", func(t *testing.T) { |
| 96 | + response, err := model.Completion(context.TODO(), "Why the sky is blue?", llm.WithFrequencyPenalty(1.0)) |
| 97 | + if assert.NoError(err) { |
| 98 | + t.Log(response) |
| 99 | + } |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +func Test_completion_003(t *testing.T) { |
| 104 | + assert := assert.New(t) |
| 105 | + |
| 106 | + // Pull the model |
| 107 | + model, err := client.PullModel(context.TODO(), "llama3.2-vision", ollama.WithPullStatus(func(status *ollama.PullStatus) { |
| 108 | + t.Log(status) |
| 109 | + })) |
| 110 | + if err != nil { |
| 111 | + t.FailNow() |
| 112 | + } |
| 113 | + |
| 114 | + t.Run("Vision", func(t *testing.T) { |
| 115 | + f, err := os.Open("testdata/guggenheim.jpg") |
| 116 | + if !assert.NoError(err) { |
| 117 | + t.FailNow() |
| 118 | + } |
| 119 | + defer f.Close() |
| 120 | + response, err := model.Completion(context.TODO(), "Describe this image", llm.WithAttachment(f)) |
| 121 | + if assert.NoError(err) { |
| 122 | + t.Log(response) |
| 123 | + } |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +func Test_completion_004(t *testing.T) { |
| 128 | + assert := assert.New(t) |
| 129 | + |
| 130 | + // Pull the model |
| 131 | + model, err := client.PullModel(context.TODO(), "mistral", ollama.WithPullStatus(func(status *ollama.PullStatus) { |
| 132 | + t.Log(status) |
| 133 | + })) |
| 134 | + if err != nil { |
| 135 | + t.FailNow() |
| 136 | + } |
| 137 | + |
| 138 | + // Test tool support |
| 139 | + t.Run("Toolkit", func(t *testing.T) { |
| 140 | + toolkit := tool.NewToolKit() |
| 141 | + toolkit.Register(&weather{}) |
| 142 | + |
| 143 | + session := model.Context(llm.WithToolKit(toolkit)) |
| 144 | + err := session.FromUser(context.TODO(), |
| 145 | + "What is the weather in the capital city of Germany?", |
| 146 | + ) |
| 147 | + if !assert.NoError(err) { |
| 148 | + t.FailNow() |
| 149 | + } |
| 150 | + |
| 151 | + assert.Equal("assistant", session.Role()) |
| 152 | + assert.Greater(session.Num(), 0) |
| 153 | + assert.NotEmpty(session.ToolCalls(0)) |
| 154 | + |
| 155 | + toolcalls := session.ToolCalls(0) |
| 156 | + assert.NotEmpty(toolcalls) |
| 157 | + assert.Equal("weather_in_city", toolcalls[0].Name()) |
| 158 | + |
| 159 | + results, err := toolkit.Run(context.TODO(), toolcalls...) |
| 160 | + if !assert.NoError(err) { |
| 161 | + t.FailNow() |
| 162 | + } |
| 163 | + |
| 164 | + assert.Len(results, len(toolcalls)) |
| 165 | + |
| 166 | + err = session.FromTool(context.TODO(), results...) |
| 167 | + if !assert.NoError(err) { |
| 168 | + t.FailNow() |
| 169 | + } |
| 170 | + }) |
| 171 | +} |
| 172 | + |
| 173 | +type weather struct { |
| 174 | + City string `json:"city" help:"The city to get the weather for" required:"true"` |
| 175 | +} |
| 176 | + |
| 177 | +func (weather) Name() string { |
| 178 | + return "weather_in_city" |
| 179 | +} |
| 180 | + |
| 181 | +func (weather) Description() string { |
| 182 | + return "Get the weather for a city" |
| 183 | +} |
| 184 | + |
| 185 | +func (w weather) Run(ctx context.Context) (any, error) { |
| 186 | + return fmt.Sprintf("The weather in %q is sunny and warm", w.City), nil |
| 187 | +} |
0 commit comments