import os from llama_index.llms.dashscope import DashScope from llama_index.embeddings.dashscope import ( DashScopeEmbedding, DashScopeTextEmbeddingType, ) from llama_index.core import Settings
PROPOSITION_SYSTEM_PROMPT = """Decompose the given content into clear and simple propositions, ensuring they are interpretable out of context. Follow these rules: 1. Split compound sentences into simple sentences. Maintain the original phrasing from the input whenever possible. 2. For any named entity that is accompanied by additional descriptive information, separate this information into its own distinct proposition. 3. Decontextualize the proposition by adding necessary modifiers to nouns or entire sentences and replacing pronouns (e.g., 'it', 'he', 'she', 'they', 'this', 'that') with the full name of the entities they refer to. 4. Present the results as a list of strings, formatted in JSON. Here's an example: Input: Title: Éostre. Section: Theories and interpretations, Connection to Easter Hares. Content: The earliest evidence for the Easter Hare (Osterhase) was recorded in south-west Germany in 1678 by the professor of medicine Georg Franck von Franckenau, but it remained unknown in other parts of Germany until the 18th century. Scholar Richard Sermon writes that "hares were frequently seen in gardens in spring, and thus may have served as a convenient explanation for the origin of the colored eggs hidden there for children. Alternatively, there is a European tradition that hares laid eggs, since a hare's scratch or form and a lapwing's nest look very similar, and both occur on grassland and are first seen in the spring. In the nineteenth century the influence of Easter cards, toys, and books was to make the Easter Hare/Rabbit popular throughout Europe. German immigrants then exported the custom to Britain and America where it evolved into the Easter Bunny." Output: [ "The earliest evidence for the Easter Hare was recorded in south-west Germany in 1678 by Georg Franck von Franckenau.", "Georg Franck von Franckenau was a professor of medicine.", "The evidence for the Easter Hare remained unknown in other parts of Germany until the 18th century.", "Richard Sermon was a scholar.", "Richard Sermon writes a hypothesis about the possible explanation for the connection between hares and the tradition during Easter", "Hares were frequently seen in gardens in spring.", "Hares may have served as a convenient explanation for the origin of the colored eggs hidden in gardens for children.", "There is a European tradition that hares laid eggs.", "A hare's scratch or form and a lapwing's nest look very similar.", "Both hares and lapwing's nests occur on grassland and are first seen in the spring.", "In the nineteenth century the influence of Easter cards, toys, and books was to make the Easter Hare/Rabbit popular throughout Europe.", "German immigrants exported the custom of the Easter Hare/Rabbit to Britain and America.", "The custom of the Easter Hare/Rabbit evolved into the Easter Bunny in Britain and America." ]"""
虽然理论上这种方式的检索效果更好,但它依赖 LLM 调用,因此处理速度较慢,成本也更高。
1 2 3 4
from llama_index.node_parser.topic import TopicNodeParser
from llama_index.core import VectorStoreIndex, StorageContext from llama_index.vector_stores.postgres import PGVectorStore import os from sqlalchemy import make_url
from llama_index.core import StorageContext, load_index_from_storage, QueryBundle from llama_index.core.response.notebook_utils import display_source_node from IPython.display import Markdown, display
# 从本地加载索引 storage_context = StorageContext.from_defaults(persist_dir="./dist/llama_index") index = load_index_from_storage(storage_context)
from llama_index.core import VectorStoreIndex from llama_index.vector_stores.postgres import PGVectorStore import os from sqlalchemy import make_url from IPython.display import Markdown, display
from llama_index.core.postprocessor.llm_rerank import LLMRerank from llama_index.core import QueryBundle from llama_index.core.response.notebook_utils import display_source_node