Skip to content

Commit

Permalink
fix prompt helper init (#11379)
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-markewich authored Feb 26, 2024
1 parent 52383c7 commit 78a4c9e
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from llama_index.core.callbacks.schema import CBEventType, EventPayload
from llama_index.core.data_structs.table import StructDatapoint
from llama_index.core.indices.prompt_helper import PromptHelper
from llama_index.core.node_parser.interface import TextSplitter
from llama_index.core.prompts import BasePromptTemplate
from llama_index.core.prompts.default_prompt_selectors import (
Expand All @@ -26,7 +27,6 @@
Settings,
callback_manager_from_settings_or_context,
llm_from_settings_or_context,
prompt_helper_from_settings_or_context,
)
from llama_index.core.utilities.sql_wrapper import SQLDatabase
from llama_index.core.utils import truncate_text
Expand Down Expand Up @@ -67,8 +67,8 @@ def __init__(
self._sql_database = sql_database
self._text_splitter = text_splitter
self._llm = llm or llm_from_settings_or_context(Settings, service_context)
self._prompt_helper = prompt_helper_from_settings_or_context(
Settings, service_context
self._prompt_helper = Settings._prompt_helper or PromptHelper.from_llm_metadata(
self._llm.metadata,
)
self._callback_manager = callback_manager_from_settings_or_context(
Settings, service_context
Expand Down
7 changes: 3 additions & 4 deletions llama-index-core/llama_index/core/indices/common_tree/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Common classes/functions for tree index operations."""


import asyncio
import logging
from typing import Dict, List, Optional, Sequence, Tuple

from llama_index.core.async_utils import run_async_tasks
from llama_index.core.callbacks.schema import CBEventType, EventPayload
from llama_index.core.data_structs.data_structs import IndexGraph
from llama_index.core.indices.prompt_helper import PromptHelper
from llama_index.core.indices.utils import get_sorted_node_list, truncate_text
from llama_index.core.llms.llm import LLM
from llama_index.core.prompts import BasePromptTemplate
Expand All @@ -17,7 +17,6 @@
Settings,
callback_manager_from_settings_or_context,
llm_from_settings_or_context,
prompt_helper_from_settings_or_context,
)
from llama_index.core.storage.docstore import BaseDocumentStore
from llama_index.core.storage.docstore.registry import get_default_docstore
Expand Down Expand Up @@ -50,8 +49,8 @@ def __init__(
self.num_children = num_children
self.summary_prompt = summary_prompt
self._llm = llm or llm_from_settings_or_context(Settings, service_context)
self._prompt_helper = prompt_helper_from_settings_or_context(
Settings, service_context
self._prompt_helper = Settings._prompt_helper or PromptHelper.from_llm_metadata(
self._llm.metadata,
)
self._callback_manager = callback_manager_from_settings_or_context(
Settings, service_context
Expand Down
1 change: 1 addition & 0 deletions llama-index-core/llama_index/core/indices/prompt_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def from_llm_metadata(
"""
context_window = llm_metadata.context_window

if llm_metadata.num_output == -1:
num_output = DEFAULT_NUM_OUTPUTS
else:
Expand Down
6 changes: 3 additions & 3 deletions llama-index-core/llama_index/core/indices/tree/inserter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Optional, Sequence

from llama_index.core.data_structs.data_structs import IndexGraph
from llama_index.core.indices.prompt_helper import PromptHelper
from llama_index.core.indices.tree.utils import get_numbered_text_from_nodes
from llama_index.core.indices.utils import (
extract_numbers_given_response,
Expand All @@ -19,7 +20,6 @@
from llama_index.core.settings import (
Settings,
llm_from_settings_or_context,
prompt_helper_from_settings_or_context,
)
from llama_index.core.storage.docstore import BaseDocumentStore
from llama_index.core.storage.docstore.registry import get_default_docstore
Expand All @@ -46,8 +46,8 @@ def __init__(
self.insert_prompt = insert_prompt
self.index_graph = index_graph
self._llm = llm or llm_from_settings_or_context(Settings, service_context)
self._prompt_helper = prompt_helper_from_settings_or_context(
Settings, service_context
self._prompt_helper = Settings._prompt_helper or PromptHelper.from_llm_metadata(
self._llm.metadata,
)
self._docstore = docstore or get_default_docstore()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from llama_index.core.base.base_retriever import BaseRetriever
from llama_index.core.base.response.schema import Response
from llama_index.core.callbacks.base import CallbackManager
from llama_index.core.indices.prompt_helper import PromptHelper
from llama_index.core.indices.query.schema import QueryBundle
from llama_index.core.indices.tree.base import TreeIndex
from llama_index.core.indices.tree.utils import get_numbered_text_from_nodes
Expand All @@ -32,7 +33,6 @@
from llama_index.core.settings import (
Settings,
callback_manager_from_settings_or_context,
prompt_helper_from_settings_or_context,
)
from llama_index.core.utils import print_text, truncate_text

Expand Down Expand Up @@ -93,8 +93,8 @@ def __init__(
self._index_struct = index.index_struct
self._docstore = index.docstore
self._service_context = index.service_context
self._prompt_helper = prompt_helper_from_settings_or_context(
Settings, index.service_context
self._prompt_helper = Settings._prompt_helper or PromptHelper.from_llm_metadata(
self._llm.metadata,
)

self._text_qa_template = text_qa_template or DEFAULT_TEXT_QA_PROMPT
Expand Down
10 changes: 7 additions & 3 deletions llama-index-core/llama_index/core/response_synthesizers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
2) create and refine separately over each chunk, 3) tree summarization.
"""

import logging
from abc import abstractmethod
from typing import Any, Dict, Generator, List, Optional, Sequence, Union
Expand Down Expand Up @@ -41,7 +42,6 @@
Settings,
callback_manager_from_settings_or_context,
llm_from_settings_or_context,
prompt_helper_from_settings_or_context,
)
from llama_index.core.types import RESPONSE_TEXT_TYPE

Expand Down Expand Up @@ -69,8 +69,12 @@ def __init__(
callback_manager
or callback_manager_from_settings_or_context(Settings, service_context)
)
self._prompt_helper = prompt_helper or prompt_helper_from_settings_or_context(
Settings, service_context
self._prompt_helper = (
prompt_helper
or Settings._prompt_helper
or PromptHelper.from_llm_metadata(
self._llm.metadata,
)
)

self._streaming = streaming
Expand Down
15 changes: 11 additions & 4 deletions llama-index-core/llama_index/core/response_synthesizers/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
Settings,
callback_manager_from_settings_or_context,
llm_from_settings_or_context,
prompt_helper_from_settings_or_context,
)
from llama_index.core.types import BasePydanticProgram

Expand Down Expand Up @@ -63,9 +62,17 @@ def get_response_synthesizer(
Settings, service_context
)
llm = llm or llm_from_settings_or_context(Settings, service_context)
prompt_helper = prompt_helper or prompt_helper_from_settings_or_context(
Settings, service_context
)

if service_context is not None:
prompt_helper = service_context.prompt_helper
else:
prompt_helper = (
prompt_helper
or Settings._prompt_helper
or PromptHelper.from_llm_metadata(
llm.metadata,
)
)

if response_mode == ResponseMode.REFINE:
return Refine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(
# deprecated
service_context: Optional[ServiceContext] = None,
) -> None:
if service_context is not None:
prompt_helper = service_context.prompt_helper

super().__init__(
llm=llm,
callback_manager=callback_manager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def __init__(
# deprecated
service_context: Optional[ServiceContext] = None,
) -> None:
if service_context is not None:
prompt_helper = service_context.prompt_helper

super().__init__(
llm=llm,
callback_manager=callback_manager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def __init__(
# deprecated
service_context: Optional[ServiceContext] = None,
) -> None:
if service_context is not None:
prompt_helper = service_context.prompt_helper

super().__init__(
llm=llm,
callback_manager=callback_manager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def __init__(
# deprecated
service_context: Optional[ServiceContext] = None,
) -> None:
if service_context is not None:
prompt_helper = service_context.prompt_helper

super().__init__(
llm=llm,
callback_manager=callback_manager,
Expand Down
12 changes: 0 additions & 12 deletions llama-index-core/llama_index/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ def text_splitter(self, text_splitter: NodeParser) -> None:
"""Set the text splitter."""
self.node_parser = text_splitter

# ---- Prompt helper ----

@property
def prompt_helper(self) -> PromptHelper:
"""Get the prompt helper."""
Expand Down Expand Up @@ -296,16 +294,6 @@ def node_parser_from_settings_or_context(
return settings.node_parser


def prompt_helper_from_settings_or_context(
settings: _Settings, context: Optional["ServiceContext"]
) -> PromptHelper:
"""Get settings from either settings or context."""
if context is not None:
return context.prompt_helper

return settings.prompt_helper


def transformations_from_settings_or_context(
settings: _Settings, context: Optional["ServiceContext"]
) -> List[TransformComponent]:
Expand Down

0 comments on commit 78a4c9e

Please sign in to comment.