Skip to content

Commit 5cd12fa

Browse files
authored
bump create-llama to 0.11 and update event handler (#260)
1 parent 72b7195 commit 5cd12fa

File tree

14 files changed

+225
-81
lines changed

14 files changed

+225
-81
lines changed

.changeset/slow-papayas-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-llama": patch
3+
---
4+
5+
Use callback manager properly

.changeset/sour-donkeys-develop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-llama": patch
3+
---
4+
5+
Bump create-llama version to 0.11.1

helpers/python.ts

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
InstallTemplateArgs,
1313
ModelConfig,
1414
TemplateDataSource,
15+
TemplateType,
1516
TemplateVectorDB,
1617
} from "./types";
1718

@@ -26,6 +27,7 @@ const getAdditionalDependencies = (
2627
vectorDb?: TemplateVectorDB,
2728
dataSources?: TemplateDataSource[],
2829
tools?: Tool[],
30+
templateType?: TemplateType,
2931
) => {
3032
const dependencies: Dependency[] = [];
3133

@@ -128,7 +130,7 @@ const getAdditionalDependencies = (
128130
case "llamacloud":
129131
dependencies.push({
130132
name: "llama-index-indices-managed-llama-cloud",
131-
version: "^0.2.7",
133+
version: "^0.3.0",
132134
});
133135
break;
134136
}
@@ -147,77 +149,99 @@ const getAdditionalDependencies = (
147149
case "ollama":
148150
dependencies.push({
149151
name: "llama-index-llms-ollama",
150-
version: "0.1.2",
152+
version: "0.3.0",
151153
});
152154
dependencies.push({
153155
name: "llama-index-embeddings-ollama",
154-
version: "0.1.2",
156+
version: "0.3.0",
155157
});
156158
break;
157159
case "openai":
158-
dependencies.push({
159-
name: "llama-index-agent-openai",
160-
version: "0.2.6",
161-
});
160+
if (templateType !== "multiagent") {
161+
dependencies.push({
162+
name: "llama-index-llms-openai",
163+
version: "^0.2.0",
164+
});
165+
dependencies.push({
166+
name: "llama-index-embeddings-openai",
167+
version: "^0.2.3",
168+
});
169+
dependencies.push({
170+
name: "llama-index-agent-openai",
171+
version: "^0.3.0",
172+
});
173+
}
162174
break;
163175
case "groq":
176+
// Fastembed==0.2.0 does not support python3.13 at the moment
177+
// Fixed the python version less than 3.13
178+
dependencies.push({
179+
name: "python",
180+
version: "^3.11,<3.13",
181+
});
164182
dependencies.push({
165183
name: "llama-index-llms-groq",
166-
version: "0.1.4",
184+
version: "0.2.0",
167185
});
168186
dependencies.push({
169187
name: "llama-index-embeddings-fastembed",
170-
version: "^0.1.4",
188+
version: "^0.2.0",
171189
});
172190
break;
173191
case "anthropic":
192+
// Fastembed==0.2.0 does not support python3.13 at the moment
193+
// Fixed the python version less than 3.13
194+
dependencies.push({
195+
name: "python",
196+
version: "^3.11,<3.13",
197+
});
174198
dependencies.push({
175199
name: "llama-index-llms-anthropic",
176-
version: "0.1.10",
200+
version: "0.3.0",
177201
});
178202
dependencies.push({
179203
name: "llama-index-embeddings-fastembed",
180-
version: "^0.1.4",
204+
version: "^0.2.0",
181205
});
182206
break;
183207
case "gemini":
184208
dependencies.push({
185209
name: "llama-index-llms-gemini",
186-
version: "0.1.10",
210+
version: "0.3.4",
187211
});
188212
dependencies.push({
189213
name: "llama-index-embeddings-gemini",
190-
version: "0.1.6",
214+
version: "^0.2.0",
191215
});
192216
break;
193217
case "mistral":
194218
dependencies.push({
195219
name: "llama-index-llms-mistralai",
196-
version: "0.1.17",
220+
version: "0.2.1",
197221
});
198222
dependencies.push({
199223
name: "llama-index-embeddings-mistralai",
200-
version: "0.1.4",
224+
version: "0.2.0",
201225
});
202226
break;
203227
case "azure-openai":
204228
dependencies.push({
205229
name: "llama-index-llms-azure-openai",
206-
version: "0.1.10",
230+
version: "0.2.0",
207231
});
208232
dependencies.push({
209233
name: "llama-index-embeddings-azure-openai",
210-
version: "0.1.11",
234+
version: "0.2.4",
211235
});
212236
break;
213237
case "t-systems":
214238
dependencies.push({
215239
name: "llama-index-agent-openai",
216-
version: "0.2.2",
240+
version: "0.3.0",
217241
});
218242
dependencies.push({
219243
name: "llama-index-llms-openai-like",
220-
version: "0.1.3",
244+
version: "0.2.0",
221245
});
222246
break;
223247
}
@@ -227,7 +251,7 @@ const getAdditionalDependencies = (
227251

228252
const mergePoetryDependencies = (
229253
dependencies: Dependency[],
230-
existingDependencies: Record<string, Omit<Dependency, "name">>,
254+
existingDependencies: Record<string, Omit<Dependency, "name"> | string>,
231255
) => {
232256
for (const dependency of dependencies) {
233257
let value = existingDependencies[dependency.name] ?? {};
@@ -246,7 +270,13 @@ const mergePoetryDependencies = (
246270
);
247271
}
248272

249-
existingDependencies[dependency.name] = value;
273+
// Serialize separately only if extras are provided
274+
if (value.extras && value.extras.length > 0) {
275+
existingDependencies[dependency.name] = value;
276+
} else {
277+
// Otherwise, serialize just the version string
278+
existingDependencies[dependency.name] = value.version;
279+
}
250280
}
251281
};
252282

@@ -388,6 +418,7 @@ export const installPythonTemplate = async ({
388418
vectorDb,
389419
dataSources,
390420
tools,
421+
template,
391422
);
392423

393424
if (observability && observability !== "none") {

templates/components/engines/python/agent/engine.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import os
22

3-
from app.engine.index import get_index
3+
from app.engine.index import IndexConfig, get_index
44
from app.engine.tools import ToolFactory
55
from llama_index.core.agent import AgentRunner
6+
from llama_index.core.callbacks import CallbackManager
67
from llama_index.core.settings import Settings
78
from llama_index.core.tools.query_engine import QueryEngineTool
89

910

10-
def get_chat_engine(filters=None, params=None):
11+
def get_chat_engine(filters=None, params=None, event_handlers=None):
1112
system_prompt = os.getenv("SYSTEM_PROMPT")
1213
top_k = int(os.getenv("TOP_K", 0))
1314
tools = []
15+
callback_manager = CallbackManager(handlers=event_handlers or [])
1416

1517
# Add query tool if index exists
16-
index = get_index()
18+
index_config = IndexConfig(callback_manager=callback_manager, **(params or {}))
19+
index = get_index(index_config)
1720
if index is not None:
1821
query_engine = index.as_query_engine(
1922
filters=filters, **({"similarity_top_k": top_k} if top_k != 0 else {})
@@ -28,5 +31,6 @@ def get_chat_engine(filters=None, params=None):
2831
llm=Settings.llm,
2932
tools=tools,
3033
system_prompt=system_prompt,
34+
callback_manager=callback_manager,
3135
verbose=True,
3236
)

templates/components/engines/python/chat/engine.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
import os
22

3-
from app.engine.index import get_index
3+
from app.engine.index import IndexConfig, get_index
44
from app.engine.node_postprocessors import NodeCitationProcessor
55
from fastapi import HTTPException
6+
from llama_index.core.callbacks import CallbackManager
67
from llama_index.core.chat_engine import CondensePlusContextChatEngine
8+
from llama_index.core.memory import ChatMemoryBuffer
9+
from llama_index.core.settings import Settings
710

811

9-
def get_chat_engine(filters=None, params=None):
12+
def get_chat_engine(filters=None, params=None, event_handlers=None):
1013
system_prompt = os.getenv("SYSTEM_PROMPT")
1114
citation_prompt = os.getenv("SYSTEM_CITATION_PROMPT", None)
1215
top_k = int(os.getenv("TOP_K", 0))
16+
llm = Settings.llm
17+
memory = ChatMemoryBuffer.from_defaults(
18+
token_limit=llm.metadata.context_window - 256
19+
)
20+
callback_manager = CallbackManager(handlers=event_handlers or [])
1321

1422
node_postprocessors = []
1523
if citation_prompt:
1624
node_postprocessors = [NodeCitationProcessor()]
1725
system_prompt = f"{system_prompt}\n{citation_prompt}"
1826

19-
index = get_index(params)
27+
index_config = IndexConfig(callback_manager=callback_manager, **(params or {}))
28+
index = get_index(index_config)
2029
if index is None:
2130
raise HTTPException(
2231
status_code=500,
@@ -29,8 +38,11 @@ def get_chat_engine(filters=None, params=None):
2938
filters=filters, **({"similarity_top_k": top_k} if top_k != 0 else {})
3039
)
3140

32-
return CondensePlusContextChatEngine.from_defaults(
41+
return CondensePlusContextChatEngine(
42+
llm=llm,
43+
memory=memory,
3344
system_prompt=system_prompt,
3445
retriever=retriever,
3546
node_postprocessors=node_postprocessors,
47+
callback_manager=callback_manager,
3648
)
Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,87 @@
11
import logging
22
import os
3-
from llama_index.indices.managed.llama_cloud import LlamaCloudIndex
3+
from typing import Optional
4+
5+
from llama_index.core.callbacks import CallbackManager
46
from llama_index.core.ingestion.api_utils import (
57
get_client as llama_cloud_get_client,
68
)
9+
from llama_index.indices.managed.llama_cloud import LlamaCloudIndex
10+
from pydantic import BaseModel, Field, validator
711

812
logger = logging.getLogger("uvicorn")
913

1014

11-
def get_client():
12-
return llama_cloud_get_client(
13-
os.getenv("LLAMA_CLOUD_API_KEY"),
14-
os.getenv("LLAMA_CLOUD_BASE_URL"),
15+
class LlamaCloudConfig(BaseModel):
16+
# Private attributes
17+
api_key: str = Field(
18+
default=os.getenv("LLAMA_CLOUD_API_KEY"),
19+
exclude=True, # Exclude from the model representation
20+
)
21+
base_url: Optional[str] = Field(
22+
default=os.getenv("LLAMA_CLOUD_BASE_URL"),
23+
exclude=True,
1524
)
25+
organization_id: Optional[str] = Field(
26+
default=os.getenv("LLAMA_CLOUD_ORGANIZATION_ID"),
27+
exclude=True,
28+
)
29+
# Configuration attributes, can be set by the user
30+
pipeline: str = Field(
31+
description="The name of the pipeline to use",
32+
default=os.getenv("LLAMA_CLOUD_INDEX_NAME"),
33+
)
34+
project: str = Field(
35+
description="The name of the LlamaCloud project",
36+
default=os.getenv("LLAMA_CLOUD_PROJECT_NAME"),
37+
)
38+
39+
# Validate and throw error if the env variables are not set before starting the app
40+
@validator("pipeline", "project", "api_key", pre=True, always=True)
41+
@classmethod
42+
def validate_env_vars(cls, value):
43+
if value is None:
44+
raise ValueError(
45+
"Please set LLAMA_CLOUD_INDEX_NAME, LLAMA_CLOUD_PROJECT_NAME and LLAMA_CLOUD_API_KEY"
46+
" to your environment variables or config them in .env file"
47+
)
48+
return value
1649

50+
def to_client_kwargs(self) -> dict:
51+
return {
52+
"api_key": self.api_key,
53+
"base_url": self.base_url,
54+
}
1755

18-
def get_index(params=None):
19-
configParams = params or {}
20-
pipelineConfig = configParams.get("llamaCloudPipeline", {})
21-
name = pipelineConfig.get("pipeline", os.getenv("LLAMA_CLOUD_INDEX_NAME"))
22-
project_name = pipelineConfig.get("project", os.getenv("LLAMA_CLOUD_PROJECT_NAME"))
23-
api_key = os.getenv("LLAMA_CLOUD_API_KEY")
24-
base_url = os.getenv("LLAMA_CLOUD_BASE_URL")
25-
organization_id = os.getenv("LLAMA_CLOUD_ORGANIZATION_ID")
26-
27-
if name is None or project_name is None or api_key is None:
28-
raise ValueError(
29-
"Please set LLAMA_CLOUD_INDEX_NAME, LLAMA_CLOUD_PROJECT_NAME and LLAMA_CLOUD_API_KEY"
30-
" to your environment variables or config them in .env file"
31-
)
32-
33-
index = LlamaCloudIndex(
34-
name=name,
35-
project_name=project_name,
36-
api_key=api_key,
37-
base_url=base_url,
38-
organization_id=organization_id,
56+
57+
class IndexConfig(BaseModel):
58+
llama_cloud_pipeline_config: LlamaCloudConfig = Field(
59+
default=LlamaCloudConfig(),
60+
alias="llamaCloudPipeline",
61+
)
62+
callback_manager: Optional[CallbackManager] = Field(
63+
default=None,
3964
)
4065

66+
def to_index_kwargs(self) -> dict:
67+
return {
68+
"name": self.llama_cloud_pipeline_config.pipeline,
69+
"project_name": self.llama_cloud_pipeline_config.project,
70+
"api_key": self.llama_cloud_pipeline_config.api_key,
71+
"base_url": self.llama_cloud_pipeline_config.base_url,
72+
"organization_id": self.llama_cloud_pipeline_config.organization_id,
73+
"callback_manager": self.callback_manager,
74+
}
75+
76+
77+
def get_index(config: IndexConfig = None):
78+
if config is None:
79+
config = IndexConfig()
80+
index = LlamaCloudIndex(**config.to_index_kwargs())
81+
4182
return index
83+
84+
85+
def get_client():
86+
config = LlamaCloudConfig()
87+
return llama_cloud_get_client(**config.to_client_kwargs())

0 commit comments

Comments
 (0)