summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/local/Ollama.py
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/Provider/local/Ollama.py')
-rw-r--r--g4f/Provider/local/Ollama.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/g4f/Provider/local/Ollama.py b/g4f/Provider/local/Ollama.py
new file mode 100644
index 00000000..c503a46a
--- /dev/null
+++ b/g4f/Provider/local/Ollama.py
@@ -0,0 +1,40 @@
+from __future__ import annotations
+
+import requests
+import os
+
+from ..needs_auth.Openai import Openai
+from ...typing import AsyncResult, Messages
+
+class Ollama(Openai):
+ label = "Ollama"
+ url = "https://ollama.com"
+ needs_auth = False
+ working = True
+
+ @classmethod
+ def get_models(cls):
+ if not cls.models:
+ host = os.getenv("OLLAMA_HOST", "127.0.0.1")
+ port = os.getenv("OLLAMA_PORT", "11434")
+ url = f"http://{host}:{port}/api/tags"
+ models = requests.get(url).json()["models"]
+ cls.models = [model["name"] for model in models]
+ cls.default_model = cls.models[0]
+ return cls.models
+
+ @classmethod
+ def create_async_generator(
+ cls,
+ model: str,
+ messages: Messages,
+ api_base: str = None,
+ **kwargs
+ ) -> AsyncResult:
+ if not api_base:
+ host = os.getenv("OLLAMA_HOST", "localhost")
+ port = os.getenv("OLLAMA_PORT", "11434")
+ api_base: str = f"http://{host}:{port}/v1"
+ return super().create_async_generator(
+ model, messages, api_base=api_base, **kwargs
+ )