Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ollama): support calling the Ollama local process #2923

Merged
merged 42 commits into from
Jan 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
256a0d1
feat: support running ollama from the local binary
mdelapenya Dec 3, 2024
c43e5af
fix: wrong working dir at CI
mdelapenya Dec 3, 2024
fa2b345
chore: extract wait to a function
mdelapenya Dec 3, 2024
3ac88ef
chore: print local binary logs on error
mdelapenya Dec 3, 2024
9e63a7e
chore: remove debug logs
mdelapenya Dec 3, 2024
57ca76a
fix(ci): kill ollama before the tests
mdelapenya Dec 3, 2024
e4d2234
chore: stop ollama using systemctl
mdelapenya Dec 3, 2024
01134eb
chore: support setting log file from the env
mdelapenya Dec 4, 2024
5c1b404
chore: support running ollama commands, only
mdelapenya Dec 4, 2024
ce04a0e
fix: release lock on error
mdelapenya Dec 13, 2024
99e2655
chore: add more test coverage for the option
mdelapenya Dec 13, 2024
6c50334
chore: simplify useLocal checks
mdelapenya Dec 13, 2024
6a06b4d
chore: simpolify
mdelapenya Dec 13, 2024
bd85c0e
chore: pass context to runLocal
mdelapenya Dec 13, 2024
6239947
chore: move ctx to the right scope
mdelapenya Dec 13, 2024
811eb6d
chore: remove not needed
mdelapenya Dec 13, 2024
5556971
chore: use a container function
mdelapenya Dec 13, 2024
c68ff22
chore: support reading OLLAMA_HOST
mdelapenya Dec 13, 2024
b5e9874
chore: return error with copy APIs
mdelapenya Dec 13, 2024
c39e554
chore: simply execute the script
mdelapenya Dec 13, 2024
556c2f5
chore: simplify var initialisation
mdelapenya Dec 13, 2024
d3e7a49
chore: return nil
mdelapenya Dec 13, 2024
c38a640
fix: return errors on terminate
mdelapenya Dec 13, 2024
77a39f3
chore: remove options type
mdelapenya Dec 13, 2024
ffa0b2a
chore: use a map
mdelapenya Dec 13, 2024
6c39254
chor: simplify error on wait
mdelapenya Dec 13, 2024
91396ee
chore: wrap start logic around the localContext
mdelapenya Dec 16, 2024
7fa26ee
chor: fold
mdelapenya Dec 16, 2024
ebad12c
chore: merge wait into start
mdelapenya Dec 16, 2024
88f58af
fix: use proper ContainersState
mdelapenya Dec 16, 2024
80c76f9
fix: remove extra conversion
mdelapenya Dec 16, 2024
8c0ee3d
chore: handle remove log file errors properly
mdelapenya Dec 16, 2024
de1339a
chore: go back to string in env vars
mdelapenya Dec 16, 2024
8a18b3b
refactor(ollama): local process
stevenh Dec 17, 2024
2a3a30d
chore(ollama): refactor local to use log sub match.
stevenh Dec 18, 2024
5e77b27
feat(ollama): validate container request
stevenh Dec 18, 2024
5c0486a
chore(ollama): remove temporary test
stevenh Dec 18, 2024
bbd6242
feat(ollama): configurable local process binary
stevenh Dec 20, 2024
cb684b4
docs(ollama): detail local process supported fields
stevenh Dec 20, 2024
9be6309
docs(ollama): update local process site docs
stevenh Dec 20, 2024
4c3a06c
chore: refactor to support TerminateOption
stevenh Jan 2, 2025
39e7af4
fix: remove unused var
stevenh Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions modules/ollama/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
_ testcontainers.ContainerCustomizer = (*localProcess)(nil)

// defaultStopTimeout is the default timeout for stopping the local Ollama process.
defaultStopTimeout = time.Second * 5

Check failure on line 47 in modules/ollama/local.go

View workflow job for this annotation

GitHub Actions / test-modules (1.22.x, ubuntu-latest, ollama) / modules/ollama/ubuntu-latest/1.22.x

var `defaultStopTimeout` is unused (unused)

Check failure on line 47 in modules/ollama/local.go

View workflow job for this annotation

GitHub Actions / test-modules (1.x, ubuntu-latest, ollama) / modules/ollama/ubuntu-latest/1.x

var `defaultStopTimeout` is unused (unused)
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

// zeroTime is the zero time value.
zeroTime time.Time
Expand Down Expand Up @@ -213,7 +213,7 @@

// Run the ollama serve command in background.
if err = cmd.Start(); err != nil {
return fmt.Errorf("start ollama serve: %w", errors.Join(err, c.cleanupLog()))
return fmt.Errorf("start ollama serve: %w", errors.Join(err, c.cleanup()))
}

// Past this point, the process was started successfully.
Expand Down Expand Up @@ -548,36 +548,44 @@

// Terminate implements testcontainers.Container interface for the local Ollama binary.
// It stops the local Ollama process, removing the log file.
func (c *localProcess) Terminate(ctx context.Context) error {
func (c *localProcess) Terminate(ctx context.Context, opts ...testcontainers.TerminateOption) error {
options := testcontainers.NewTerminateOptions(ctx, opts...)
// First try to stop gracefully.
if err := c.Stop(ctx, &defaultStopTimeout); !c.isCleanupSafe(err) {
if err := c.Stop(options.Context(), options.StopTimeout()); !c.isCleanupSafe(err) {
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("stop: %w", err)
}

var errs []error
if c.IsRunning() {
// Still running, force kill.
if err := c.cmd.Process.Kill(); !c.isCleanupSafe(err) {
return fmt.Errorf("kill: %w", err)
// Best effort so we can continue with the cleanup.
errs = append(errs, fmt.Errorf("kill: %w", err))
}

// Wait for the process to exit so capture any error.
// Wait for the process to exit so we can capture any error.
c.wg.Wait()
}

c.mtx.Lock()
exitErr := c.exitErr
c.mtx.Unlock()
errs = append(errs, c.cleanup(), options.Cleanup())

return errors.Join(exitErr, c.cleanupLog())
return errors.Join(errs...)
}

// cleanupLog closes the log file and removes it.
func (c *localProcess) cleanupLog() error {
// cleanup performs all clean up, closing and removing the log file if set.
func (c *localProcess) cleanup() error {
c.mtx.Lock()
defer c.mtx.Unlock()

if c.logFile == nil {
return nil
return c.exitErr
}

var errs []error
if c.exitErr != nil {
errs = append(errs, fmt.Errorf("exit: %w", c.exitErr))
}

if err := c.logFile.Close(); err != nil {
errs = append(errs, fmt.Errorf("close log: %w", err))
}
Expand Down
Loading