希言资源网

专业游戏攻略及百科知识分享平台

tender是什么意思中文(20秒记一个单词(第750个)tender)

RAG技术:使用 LlamaIndex Workflow和 Groq 实现高级 RAG

AI 应用程序由不同组件执行的各种任务组成。为了简化 AI 工程师的工作,开源框架为数据加载器、大型语言模型 (LLM)、向量数据库和重新排序器等基本元素提供了用户友好的抽象,并扩展到外部服务。这些框架还在探索协调这些组件的最有效方法,专注于开发人员创建有凝聚力的 AI 系统最直观、最有效的方法。

正在考虑的编排方法包括chains和pipelines,两者都基于有向无环图 (DAG) 模型。

今年早些时候( 年),LlamaIndex 推出了我们的查询管道,这是一种声明性 API,旨在促进各种查询工作流的编排,以用于问答、结构化数据提取和自动化流程等应用程序。然而,当我们尝试通过集成更复杂的工作流的循环来增强这一点时,我们遇到了一些挑战。这促使我们重新考虑 DAG 模型是否适用于代理环境,并探索我们框架内的替代解决方案。

这导致了对工作流的探索。工作流是实现代理的一种新方法。它遵循事件驱动架构。

基于图形的框架的局限性是什么?

基于 Graph/DAG 的方法(如 LangGraph,以及我们之前的查询管道)必须在后台进行大量繁重的工作,才能确定“接下来运行什么,输入是什么,等等”。所有这些逻辑都伴随着大量边缘情况,这在我们日益复杂的查询管道代码库中变得非常明显。

在有向无环图 (DAG)的背景下,无环特性意味着没有循环,这可能会限制日益代理的 AI 应用。当组件产生不良结果时,开发人员需要能够实现自我纠正机制。即使没有纳入循环,查询管道也面临着几个挑战:

  • 调试困难识别问题变得麻烦。
  • 模糊执行: 组件的执行流程不透明。
  • 复杂的编排: 编排器变得过于复杂,需要管理大量边缘情况。
  • 可读性问题: 复杂的管道难以理解。

当查询管道中添加循环时,这些开发人员体验问题会加剧。遇到了重大痛点,例如:

  • 繁琐的逻辑: 核心编排逻辑,例如 if-else 和 while 循环,使图的边缘变得混乱,变得冗长。
  • 边缘情况处理:管理可选值和默认值变得有问题,因为不清楚参数是否会从上游节点传递。
  • 自然流中断: 对于创建基于代理的系统的开发人员来说,定义带有循环的图表感觉不自然。“代理”节点需要明确的传入和传出边,这导致与其他节点的通信模式冗长。

为了克服上述问题,LlamaIndex 提出了一种新的设计范式转变,从而产生了称为工作流的甚至驱动架构。

什么是工作流?

这是一种基于事件驱动步骤的方式来控制应用程序的执行流程。在这里,应用程序被划分为由事件触发的部分,称为步骤。

步骤实际上是 Python 函数。它们可以是单行代码,也可以是多行复杂代码。

事件实际上是指状态的变化或我们可以注意、观察或记录的任何事物。

通过结合步骤和事件,我们可以创建任意复杂的流程,封装逻辑并使我们的应用程序更易于维护和理解。

总而言之,工作流(以及一般的事件驱动编程)提供了更强大的解决方案。代码在底层更加简单,因为现在完全由用户决定下一步运行哪些步骤、用户如何调试,并且消除了其他方法(如我们的查询管道)存在的许多“黑盒”问题。

LlamaIndex Workflow相对于 LangGraph 的价值主张?

LlamaIndex Workflows 提供了几个独特的特性和功能,使其有别于 LangGraph:

  1. 数据连接器: 从各种本机来源和格式(例如 API、PDF 和 SQL 数据库)提取数据的工具
  2. 数据索引: 以中间表示形式构建数据,以便 LLM 轻松且高效地使用。
  3. 引擎:用于自然语言访问数据的不同类型的引擎:
  • 查询引擎:用于问答界面(例如,RAG 管道)。
  • 聊天引擎:用于具有多消息交互的对话界面

