-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add the ability to start Ollama when it's stopped #3653
Open
fbricon
wants to merge
4
commits into
continuedev:main
Choose a base branch
from
fbricon:start-ollama
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -55,6 +55,7 @@ import { | |||||
toCompleteBody, | ||||||
toFimBody, | ||||||
} from "./openaiTypeConverters.js"; | ||||||
import { isOllamaInstalled } from "../util/ollamaHelper.js"; | ||||||
|
||||||
export abstract class BaseLLM implements ILLM { | ||||||
static providerName: string; | ||||||
|
@@ -414,9 +415,11 @@ export abstract class BaseLLM implements ILLM { | |||||
e.code === "ECONNREFUSED" && | ||||||
e.message.includes("http://127.0.0.1:11434") | ||||||
) { | ||||||
throw new Error( | ||||||
"Failed to connect to local Ollama instance, please ensure Ollama is both installed and running. You can download Ollama from https://ollama.ai.", | ||||||
); | ||||||
const message = (await isOllamaInstalled()) ? | ||||||
"Failed to connect to local Ollama instance. Ollama appears to be stopped. It needs to be running." : | ||||||
"Failed to connect to local Ollama instance, please ensure Ollama is both installed and running. You can download Ollama from https://ollama.ai." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The notification text is a bit long here. Personally I'd drop the download link - it's too much detail for a notification and you'd hope that someone could figure out what to do.
Suggested change
|
||||||
; | ||||||
throw new Error(message); | ||||||
} | ||||||
} | ||||||
throw new Error(e.message); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import path from "node:path"; | ||
import { IDE } from ".."; | ||
import { exec } from "node:child_process"; | ||
|
||
export async function isOllamaInstalled(): Promise<boolean> { | ||
return new Promise((resolve, _reject) => { | ||
const command = process.platform === "win32" ? "where.exe ollama" : "which ollama"; | ||
exec(command, (error, _stdout, _stderr) => { | ||
resolve(!error); | ||
}); | ||
}); | ||
} | ||
|
||
export async function startLocalOllama(ide: IDE): Promise<void> { | ||
let startCommand: string | undefined; | ||
|
||
switch (process.platform) { | ||
case "darwin"://MacOS | ||
startCommand = "open -a Ollama.app\n"; | ||
break; | ||
|
||
case "win32"://Windows | ||
startCommand = `& "ollama app.exe"\n`; | ||
break; | ||
|
||
default: //Linux... | ||
const start_script_path = path.resolve(__dirname, './start_ollama.sh'); | ||
if (await ide.fileExists(`file:/${start_script_path}`)) { | ||
startCommand = `set -e && chmod +x ${start_script_path} && ${start_script_path}\n`; | ||
console.log(`Ollama Linux startup script at : ${start_script_path}`); | ||
} else { | ||
ide.showToast("error", `Cannot start Ollama: could not find ${start_script_path}!`) | ||
} | ||
} | ||
if (startCommand) { | ||
return ide.runCommand(startCommand, { | ||
reuseTerminal: true, | ||
terminalName: "Start Ollama" | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/bin/bash | ||
|
||
# Timeout for waiting for Ollama to start (in seconds) | ||
readonly TIMEOUT=60 | ||
readonly OLLAMA_API_URL="http://localhost:11434/api/version" | ||
|
||
# Check if a command exists | ||
command_exists() { | ||
command -v "$1" >/dev/null 2>&1 | ||
} | ||
|
||
# Check if Ollama is already running | ||
ollama_is_running() { | ||
VERSION=$(curl -s --fail --max-time 1 "$OLLAMA_API_URL" | grep '"version"' | awk -F: '{ print $2 }' | sed -e 's/.*"\([^"]*\)".*/\1/') | ||
test -n "$VERSION" | ||
} | ||
|
||
# Determine if running in a container environment | ||
in_container() { | ||
[[ -f /.dockerenv || -f /run/.containerenv ]] | ||
} | ||
|
||
# Start Ollama service or background process | ||
start_ollama() { | ||
local start_method="${1:-background}" | ||
echo "Starting Ollama ($start_method)..." | ||
|
||
if [[ "$start_method" == "service" ]]; then | ||
if in_container; then | ||
service ollama start || return $? | ||
else | ||
sudo systemctl start ollama || return $? | ||
fi | ||
else | ||
nohup ollama serve >/dev/null 2>&1 & | ||
fi | ||
|
||
# Wait for Ollama to start. TIMEOUT * 4, since we sleep 1/4 sec on each iteration | ||
for _ in $(seq 1 $((TIMEOUT * 4))); do | ||
if ollama_is_running; then | ||
echo -e "\nOllama started successfully." | ||
return 0 | ||
fi | ||
(( _ % 2 == 0 )) && printf "." | ||
sleep 0.25 | ||
done | ||
|
||
echo -e "\nTimeout: Failed to start Ollama." | ||
return 1 | ||
} | ||
|
||
# Main script execution | ||
main() { | ||
# Early exit if Ollama is already running | ||
if ollama_is_running; then | ||
echo "Ollama is already running." | ||
return 0 | ||
fi | ||
|
||
# Try starting via service if possible | ||
if in_container; then | ||
if service --status-all 2>&1 | grep -qw 'ollama'; then | ||
start_ollama service || return $? | ||
return 0 | ||
fi | ||
elif command_exists systemctl; then | ||
if systemctl list-unit-files ollama.service >/dev/null 2>&1; then | ||
start_ollama service || return $? | ||
return 0 | ||
fi | ||
fi | ||
|
||
# Fallback to background process | ||
if command_exists ollama; then | ||
start_ollama || return 1 | ||
else | ||
echo "Error: Ollama is not installed or not in the PATH." >&2 | ||
return 1 | ||
fi | ||
} | ||
|
||
# Run the main function and exit with its status | ||
main | ||
exit $? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Three sentences feels a bit hard to read, and reads a bit abruptly. Reducing it down to two sentences would be easier to digest.