From 167884726809abb15f339cb50a033b1b68bd9648 Mon Sep 17 00:00:00 2001 From: "Huu Le (Lee)" <39040748+leehuwuj@users.noreply.github.com> Date: Thu, 22 Feb 2024 13:50:53 +0700 Subject: [PATCH] Upgrade llama-index version to v0.10+ for create-llama (#556) --- helpers/index.ts | 6 ++ helpers/python.ts | 60 ++++++++++++++++--- helpers/tools.ts | 50 ++++++++++++++-- helpers/types.ts | 3 +- index.ts | 18 +----- questions.ts | 7 ++- .../engines/python/agent/__init__.py | 36 ++--------- .../components/engines/python/agent/tools.py | 8 +-- .../components/loaders/python/file/loader.py | 4 +- .../components/loaders/python/web/loader.py | 4 +- .../vectordbs/python/mongo/context.py | 14 ----- .../vectordbs/python/mongo/generate.py | 21 +++---- .../vectordbs/python/mongo/index.py | 13 ++-- .../vectordbs/python/mongo/settings.py | 10 ++++ .../vectordbs/python/none/context.py | 14 ----- .../vectordbs/python/none/generate.py | 24 ++++---- .../components/vectordbs/python/none/index.py | 18 +++--- .../vectordbs/python/none/settings.py | 10 ++++ .../components/vectordbs/python/pg/context.py | 14 ----- .../vectordbs/python/pg/generate.py | 19 +++--- .../components/vectordbs/python/pg/index.py | 15 ++--- .../vectordbs/python/pg/settings.py | 10 ++++ .../components/vectordbs/python/pg/utils.py | 2 +- .../vectordbs/python/pinecone/context.py | 14 ----- .../vectordbs/python/pinecone/generate.py | 21 +++---- .../vectordbs/python/pinecone/index.py | 13 ++-- .../vectordbs/python/pinecone/settings.py | 10 ++++ .../simple/fastapi/app/api/routers/chat.py | 8 +-- templates/types/simple/fastapi/app/context.py | 11 ---- .../simple/fastapi/app/engine/__init__.py | 8 +-- .../types/simple/fastapi/app/settings.py | 8 +++ templates/types/simple/fastapi/pyproject.toml | 6 +- .../streaming/fastapi/app/api/routers/chat.py | 11 ++-- .../types/streaming/fastapi/app/context.py | 11 ---- .../streaming/fastapi/app/engine/__init__.py | 8 +-- .../types/streaming/fastapi/app/settings.py | 8 +++ .../types/streaming/fastapi/pyproject.toml | 6 +- 37 files changed, 260 insertions(+), 263 deletions(-) delete mode 100644 templates/components/vectordbs/python/mongo/context.py create mode 100644 templates/components/vectordbs/python/mongo/settings.py delete mode 100644 templates/components/vectordbs/python/none/context.py create mode 100644 templates/components/vectordbs/python/none/settings.py delete mode 100644 templates/components/vectordbs/python/pg/context.py create mode 100644 templates/components/vectordbs/python/pg/settings.py delete mode 100644 templates/components/vectordbs/python/pinecone/context.py create mode 100644 templates/components/vectordbs/python/pinecone/settings.py delete mode 100644 templates/types/simple/fastapi/app/context.py create mode 100644 templates/types/simple/fastapi/app/settings.py delete mode 100644 templates/types/streaming/fastapi/app/context.py create mode 100644 templates/types/streaming/fastapi/app/settings.py diff --git a/helpers/index.ts b/helpers/index.ts index 237bbeca..70a11244 100644 --- a/helpers/index.ts +++ b/helpers/index.ts @@ -60,6 +60,12 @@ const createEnvLocalFile = async ( content += `PG_CONNECTION_STRING=\n`; break; } + case "pinecone": { + content += `PINECONE_API_KEY=\n`; + content += `PINECONE_ENVIRONMENT=\n`; + content += `PINECONE_INDEX_NAME=\n`; + break; + } } switch (opts?.dataSource?.type) { diff --git a/helpers/python.ts b/helpers/python.ts index 6ba0a63e..39be2f32 100644 --- a/helpers/python.ts +++ b/helpers/python.ts @@ -6,8 +6,12 @@ import terminalLink from "terminal-link"; import { copy } from "./copy"; import { templatesDir } from "./dir"; import { isPoetryAvailable, tryPoetryInstall } from "./poetry"; -import { getToolConfig } from "./tools"; -import { InstallTemplateArgs, TemplateVectorDB } from "./types"; +import { Tool } from "./tools"; +import { + InstallTemplateArgs, + TemplateDataSource, + TemplateVectorDB, +} from "./types"; interface Dependency { name: string; @@ -15,25 +19,59 @@ interface Dependency { extras?: string[]; } -const getAdditionalDependencies = (vectorDb?: TemplateVectorDB) => { +const getAdditionalDependencies = ( + vectorDb?: TemplateVectorDB, + dataSource?: TemplateDataSource, + tools?: Tool[], +) => { const dependencies: Dependency[] = []; + // Add vector db dependencies switch (vectorDb) { case "mongo": { dependencies.push({ - name: "pymongo", - version: "^4.6.1", + name: "llama-index-vector-stores-mongodb", + version: "^0.1.3", }); break; } case "pg": { dependencies.push({ - name: "llama-index", - extras: ["postgres"], + name: "llama-index-vector-stores-postgres", + version: "^0.1.1", }); } + case "pinecone": { + dependencies.push({ + name: "llama-index-vector-stores-pinecone", + version: "^0.1.3", + }); + break; + } } + // Add data source dependencies + const dataSourceType = dataSource?.type; + if (dataSourceType === "file" || dataSourceType === "folder") { + // llama-index-readers-file (pdf, excel, csv) is already included in llama_index package + dependencies.push({ + name: "docx2txt", + version: "^0.8", + }); + } else if (dataSourceType === "web") { + dependencies.push({ + name: "llama-index-readers-web", + version: "^0.1.6", + }); + } + + // Add tools dependencies + tools?.forEach((tool) => { + tool.dependencies?.forEach((dep) => { + dependencies.push(dep); + }); + }); + return dependencies; }; @@ -190,7 +228,7 @@ export const installPythonTemplate = async ({ // Write tools_config.json const configContent: Record<string, any> = {}; tools.forEach((tool) => { - configContent[tool] = getToolConfig(tool) ?? {}; + configContent[tool.name] = tool.config ?? {}; }); const configFilePath = path.join(root, "tools_config.json"); await fs.writeFile( @@ -217,7 +255,11 @@ export const installPythonTemplate = async ({ } } - const addOnDependencies = getAdditionalDependencies(vectorDb); + const addOnDependencies = getAdditionalDependencies( + vectorDb, + dataSource, + tools, + ); await addDependencies(root, addOnDependencies); if (postInstallAction !== "none") { diff --git a/helpers/tools.ts b/helpers/tools.ts index eae53d20..f7b844cb 100644 --- a/helpers/tools.ts +++ b/helpers/tools.ts @@ -1,33 +1,71 @@ +import { red } from "picocolors"; + export type Tool = { display: string; name: string; config?: Record<string, any>; + dependencies?: ToolDependencies[]; +}; +export type ToolDependencies = { + name: string; + version?: string; }; export const supportedTools: Tool[] = [ { display: "Google Search (configuration required after installation)", - name: "google_search", + name: "google.GoogleSearchToolSpec", config: { engine: "Your search engine id, see https://developers.google.com/custom-search/v1/overview#prerequisites", key: "Your search api key", num: 2, }, + dependencies: [ + { + name: "llama-index-tools-google", + version: "0.1.2", + }, + ], }, { display: "Wikipedia", - name: "wikipedia", + name: "wikipedia.WikipediaToolSpec", + dependencies: [ + { + name: "llama-index-tools-wikipedia", + version: "0.1.2", + }, + ], }, ]; -export const getToolConfig = (name: string) => { - return supportedTools.find((tool) => tool.name === name)?.config; +export const getTool = (toolName: string): Tool | undefined => { + return supportedTools.find((tool) => tool.name === toolName); +}; + +export const getTools = (toolsName: string[]): Tool[] => { + let tools: Tool[] = []; + for (const toolName of toolsName) { + const tool = getTool(toolName); + if (!tool) { + console.log( + red( + `Error: Tool '${toolName}' is not supported. Supported tools are: ${supportedTools + .map((t) => t.name) + .join(", ")}`, + ), + ); + process.exit(1); + } + tools.push(tool); + } + return tools; }; -export const toolsRequireConfig = (tools?: string[]): boolean => { +export const toolsRequireConfig = (tools?: Tool[]): boolean => { if (tools) { - return tools.some((tool) => getToolConfig(tool)); + return tools?.some((tool) => Object.keys(tool.config || {}).length > 0); } return false; }; diff --git a/helpers/types.ts b/helpers/types.ts index 19253cc1..019a1e2a 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -1,4 +1,5 @@ import { PackageManager } from "../helpers/get-pkg-manager"; +import { Tool } from "./tools"; export type TemplateType = "simple" | "streaming" | "community" | "llamapack"; export type TemplateFramework = "nextjs" | "express" | "fastapi"; @@ -41,5 +42,5 @@ export interface InstallTemplateArgs { vectorDb?: TemplateVectorDB; externalPort?: number; postInstallAction?: TemplatePostInstallAction; - tools?: string[]; + tools?: Tool[]; } diff --git a/index.ts b/index.ts index 6fa19cc5..3dbc9ffd 100644 --- a/index.ts +++ b/index.ts @@ -11,7 +11,7 @@ import { createApp } from "./create-app"; import { getPkgManager } from "./helpers/get-pkg-manager"; import { isFolderEmpty } from "./helpers/is-folder-empty"; import { runApp } from "./helpers/run-app"; -import { supportedTools } from "./helpers/tools"; +import { getTools } from "./helpers/tools"; import { validateNpmName } from "./helpers/validate-pkg"; import packageJson from "./package.json"; import { QuestionArgs, askQuestions, onPromptState } from "./questions"; @@ -153,7 +153,7 @@ const program = new Commander.Command(packageJson.name) "--tools <tools>", ` - Specify the tools you want to use by providing a comma-separated list. For example, 'google_search,wikipedia'. Use 'none' to not using any tools. + Specify the tools you want to use by providing a comma-separated list. For example, 'wikipedia.WikipediaToolSpec,google.GoogleSearchToolSpec'. Use 'none' to not using any tools. `, ) .allowUnknownOption() @@ -168,19 +168,7 @@ if (process.argv.includes("--tools")) { if (program.tools === "none") { program.tools = []; } else { - program.tools = program.tools.split(","); - // Check if tools are available - const toolsName = supportedTools.map((tool) => tool.name); - program.tools.forEach((tool: string) => { - if (!toolsName.includes(tool)) { - console.error( - `Error: Tool '${tool}' is not supported. Supported tools are: ${toolsName.join( - ", ", - )}`, - ); - process.exit(1); - } - }); + program.tools = getTools(program.tools.split(",")); } } diff --git a/questions.ts b/questions.ts index b0cca28c..f8a086a4 100644 --- a/questions.ts +++ b/questions.ts @@ -583,13 +583,16 @@ export const askQuestions = async ( title: tool.display, value: tool.name, })); - const { tools } = await prompts({ + const { toolsName } = await prompts({ type: "multiselect", - name: "tools", + name: "toolsName", message: "Would you like to build an agent using tools? If so, select the tools here, otherwise just press enter", choices: toolChoices, }); + const tools = toolsName?.map((tool: string) => + supportedTools.find((t) => t.name === tool), + ); program.tools = tools; preferences.tools = tools; } diff --git a/templates/components/engines/python/agent/__init__.py b/templates/components/engines/python/agent/__init__.py index 86e777ed..1826d0e0 100644 --- a/templates/components/engines/python/agent/__init__.py +++ b/templates/components/engines/python/agent/__init__.py @@ -1,33 +1,8 @@ -import os - -from typing import Any, Optional -from llama_index.llms import LLM -from llama_index.agent import AgentRunner - +from llama_index.core.settings import Settings +from llama_index.core.agent import AgentRunner +from llama_index.core.tools.query_engine import QueryEngineTool from app.engine.tools import ToolFactory from app.engine.index import get_index -from llama_index.agent import ReActAgent -from llama_index.tools.query_engine import QueryEngineTool - - -def create_agent_from_llm( - llm: Optional[LLM] = None, - **kwargs: Any, -) -> AgentRunner: - from llama_index.agent import OpenAIAgent, ReActAgent - from llama_index.llms.openai import OpenAI - from llama_index.llms.openai_utils import is_function_calling_model - - if isinstance(llm, OpenAI) and is_function_calling_model(llm.model): - return OpenAIAgent.from_tools( - llm=llm, - **kwargs, - ) - else: - return ReActAgent.from_tools( - llm=llm, - **kwargs, - ) def get_chat_engine(): @@ -35,7 +10,6 @@ def get_chat_engine(): # Add query tool index = get_index() - llm = index.service_context.llm query_engine = index.as_query_engine(similarity_top_k=3) query_engine_tool = QueryEngineTool.from_defaults(query_engine=query_engine) tools.append(query_engine_tool) @@ -43,8 +17,8 @@ def get_chat_engine(): # Add additional tools tools += ToolFactory.from_env() - return create_agent_from_llm( - llm=llm, + return AgentRunner.from_llm( + llm=Settings.llm, tools=tools, verbose=True, ) diff --git a/templates/components/engines/python/agent/tools.py b/templates/components/engines/python/agent/tools.py index 9fb9d488..fafc1fcc 100644 --- a/templates/components/engines/python/agent/tools.py +++ b/templates/components/engines/python/agent/tools.py @@ -1,8 +1,8 @@ import json import importlib -from llama_index.tools.tool_spec.base import BaseToolSpec -from llama_index.tools.function_tool import FunctionTool +from llama_index.core.tools.tool_spec.base import BaseToolSpec +from llama_index.core.tools.function_tool import FunctionTool class ToolFactory: @@ -10,9 +10,9 @@ class ToolFactory: @staticmethod def create_tool(tool_name: str, **kwargs) -> list[FunctionTool]: try: - module_name = f"llama_hub.tools.{tool_name}.base" + tool_package, tool_cls_name = tool_name.split(".") + module_name = f"llama_index.tools.{tool_package}" module = importlib.import_module(module_name) - tool_cls_name = tool_name.title().replace("_", "") + "ToolSpec" tool_class = getattr(module, tool_cls_name) tool_spec: BaseToolSpec = tool_class(**kwargs) return tool_spec.to_tool_list() diff --git a/templates/components/loaders/python/file/loader.py b/templates/components/loaders/python/file/loader.py index d343cec3..dd7627a7 100644 --- a/templates/components/loaders/python/file/loader.py +++ b/templates/components/loaders/python/file/loader.py @@ -1,7 +1,5 @@ -import os +from llama_index.core.readers import SimpleDirectoryReader from app.engine.constants import DATA_DIR -from llama_index import VectorStoreIndex, download_loader -from llama_index import SimpleDirectoryReader def get_documents(): diff --git a/templates/components/loaders/python/web/loader.py b/templates/components/loaders/python/web/loader.py index 026dd101..bc6d0496 100644 --- a/templates/components/loaders/python/web/loader.py +++ b/templates/components/loaders/python/web/loader.py @@ -1,10 +1,8 @@ import os -from llama_index import VectorStoreIndex, download_loader +from llama_index.readers.web import WholeSiteReader def get_documents(): - WholeSiteReader = download_loader("WholeSiteReader") - # Initialize the scraper with a prefix URL and maximum depth scraper = WholeSiteReader( prefix=os.environ.get("URL_PREFIX"), max_depth=int(os.environ.get("MAX_DEPTH")) diff --git a/templates/components/vectordbs/python/mongo/context.py b/templates/components/vectordbs/python/mongo/context.py deleted file mode 100644 index ceb8a50a..00000000 --- a/templates/components/vectordbs/python/mongo/context.py +++ /dev/null @@ -1,14 +0,0 @@ -from llama_index import ServiceContext - -from app.context import create_base_context -from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP - - -def create_service_context(): - base = create_base_context() - return ServiceContext.from_defaults( - llm=base.llm, - embed_model=base.embed_model, - chunk_size=CHUNK_SIZE, - chunk_overlap=CHUNK_OVERLAP, - ) diff --git a/templates/components/vectordbs/python/mongo/generate.py b/templates/components/vectordbs/python/mongo/generate.py index f52b3b48..66730839 100644 --- a/templates/components/vectordbs/python/mongo/generate.py +++ b/templates/components/vectordbs/python/mongo/generate.py @@ -1,26 +1,21 @@ from dotenv import load_dotenv load_dotenv() + import os import logging -from llama_index.vector_stores import MongoDBAtlasVectorSearch - +from llama_index.core.storage import StorageContext +from llama_index.core.indices import VectorStoreIndex +from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch +from app.engine.settings import init_settings from app.engine.constants import DATA_DIR -from app.engine.context import create_service_context from app.engine.loader import get_documents - -from llama_index import ( - SimpleDirectoryReader, - VectorStoreIndex, - StorageContext, -) - logging.basicConfig(level=logging.INFO) logger = logging.getLogger() -def generate_datasource(service_context): +def generate_datasource(): logger.info("Creating new index") # load the documents and create the index documents = get_documents() @@ -32,7 +27,6 @@ def generate_datasource(service_context): storage_context = StorageContext.from_defaults(vector_store=store) VectorStoreIndex.from_documents( documents, - service_context=service_context, storage_context=storage_context, show_progress=True, # this will show you a progress bar as the embeddings are created ) @@ -46,4 +40,5 @@ See https://github.com/run-llama/mongodb-demo/tree/main?tab=readme-ov-file#creat if __name__ == "__main__": - generate_datasource(create_service_context()) + init_settings() + generate_datasource() diff --git a/templates/components/vectordbs/python/mongo/index.py b/templates/components/vectordbs/python/mongo/index.py index 173e7b57..6dba7c1d 100644 --- a/templates/components/vectordbs/python/mongo/index.py +++ b/templates/components/vectordbs/python/mongo/index.py @@ -1,23 +1,20 @@ import logging import os -from llama_index import ( - VectorStoreIndex, -) -from llama_index.vector_stores import MongoDBAtlasVectorSearch +from llama_index.core.indices import VectorStoreIndex +from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch -from app.engine.context import create_service_context + +logger = logging.getLogger("uvicorn") def get_index(): - service_context = create_service_context() - logger = logging.getLogger("uvicorn") logger.info("Connecting to index from MongoDB...") store = MongoDBAtlasVectorSearch( db_name=os.environ["MONGODB_DATABASE"], collection_name=os.environ["MONGODB_VECTORS"], index_name=os.environ["MONGODB_VECTOR_INDEX"], ) - index = VectorStoreIndex.from_vector_store(store, service_context) + index = VectorStoreIndex.from_vector_store(store) logger.info("Finished connecting to index from MongoDB.") return index diff --git a/templates/components/vectordbs/python/mongo/settings.py b/templates/components/vectordbs/python/mongo/settings.py new file mode 100644 index 00000000..d41f0227 --- /dev/null +++ b/templates/components/vectordbs/python/mongo/settings.py @@ -0,0 +1,10 @@ +from llama_index.core.settings import Settings +from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP +from app.settings import init_base_settings + + +def init_settings(): + init_base_settings() + + Settings.chunk_size = CHUNK_SIZE + Settings.chunk_overlap = CHUNK_OVERLAP diff --git a/templates/components/vectordbs/python/none/context.py b/templates/components/vectordbs/python/none/context.py deleted file mode 100644 index ceb8a50a..00000000 --- a/templates/components/vectordbs/python/none/context.py +++ /dev/null @@ -1,14 +0,0 @@ -from llama_index import ServiceContext - -from app.context import create_base_context -from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP - - -def create_service_context(): - base = create_base_context() - return ServiceContext.from_defaults( - llm=base.llm, - embed_model=base.embed_model, - chunk_size=CHUNK_SIZE, - chunk_overlap=CHUNK_OVERLAP, - ) diff --git a/templates/components/vectordbs/python/none/generate.py b/templates/components/vectordbs/python/none/generate.py index 7ff20012..29140e11 100644 --- a/templates/components/vectordbs/python/none/generate.py +++ b/templates/components/vectordbs/python/none/generate.py @@ -1,32 +1,32 @@ -import logging - from dotenv import load_dotenv -from app.engine.constants import DATA_DIR, STORAGE_DIR -from app.engine.context import create_service_context -from app.engine.loader import get_documents - load_dotenv() -from llama_index import ( - SimpleDirectoryReader, +import logging +from llama_index.core.indices import ( VectorStoreIndex, ) +from app.engine.constants import DATA_DIR, STORAGE_DIR +from app.engine.loader import get_documents +from app.engine.settings import init_settings + logging.basicConfig(level=logging.INFO) logger = logging.getLogger() -def generate_datasource(service_context): +def generate_datasource(): logger.info("Creating new index") # load the documents and create the index documents = get_documents() - index = VectorStoreIndex.from_documents(documents, service_context=service_context) + index = VectorStoreIndex.from_documents( + documents, + ) # store it for later index.storage_context.persist(STORAGE_DIR) logger.info(f"Finished creating new index. Stored in {STORAGE_DIR}") if __name__ == "__main__": - service_context = create_service_context() - generate_datasource(service_context) + init_settings() + generate_datasource() diff --git a/templates/components/vectordbs/python/none/index.py b/templates/components/vectordbs/python/none/index.py index 8e16975b..4446eed0 100644 --- a/templates/components/vectordbs/python/none/index.py +++ b/templates/components/vectordbs/python/none/index.py @@ -2,24 +2,26 @@ import logging import os from app.engine.constants import STORAGE_DIR -from app.engine.context import create_service_context -from llama_index import ( - StorageContext, - load_index_from_storage, -) +from app.engine.settings import init_settings +from llama_index.core.storage import StorageContext +from llama_index.core.indices import load_index_from_storage + +logger = logging.getLogger("uvicorn") def get_index(): - service_context = create_service_context() + # Init default app global settings + init_settings() + # check if storage already exists if not os.path.exists(STORAGE_DIR): raise Exception( "StorageContext is empty - call 'python app/engine/generate.py' to generate the storage first" ) - logger = logging.getLogger("uvicorn") + # load the existing index logger.info(f"Loading index from {STORAGE_DIR}...") storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR) - index = load_index_from_storage(storage_context, service_context=service_context) + index = load_index_from_storage(storage_context) logger.info(f"Finished loading index from {STORAGE_DIR}") return index diff --git a/templates/components/vectordbs/python/none/settings.py b/templates/components/vectordbs/python/none/settings.py new file mode 100644 index 00000000..d41f0227 --- /dev/null +++ b/templates/components/vectordbs/python/none/settings.py @@ -0,0 +1,10 @@ +from llama_index.core.settings import Settings +from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP +from app.settings import init_base_settings + + +def init_settings(): + init_base_settings() + + Settings.chunk_size = CHUNK_SIZE + Settings.chunk_overlap = CHUNK_OVERLAP diff --git a/templates/components/vectordbs/python/pg/context.py b/templates/components/vectordbs/python/pg/context.py deleted file mode 100644 index ceb8a50a..00000000 --- a/templates/components/vectordbs/python/pg/context.py +++ /dev/null @@ -1,14 +0,0 @@ -from llama_index import ServiceContext - -from app.context import create_base_context -from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP - - -def create_service_context(): - base = create_base_context() - return ServiceContext.from_defaults( - llm=base.llm, - embed_model=base.embed_model, - chunk_size=CHUNK_SIZE, - chunk_overlap=CHUNK_OVERLAP, - ) diff --git a/templates/components/vectordbs/python/pg/generate.py b/templates/components/vectordbs/python/pg/generate.py index 5c77ee07..b184d3a4 100644 --- a/templates/components/vectordbs/python/pg/generate.py +++ b/templates/components/vectordbs/python/pg/generate.py @@ -1,24 +1,21 @@ from dotenv import load_dotenv load_dotenv() + import logging +from llama_index.core.indices import VectorStoreIndex +from llama_index.core.storage import StorageContext from app.engine.constants import DATA_DIR -from app.engine.context import create_service_context -from app.engine.utils import init_pg_vector_store_from_env from app.engine.loader import get_documents - -from llama_index import ( - SimpleDirectoryReader, - VectorStoreIndex, - StorageContext, -) +from app.engine.settings import init_settings +from app.engine.utils import init_pg_vector_store_from_env logging.basicConfig(level=logging.INFO) logger = logging.getLogger() -def generate_datasource(service_context): +def generate_datasource(): logger.info("Creating new index") # load the documents and create the index documents = get_documents() @@ -26,7 +23,6 @@ def generate_datasource(service_context): storage_context = StorageContext.from_defaults(vector_store=store) VectorStoreIndex.from_documents( documents, - service_context=service_context, storage_context=storage_context, show_progress=True, # this will show you a progress bar as the embeddings are created ) @@ -36,4 +32,5 @@ def generate_datasource(service_context): if __name__ == "__main__": - generate_datasource(create_service_context()) + init_settings() + generate_datasource() diff --git a/templates/components/vectordbs/python/pg/index.py b/templates/components/vectordbs/python/pg/index.py index 368fb432..9cce1b95 100644 --- a/templates/components/vectordbs/python/pg/index.py +++ b/templates/components/vectordbs/python/pg/index.py @@ -1,16 +1,17 @@ import logging -from llama_index import ( - VectorStoreIndex, -) -from app.engine.context import create_service_context +from llama_index.core.indices.vector_store import VectorStoreIndex +from app.engine.settings import init_settings from app.engine.utils import init_pg_vector_store_from_env +logger = logging.getLogger("uvicorn") + def get_index(): - service_context = create_service_context() - logger = logging.getLogger("uvicorn") + # Init default app global settings + init_settings() + logger.info("Connecting to index from PGVector...") store = init_pg_vector_store_from_env() - index = VectorStoreIndex.from_vector_store(store, service_context) + index = VectorStoreIndex.from_vector_store(store) logger.info("Finished connecting to index from PGVector.") return index diff --git a/templates/components/vectordbs/python/pg/settings.py b/templates/components/vectordbs/python/pg/settings.py new file mode 100644 index 00000000..d41f0227 --- /dev/null +++ b/templates/components/vectordbs/python/pg/settings.py @@ -0,0 +1,10 @@ +from llama_index.core.settings import Settings +from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP +from app.settings import init_base_settings + + +def init_settings(): + init_base_settings() + + Settings.chunk_size = CHUNK_SIZE + Settings.chunk_overlap = CHUNK_OVERLAP diff --git a/templates/components/vectordbs/python/pg/utils.py b/templates/components/vectordbs/python/pg/utils.py index 808fda2c..39127846 100644 --- a/templates/components/vectordbs/python/pg/utils.py +++ b/templates/components/vectordbs/python/pg/utils.py @@ -1,5 +1,5 @@ import os -from llama_index.vector_stores import PGVectorStore +from llama_index.vector_stores.postgres import PGVectorStore from urllib.parse import urlparse from app.engine.constants import PGVECTOR_SCHEMA, PGVECTOR_TABLE diff --git a/templates/components/vectordbs/python/pinecone/context.py b/templates/components/vectordbs/python/pinecone/context.py deleted file mode 100644 index ceb8a50a..00000000 --- a/templates/components/vectordbs/python/pinecone/context.py +++ /dev/null @@ -1,14 +0,0 @@ -from llama_index import ServiceContext - -from app.context import create_base_context -from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP - - -def create_service_context(): - base = create_base_context() - return ServiceContext.from_defaults( - llm=base.llm, - embed_model=base.embed_model, - chunk_size=CHUNK_SIZE, - chunk_overlap=CHUNK_OVERLAP, - ) diff --git a/templates/components/vectordbs/python/pinecone/generate.py b/templates/components/vectordbs/python/pinecone/generate.py index 8c0e1c0b..0e6486f8 100644 --- a/templates/components/vectordbs/python/pinecone/generate.py +++ b/templates/components/vectordbs/python/pinecone/generate.py @@ -1,26 +1,21 @@ from dotenv import load_dotenv load_dotenv() + import os import logging -from llama_index.vector_stores import PineconeVectorStore - +from llama_index.core.storage import StorageContext +from llama_index.core.indices import VectorStoreIndex +from llama_index.vector_stores.pinecone import PineconeVectorStore +from app.engine.settings import init_settings from app.engine.constants import DATA_DIR -from app.engine.context import create_service_context from app.engine.loader import get_documents - -from llama_index import ( - SimpleDirectoryReader, - VectorStoreIndex, - StorageContext, -) - logging.basicConfig(level=logging.INFO) logger = logging.getLogger() -def generate_datasource(service_context): +def generate_datasource(): logger.info("Creating new index") # load the documents and create the index documents = get_documents() @@ -32,7 +27,6 @@ def generate_datasource(service_context): storage_context = StorageContext.from_defaults(vector_store=store) VectorStoreIndex.from_documents( documents, - service_context=service_context, storage_context=storage_context, show_progress=True, # this will show you a progress bar as the embeddings are created ) @@ -42,4 +36,5 @@ def generate_datasource(service_context): if __name__ == "__main__": - generate_datasource(create_service_context()) + init_settings() + generate_datasource() diff --git a/templates/components/vectordbs/python/pinecone/index.py b/templates/components/vectordbs/python/pinecone/index.py index 6e9b8810..98824ffd 100644 --- a/templates/components/vectordbs/python/pinecone/index.py +++ b/templates/components/vectordbs/python/pinecone/index.py @@ -1,23 +1,20 @@ import logging import os -from llama_index import ( - VectorStoreIndex, -) -from llama_index.vector_stores import PineconeVectorStore +from llama_index.core.indices import VectorStoreIndex +from llama_index.vector_stores.pinecone import PineconeVectorStore -from app.engine.context import create_service_context + +logger = logging.getLogger("uvicorn") def get_index(): - service_context = create_service_context() - logger = logging.getLogger("uvicorn") logger.info("Connecting to index from Pinecone...") store = PineconeVectorStore( api_key=os.environ["PINECONE_API_KEY"], index_name=os.environ["PINECONE_INDEX_NAME"], environment=os.environ["PINECONE_ENVIRONMENT"], ) - index = VectorStoreIndex.from_vector_store(store, service_context) + index = VectorStoreIndex.from_vector_store(store) logger.info("Finished connecting to index from Pinecone.") return index diff --git a/templates/components/vectordbs/python/pinecone/settings.py b/templates/components/vectordbs/python/pinecone/settings.py new file mode 100644 index 00000000..d41f0227 --- /dev/null +++ b/templates/components/vectordbs/python/pinecone/settings.py @@ -0,0 +1,10 @@ +from llama_index.core.settings import Settings +from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP +from app.settings import init_base_settings + + +def init_settings(): + init_base_settings() + + Settings.chunk_size = CHUNK_SIZE + Settings.chunk_overlap = CHUNK_OVERLAP diff --git a/templates/types/simple/fastapi/app/api/routers/chat.py b/templates/types/simple/fastapi/app/api/routers/chat.py index 09efdcaa..8405f2ac 100644 --- a/templates/types/simple/fastapi/app/api/routers/chat.py +++ b/templates/types/simple/fastapi/app/api/routers/chat.py @@ -1,10 +1,8 @@ from typing import List - -from fastapi import APIRouter, Depends, HTTPException, status -from llama_index.chat_engine.types import BaseChatEngine -from llama_index.llms.base import ChatMessage -from llama_index.llms.types import MessageRole from pydantic import BaseModel +from fastapi import APIRouter, Depends, HTTPException, status +from llama_index.core.chat_engine.types import BaseChatEngine +from llama_index.core.llms import ChatMessage, MessageRole from app.engine import get_chat_engine chat_router = r = APIRouter() diff --git a/templates/types/simple/fastapi/app/context.py b/templates/types/simple/fastapi/app/context.py deleted file mode 100644 index ae00de21..00000000 --- a/templates/types/simple/fastapi/app/context.py +++ /dev/null @@ -1,11 +0,0 @@ -import os - -from llama_index import ServiceContext -from llama_index.llms import OpenAI - - -def create_base_context(): - model = os.getenv("MODEL", "gpt-3.5-turbo") - return ServiceContext.from_defaults( - llm=OpenAI(model=model), - ) diff --git a/templates/types/simple/fastapi/app/engine/__init__.py b/templates/types/simple/fastapi/app/engine/__init__.py index 663b595a..50f92374 100644 --- a/templates/types/simple/fastapi/app/engine/__init__.py +++ b/templates/types/simple/fastapi/app/engine/__init__.py @@ -1,7 +1,7 @@ -from llama_index.chat_engine import SimpleChatEngine - -from app.context import create_base_context +from llama_index.core.chat_engine import SimpleChatEngine +from app.settings import init_base_settings def get_chat_engine(): - return SimpleChatEngine.from_defaults(service_context=create_base_context()) + init_base_settings() + return SimpleChatEngine.from_defaults() diff --git a/templates/types/simple/fastapi/app/settings.py b/templates/types/simple/fastapi/app/settings.py new file mode 100644 index 00000000..fec9955e --- /dev/null +++ b/templates/types/simple/fastapi/app/settings.py @@ -0,0 +1,8 @@ +import os +from llama_index.llms.openai import OpenAI +from llama_index.core.settings import Settings + + +def init_base_settings(): + model = os.getenv("MODEL", "gpt-3.5-turbo") + Settings.llm = OpenAI(model=model) diff --git a/templates/types/simple/fastapi/pyproject.toml b/templates/types/simple/fastapi/pyproject.toml index 46448617..45c3ae56 100644 --- a/templates/types/simple/fastapi/pyproject.toml +++ b/templates/types/simple/fastapi/pyproject.toml @@ -9,12 +9,8 @@ readme = "README.md" python = "^3.11,<3.12" fastapi = "^0.109.1" uvicorn = { extras = ["standard"], version = "^0.23.2" } -llama-index = "^0.9.19" -pypdf = "^3.17.0" python-dotenv = "^1.0.0" -docx2txt = "^0.8" -llama-hub = "^0.0.77" -wikipedia = "^1.4.0" +llama-index = "^0.10.7" [build-system] requires = ["poetry-core"] diff --git a/templates/types/streaming/fastapi/app/api/routers/chat.py b/templates/types/streaming/fastapi/app/api/routers/chat.py index 0afe14e4..278a9a75 100644 --- a/templates/types/streaming/fastapi/app/api/routers/chat.py +++ b/templates/types/streaming/fastapi/app/api/routers/chat.py @@ -1,13 +1,10 @@ from typing import List - +from pydantic import BaseModel from fastapi.responses import StreamingResponse -from llama_index.chat_engine.types import BaseChatEngine - -from app.engine import get_chat_engine from fastapi import APIRouter, Depends, HTTPException, Request, status -from llama_index.llms.base import ChatMessage -from llama_index.llms.types import MessageRole -from pydantic import BaseModel +from llama_index.core.chat_engine.types import BaseChatEngine +from llama_index.core.llms import ChatMessage, MessageRole +from app.engine import get_chat_engine chat_router = r = APIRouter() diff --git a/templates/types/streaming/fastapi/app/context.py b/templates/types/streaming/fastapi/app/context.py deleted file mode 100644 index ae00de21..00000000 --- a/templates/types/streaming/fastapi/app/context.py +++ /dev/null @@ -1,11 +0,0 @@ -import os - -from llama_index import ServiceContext -from llama_index.llms import OpenAI - - -def create_base_context(): - model = os.getenv("MODEL", "gpt-3.5-turbo") - return ServiceContext.from_defaults( - llm=OpenAI(model=model), - ) diff --git a/templates/types/streaming/fastapi/app/engine/__init__.py b/templates/types/streaming/fastapi/app/engine/__init__.py index 663b595a..50f92374 100644 --- a/templates/types/streaming/fastapi/app/engine/__init__.py +++ b/templates/types/streaming/fastapi/app/engine/__init__.py @@ -1,7 +1,7 @@ -from llama_index.chat_engine import SimpleChatEngine - -from app.context import create_base_context +from llama_index.core.chat_engine import SimpleChatEngine +from app.settings import init_base_settings def get_chat_engine(): - return SimpleChatEngine.from_defaults(service_context=create_base_context()) + init_base_settings() + return SimpleChatEngine.from_defaults() diff --git a/templates/types/streaming/fastapi/app/settings.py b/templates/types/streaming/fastapi/app/settings.py new file mode 100644 index 00000000..fec9955e --- /dev/null +++ b/templates/types/streaming/fastapi/app/settings.py @@ -0,0 +1,8 @@ +import os +from llama_index.llms.openai import OpenAI +from llama_index.core.settings import Settings + + +def init_base_settings(): + model = os.getenv("MODEL", "gpt-3.5-turbo") + Settings.llm = OpenAI(model=model) diff --git a/templates/types/streaming/fastapi/pyproject.toml b/templates/types/streaming/fastapi/pyproject.toml index 46448617..45c3ae56 100644 --- a/templates/types/streaming/fastapi/pyproject.toml +++ b/templates/types/streaming/fastapi/pyproject.toml @@ -9,12 +9,8 @@ readme = "README.md" python = "^3.11,<3.12" fastapi = "^0.109.1" uvicorn = { extras = ["standard"], version = "^0.23.2" } -llama-index = "^0.9.19" -pypdf = "^3.17.0" python-dotenv = "^1.0.0" -docx2txt = "^0.8" -llama-hub = "^0.0.77" -wikipedia = "^1.4.0" +llama-index = "^0.10.7" [build-system] requires = ["poetry-core"] -- GitLab