4.代理: 由 LLM 提供支持的知识工作者,通过工具进行增强,包括简单的辅助函数和 API 集成。ReActAgent 实现允许定义一组工具,这些工具可以是 Python 函数或 LlamaIndex 查询引擎,以对数据进行自动推理

5.可观察性/评估: 对应用程序进行严格实验、评估和监控的集成。

6. LlamaCloud:数据解析、提取、索引和检索的托管服务,包括最先进的文档解析解决方案 LlamaParse。

7.社区和生态系统: 强大的社区影响力和相关项目,如用于自定义数据连接器的 LlamaHub 和用于快速搭建项目的 create-llama。

8.集成灵活性:既可以用于入门构建,也可以用于定制构建。用户可以从llama-index软件包开始快速设置,也可以llama-index-core从 LlamaHub 添加特定集成,LlamaHub 提供 多个集成包。

9.高级检索/查询接口: 提供高级接口,用于提供 LLM 输入提示并检索上下文和知识增强输出。

.方便初学者和高级用户使用: 高级 API 方便初学者仅用几行代码即可提取和查询数据,同时还为高级用户提供低级 API 以自定义和扩展模块。

. Agentic Components:核心模块能够自动推理数据的不同用例,使它们本质上成为代理。示例包括用于多文档分析、查询转换、路由和 LLM 重新排序的 SubQuestionQueryEngine。

. 原生 OpenAIAgent: 包含OpenAIAgent基于 OpenAI API 构建的用于函数调用的实现,允许快速进行代理开发。

. 与其他框架集成: 可用作其他代理框架(如 LangChain 和 ChatGPT)内的工具,提供深度集成和附加功能。

这些功能共同使 LlamaIndex 成为使用 LLM 构建情境增强生成式 AI 应用程序的综合框架。

设计工作流程

在这里,我们将使用 LlamaIndex 工作流实现高级 RAG 系统

  1. 索引数据,创建索引
  2. 使用该索引 + 查询来检索相关文本块
  3. 使用原始查询对文本检索到的文本块进行重新排序
  4. 综合最终回应

高级 RAG 工作流程

实现所用的技术堆栈

代码实现

  1. 安装所需的依赖项
pip install -qU llama-index 
pip install -qU llama-index-llms-groq
pip install -qU llama-index-embeddings-huggingface
pip install -qU lama-index-utils-workflow
pip install python-dotenv
pip install pyvis

2. 创建 .env 文件来保存 api key

GROQ_API_KEY = 

3. 设置 Groq_API_Key

import os
from dotenv import load_dotenv

load_dotenv()  # take environment variables from .env.
os.getenv("GROQ_API_KEY")

4.导入所需的依赖项

from llama_index.core import VectorStoreIndex
from llama_index.core.schema import NodeWithScore
from llama_index.core.response_synthesizers import CompactAndRefine
from llama_index.core import SimpleDirectoryReader
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.core.response_synthesizers import CompactAndRefine
from llama_index.core.postprocessor.llm_rerank import LLMRerank
from llama_index.core.workflow import (
    Context,
    Workflow,
    StartEvent,
    StopEvent,
    step,
    Event
)
from llama_index.core.workflow.utils import get_steps_from_class, get_steps_from_instance
from llama_index.llms.groq import Groq
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

5. 设置工作流事件。

为了处理这些步骤,我们需要定义一些事件:

  1. 将检索到的节点传递给重新排序器的事件
  2. 将重新排序的节点传递给合成器的事件

其余步骤将使用内置StartEventStopEvent事件。

class RetrieverEvent(Event):
    """Result of running retrieval"""
    nodes: list[NodeWithScore]

class RerankEvent(Event):
    """Result of running reranking on retrieved nodes"""
    nodes: list[NodeWithScore]

6.设置工作流程

