Skip to content

Commit

Permalink
Make ollama base_url more configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Dec 9, 2024
1 parent fff7dbb commit b6c8bb9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions chatlas/_ollama.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from pathlib import Path
import json
import re
import urllib.request
Expand All @@ -19,7 +20,7 @@ def ChatOllama(
*,
system_prompt: Optional[str] = None,
turns: Optional[list[Turn]] = None,
base_url: str = "http://localhost:11434/v1",
base_url: str = "http://localhost:11434",
seed: Optional[int] = None,
kwargs: Optional["ChatClientArgs"] = None,
) -> Chat["SubmitInputArgs", ChatCompletion]:
Expand Down Expand Up @@ -89,6 +90,9 @@ def ChatOllama(
`ChatOllama` currently doesn't work with streaming tools, and tool calling more
generally doesn't seem to work very well with currently available models.
"""

base_url = re.sub("/+$", "", base_url)

if not has_ollama(base_url):
raise RuntimeError("Can't find locally running ollama.")

Expand All @@ -101,22 +105,20 @@ def ChatOllama(
return ChatOpenAI(
system_prompt=system_prompt,
turns=turns,
base_url=base_url,
base_url=f"{base_url}/v1",
model=model,
seed=seed,
kwargs=kwargs,
)


def ollama_models(base_url: str) -> list[str]:
base_url = re.sub("/v[0-9]+$", "", base_url)
res = urllib.request.urlopen(url=f"{base_url}/api/tags")
data = json.loads(res.read())
return [re.sub(":latest$", "", x["name"]) for x in data["models"]]


def has_ollama(base_url):
base_url = re.sub("/v[0-9]+$", "", base_url)
try:
urllib.request.urlopen(url=f"{base_url}/api/tags")
return True
Expand Down

0 comments on commit b6c8bb9

Please sign in to comment.