Skip to content

Commit af54f21

Browse files
author
natn0
committed
Move constants to constants.py, slight refactor
1 parent 54ad9ca commit af54f21

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

letta/constants.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
ADMIN_PREFIX = "/v1/admin"
1212
API_PREFIX = "/v1"
13+
OLLAMA_API_PREFIX = "/v1"
1314
OPENAI_API_PREFIX = "/openai"
1415

1516
COMPOSIO_ENTITY_ENV_VAR_KEY = "COMPOSIO_ENTITY"
@@ -50,8 +51,9 @@
5051
# Max steps for agent loop
5152
DEFAULT_MAX_STEPS = 50
5253

53-
# minimum context window size
54+
# context window size
5455
MIN_CONTEXT_WINDOW = 4096
56+
DEFAULT_CONTEXT_WINDOW = 32000
5557

5658
# number of concurrent embedding requests to sent
5759
EMBEDDING_BATCH_SIZE = 200
@@ -63,6 +65,7 @@
6365
# embeddings
6466
MAX_EMBEDDING_DIM = 4096 # maximum supported embeding size - do NOT change or else DBs will need to be reset
6567
DEFAULT_EMBEDDING_CHUNK_SIZE = 300
68+
DEFAULT_EMBEDDING_DIM = 1024
6669

6770
# tokenizers
6871
EMBEDDING_TO_TOKENIZER_MAP = {

letta/schemas/providers/ollama.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import aiohttp
44
from pydantic import Field
55

6-
from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE
6+
from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, DEFAULT_CONTEXT_WINDOW, DEFAULT_EMBEDDING_DIM, OLLAMA_API_PREFIX
77
from letta.log import get_logger
88
from letta.schemas.embedding_config import EmbeddingConfig
99
from letta.schemas.enums import ProviderCategory, ProviderType
@@ -12,8 +12,6 @@
1212

1313
logger = get_logger(__name__)
1414

15-
ollama_prefix = "/v1"
16-
1715

1816
class OllamaProvider(OpenAIProvider):
1917
"""Ollama provider that uses the native /api/generate endpoint
@@ -41,8 +39,9 @@ async def list_llm_models_async(self) -> list[LLMConfig]:
4139
response_json = await response.json()
4240

4341
configs = []
44-
for model in response_json["models"]:
45-
model_details = await self._get_model_details_async(model["name"])
42+
for model in response_json.get("models", []):
43+
model_name = model["name"]
44+
model_details = await self._get_model_details_async(model_name)
4645
if not model_details or "completion" not in model_details.get("capabilities", []):
4746
continue
4847

@@ -53,17 +52,17 @@ async def list_llm_models_async(self) -> list[LLMConfig]:
5352
context_window = int(context_length)
5453

5554
if context_window is None:
56-
print(f"Ollama model {model['name']} has no context window, using default 32000")
57-
context_window = 32000
55+
logger.warning(f"Ollama model {model_name} has no context window, using default {DEFAULT_CONTEXT_WINDOW}")
56+
context_window = DEFAULT_CONTEXT_WINDOW
5857

5958
configs.append(
6059
LLMConfig(
61-
model=model["name"],
60+
model=model_name,
6261
model_endpoint_type=ProviderType.ollama,
63-
model_endpoint=f"{self.base_url}{ollama_prefix}",
62+
model_endpoint=f"{self.base_url}{OLLAMA_API_PREFIX}",
6463
model_wrapper=self.default_prompt_formatter,
6564
context_window=context_window,
66-
handle=self.get_handle(model["name"]),
65+
handle=self.get_handle(model_name),
6766
provider_name=self.name,
6867
provider_category=self.provider_category,
6968
)
@@ -83,8 +82,9 @@ async def list_embedding_models_async(self) -> list[EmbeddingConfig]:
8382
response_json = await response.json()
8483

8584
configs = []
86-
for model in response_json["models"]:
87-
model_details = await self._get_model_details_async(model["name"])
85+
for model in response_json.get("models", []):
86+
model_name = model["name"]
87+
model_details = await self._get_model_details_async(model_name)
8888
if not model_details or "embedding" not in model_details.get("capabilities", []):
8989
continue
9090

@@ -95,17 +95,17 @@ async def list_embedding_models_async(self) -> list[EmbeddingConfig]:
9595
embedding_dim = int(embedding_length)
9696

9797
if not embedding_dim:
98-
print(f"Ollama model {model['name']} has no embedding dimension, using default 1024")
99-
embedding_dim = 1024
98+
logger.warning(f"Ollama model {model_name} has no embedding dimension, using default {DEFAULT_EMBEDDING_DIM}")
99+
embedding_dim = DEFAULT_EMBEDDING_DIM
100100

101101
configs.append(
102102
EmbeddingConfig(
103-
embedding_model=model["name"],
103+
embedding_model=model_name,
104104
embedding_endpoint_type=ProviderType.ollama,
105-
embedding_endpoint=f"{self.base_url}{ollama_prefix}",
105+
embedding_endpoint=f"{self.base_url}{OLLAMA_API_PREFIX}",
106106
embedding_dim=embedding_dim,
107107
embedding_chunk_size=DEFAULT_EMBEDDING_CHUNK_SIZE,
108-
handle=self.get_handle(model["name"], is_embedding=True),
108+
handle=self.get_handle(model_name, is_embedding=True),
109109
)
110110
)
111111
return configs
@@ -125,5 +125,4 @@ async def _get_model_details_async(self, model_name: str) -> dict | None:
125125
return await response.json()
126126
except Exception as e:
127127
logger.warning(f"Failed to get model details for {model_name} with error: {e}")
128-
129-
return None
128+
return None

0 commit comments

Comments
 (0)