class RAGWorkflow(Workflow):
    @step
    async def ingest(self, ctx: Context, ev: StartEvent) -> StopEvent | None:
        """Entry point to ingest a document, triggered by a StartEvent with `dirname`."""
        dirname = ev.get("dirname")
        if not dirname:
            return None

        documents = SimpleDirectoryReader(dirname).load_data()
        index = VectorStoreIndex.from_documents(
            documents=documents,
            embed_model=HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"),
        )
        return StopEvent(result=index)

    @step
    async def retrieve(
        self, ctx: Context, ev: StartEvent
    ) -> RetrieverEvent | None:
        "Entry point for RAG, triggered by a StartEvent with `query`."
        query = ev.get("query")
        index = ev.get("index")

        if not query:
            return None

        print(f"Query the database with: {query}")

        # store the query in the global context
        await ctx.set("query", query)

        # get the index from the global context
        if index is None:
            print("Index is empty, load some documents before querying!")
            return None

        retriever = index.as_retriever(similarity_top_k=2)
        nodes = await retriever.aretrieve(query)
        print(f"Retrieved {len(nodes)} nodes.")
        return RetrieverEvent(nodes=nodes)

    @step
    async def rerank(self, ctx: Context, ev: RetrieverEvent) -> RerankEvent:
        # Rerank the nodes
        ranker = LLMRerank(
            choice_batch_size=5, top_n=3, 
            llm=Groq(model="llama--70b-versatile")
        )
        print(await ctx.get("query", default=None), flush=True)
        new_nodes = ranker.postprocess_nodes(
            ev.nodes, query_str=await ctx.get("query", default=None)
        )
        print(f"Reranked nodes to {len(new_nodes)}")
        print(new_nodes)
        return RerankEvent(nodes=new_nodes)

    @step
    async def synthesize(self, ctx: Context, ev: RerankEvent) -> StopEvent:
        """Return a streaming response using reranked nodes."""
        llm = Groq(model="llama--70b-versatile")
        summarizer = CompactAndRefine(llm=llm, streaming=True, verbose=True)
        query = await ctx.get("query", default=None)

        response = await summarizer.asynthesize(query, nodes=ev.nodes)
        return StopEvent(result=response)        

7.检查步骤是否正确实例化

# Check if steps have __step_config attribute
workflow = RAGWorkflow()
steps = get_steps_from_class(RAGWorkflow)
if not steps:
    steps = get_steps_from_instance(workflow)
print(f"steps class :{steps}")
for step_name, step_func in steps.items():
    step_config = getattr(step_func, "__step_config", None)
    print(f"step config :{step_config}")
    if step_config is None:
        print(f"Step {step_name} is missing __step_config")

回复

steps class :{'_done': , 'ingest': , 'rerank': , 'retrieve': , 'synthesize': }
step config :accepted_events=[] event_name='ev' return_types=[] context_parameter='ctx' num_workers=1 requested_services=[]
step config :accepted_events=[] event_name='ev' return_types=[] context_parameter='ctx' num_workers=1 requested_services=[]
step config :accepted_events=[] event_name='ev' return_types=[] context_parameter='ctx' num_workers=1 requested_services=[]
step config :accepted_events=[] event_name='ev' return_types=[] context_parameter='ctx' num_workers=1 requested_services=[]
step config :accepted_events=[] event_name='ev' return_types=[] context_parameter='ctx' num_workers=1 requested_services=[]

8. 调用工作流并进行可视化

import nest_asyncio
nest_asyncio.apply()

# Visualization
from llama_index.utils.workflow import draw_all_possible_flows, draw_most_recent_execution

# Draw all possible flows
draw_all_possible_flows(RAGWorkflow, filename="multi_step_workflow.html")

# Draw the most recent execution
w = RAGWorkflow()

# Ingest the documents
index = await w.run(dirname="Data")
result = await w.run(query="What is Fibromyalgia?", index=index)
async for chunk in result.async_response_gen():
    print(chunk, end="", flush=True)
draw_most_recent_execution(w, filename="rag_flow_recent.html")

回复

