2024-05-06 00:38:31 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import requests
|
2024-09-30 14:35:21 +03:00
|
|
|
import os
|
2024-05-06 00:38:31 +03:00
|
|
|
|
|
|
|
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:
|
2024-09-30 14:35:21 +03:00
|
|
|
host = os.getenv("OLLAMA_HOST", "127.0.0.1")
|
|
|
|
port = os.getenv("OLLAMA_PORT", "11434")
|
|
|
|
url = f"http://{host}:{port}/api/tags"
|
2024-05-06 00:38:31 +03:00
|
|
|
models = requests.get(url).json()["models"]
|
2024-09-30 14:35:21 +03:00
|
|
|
cls.models = [model["name"] for model in models]
|
2024-05-06 00:38:31 +03:00
|
|
|
cls.default_model = cls.models[0]
|
|
|
|
return cls.models
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
2024-09-30 14:35:21 +03:00
|
|
|
api_base: str = None,
|
2024-05-06 00:38:31 +03:00
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
2024-09-30 14:35:21 +03:00
|
|
|
if not api_base:
|
|
|
|
host = os.getenv("OLLAMA_HOST", "localhost")
|
|
|
|
port = os.getenv("OLLAMA_PORT", "11434")
|
|
|
|
api_base: str = f"http://{host}:{port}/v1"
|
2024-05-06 00:38:31 +03:00
|
|
|
return super().create_async_generator(
|
|
|
|
model, messages, api_base=api_base, **kwargs
|
|
|
|
)
|