import os from llama_index.llms.openai import OpenAI from llama_index.embeddings.openai import OpenAIEmbedding from llama_index.core import ( Settings, SimpleDirectoryReader, StorageContext, VectorStoreIndex, load_index_from_storage, ) from llama_index.core.node_parser import SentenceSplitter
if not os.path.exists("storage"): index = VectorStoreIndex.from_documents(documents) index.set_index_id("avengers") index.storage_context.persist("./storage") else: store_context = StorageContext.from_defaults(persist_dir="./storage") index = load_index_from_storage( storage_context=store_context, index_id="avengers" )
from llama_index.core.query_pipeline import QueryPipeline, InputComponent from llama_index.core.response_synthesizers.simple_summarize import SimpleSummarize
question = "Which two members of the Avengers created Ultron?" output = p.run(input=question) print(str(output))
# 结果显示 > Running module input with input: input: Which two members of the Avengers created Ultron?
> Running module retriever with input: input: Which two members of the Avengers created Ultron?
> Running module output with input: query_str: Which two members of the Avengers created Ultron? nodes: [NodeWithScore(node=TextNode(id_='53d32f3a-a2d5-47b1-aa8f-a9679e83e0b0', embedding=None, metadata={'file_path': '/data/Avengers:Age-of-Ul...
from llama_index.core.query_pipeline import CustomQueryComponent from typing import Dict, Any from llama_index.core.indices.query.query_transform import HyDEQueryTransform
class HydeComponent(CustomQueryComponent): """HyDE query rewrite component."""
def _validate_component_inputs(self, input: Dict[str, Any]) -> Dict[str, Any]: """Validate component inputs during run_component.""" assert "input" in input, "input is required" return input
output, intermediates = p.run_with_intermediates(input=question) retriever_output = intermediates["retriever"].outputs["output"] print(f"retriever output:") for node in retriever_output: print(f"node: {node.text}\n") meta_replacer_output = intermediates["meta_replacer"].outputs["nodes"] print(f"meta_replacer output:") for node in meta_replacer_output: print(f"node: {node.text}\n")
# 显示结果 retriever output: node: In the Eastern European country of Sokovia, the Avengers—Tony Stark, Thor, Bruce Banner, Steve Rogers, Natasha Romanoff, and Clint Barton—raid a Hydra facility commanded by Baron Wolfgang von Strucker, who has experimented on humans using the scepter previously wielded by Loki.
node: They meet two of Strucker's test subjects—twins Pietro (who has superhuman speed) and Wanda Maximoff (who has telepathic and telekinetic abilities)—and apprehend Strucker, while Stark retrieves Loki's scepter.
meta_replacer output: node: and attacks the Avengers at their headquarters.Escaping with the scepter, Ultron uses the resources in Strucker's Sokovia base to upgrade his rudimentary body and build an army of robot drones.Having killed Strucker, he recruits the Maximoffs, who hold Stark responsible for their parents' deaths by his company's weapons, and goes to the base of arms dealer Ulysses Klaue in Johannesburg to get vibranium.The Avengers attack Ultron and the Maximoffs, but Wanda subdues them with haunting visions, causing Banner to turn into the Hulk and rampage until Stark stops him with his anti-Hulk armor. [a] A worldwide backlash over the resulting destruction, and the fears Wanda's hallucinations incited, send the team into hiding at Barton's farmhouse.Thor departs to consult with Dr.Erik Selvig on the apocalyptic future he saw in his hallucination, while Nick Fury arrives and encourages the team to form a plan to stop Ultron.
node: In the Eastern European country of Sokovia, the Avengers—Tony Stark, Thor, Bruce Banner, Steve Rogers, Natasha Romanoff, and Clint Barton—raid a Hydra facility commanded by Baron Wolfgang von Strucker, who has experimented on humans using the scepter previously wielded by Loki.They meet two of Strucker's test subjects—twins Pietro (who has superhuman speed) and Wanda Maximoff (who has telepathic and telekinetic abilities)—and apprehend Strucker, while Stark retrieves Loki's scepter. Stark and Banner discover an artificial intelligence within the scepter's gem, and secretly decide to use it to complete Stark's "Ultron" global defense program.The unexpectedly sentient Ultron, believing he must eradicate humanity to save Earth, eliminates Stark's A.I.
from ragas.metrics import faithfulness, answer_relevancy, context_precision, context_recall from ragas import evaluate from datasets import Dataset from llama_index.core.query_pipeline import CustomQueryComponent from typing import Dict, Any
question = "Which two members of the Avengers created Ultron?" ground_truth = "Tony Stark (Iron Man) and Bruce Banner (The Hulk)." output = p.run(input=question, ground_truth=ground_truth) print(f"answer: {output['answer']}") print(f"evaluation: {output['evaluation']}")
# 显示结果 answer: Tony Stark and Bruce Banner evaluation: {'faithfulness': 1.0000, 'answer_relevancy': 0.8793, 'context_precision': 1.0000, 'context_recall': 1.0000}