multi_step_workflow.html
Query the database with: What is Fibromyalgia?
Retrieved 2 nodes.
What is Fibromyalgia?
Reranked nodes to 2
[NodeWithScore(node=TextNode(id_='abde4b4c-b787--acf5-2f5bd05d867c', embedding=None, metadata={'page_label': '', 'file_name': 'fibromyalgia.pdf', 'file_path': 'c:\\Users\\PLNAYAK\\Documents\\workflow\\Data\\fibromyalgia.pdf', 'file_type': 'application/pdf', 'file_size': , 'creation_date': '', 'last_modified_date': ''}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={: RelatedNodeInfo(node_id='7b100860-f0b3-445b-b5d6-21f021d8c3c0', node_type=, metadata={'page_label': '', 'file_name': 'fibromyalgia.pdf', 'file_path': 'c:\\Users\\PLNAYAK\\Documents\\workflow\\Data\\fibromyalgia.pdf', 'file_type': 'application/pdf', 'file_size': , 'creation_date': '', 'last_modified_date': ''}, hash='65d90d8fae093e6a574c784e808701ce0596d595e3e6136f7f0b4a70be8d2b57'), : RelatedNodeInfo(node_id='7edbd742-55e9--9e9f-55d8688c6e62', node_type=, metadata={}, hash='08366d434882c9ea475468bcff6e0fe183d4192c93617ec3cf3700cf03fd5a65')}, text='February   ◆ Volume , Number 2  www.aafp.org/afp  American Family Physician  137Fibromyalgia is characterized by diffuse mus -\nculoskeletal pain, fatigue, poor sleep, and other \nsomatic symptoms.1 Chronic diffuse pain affects \n10% to % of adults in the general population worldwide, many of whom have fibromyalgia.\n2,3 \nApproximately 2% of people in the United States have fibromyalgia, although the prevalence var -\nies across populations and with the diagnostic criteria used.\n3 Fibromyalgia can occur in chil -\ndren and adults and is found worldwide and across cultures. Women are diagnosed more \nfrequently than men;   a Scot tish survey found \nthat women are diagnosed between two and  times as often as men depending on the crite -\nria used.\n3,4 Changes in the diagnostic criteria over the past decade, including the elimination of specific tender points, have resulted in more patients with chronic pain meeting the criteria for fibromyalgia.\n3-5\nPathophysiology\nFibromyalgia is likely caused by disordered cen -\ntral nociceptive signal processing that leads to sensitization expressed as hyperalgesia and allo -\ndynia, which is similar to chronic pain conditions such as irritable bowel syndrome, interstitial cys -\ntitis, chronic pelvic pain, and chronic low back pain.\n6,7 Functional brain imaging suggests that \nthis aberrant processing may be attributed to an imbalance between excitatory and inhibitory neu -\nrotransmitters, particularly within the insula.\n8 \nSuggested etiologies include dysfunction of the hypothalamic-pituitary-adrenal axis and the autonomic nervous system, diffuse inflammation, glial cell activation, small fiber neuropathy, and infections such as the Epstein-Barr virus, Lyme disease, and viral hepatitis.\n9 Twin studies suggest \na genetic component may also be a factor.10Fibromyalgia:   Diagn osis and Management\nBradford T. Winslow, MD, University of Colorado School of Medicine, Aurora, \nColorado;   Swedi sh Family Medicine Residency, Englewood, Colorado\nCarmen Vandal, MD, and Laurel Dang, MD, Swedish Family Medicine Residency, Englewood, Colorado\n CME  This clinical content conforms to AAFP criteria for \nCME. See CME Quiz on page .\nAuthor disclosure:   No relevant financial relationships.\nPatient information:   A handout on this topic, written by the \nauthors of this article, is available with the online version of \nthis article.Fibromyalgia is a chronic, centralized pain syndrome characterized by disordered processing of painful stimuli. Fibromyal -\ngia is diagnosed more frequently in women and occurs globally, affecting 2% of people in the United States. Patients with \nfibromyalgia have diffuse chronic pain, poor sleep, fatigue, cognitive dysfunc -\ntion, and mood disturbances. Comorbid conditions, such as functional somatic syndromes, psychiatric diagnoses, and rheumatologic conditions may be pres -\nent. The Fibromyalgia Rapid Screening Tool is a helpful screening method for patients with diffuse chronic pain. The American College of Rheumatology criteria or the Analgesic, Anesthetic, and Addiction Clinical Trial Translations Innovations Opportunities and Networks–American Pain Society Pain Taxonomy diagnostic criteria can diagnose fibromyalgia. Establishing the diagnosis and providing education can reassure patients and decrease unnecessary testing. A multidisciplinary approach that incorporates nonpharmacologic therapies and medications to address problematic symptoms is most effective. Patient educa -\ntion, exercise, and cognitive behavior therapy can improve pain and function. Duloxetine, milnacipran, pregabalin, and amitriptyline are potentially effective medications for fibromyalgia. Nonsteroi -\ndal anti-inflammatory drugs and opioids have not demonstrated benefits for fibromyalgia and have significant limitations.  \n(Am Fam Physician\n. ;  (2):  . Copyright (c)  American Academy of Family Physicians.)\nIllustration by Jonathan Dimes\nDownloaded from the American Family Physician website at www.aafp.org/afp. Copyright (c)   American Academy of Family Physicians. For the private, non -\ncommercial use of one individual user of the website. All other rights reserved. Contact copyrights@aafp.org for copyright questions and/or permission requests.Downloaded from the American Family Physician website at www.aafp.org/afp. Copyright (c)   American Academy of Family Physicians.', mimetype='text/plain', start_char_idx=0, end_char_idx=, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=), NodeWithScore(node=TextNode(id_='0a680a09-1f8d-409e-bbdc-b562b4879f6f', embedding=None, metadata={'page_label': '', 'file_name': 'fibromyalgia.pdf', 'file_path': 'c:\\Users\\PLNAYAK\\Documents\\workflow\\Data\\fibromyalgia.pdf', 'file_type': 'application/pdf', 'file_size': , 'creation_date': '', 'last_modified_date': ''}, excluded_embed_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], excluded_llm_metadata_keys=['file_name', 'file_type', 'file_size', 'creation_date', 'last_modified_date', 'last_accessed_date'], relationships={: RelatedNodeInfo(node_id='f2591e71-4fd5-48ef-8c08-ab465fdabf88', node_type=, metadata={'page_label': '', 'file_name': 'fibromyalgia.pdf', 'file_path': 'c:\\Users\\PLNAYAK\\Documents\\workflow\\Data\\fibromyalgia.pdf', 'file_type': 'application/pdf', 'file_size': , 'creation_date': '', 'last_modified_date': ''}, hash='e0f0653ead2af78ad773abacd139db824a7e40216476d9aa63077b83a7370686')}, text='  American Family Physician  www.aafp.org/afp  Volume , Number 2  ◆ February \nFIBROMYALGIAClinical Presentation\nChronic diffuse pain is the predominant \nsymptom in most patients with fibromyalgia. Patients may also experience muscle stiffness and tenderness. The physical examination in patients with fibromyalgia generally finds diffuse tenderness without other unusual findings. If joint swelling, inflammation, or deformities are present, an alternative or additional diagnosis should be investigated.\n5 \nFatigue and sleep disturbances are also com -\nmon.5, Sleep disturbances include difficulty \nfalling and staying asleep, frequent awaken -\nings, or feeling unrefreshed after sleeping. Comorbid mental health diagnoses are com -\nmon, as are cognitive symptoms such as poor concentration, forgetfulness, or altered think -\ning.\n5,6, This cognitive dysfunction has been \ntermed “fibrofog” and is described by patients as a mental slowing that adversely affects daily activities.\n13\nThe presence of another painful disorder \ndoes not exclude the diagnosis of fibromyal -\ngia. The Fibromyalgia Rapid Screening Tool can screen patients with diffuse chronic pain to help distinguish between fibromyalgia and other conditions (Table 1) .\n14 The tool may SORT:   KEY RECOMMENDATIONS FOR PRACTICE\nClinical recommendationEvidence \nrating Comments\nThe diagnosis of fibromyalgia should be considered in patients \nwith diffuse pain, fatigue, and sleep disturbances that have been present for at least three months.\n5,11C Diagnosis of fibromyalgia can be made using AAPT  diagnostic criteria or the American College of Radiology / criteria\nPatients with fibromyalgia should be offered a multidisci -\nplinary treatment approach that includes education, exercise, and nonpharmacologic and pharmacologic options.\n27,28C Consensus guidelines and systematic reviews\nCognitive behavior therapy leads to improvement in pain and disability in patients with fibromyalgia in the short and medium term.\n32,,35A Systematic reviews demonstrate improvement\nAmitriptyline, cyclobenzaprine, duloxetine (Cymbalta), mil -\nnacipran (Savella), and pregabalin (Lyrica) are effective for pain in fibromyalgia.\n43,,,,54A Systematic reviews demonstrate effectiveness of these medications\nAAPT = Analgesic, Anesthetic, and Addiction Clinical Trial Translations Innovations Opportunities and Networks–American Pain Society Pain \nTaxonomy.\nA = consistent, good-quality patient-oriented evidence;   B = inconsistent or limited-quality patient-oriented evidence;   C = consensus, disease -\noriented evidence, usual practice, expert opinion, or case series. For information about the SORT evidence rating system, go to https://  www.aafp.\norg/afpsort.\nTABLE 1\nFibromyalgia Rapid Screening Tool (FiRST)\n Yes\nI have pain all over my body.  \nMy pain is accompanied by a continuous and very unpleas -\nant general fatigue. \nMy pain feels like burns, electric shocks, or cramps.  \nMy pain is accompanied by other unusual sensations \nthroughout my body, such as pins and needles, tingling, or numbness. \nMy pain is accompanied by other health problems such as digestive problems, urinary problems, headaches, or restless legs. \nMy pain has a significant impact on my life, particularly on my sleep and my ability to concentrate, making me feel slower in general. \nTotal*  \n*—One point for each yes answer. A score of 5 or greater suggests fibromyalgia.\nAdapted with permission from Perrot S, Bouhassira D, Fermanian J;   CEDR (C ercle \nd’Etude de la Douleur en Rhumatologie). Development and validation of the Fibro -\nmyalgia Rapid Screening Tool (FiRST). Pain. ;  (2):  .\nDescargado para Boletin -BINASSS (bolet-binas@binasss.sa.cr) en National Library of Health and Social Security de ClinicalKey.es por Elsevier en marzo , \n2023. Para uso personal exclusivamente. No se permiten otros usos sin autorización. Copyright (c). Elsevier Inc. Todos los derechos reservados.', mimetype='text/plain', start_char_idx=0, end_char_idx=, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=)]
Fibromyalgia is a chronic, centralized pain syndrome characterized by disordered processing of painful stimuli. It is characterized by diffuse musculoskeletal pain, fatigue, poor sleep, and other somatic symptoms.rag_flow_recent.html

可视化工作流程

工作流程观察:

  • 我们有两个入口点(接受的步骤StartEvent
  • 步骤本身决定何时可以运行
  • 工作流上下文用于存储用户查询
  • 节点被传递,最后返回流式响应

RAG工作流程:

  • 工作流由StartEvent触发。
  • StartEvent触发摄取步骤,对文档进行索引、分块并将其加载到 VectorStore 中。
  • 摄取步骤返回生成的索引,此步骤由StopEvent关闭
  • 然后StartEvent触发以查询和索引作为输入的检索步骤
  • 检索步骤触发RetrieverEvent,返回与查询匹配的节点。
  • RetrieverEvent以匹配的节点作为输入触发重新排序步骤。
  • 重新排序步骤根据匹配节点与查询的接近程度对其进行重新排序,并触发 RerankEvent
  • 然后,RerankEvent触发合成步骤,根据重新排序的节点生成最终响应。

可视化当前执行工作流

关于工作流执行的一些观察:

以下是根据我的观察得出的一些详细见解和建议:

1. Python 兼容性

观察:工作流程和可视化在 Python 下可以正常工作。

建议:Python 支持类型注释的 | 运算符,这简化了类型提示。如果可能,请使用 Python 或更高版本进行开发以利用这些功能并避免兼容性问题。

2. Python 兼容性

观察:| 运算符在 Python 中不起作用,您必须修改 utils.py 中的 num_of_events。

推荐:

类型提示: 在 Python 中,使用 typing 模块中的 Union 而不是 | 运算符进行类型注释。

验证修复: 直接在 utils.py 中修改 num_of_events 是一种解决方法,但并不理想。相反,请确保每个步骤函数只有一个用 Event 类型注释的参数才能通过验证。

3. Google Colab 中的可视化

观察:即使 notebook=True,可视化在 Google Colab 中也无法工作。

推荐:

确保正确安装:确保所有必需的包都已安装在 Colab 中。

!pip install pyvis llama-index-core llama-index-llms-openai llama-index-embeddings-openai llama-index-readers-file llama-index-utils-workflow

#Check HTML Rendering: Use the following approach to render the HTML content in Colab:

from pyvis.network import Network
from IPython.core.display import display, HTML

def draw_all_possible_flows(
    workflow: Workflow,
    filename: str = "workflow_all_flows.html",
    notebook: bool = True,  # Set notebook to True
) -> None:
  
    net = Network(directed=True, height="750px", width="%")

    # Add the nodes + edge for stop events
    net.add_node(
        StopEvent.__name__,
        label=StopEvent.__name__,
        color="#FFA07A",
        shape="ellipse",
    )
    net.add_node("_done", label="_done", color="#ADD8E6", shape="box")
    net.add_edge(StopEvent.__name__, "_done")

    # Add nodes from all steps
    steps = get_steps_from_class(workflow)
    if not steps:
        # If no steps are defined in the class, try to get them from the instance
        steps = get_steps_from_instance(workflow)

    step_config: Optional[StepConfig] = None
    for step_name, step_func in steps.items():
        step_config = getattr(step_func, "__step_config", None)
        if step_config is None:
            continue

        net.add_node(
            step_name, label=step_name, color="#ADD8E6", shape="box"
        )  # Light blue for steps

        for event_type in step_config.accepted_events:
            net.add_node(
                event_type.__name__,
                label=event_type.__name__,
                color="#90EE90" if event_type != StartEvent else "#E27AFF",
                shape="ellipse",
            )  # Light green for events

    # Add edges from all steps
    for step_name, step_func in steps.items():
        step_config = getattr(step_func, "__step_config", None)

        if step_config is None:
            continue

        for return_type in step_config.return_types:
            if return_type != type(None):
                net.add_edge(step_name, return_type.__name__)

        for event_type in step_config.accepted_events:
            net.add_edge(event_type.__name__, step_name)

    if notebook:
        net.show(filename, notebook=True)
        with open(filename, "r") as file:
            display(HTML(file.read()))
    else:
        net.show(filename)

# Example usage in Google Colab
draw_all_possible_flows(
    RAGWorkflow, filename="multi_step_workflow.html", notebook=True
)

概括

  • Python :与|运算符和工作流可视化配合良好。
  • Python :使用 Union 进行类型注释并确保正确的事件参数注释,以避免修改 utils.py。
  • Google Colab:确保所有依赖项都已安装并使用提供的方法来呈现 HTML 内容。

通过遵循这些建议,你应该能够在不同的环境和 Python 版本中实现一致的功能

结论

随着人工智能应用的复杂性和代理功能性不断提高,传统有向无环图 (DAG) 的局限性也日益凸显。无法加入循环限制了开发人员实施自我修正机制的能力,而自我修正机制对于维护稳健且自适应的人工智能系统至关重要。

展望未来,探索适应现代人工智能应用动态和迭代特性的替代框架至关重要。通过优先考虑直观的设计和高效的编排方法,我们可以让开发人员创建更具弹性和适应性的人工智能系统,这些系统可以从错误中吸取教训,并在日益复杂的环境中蓬勃发展。

这里我们探索并实现了使用 LlamaIndex Workflow 的用例。我们仍需要进行更多实验才能了解其全部潜力,并轻松实现和解释复杂的架构。

参考:

https://medium.com/the-ai-forum/implementing-advanced-rag-using-llamaindex-workflow-and-groq-bd6047299fa5

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言