<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tredence Studio on Medium]]></title>
        <description><![CDATA[Stories by Tredence Studio on Medium]]></description>
        <link>https://medium.com/@tredencestudio?source=rss-a87d5b3eb4c0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Xh8IA50wHUiZPJXqStrqyw.png</url>
            <title>Stories by Tredence Studio on Medium</title>
            <link>https://medium.com/@tredencestudio?source=rss-a87d5b3eb4c0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 08:58:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tredencestudio/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Multi-Modal Retrieval-Augmented Generation (RAG) Systems: An In-Depth Overview and Implementation]]></title>
            <link>https://medium.com/@tredencestudio/multi-modal-retrieval-augmented-generation-rag-systems-an-in-depth-overview-and-implementation-f5a7531be8a9?source=rss-a87d5b3eb4c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5a7531be8a9</guid>
            <category><![CDATA[augmented-generation]]></category>
            <dc:creator><![CDATA[Tredence Studio]]></dc:creator>
            <pubDate>Mon, 22 Jul 2024 11:00:48 GMT</pubDate>
            <atom:updated>2024-07-22T11:00:48.856Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cgnn5j_E6oyPS2AL0ar_fw.png" /></figure><h3>Introduction</h3><p>In the contemporary landscape of artificial intelligence, the need for systems capable of efficiently processing and interpreting diverse data types is paramount. Multi-modal Retrieval-Augmented Generation (RAG) is a pioneering approach that addresses this need by integrating the strengths of retrieval and generative models to handle text, tables, and images. This system enhances the capability to extract meaningful insights from complex documents, such as PDFs, which often contain a mixture of textual, tabular, and visual information.</p><p>This report provides an extensive overview of multi-modal RAG systems, focusing on their architecture, capabilities, and implementation. We will delve into the specifics of a Python-based multi-modal RAG system designed to summarize and process diverse data types and discuss its potential applications, benefits, and challenges.</p><h3>Understanding Multi-Modal RAG Systems</h3><h4>What is RAG?</h4><p>Retrieval-augmented generation (RAG) is an AI (Artificial Intelligence) framework that combines retrieval mechanisms with generative models to produce more accurate and contextually relevant outputs. In a typical RAG setup, a retrieval component searches for relevant information from a large corpus of documents or data, which a generative model then uses to produce responses or summaries. This combination allows for more informed and context-aware generation, improving the output quality.</p><h4>Multi-Modal RAG</h4><p>Multi-modal RAG extends this concept by incorporating multiple data modalities, such as text, images, and tables. The ability to process and integrate information from distinct data sources makes multi-modal RAG systems highly versatile and applicable to various use cases. These systems can understand and generate outputs considering the full spectrum of available information, leading to more comprehensive and accurate results.</p><h3>Critical Components of a Multi-Modal RAG System</h3><p><strong>Data Ingestion and Processing: </strong>The system needs to handle various data types, extracting and preprocessing information from text, tables, and images.</p><p><strong>Retrieval Mechanism:</strong> This component searches for relevant data from a corpus or database, using embeddings and other search algorithms to find the most pertinent information.</p><p><strong>Generative Model:</strong> A large language model (LLM) like GPT-4o, Gemini-vision pro generates summaries, answers, or other outputs based on the retrieved information.</p><p><strong>Integration and Output:</strong> The retrieved data and generative outputs are integrated to provide a coherent and contextually appropriate response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*65C62U2xFBMXbxytOQTUbw.png" /></figure><h3>System Architecture</h3><p>The system uses Python and integrates various libraries and tools to handle different data modalities. Key components include:</p><p><strong>Large Language Models (LLMs):</strong> Two LLMs are used — gemini-1.0-pro-vision-001 and GPT-4 omni.</p><p><strong>Embeddings:</strong> Used to represent text, tables, and images in a vector space for efficient retrieval.</p><p><strong>PostgreSQL:</strong> A database system for storing and managing tabular data.</p><p><strong>Milvus:</strong> A vector database used for storing and retrieving high-dimensional vectors.</p><h3><strong>Implementation</strong></h3><p>The implementation revolves around a multi-modal retrieval-augmented generation (RAG) system that processes and summarizes data from PDF files, including text, tables, and images.</p><p>The Multimodal class is designed to handle multiple data modalities, including text, tables, and images. It integrates with a large language model (LLM) for vision and text processing and provides various utility functions to summarize and process different data types.</p><h4>PDF Extraction</h4><p>To extract and classify distinct types of content from PDF files, facilitating efficient data processing and summarization. Here is a concise overview:</p><p>The file processing module is to dissect a PDF document into its constituent elements, including text, tables, and images, to facilitate further analysis and processing.</p><p>Its key features encompass taking a PDF file path and extracting various elements using a chunking strategy, wherein the document is divided into manageable chunks based on headings or titles. The module extracts and stores images for subsequent processing if image extraction is enabled.</p><p>Moreover, it identifies and extracts tables while converting them into a structured format. The module also incorporates character limits, such as max_characters, to control the chunk size, ensuring efficient data processing. Ultimately, the output of this module is a categorized list of elements, including texts, tables, and images, extracted from the PDF document, ready for further analysis and manipulation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*cRtyjuv64-adoW8kSpGmfg.png" /></figure><h3>Summarization</h3><p>The implementation includes distinct summarization processes for text, tables, and images, each tailored to its data type. <strong>Text Summarization</strong> returns the input texts as summaries, serving as a direct passthrough. <strong>Table Summarization</strong> employs a templated prompt to query with GPT-4o, which summarizes the tables, and this process allows for batch processing with a set concurrency limit to manage multiple tables efficiently. <strong>Image Summarization</strong> involves encoding images in base64 format, using Gemini-vision-pro to generate detailed descriptions, and returning both the encoded image and its summary, thus providing a comprehensive approach to handling and interpreting visual data.</p><h4>Storing Data in Milvus</h4><p>Preparation for data insertion involves processing and converting summarized elements (texts, tables, images) into suitable formats. Each component is encapsulated in a Document object with relevant metadata. Connecting to Milvus involves establishing a server connection by providing details such as host, port, and collection name derived from the selected file. The storage process entails inserting these documents into the Milvus vector database, where embeddings for each document are computed and stored. This enables efficient similarity search and retrieval, optimizing the system’s ability to fetch relevant information based on user queries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*lVcJQ63Vy9ADaybtchUHog.png" /></figure><h4>Storing Tabular data in PostgreSQL</h4><p>Adding table data to PostgreSQL involves converting the table into a DataFrame, mapping its data types to SQL-compatible types, and generating an SQL query to create the table in the database. The data is then inserted row-by-row, and the transaction is committed to ensure persistence. Synchronizing with PostgreSQL entails pushing the table data to the database, retrieving it, and displaying it in a DataFrame format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*W3FNaIyFLdG1M9r6QmoTmA.png" /></figure><h4>Using Multi-Vector-Retriever</h4><p>A MultiVectorRetriever object is created with the following components:</p><ul><li><strong>vectorstore:</strong> Reference to the Milvus vector database containing the stored embeddings.</li><li><strong>docstore:</strong> An in-memory document store to hold the original documents for quick access.</li><li><strong>id_key:</strong> A unique identifier key for each document.</li></ul><h3>Generating Responses</h3><p>When handling queries, the system employs a tailored workflow depending on whether the query relates to text, tables, or images.</p><p>For text-related queries, it begins by searching for semantically matching text summaries stored as documents in Milvus. The relevant text documents are fetched based on their similarity to the query, and the results are displayed using Streamlit’s text display functions. The language model then generates a response, shown below the text, explaining based on the retrieved data.</p><p>For table-related queries, the process involves searching Milvus for relevant table summaries. If a matching summary is found, the corresponding full table data is fetched from PostgreSQL, ensuring the response is based on actual data. The results, including text and table data, are displayed in Streamlit, with the language model’s response explaining the query results in context.</p><p>For image-related queries, the retriever searches Milvus for image summaries. The relevant summaries are fetched, and the images are displayed using Streamlit’s image display, which shows decoded base64 images. The vision model then generates a detailed description of the image, which is displayed below the image, explaining the query results based on the visual data.</p><h3>Conclusion</h3><p>The implementation of the multi-modal retrieval-augmented generation (RAG) system efficiently processes and responds to diverse queries related to text, tables, and images from PDF documents. The system ensures accurate and contextually relevant responses by leveraging advanced search capabilities in Milvus for semantic matching and integrating powerful language and vision models such as GPT-4o and Gemini-vision pro. The system retrieves and presents relevant summaries for text-related queries, supplemented by detailed explanations from a language model. Table-related queries benefit from a robust mechanism that fetches summaries from Milvus and synchronizes with PostgreSQL to utilize actual data, providing comprehensive insights. Image-related queries are handled through encoding, retrieval, and detailed description generation, ensuring users receive thorough explanations of visual content. This multi-modal approach enhances the user experience by offering tailored responses that cater to different data types, demonstrating the system’s versatility and effectiveness. Overall, the implementation highlights a seamless integration of retrieval and generation technologies, providing an efficient tool for extracting and summarizing information from complex documents.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5a7531be8a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Visualizing Data Seamlessly: LangGraph’s Role in Multi-Agent Workflows]]></title>
            <link>https://medium.com/@tredencestudio/visualizing-data-seamlessly-langgraphs-role-in-multi-agent-workflows-962e051f5020?source=rss-a87d5b3eb4c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/962e051f5020</guid>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[visualizing-data]]></category>
            <dc:creator><![CDATA[Tredence Studio]]></dc:creator>
            <pubDate>Mon, 22 Jul 2024 10:49:56 GMT</pubDate>
            <atom:updated>2024-07-22T10:49:56.347Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*osedHeaRU0gsl3bNoeM7iA.png" /></figure><h3>About LangGraph</h3><p>LangGraph is a programming library designed for graph-based processing and analysis of natural language. It provides tools for constructing, manipulating, and querying graphs where nodes represent linguistic elements (words, phrases, or sentences) and edges capture various relationships between these elements (such as syntactic dependencies, semantic similarities, or co-occurrences). By leveraging graph theory, LangGraph facilitates advanced NLP tasks, including text summarization, semantic search, information extraction, and knowledge representation. Its flexible and efficient framework supports integration with other NLP libraries and can handle complex language models, making it a valuable resource for researchers and developers in computational linguistics.</p><h3>About Data Visualization using LangGraph</h3><p>Data visualization using LangGraph involves orchestrating a multi-agent system to analyze data and create visual representations efficiently. LangGraph dynamically routes tasks to specialized agents for data analysis and chart generation by employing components like prompt templates and AI models. This approach ensures seamless data processing and visualization integration, leveraging LangGraph’s capabilities to manage complex workflows and produce insightful visual data representations.</p><h3>Functionality and Features</h3><p>LangGraph is a powerful library for advanced NLP using graph-based methods. Its key features include:</p><ol><li><strong>Graph Construction</strong>:- Create graphs from text data with nodes for words, phrases, or sentences and edges for relationships.</li><li><strong>Graph Manipulation</strong>: Modify graphs by adding, removing, merging, or splitting nodes and edges.</li><li><strong>Graph Querying</strong>: Perform complex queries and graph traversal to extract patterns.</li><li><strong>NLP Integration</strong>: Integrate with NLP tools and support machine learning models for tasks like named entity recognition.</li><li><strong>Visualization</strong>: Visualize graphs with customizable options to understand text structure.</li><li><strong>Analysis</strong>: Use centrality measures and graph metrics for text analysis.</li><li><strong>Applications</strong>: Text summarization, semantic search, information extraction, and knowledge representation.</li></ol><h3>Procedures for Utilizing LangGraph</h3><h4>1. Models Utilized</h4><ul><li>Large Language Model(LLM): Azure Open AI GPT-4o</li></ul><h4>2. Tools Used</h4><p>This function connects to a specified database and extracts information about a particular table, saving its contents as a CSV file. It verifies the existence of the schema and table, then retrieves the table’s data into a pandas DataFrame. The function outputs the first five rows, table dimensions, column data types, memory usage, number of unique values per column, and statistical data descriptions. If any errors occur, it returns an appropriate message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*kfhXCPF8dE2P_-G1XRpaXg.png" /></figure><p>This code snippet defines a tool for executing Python code within a REPL (Read-Eval-Print Loop) environment. The ‘python_repl’ function takes a string of Python code as input, runs it, and returns the output. If an error occurs during execution, it captures and returns the error message. Upon successful execution, it provides a formatted response, including the executed code and its output. This tool is helpful for dynamically running and debugging Python code snippets, making it easier to visualize results and perform on-the-fly computations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*I9YKqkx-U6YhV66G9wB_4w.png" /></figure><h4>3. Node Description</h4><ul><li>Node 1(data_analyser_node):- In this snippet, `data_analyser_agent` is created using the `create_agent` method from `utils,` with `get_table_info` as its tool and `data_analyser_prompt` as the system prompt. `data_analyser_node` is defined using `functools.partial` to create a reusable function for this agent, specifying it as the “data_analyser” node. This node, referred to as node 1, is responsible for analyzing table data by invoking the `get_table_info` tool within the agent framework.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*BUMooLV5phcNkty5KAwAmA.png" /></figure><ul><li>Node 2 (chart_generator_node):- In this snippet, a `chart_generator_agent` is created using the `create_chain_runnable` method from `utils,` with `chart_generator_prompt` as the system prompt. This agent is designed to generate charts based on the instructions provided. The `chart_generator_node` is defined using `functools.partial` to create a reusable function, specifying the `chart_generator_agent` as the chain and naming it “chart_generator.” This node will process the given state and generate charts, integrating with the rest of the workflow to visualize data as required.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*yAJ1KQYkUalUCUkwyYYwaw.png" /></figure><h4>4. Final Multi-Agent graph structure</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*l4LIE1GnIF7rb2RSvNQDBQ.png" /></figure><p>This graph illustrates a multi-agent workflow that begins with a `ChatPromptTemplate,` which sets up the prompt for the conversation. It then utilizes the `AzureChatOpenAI` model to process the input, followed by the `JsonOutputFunctionsParser` to parse the AI’s output and determine the next steps. ChatPrompt, AzureChat, and JsonOutputFunction comprise the LLM Chain that makes up the supervisor agent. The workflow routes tasks to either the `data_analyser` node, which analyzes data using the `data_analyser_agent`, or the `chart_generator` node, which generates charts using the `chart_generator_agent`. The process continues routing tasks until the `FINISH` condition is met, concluding at the `__end__` node. This setup demonstrates how an AI-driven supervisor manages the flow between data analysis and chart generation.</p><h4>5. Results</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/1*wIW57PnV5vBffRO72iSBuQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/1*xM1qceTQkRZK1RvR4NJCZw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*qv9SM_0t-FjGcxjTBEypLQ.png" /></figure><h3>Conclusion</h3><p>The provided code leverages `LangGraph` for multi-agent workflows and integrates with Datwhiz to visualize tables. The `GraphUtils` class orchestrates the creation of agents and chains, enabling them to process tasks and communicate results efficiently. By compiling a state graph with a supervisor node to manage task routing, this setup ensures that agents can dynamically report and delegate work. This combination enhances workflow management and table visualization, streamlining complex processes within any ecosystem.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=962e051f5020" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LlamaIndex Versus LangChain: Building Better Knowledge Graphs]]></title>
            <link>https://medium.com/@tredencestudio/building-better-knowledge-graphs-llamaindex-versus-langchain-9661eefb1bb4?source=rss-a87d5b3eb4c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/9661eefb1bb4</guid>
            <category><![CDATA[llamaindex]]></category>
            <category><![CDATA[knowledge-graph]]></category>
            <category><![CDATA[langchain]]></category>
            <dc:creator><![CDATA[Tredence Studio]]></dc:creator>
            <pubDate>Mon, 10 Jun 2024 09:22:56 GMT</pubDate>
            <atom:updated>2024-06-10T10:24:56.820Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Building Better Knowledge Graphs: LlamaIndex Versus LangChain" src="https://cdn-images-1.medium.com/max/1024/1*BlY9KmeqjhfFjX2P9cNO0w.png" /></figure><p><strong>Problem Statement</strong></p><p>We aim to compare the effectiveness of the LlamaIndex and LangChain approaches in building knowledge graphs. This involves examining how well each method organizes information and helps us find answers to questions. By comparing LlamaIndex and LangChain, we want to understand which is better at creating useful knowledge graphs.</p><h3><strong>Steps Involve for LlamaIndex</strong></h3><p><strong>1.LLM Model and Embedding Model Used</strong></p><ul><li>LLM model — anthropic.claude-v2</li><li>Embedding model — titan-embed</li></ul><p><strong>2. Reading PDF Files Using LlamaIndex and Converting Them to Document Type</strong></p><ul><li>PDFReader extracts text from each page of PDF and converts it to document type.</li><li>Only document-type chunks can be passed to the knowledge graph generator `KnowledgeGraphIndex.`</li><li>Following open-domain sustainability reports were used during the experimentation.</li><li><strong>‘Pepsico_AnnualReport_2021’</strong></li><li><strong>‘Pepsico_SustainabilityReport_2022’</strong></li><li><strong>‘Pepsico_SustainabilityReport_2020’</strong></li><li><strong>‘Pepsico_SustainabilityReport_2021’</strong></li><li><strong>‘Nestle_SustainabilityReport_2022’</strong></li><li><strong>‘KimberlyClark_AnnualReport_2020’</strong></li></ul><p><strong>3. Changing the Default LLM Model and Embedding Model</strong></p><ul><li>Changing the LLM model and embedding the model to ours because LlamaIndex uses OpenAI GPT models by default.</li><li>Added custom chunk_size (number of characters into text chunk) and chunk_overlap (how many common characters are between two chunks) to ensure consistency for both LlamaIndex and LangChain. The same chunk creator was used for both frameworks.</li></ul><p><strong>4. Creating the Knowledge Graph for the Documents Using Llamaindex</strong></p><p><strong>KnowledgeGraphIndex</strong> in LlamaIndex works by extracting knowledge triplets from unstructured text and organizing them into a graph structure that can be queried. Here’s a detailed explanation of how it works:</p><ul><li><strong>Document Loading</strong>: The first step involves loading the documents from which the knowledge graph will be built.</li><li><strong>LLM Definition</strong>: The next step is to define the Language Model (LLM) because it uses the LLM model to create a knowledge graph.</li><li><strong>Knowledge Graph Index Creation</strong>: The KnowledgeGraphIndex is created from the documents. This takes in the documents, max_triplets_per_chunk, and the service context (in which our llm model and embedding model are defined) as arguments. During this process, the LLM extracts knowledge triplets from the text. These triplets are then organized into a graph structure.</li></ul><p><strong>5.</strong> <strong>Ingesting Graphs into Opensearch Vector Database</strong></p><p><strong>OpenSearch Vector Database</strong> is a specialized storage system for vector embeddings, optimized for similarity searches. It efficiently organizes and retrieves data based on the similarity of their vector representations.</p><ul><li><strong>Setting Up Variables:</strong> Setting up variables that will be used to initialize the OpensearchVectorClient. These include the endpoint (the URL of the Opensearch server), the index (the name of the index in Opensearch in which data will be stored), the dimension of the vectors (dimension of embedding for each word), the text field (the name of the field in the index that contains the text data), the embedding field (the name of the field in the index that includes the vector embeddings), and the username and password for authentication.</li><li><strong>Initializing the OpensearchVectorClient:</strong> The OpensearchVectorClient is initialized with the previously set variables. This client is used to interact with the OpenSearch server.</li><li><strong>Initializing the OpensearchVectorStore:</strong> The OpensearchVectorStore is initialized with the OpensearchVectorClient. The OpensearchVectorStore is a vector store that uses OpenSearch as its backend. It is used to store and retrieve vector embeddings.</li><li><strong>Setting Up the StorageContext:</strong> The StorageContext is initialized with the OpensearchVectorStore as its vector store. The StorageContext in LlamaIndex is a container that holds the document, index, and vector stores. In this case, the vector store is set to the OpensearchVectorStore, and the document store and index store are set to their default values.</li><li><strong>Ingesting Data Using VectorStoreIndex:</strong> A VectorStoreIndex is a type of index in LlamaIndex that uses a vector store for storing and retrieving vector embeddings. VectorStoreIndex requires the documents(representing the list of documents to be stored), the storage_context(in which vector store is defined), and service_context(service context contains the services that are used by the index, such as the LLM model, embedding model, and chunk creator) as input parameters.</li></ul><p><strong>6. Creating OpensearchVectorClient Instance for Index Name in Which Data is Stored</strong></p><ul><li><strong>Setting Up variables:</strong> setting up variables that will be used to initialize the OpensearchVectorClient. These include the endpoint (the URL of the Opensearch server), the index (the name of the index in Opensearch in which data is stored), the dimension of the vectors (dimension of embedding for each word), the text field (the name of the field in the index that contains the text data), the embedding field (the name of the field in the index that includes the vector embeddings), and the username and password for authentication.</li><li><strong>Initializing the OpensearchVectorClient:</strong> The OpensearchVectorClient is initialized with the previously set variables. This client is used to interact with the OpenSearch server.</li><li><strong>Initializing the OpensearchVectorStore:</strong> The OpensearchVectorStore is initialized with the OpensearchVectorClient. The OpensearchVectorStore is a vector store that uses OpenSearch as its backend. It is used to store and retrieve vector embeddings.</li><li><strong>VectorStoreIndex.from_vector_store:</strong> creates index object taking vector_store as input, which is initialized with the index name in which data is stored.</li></ul><p><strong>7. Creating Q&amp;A Query Engine with Custom Prompt Using LlamaIndex</strong></p><p><strong>llama_index_qa</strong> function will take the question as input and retrieve graph nodes and edges from the vector store according to the question. Then, these retrieved graph nodes and edges will be passed to the prompt as context, and then it asks LLM to generate an answer for the question by passing a modified prompt as input to LLM.</p><h3>Steps Involve for LangChain</h3><ol><li><strong>LLM Model and Embedding Model Used</strong></li></ol><ul><li>LLM model — anthropic.claude-v2</li><li>Embedding model — titan-embed</li></ul><p><strong>2. Reading PDF Files Using Langchain and Converting Them to Documents Type</strong></p><ul><li><strong>PyPDFLoader </strong>extracts and converts text from each PDF page into a document type.</li><li>Open-domain sustainability reports were used during the experiment.</li><li><strong>‘Pepsico_AnnualReport_2021’</strong></li><li><strong>‘Pepsico_SustainabilityReport_2022’</strong></li><li><strong>‘Pepsico_SustainabilityReport_2020’</strong></li><li><strong>‘Pepsico_SustainabilityReport_2021’</strong></li><li><strong>‘Nestle_SustainabilityReport_2022’</strong></li><li><strong>‘KimberlyClark_AnnualReport_2020’</strong></li></ul><p><strong>3. Converting Loaded Documents into Chunks</strong></p><ul><li>Using <strong>RecursiveCharacterTextSplitter</strong>, same as llama-index, with the same chunk_size and chunk_overlap for converting documents into chunks.</li><li>The <strong>RecursiveCharacterTextSplitter</strong> is designed to split text into smaller, semantically meaningful chunks and then combine them into larger ones until they reach a specific size, with some overlap to maintain context between chunks. The number of characters measures the chunk size, and the chunk overlap is the maximum overlap between chunks.</li></ul><p><strong>4. Creating an Instance of a Knowledge Graph Generator Using LangChain</strong></p><ul><li>Using an LLM model, the <strong>GraphIndexCreator</strong> class in LangChain creates graph indexes from text. Specifically, when you instantiate a GraphIndexCreator and pass in an LLM, it configures the class to use that LLM model to parse text and extract knowledge graph triples.</li><li>So, creating the <strong>GraphIndexCreator</strong> initializes the graph creation tool and allows you to call the language model and build graphs using text. The graphs can then be used to power things like question answering.</li><li>In summary, it sets up a system to automate building knowledge graphs from text using the power of large language models. The LLM processes the text, and GraphIndexCreator prompts it appropriately and organizes the output into graph structures.</li></ul><p><strong>5. Storing All the Nodes and Edges of the Graph into a Vector Database Called OpenSearch</strong></p><ul><li><strong>Setting up authentication</strong> and configuration to interact with Amazon OpenSearch Service (AOS) from LangChain’s OpenSearchVectorSearch. This includes setting the service name as ‘es’, specifying the AWS region for AOS, retrieving temporary AWS credentials using boto3, creating an AWS4Auth object for authentication to AOS, and using OpenSearchVectorSearch to index documents and enable vector similarity search. Additionally, it configures the connection details to point to the AOS endpoint, including HTTP authentication, timeout, SSL usage, certificate verification, connection class, bulk indexing batch size, and index name.</li><li><strong>OpenSearchVectorSearch</strong> ingests the graph data into OpenSearch by taking all the specified documents and variables, enabling efficient storage and retrieval of vector embeddings within the OpenSearch backend.</li></ul><p><strong>6. Creating an Instance of OpenSearch Vector Database for Index Name in Which Data is Stored</strong></p><p>The instance of the OpenSearch vector database for the index name is created using the OpenSearchVectorSearch object. This enables similarity search over documents stored in Amazon OpenSearch Service. The process involves connecting to OpenSearch using credentials, specifying the embedding function for encoding text, defining the OpenSearch domain endpoint, setting the index name for the stored documents, and utilizing AWS Sigv4 authentication along with RequestsHttpConnection for HTTP requests.</p><p><strong>7. Creating Q&amp;A Query Chain with Custom Prompt Using LangChain</strong></p><p><strong>langchain_qa </strong>function will take the question as input and retrieve graph nodes and edges from the vector store according to the question. Then, these retrieved graph nodes and edges will be passed to the prompt as context, and then it asks llm to generate an answer for the question by passing a modified prompt as input to llm.</p><h3>Plotting Knowledge Graph as Image</h3><p>The created function takes an LLM result object as input, extracts graph data from it, constructs a directed graph using the NetworkX library and visualizes it using Matplotlib. The graph represents a knowledge graph generated by the llama-index system. Each node represents a concept, and edges represent relationships between concepts. The function displays the graph with labeled nodes and edges, visually representing the knowledge structure.</p><h3>Hallucination Evaluation</h3><p>Evaluates the quality of a language model’s answer. It takes the question, the model’s generated answer, and the context from which the answer was derived. It then measures the answer against the context to determine if it contains any hallucinated information. The function returns a score indicating the degree of hallucination, with a score above 0.5, meaning that the answer contains no hallucinated content.</p><h3>Implemented Code</h3><h3><strong>Results</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8e-vSlcIMEw45D-2TwiXCA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rz7waLS4tLENfFPEAzLwZQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4fnEuayiiYKiCesMbAzKFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eONQIxXsbQZHFPpYX-lJcQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T5EBtMtV9TMfDMCYkUOvnA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-qMs0O8QsJzg9arpcK1Log.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Su6s3VvhBSNMZgZ0HMG_w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gbM1_Z_AL19wRC2UOz29vg.png" /></figure><h3>Conclusion</h3><p>The comparative analysis of LlamaIndex and LangChain for knowledge graph construction reveals nuanced insights into their strengths and weaknesses. LlamaIndex, utilizing the anthropic.claude-v2 LLM and titan-embed model demonstrate strong document processing capabilities and knowledge graph generation capabilities. It excels in extracting and organizing knowledge triplets, making it highly effective for creating structured and queryable knowledge graphs. On the contrary, LangChain, with its similar setup, showcases efficiency in chunking documents and generating graph indexes, offering a streamlined approach to embedding and vector similarity search in OpenSearch. Ultimately, the choice between LlamaIndex and LangChain will depend on specific project requirements, but both frameworks provide potent tools for advancing knowledge graph technology.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9661eefb1bb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unlocking the Power of Retrieval Augmented Generation (RAG): A Comprehensive Evaluation]]></title>
            <link>https://medium.com/@tredencestudio/unlocking-the-power-of-retrieval-augmented-generation-rag-a-comprehensive-evaluation-e83a05ad3493?source=rss-a87d5b3eb4c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e83a05ad3493</guid>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[augmented-reality]]></category>
            <dc:creator><![CDATA[Tredence Studio]]></dc:creator>
            <pubDate>Mon, 03 Jun 2024 15:13:50 GMT</pubDate>
            <atom:updated>2024-06-04T18:57:04.793Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SnDDlZ70rhQu_Q1o-w2OwQ.png" /></figure><p>In natural language processing (NLP), the Retrieval Augmented Generation (RAG) paradigm represents a significant advancement, offering a hybrid approach that seamlessly integrates retrieval-based and generative methods. By combining the strengths of both approaches, RAG aims to overcome the limitations of traditional generative models, such as lack of context awareness and difficulty in producing relevant responses to diverse queries. At the heart of RAG lies the integration of a powerful retrieval mechanism, which leverages a large-scale database to gather relevant information that informs the subsequent generation of text responses.</p><p>In this article, we delve into evaluating RAG models using visualization methods. We aim to leverage the Chroma database for querying relevant documents and utilize UMAP projection to visualize embeddings in a two-dimensional space.</p><h3><strong>Objective: Assessing Contextual Relevance in RAG Models</strong></h3><p>The primary objective of this evaluation is to assess the effectiveness of RAG models in generating contextually relevant responses by visualizing the relationship between queries, retrieved documents, and generated text embeddings. Traditional evaluation metrics may provide insights into the fluency and coherence of generated text, but they cannot often assess the relevance of generated responses in relation to the input queries and retrieved documents.</p><h3><strong>Methodology: A Step-by-Step Approach</strong></h3><p><strong>Step 1: Data Collection</strong></p><p>We first identify relevant sources containing textual information in PDF format to collect a set of PDF documents for retrieval. Next, we systematically acquire these documents, ensuring comprehensive coverage of the desired content. The curated collection of PDFs serves as the knowledge base for retrieving information in response to user queries.</p><p><strong>Step 2: Chunking Strategies</strong></p><p>Chunking refers to dividing large text documents into smaller, manageable chunks for processing. There are several strategies for chunking text data, including:</p><p>Sentence-Level Chunks: This strategy involves dividing the text into chunks based on sentence boundaries. Each chunk contains one or more complete sentences. This approach ensures that the context is preserved within each chunk.</p><p>The choice of chunking strategy depends on factors such as the nature of the text data, the desired granularity of chunks, and the computational resources available for processing.</p><p><strong>Step 3: Generating Embeddings of the Chunks</strong></p><p>Once the text data is chunked into smaller segments, we generate embeddings for each chunk. Embeddings are dense vector representations of text data that capture semantic information about the text. There are several techniques for generating embeddings of text data, including:</p><p>Sentence Transformer Embeddings: We can also use techniques such as Universal Sentence Encoder (USE) to generate embeddings for entire sentences. This approach is instrumental when dealing with sentence-level chunks.</p><ul><li><strong>BAAI/gte-large-en:</strong> This is a larger version of the above model, offering improved performance in machine translation compared to the base version.</li><li><strong>Sentence-transformers/all-MiniLM-L12-v2 and all-MiniLM-L6-v2:</strong> These models are trained on massive text corpora and specialize in generating sentence embeddings. These embeddings can be used for downstream tasks like semantic search, clustering, and text classification.</li></ul><p>The choice of embedding technique depends on factors such as the size of the text data, the computational resources available, and the specific requirements of the task.</p><p><strong>Step 4: Creating a Chroma Collection</strong></p><p>Once we have generated embeddings for the text chunks, we create a Chroma collection to store these embeddings. Chroma is a database system optimized for storing and querying high-dimensional embeddings. To create a Chroma collection, we perform the following steps:</p><ul><li><strong>Initialize Chroma Client: </strong>We initialize a Chroma client object to connect to the Chroma database.</li><li><strong>Define Collection Name: </strong>We specify a name for the Chroma collection that will store the embeddings.</li><li><strong>Add Documents:</strong> We add the corresponding embedding to the Chroma collection using the add method for each text chunk. The document ID is used as the key to retrieve embeddings during querying.</li></ul><p>By storing the embeddings in a Chroma collection, we can efficiently query the collection to retrieve relevant chunks based on similarity to a given query.</p><p><strong>Step 5: Querying the Chroma Collection</strong></p><p>Once the Chroma collection is created and populated with embeddings, we can query the collection to retrieve relevant chunks based on a given query. The querying process involves the following steps:</p><ul><li><strong>Input Query: </strong>We provide a query to the RAG model, representing the user’s input or question.</li><li><strong>Generate Query Embedding:</strong> We generate an embedding for the input query using the same embedding technique for generating chunk embeddings.</li><li><strong>Query Chroma Collection: </strong>We query the Chroma collection using the generated query embedding to retrieve the most similar chunks. This is typically done using nearest-neighbor search algorithms such as k-nearest neighbors (KNN) or approximate nearest neighbors (ANN).</li><li><strong>Retrieve Relevant Chunks:</strong> The retrieved chunks are then passed to the generator component of the RAG model to generate contextually relevant responses.</li></ul><p>By querying the Chroma collection, we can efficiently retrieve relevant chunks based on the semantic similarity between the query and the stored embeddings.</p><p><strong>Step 6: Visualization</strong></p><p>To visualize the performance of the RAG model, we employ UMAP (Uniform Manifold Approximation and Projection) to project the high-dimensional embeddings into a two-dimensional space. UMAP is a dimensionality reduction technique that preserves the local structure of the data, making it well-suited for visualizing embeddings. We utilize Streamlit, a Python library for creating interactive web applications and developing interactive plots and dashboards for visualization.</p><p>The visualization components include:</p><ul><li><strong>Query-Document Visualization: </strong>We plot the embeddings of queries and retrieved documents in a two-dimensional space using UMAP. Each point in the plot represents a query or a document, and we use different colors or markers to distinguish between them. Users can interactively explore the spatial relationship between queries and relevant documents.</li><li><strong>Query-Response Visualization:</strong> We visualize the embeddings of queries and generated responses in a two-dimensional space. Each point in the plot represents a query or a generated response. Users can analyze how well the generated responses align with the context provided by the queries.</li></ul><h3>Analysis and Interpretation of Visualization Results</h3><p>Finally, we analyze the visualizations and evaluation metrics to assess the performance of the RAG model. We identify patterns, trends, and areas for improvement based on the visualization and evaluation results. This analysis provides insights into the effectiveness of the RAG model in generating contextually relevant responses and informs future research directions.</p><p>By following this methodology, we aim to provide a comprehensive evaluation of RAG models using visualization methods, thereby facilitating a deeper understanding of their capabilities and limitations in natural language generation tasks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e83a05ad3493" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAG Evaluation: Using Milvus and UMAP for Contextual Visualization]]></title>
            <link>https://medium.com/@tredencestudio/rag-evaluation-78170a4b13b5?source=rss-a87d5b3eb4c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/78170a4b13b5</guid>
            <category><![CDATA[rag-evaluation]]></category>
            <category><![CDATA[tredence-studio]]></category>
            <category><![CDATA[milvus]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <dc:creator><![CDATA[Tredence Studio]]></dc:creator>
            <pubDate>Thu, 23 May 2024 11:08:33 GMT</pubDate>
            <atom:updated>2024-06-10T10:26:02.384Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K5f0GjWmJIPa0-mHkGIbgA.png" /></figure><p>In natural language processing (NLP), the Retrieval Augmented Generation (RAG) paradigm represents a significant advancement, offering a hybrid approach that seamlessly integrates retrieval-based and generative methods. By combining the strengths of both approaches, RAG aims to overcome the limitations of traditional generative models, such as lack of context awareness and difficulty in producing relevant responses to diverse queries. At the core of RAG lies the integration of a powerful retrieval mechanism, which leverages a large-scale database to gather pertinent information that informs the subsequent generation of text responses.</p><p>In this report, we will evaluate RAG models using visualization methods. We aim to utilize the Milvus collection to query relevant documents and utilize UMAP projection to visualize embeddings in a two-dimensional space.</p><h3><strong>Objective</strong></h3><p>The primary objective of this evaluation is to assess the effectiveness of RAG models in generating contextually relevant responses by visualizing the relationship between queries, retrieved documents, and generated text embeddings. Traditional evaluation metrics may provide insights into the fluency and coherence of generated text, but they cannot often assess the relevance of generated responses in relation to the input queries and retrieved documents.</p><h3>Methodology</h3><p><strong>1. Creating a Milvus Collection</strong></p><p>Utilizing the Milvus server, we establish a Milvus collection to house the embeddings. Milvus is a highly efficient database system specially designed for storing and querying high-dimensional embeddings. To initiate the process, we establish a connection with the Milvus server and assign a name to the collection where the embeddings will be stored. This collection acts as a repository for the embeddings extracted from the text chunks.</p><p><strong>2. Querying the Milvus Collection</strong></p><p>Upon the successful creation of the Milvus collection, we proceed to query it to retrieve pertinent chunks of text based on a given query. The querying process encompasses the following sequential steps:</p><ul><li><strong>Input Query:</strong> We furnish the RAG model with a query, which represents the user’s input or inquiry.</li><li><strong>Generating Query Embedding:</strong> Employing the same embedding technique utilized for generating embeddings of text chunks, we generate an embedding for the input query. This embedding encapsulates the semantic essence of the query.</li><li><strong>Querying Milvus Collection:</strong> Leveraging the generated query embedding, we execute a query on the Milvus collection to retrieve the most akin chunks of text. Typically, this is accomplished using nearest-neighbor search algorithms like k-nearest neighbors (KNN) or approximate nearest neighbors (ANN).</li><li><strong>Retrieving Relevant Chunks:</strong> The retrieved text chunks, deemed most similar to the input query based on their embeddings, are subsequently forwarded to the generator component of the RAG model to generate contextually relevant responses. This step ensures the responses are closely aligned with the semantic context provided by the user’s query.</li></ul><p>By employing the Milvus collection, we streamline the process of retrieving relevant text chunks, enabling efficient and accurate responses to user queries.</p><p><strong>3. Visualization</strong></p><p>To provide a comprehensive evaluation of the RAG model’s performance, we utilize UMAP (Uniform Manifold Approximation and Projection) to project the high-dimensional embeddings into a two-dimensional space for visualization purposes. UMAP, a dimensionality reduction technique, is adept at preserving the local structure of the data, rendering it ideal for visualizing embeddings. Additionally, we employ Streamlit, a Python library renowned for creating interactive web applications, to craft interactive plots and dashboards for visualization.</p><p>The visualization components encompass:</p><ul><li><strong>Query-Document Visualization:</strong> Utilizing UMAP, we plot the embeddings of queries alongside those of retrieved documents in a two-dimensional space. Each point on the plot represents either a query or a document, with different colors or markers distinguishing between them. This visualization empowers users to interactively explore the spatial relationship between queries and the relevant documents retrieved.</li><li><strong>Query-Response Visualization:</strong> Similarly, we visualize the embeddings of queries juxtaposed with those of the generated responses in a two-dimensional space. Each point on the plot corresponds to either a query or a generated response. This visualization facilitates an analysis of how well the generated responses align with the contextual information provided by the queries, offering insights into the effectiveness of the RAG model.</li></ul><h3><strong>Analysis and Interpretation</strong></h3><p>Subsequently, we conducted a thorough analysis of the visualizations and evaluation metrics to assess the performance of the RAG model. This analysis entails identifying patterns, trends, and potential areas for improvement based on the visualization and evaluation results. By scrutinizing these findings, we gain valuable insights into the RAG model’s efficacy in generating contextually relevant responses, thereby informing future research endeavors in the domain of natural language generation tasks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78170a4b13b5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unlocking New Dimensions in LLM Observability with Phoenix]]></title>
            <link>https://medium.com/@tredencestudio/unlocking-new-dimensions-in-llm-observability-with-phoenix-5f64c8bc3388?source=rss-a87d5b3eb4c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f64c8bc3388</guid>
            <category><![CDATA[observability]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[llamaindex]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[llm-evaluation]]></category>
            <dc:creator><![CDATA[Tredence Studio]]></dc:creator>
            <pubDate>Wed, 24 Apr 2024 11:10:42 GMT</pubDate>
            <atom:updated>2024-04-24T11:35:17.021Z</atom:updated>
            <content:encoded><![CDATA[<h4>Enhancing LlamaIndex for Improved Observability and Performance Analysis</h4><p>Phoenix, an advanced observability platform, provides exceptional insights into LLM operations, enabling quick identification, diagnosis, and rectification of issues. Its integration with LlamaIndex enhances these capabilities further, offering an innovative approach to observability. This combination allows data scientists and developers to gain better insights into their models’ operations, significantly improving LLM functionality and troubleshooting capabilities.</p><h3>Key Pillars of LLM Observability</h3><p>The concept of LLM Observability rests on five crucial pillars, each integral for maintaining a high-performance LLM system.</p><ul><li><strong>Evaluation</strong>: It plays a pivotal role as it involves utilizing LLM Eval to understand and validate LLM performance comprehensively. This process helps identify and rectify issues such as hallucinations or Q&amp;A problems, ensuring the model’s reliability and accuracy.</li><li><strong>LLM Traces and Spans</strong>: It offers a unique insight into LLM applications, capturing spans and traces from common LLM App frameworks like LangChain and LlamaIndex. This data is instrumental in understanding the internal workings of the LLM, aiding in debugging and optimizing its performance.</li><li><strong>Prompt Analysis and Troubleshooting: </strong>It enables tracking performance issues using Evals or traditional metrics. By analyzing prompts, one can identify and rectify problems, iterating using live production data to reproduce and address issues as they arise precisely.</li><li><strong>Search and Retrieval: </strong>These are essential aspects of LLM observability, particularly in integrating proprietary data into an LLM using techniques like Retrieval-Augmented Generation (RAG). Troubleshooting and evaluating RAG are paramount for maintaining optimal LLM performance and functionality.</li><li><strong>Fine-Tuning: </strong>It involves<strong> </strong>collecting data examples or clusters of problems as data points for export into fine-tuning workflows. This process ensures that the LLM adapts and improves over time, enhancing its performance and effectiveness in various applications.</li></ul><figure><img alt="Key Pillars of LLM Observability" src="https://cdn-images-1.medium.com/max/1024/1*5yi6S-f1gIO5jL1K7ezonw.png" /></figure><h3>Enhancing Observability in Large Language Models with Arize</h3><figure><img alt="Enhancing Observability in Large Language Models with Arize" src="https://cdn-images-1.medium.com/max/1024/1*TBErPaQHO7-qiCJ4xgH2nQ.png" /></figure><p><strong>Arize Phoenix</strong> is an open-source observability library and platform tailored for experimentation, evaluation, and troubleshooting in machine learning applications. The toolset is adept at ingesting inference data for various machine learning models, including LLMs, computer vision (CV), natural language processing (NLP), tabular datasets, and LLM traces. It empowers AI engineers and data scientists to rapidly visualize their data, assess performance, pinpoint issues and insights, and seamlessly export findings for improvement.</p><ul><li><strong>Comprehensive Observability:</strong> Arize offers a holistic solution for enhancing observability across the entire stack of LLMs. By enabling teams to work backward from the output, it helps pinpoint the exact source of issues, thus facilitating efficient troubleshooting and analysis.</li><li><strong>Evaluation and Feedback: </strong>Arize helps collect and generate evaluations, including responses and user feedback. This allows teams to identify clusters of bad responses, providing valuable insights for further troubleshooting and analysis, ultimately improving the overall performance of their LLMs.</li><li><strong>Troubleshooting Retrieval Issues: </strong>Arize provides tools to troubleshoot bad retrieval, particularly concerning search and retrieval with vector stores. Teams can evaluate whether the system pulled all relevant context and if enough relevant context is available, addressing issues at the root of retrieval problems.</li><li><strong>Detection of Faults: </strong>Arize can help catch LLM hallucinations and bad responses. By identifying and analyzing these, teams can uncover trends and areas for improvement, ensuring that their LLMs deliver accurate and reliable results.</li><li><strong>Prompt Optimization: </strong>Arize helps uncover bad prompts and prompt templates, offering analysis to understand which templates perform better. This insight allows teams to refine their prompts for improved LLM performance.</li><li><strong>Agent Tracing and Spans: </strong>Arize’s agent tracing and spans feature enables teams to trace where the chain failed, providing a detailed understanding of failed calls or issues within spans. This functionality is invaluable for identifying and rectifying issues quickly and effectively.</li><li><strong>Fine-Tuning Support: </strong>For teams fine-tuning their LLMs, Arize provides workflows to understand patterns in bad responses. This information allows teams to fine-tune their models more effectively, improving performance on specific phrases or inputs where the model typically provides a bad response.</li></ul><h3>Traces</h3><p>Tracing plays a crucial role in understanding the behavior of LLM applications, which often exhibit nondeterministic problems or complexity beyond local reproduction. A trace records the paths followed by requests originating from either applications or end-users as they traverse various steps within a system. This capability is essential for diagnosing performance issues, as it enhances visibility into the health of an application or system, enabling the debugging of challenging behaviors to replicate locally. Tracing down the sequence of events within a request as it progresses through a system simplifies debugging and understanding LLM applications.</p><p>Each trace consists of one or more spans; the first span represents the root span, encapsulating the entire request lifecycle. Subsequent spans provide detailed contextual information about the steps in processing a request, offering a comprehensive view of the request’s execution path.</p><h3>Components in Traces</h3><figure><img alt="Components in Traces" src="https://cdn-images-1.medium.com/max/1024/1*Qz-h8AJtFsNhVnpNDKnGMw.png" /></figure><p>Traces consist of the following metrics:</p><p><strong>Total Traces </strong>— Provides us with the total number of traces created to generate an answer by the model.</p><p><strong>Total Tokens </strong>— It is the total amount of input and output tokens that the LLM model has used to generate the required answer.</p><p><strong>Latency P50 — </strong>Often referred to in the context of latency or response time, P50 represents the median value in a dataset. In the context of transaction durations or call latencies, the P50 threshold is the value at which 50% of the transactions or calls have durations greater than this threshold. This means that P50 latency is the median latency, indicating that half of the transactions or calls experienced latencies higher than this value. In practical terms, P50 provides a measure of central tendency, helpful in understanding a system or process’s typical or median performance.</p><p><strong>Latency P99 </strong>— P99, or the 99th percentile, is a latency metric that indicates the threshold below which 99% of requests to an endpoint fall in response time. For example, if the P99 latency for an endpoint is 100 milliseconds, it means that 99% of the requests made to that endpoint are completed in 100 milliseconds or less. P99 is particularly useful because it focuses on the worst-case scenario, helping to identify and address issues that might only affect a small percentage of users but could still be significant in the overall user experience.</p><p><strong>Evaluation Metrics — </strong>This metric provides us with details about the credibility of our model’s output. Here, we evaluate the model on Hallucination, QA Correctness, and Relevance.</p><ul><li><strong>Hallucination — </strong>In generating responses or answers, “hallucination” refers to providing information not supported by the reference text or assuming details that are not explicitly stated or available in the reference text. It essentially means generating content that goes beyond the scope or context of the original information provided. Hallucinations can lead to inaccurate or misleading responses, as the generated content may not be grounded in the facts or details provided in the reference text.</li><li><strong>QA Correctness — </strong>QA Correctness refers to evaluating whether a question posed to a system was correctly answered based on the data retrieved by the system. Unlike retrieval evaluations focusing on the chunks of data returned, QA Correctness evaluates the system’s overall performance in providing accurate answers to questions. This evaluation is crucial for assessing the effectiveness of a LLM in detecting issues in Q&amp;A systems, particularly regarding the accuracy and correctness of the answers provided based on the context data retrieved.</li><li><strong>Relevance — </strong>A relevance evaluator is critical in determining whether a retrieved document chunk contains relevant information that answers a given query. This evaluation is essential for assessing the effectiveness of retrieval systems, ensuring that the retrieved information is pertinent and can adequately address the specific question posed by the user.</li></ul><ol><li><strong>NDCG (Normalized Discounted Cumulative Gain)</strong> evaluates the effectiveness of ranking retrieved documents, giving more weight to relevant documents appearing higher in the list. This metric is valuable when the order of search results matters.</li><li><strong>Precision</strong> assesses the accuracy of a retrieval system by calculating the ratio of relevant documents among those retrieved, focusing on result relevancy.</li><li><strong>Hit Rate</strong>, commonly used in recommendation systems, indicates the frequency of relevant user recommendations. In retrieval systems, it reflects the success rate of retrieving pertinent documents for queries.</li></ol><h3>Trace Details</h3><p>Phoenix provides a comprehensive set of features for understanding and troubleshooting your system, including:</p><ul><li>Application latency tracking, which highlights slow invocations of LLMs, retrievers, and other components.</li><li>Token usage breakdown with LLMs, allowing you to identify the most resource-intensive LLM calls.</li><li>Capture of critical runtime exceptions, such as rate-limiting, as exception events.</li><li>Viewing all retrieved documents during a retriever call, along with their scores and return order.</li><li>Access to embeddings used for retrieval and the underlying embedding model.</li><li>Viewing LLM parameters, such as temperature and system prompts, used during a call to debug.</li><li>Identifying the prompt template used during the prompting step and the variables involved.</li><li>Viewing descriptions and function signatures of tools accessed by your LLM.</li><li>Accessing function selection and function messages in input messages to the LLM when using models with function calls, such as OpenAI or others.</li></ul><figure><img alt="Trace Details" src="https://cdn-images-1.medium.com/max/725/1*ZsZwJ-gnyo6zNRyf_UrNfw.png" /></figure><h3>Problem Statement</h3><p>LlamaIndex offers convenient high-level APIs for building powerful applications with minimal code. However, understanding its internal operations and diagnosing performance issues can be challenging. To address this, we use Phoenix, a tool that enhances observability of LlamaIndex applications. Phoenix visualizes the underlying structure of each query engine call, highlighting problematic execution spans based on latency, token count, and other evaluation metrics. This paper presents the design and implementation of Phoenix, demonstrating its effectiveness in improving the development and maintenance of LlamaIndex applications.</p><h3>Procedures for Utilizing LlamaIndex</h3><p><strong>1.</strong> <strong>Models Utilized</strong></p><ul><li>Large Language Model (LLM): Azure Open AI GPT-4</li><li>Embedding Model: text-embedding-ada-002</li></ul><p><strong>2.</strong> <strong>PDF Processing with LlamaIndex</strong></p><ul><li>Utilizing PDFReader to extract text from each PDF page and convert it to a document type.</li><li>Only document-type chunks are compatible with the vector database.</li><li>Experimentation involved open domain sustainability reports:</li></ul><ol><li>Pepsico_AnnualReport_2021</li><li>Pepsico_SustainabilityReport_2022</li><li>Pepsico_SustainabilityReport_2020</li><li>Pepsico_SustainabilityReport_2021</li><li>Nestle_SustainabilityReport_2022</li><li>KimberlyClark_AnnualReport_2020</li></ol><p><strong>3.</strong> <strong>Customization of LLM and Embedding Models in LlamaIndex</strong></p><ul><li>Modification of the LLM and embedding models to our custom versions, as llama-index defaults to OpenAI GPT models.</li><li>Introduction of custom chunk size (number of characters per text chunk) and chunk overlap (common characters between two chunks) parameters.</li><li>Utilization of the same chunk creator for both llama-index functions.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mwUKRj8HkdrZ6eWA70uHZw.png" /></figure><p><strong>4.</strong> <strong>Ingesting Documents into OpenSearch Vector Database</strong></p><ul><li>The OpenSearch vector database is a specialized storage system designed for vector embeddings, offering optimized similarity searches. It efficiently organizes and retrieves data based on the similarity of their vector representations. Leveraging OpenSearch Service’s vector database capabilities enables the implementation of semantic search, Retrieval Augmented Generation (RAG) with LLMs, recommendation engines, and search for rich media.</li></ul><p><strong>a)</strong> <strong>Setting Up Variables</strong></p><ul><li>Initialize variables required to initialize the OpenSearchVectorClient. These variables include the endpoint (URL of the OpenSearch server), the index (name of the index in OpenSearch where data will be stored), the vector dimensions (dimension of embedding for each word), the text field (name of the field in the index containing text data), the embedding field (name of the field in the index containing vector embeddings), and the username and password for authentication.</li></ul><p><strong>b)</strong> <strong>Initializing the OpenSearchVectorClient</strong></p><ul><li>Initialize the OpenSearchVectorClient using the previously set variables. This client is used to interact with the OpenSearch server.</li></ul><p><strong>c)</strong> <strong>Initializing the OpenSearchVectorStore</strong></p><ul><li>Initialize the OpenSearchVectorStore with the OpenSearchVectorClient. The OpenSearchVectorStore is a vector store that utilizes OpenSearch as its backend, which is used for storing and retrieving vector embeddings.</li></ul><p><strong>d)</strong> <strong>Setting Up the StorageContext</strong></p><ul><li>nitialize the StorageContext with the OpensearchVectorStore as its vector store. The StorageContext in LlamaIndex is a container holding the document, index, and vector stores. The vector store is set to the OpensearchVectorStore, while the document store and index store are set to their default values.</li></ul><p><strong>e)</strong> <strong>Ingesting Data using VectorStoreIndex</strong></p><ul><li>Utilize VectorStoreIndex, a LlamaIndex type that uses a vector store for storing and retrieving vector embeddings. VectorStoreIndex requires documents (representing the list of documents to be stored), the storage_context (in which the vector store is defined), and service_context (containing the services used by the index, such as the LLM model, embedding model, and chunk creator) as input parameters.</li></ul><p><strong>5.</strong> <strong>Establishing an OpenSearchVectorClient Instance for the Target Index</strong></p><p><strong>a)</strong> <strong>Setting Up Variables</strong></p><p>Variables are configured for initializing the OpenSearchVectorClient. These include the endpoint (URL of the OpenSearch server), the index (name of the index in OpenSearch where data is stored), the vector dimension (embedding dimension for each word), the text field (field name in the index containing text data), the embedding field (field name in the index containing vector embeddings), and the username and password for authentication.</p><p><strong>b)</strong> <strong>Initializing the OpenSearchVectorClient</strong></p><p>The OpenSearchVectorClient is initialized with the configured variables, allowing interaction with the OpenSearch server.</p><p><strong>c)</strong> <strong>Initializing the OpenSearchVectorStore</strong></p><p>Initialization of the OpenSearchVectorStore is performed using the OpenSearchVectorClient. This store utilizes OpenSearch as its backend and is used for storing and retrieving vector embeddings.</p><p><strong>d)</strong> <strong>Creating the VectorStoreIndex</strong></p><p>The VectorStoreIndex.from_vector_store method is used to create an index object, taking the vector_store as input, which was initialized with the index name where the data is stored.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GZYteVa6ulV1FGEC9-qMBQ.png" /></figure><p><strong>6.</strong> <strong>Creating a Query Engine with Custom Prompt Using LlamaIndex</strong></p><ul><li>The process involves a retriever function that takes a question as input and retrieves document chunks from the vector store based on the question’s similarity search.</li><li>These retrieved document chunks are then passed to the prompt as context.</li><li>Subsequently, the prompt asks the LLM to generate an answer for the question by passing the modified prompt as input to the LLM.</li></ul><p><strong>7.</strong> <strong>Phoenix Traces:</strong></p><ul><li>Giving the user a prompt to open the Phoenix UI.</li><li>LLM trace observability aims to simplify the troubleshooting of LLM frameworks and enhance the visibility of your LLM system.</li><li>Arize Phoenix aims to enhance development tools for close-to-code LLM span and trace debugging, prioritizing the alignment of AI systems with desired functionality over troubleshooting distributed system timing.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ht9TS-WAr58oR_KtxdAmbg.png" /></figure><p><strong>8.</strong> <strong>Phoenix Eval</strong></p><p><strong>a)</strong> <strong>Retrieving QA Data and Retrieved Documents</strong></p><ul><li><strong>Fetching QA Data</strong></li></ul><p>The function get_qa_with_reference fetches question-answer pairs along with reference data from the Phoenix instance. It communicates with the Phoenix API endpoint provided in the px. Client constructor to retrieve this data. The returned data is stored in a DataFrame named queries_df.</p><ul><li><strong>Fetching Retrieved Documents</strong></li></ul><p>The function get_retrieved_documents is to retrieve documents that were retrieved or accessed during the process. It communicates with the Phoenix API endpoint using the px.Client constructor to fetch the data. The retrieved documents are stored in a data frame named retrieved_documents_df.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VIKu9X6V4RKjJ8upy-GyFA.png" /></figure><p><strong>b)</strong> <strong>Initializing Evaluation Model</strong></p><ul><li>Initializing an evaluation model configured to utilize OpenAI’s GPT-4 model for natural language processing tasks. It sets up the necessary parameters for authentication.</li></ul><p><strong>c)</strong> <strong>Initializing Evaluators: Three evaluators are initialized</strong></p><ul><li><strong>hallucination_evaluator: </strong>Evaluates hallucination, i.e., whether the model generates responses containing fabricated or irrelevant information.</li><li><strong>qa_correctness_evaluator: </strong>Evaluates the correctness of question-answer pairs generated by the model.</li><li><strong>relevance_evaluator: </strong>Evaluates the relevance of retrieved documents to the queries.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ydZOrLOIAZUVqnQuV5c1YA.png" /></figure><p><strong>d)</strong> <strong>Running Evaluations</strong></p><ul><li>For hallucination and QA correctness evaluation, the run_evals() function is called with the DataFrame containing question-answer pairs (queries_df) and a list of evaluators (hallucination_evaluator and qa_correctness_evaluator). Explanations for evaluation results are provided.</li><li>The run_evals() function is called for relevance evaluation with the DataFrame containing retrieved documents (retrieved_documents_df) and the relevance_evaluator. Explanations for evaluation results are also provided<strong>.</strong></li></ul><p><strong>e)</strong> <strong>Logging Evaluations</strong></p><ul><li>Evaluation results for hallucination and QA correctness are logged using px.log_evaluations() with SpanEvaluations for span-level evaluation results.</li><li>Evaluation results for relevance are logged using px.log_evaluations() with DocumentEvaluations for document-level evaluation results.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*so66FCXKbog99-5uxCekvA.png" /></figure><h3>Example</h3><p>Let us consider an example with a user question, an output generated by the model, and the traces created by the Phoenix.</p><p>This methodology follows a chain process, where user questions trigger retrieval of answers from a vector database using similarity search. Through this approach, we can assess the chain’s performance in terms of metrics such as Hallucination and QA correctness as evaluation metrics. Additionally, we can track the processing time for each question and record the total token usage necessary for executing this chain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A3xUmD5pRJmAho0-IhI3PA.png" /></figure><h3>Spans</h3><p>A span is a fundamental unit of work or operation within an application akin to a time segment. It tracks specific operations a request undergoes, providing a detailed account of the activities conducted during its execution. Each span comprises essential components such as a name, time-related data, structured log messages, and additional metadata known as attributes, which collectively offer insights into the operation being tracked. Notably, spans can be nested, as indicated by the presence of a parent span ID, allowing them to represent sub-operations within a larger task. This nesting capability enhances the granularity with which operations are captured, enabling a more comprehensive understanding of the work performed in an application. Upon creation, a span is classified into several categories, including Chain, Retriever, Reranker, LLM, Embedding, Agent, or Tool, based on the nature of the operation it represents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n6-pPYaJ8WQex5Lgw0CIjA.png" /></figure><ul><li><strong>Chain: </strong>It represents a pivotal point or link between various steps within the application’s workflow. It denotes sequences of calls, including those to an LLM model, a tool, or a data preprocessing step. Essentially, a Chain span serves as a starting point or a connector that facilitates the execution flow between different components or stages of the application. For instance, it could signify initiating a request to an LLM application or the mechanism through which context is transferred from one operation, such as data retrieval, to another, such as an LLM call. By serving as a linkage mechanism, the Chain span aids in tracing the execution path of an application, thereby enabling a clear visualization of the interactions and dependencies among its various components or steps.</li><li><strong>Retriever: </strong>It is<strong> </strong>a vital component within the data processing pipeline, tasked with retrieving the most pertinent context based on a user query or chat message. While it can be constructed on top of indexes, it can also exist as an independent entity. In query engines and Chat Engines, the retriever is a foundational element for retrieving relevant context. In the context of spans, as described in the provided sources, a Retriever span represents a step-in data retrieval. It signifies a call to a vector store or database, which is crucial for systems that retrieve information from extensive data sets based on queries or criteria. The retriever’s primary function is to efficiently fetch the most relevant documents or information for subsequent application steps, such as input for a LLM for generating responses or further analysis. In the broader landscape of LLM applications, particularly those involving Retrieval-Augmented Generation (RAG), the retriever is critical in ensuring the quality and relevance of processed information. By adeptly retrieving pertinent documents, the system significantly enhances the accuracy and utility of generated outputs, underscoring the retriever’s indispensability in LLM application architectures.</li><li><strong>Embedding:</strong> Commonly known as vector embedding, embedding acts as a numerical representation of the semantics or meaning contained within the text. It is critical in LLMs and their applications. It enables the mathematical comparison of two pieces of text with similar meanings, even when the actual text varies significantly. This mathematical relationship enables semantic search, where a user’s query terms can be matched with text related to the meaning rather than keyword matching. There are various types of embeddings, each differing in efficiency, effectiveness, and computational cost. For example, LlamaIndex employs text-embedding-ada-002 by default, the default embedding used by OpenAI. Different LLMs may utilize distinct embeddings to suit their specific requirements.</li></ul><p>In the LLM Traces and Observability context, an embedding within a span denotes a call made to an LLM (Language Model) to generate an embedding vector. This vector represents information in a dense format within a high-dimensional space, where similar items are positioned closer together. Within a span — a unit of work or operation in a trace — an “Embedding” span indicates that during the execution of a request or operation, a call was made to an LLM to obtain such an embedding. This process may involve retrieving a dense vector representation of text for further processing steps, such as similarity comparison or information retrieval. The span tracks this operation, including its start and end times, and may include additional metadata in its attributes, such as the model’s name used for generating the embedding or the input text.</p><ul><li><strong>Chunking:</strong> It involves retrieving document chunks from the vector database. These chunks are selected based on a specified top k value and then forwarded to the LLM for answer generation.</li><li><strong>LLM:</strong> It is a sophisticated computational system capable of completing text, engaging in chat-like interactions with users, and executing instructions. Its primary function is to comprehend and generate human-like text based on the input it receives. LLMs find applications in various fields, including chatbots, text completion tasks, etc. In performance analysis, an LLM span represents a call to an LLM system, such as those provided by OpenAI or Llama. This span signifies a request made to the Language Learning Model, which could involve generating text, answering questions, or performing any other task within the capabilities of LLMs.</li></ul><h3>Span Lookup</h3><h4>Query Span</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*223j2z4e0kW5JJYhwT5FcQ.png" /></figure><p>The Query Span serves as a container for the user’s query, encompassing details about the input question and its corresponding output. Evaluation metrics such as Hallucination and QA Correctness play a crucial role, with a score of 1 indicating a hallucinated answer and 0 denoting incorrect QA Correctness. Additionally, Attributes within the Query Span consist of key-value pairs designed to hold metadata, facilitating the annotation of spans with pertinent information related to the monitored operation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dv1v9RV4oEqBZVcBZjHwlg.png" /></figure><h4>Retrieve Span</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aQAVKOoALxaX-JrABhVXsQ.png" /></figure><p>The Retrieve span is responsible for fetching the documents relevant to a user query. It retrieves the top k most relevant documents and their respective relevance scores. Additionally, it provides information on the time taken to retrieve these documents, offering insights into the efficiency of the retrieval process.</p><h4>Embedding Span</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HiRU4TdZi0zA7LN8EN240w.png" /></figure><p>The Embedding span is a crucial component in retrieving and converting query text to embeddings within a system. It encompasses the embedding model for retrieving documents and converting text into embeddings. Additionally, the Embedding span includes attributes such as the dimensional vector employed and the time taken to complete the embedding process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-SVxQ6BkgNsGjW1zZZYxxg.png" /></figure><h3>LLM</h3><p>LLM leverages context retrieved from the vector database to generate answers based on the provided prompt. Using GPT-3.5 Turbo as the underlying model, the LLM processes the prompt to generate answers formatted according to the specified requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R8vYITUqxulpBAUGuqtFzg.png" /></figure><p>The prompt template offers a detailed overview of the template utilized within an application, including the input and output variables employed and the format of the resulting output. On the other hand, invocation parameters aid in comprehending additional parameters like temperature and the model utilized during the result-generation process.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f64c8bc3388" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Enhancing Llama Index with Phoenix for LLM Evaluation]]></title>
            <link>https://medium.com/@tredencestudio/enhancing-llama-index-with-phoenix-for-llm-evaluation-86639a465311?source=rss-a87d5b3eb4c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/86639a465311</guid>
            <category><![CDATA[llamaindex]]></category>
            <category><![CDATA[genai]]></category>
            <category><![CDATA[ai-innovation]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[llm-evaluation]]></category>
            <dc:creator><![CDATA[Tredence Studio]]></dc:creator>
            <pubDate>Mon, 08 Apr 2024 12:10:14 GMT</pubDate>
            <atom:updated>2024-06-10T10:27:34.437Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In this article, we explore the integration of Phoenix with the Llama Index to enhance the evaluation of Large Language Models (LLMs) and Retrieval-Augmented Generation (RAG) applications. We aim to address the challenges associated with LLM evaluation and provide a structured approach for utilizing the Llama Index effectively.</p><p><strong>Problem Statement</strong></p><p>Llama Index offers convenient high-level APIs for building powerful applications with minimal code. However, understanding the performance of a Large Language Model (LLM) model and the Retrieval Augmented Generation (RAG) application can be challenging. To address this, we use Phoenix, a tool that evaluates Llama Index applications. Existing LLM evaluation lacks rigor (focusing only on models, not prompt templates) and faces integration issues across development and deployment, hindering efficient processing and flexibility. Phoenix LLM Evals solves this by offering pre-tested and custom templates, enabling efficient batch processing, and ensuring seamless integration within Llama Index workflows.</p><h3>Procedures for Utilizing the Llama Index</h3><h3>Models Utilized</h3><ul><li>Large Language Model (LLM): anthropic.claude-v2</li><li>Embedding Model: titan-embed</li></ul><h3>PDF Processing with Llama Index</h3><ol><li>Utilizing PDFReader to extract text from each PDF page and convert it to a document type.</li><li>Only document-type chunks are compatible with the vector database.</li></ol><p>Experimentation involved open domain sustainability reports:</p><ul><li>‘Pepsico_AnnualReport_2021’</li><li>‘Pepsico_SustainabilityReport_2022’</li><li>‘Pepsico_SustainabilityReport_2020’</li><li>‘Pepsico_SustainabilityReport_2021’</li><li>‘Nestle_SustainabilityReport_2022’</li><li>‘KimberlyClark_AnnualReport_2020’</li></ul><h3><strong>Customization of LLM and Embedding Models in Llama Index</strong></h3><ul><li>Modification of the LLM and embedding models to our custom versions, as llama-index defaults to OpenAI GPT models.</li><li>Introduction of custom chunk creator, chunk_size (number of characters per text chunk), and chunk_overlap (common characters between two chunks) parameters.</li></ul><h3>Ingesting Documents into OpenSearch Vector Database</h3><ul><li>The OpenSearch vector database is a specialized storage system designed for vector embeddings, offering optimized similarity searches. It efficiently organizes and retrieves data based on the similarity of their vector representations. Leveraging OpenSearch Service’s vector database capabilities enables the implementation of semantic search, Retrieval Augmented Generation (RAG) with LLMs, recommendation engines, and search for rich media.</li></ul><p><strong>a)</strong> <strong>Setting Up Variables</strong></p><ul><li>Initialize variables required to initialize the OpenSearchVectorClient. These variables include the endpoint (URL of the OpenSearch server), the index (name of the index in OpenSearch where data will be stored), the vector dimensions (dimension of embedding for each word), the text field (name of the field in the index containing text data), the embedding field (name of the field in the index containing vector embeddings), and the username and password for authentication.</li></ul><p><strong>b)</strong> <strong>Initializing the OpenSearchVectorClient</strong></p><ul><li>Initialize the OpenSearchVectorClient using the previously set variables. This client is used to interacting with the OpenSearch server.</li></ul><p><strong>c)</strong> <strong>Initializing the OpenSearchVectorStore</strong></p><ul><li>Initialize the OpenSearchVectorStore with the OpenSearchVectorClient. The OpenSearchVectorStore is a vector store that utilizes OpenSearch as its backend, which is used for storing and retrieving vector embeddings.</li></ul><p><strong>d)</strong> <strong>Setting Up the StorageContext</strong></p><ul><li>Initialize the StorageContext with the OpensearchVectorStore as its vector store. The StorageContext in Llama Index is a container holding the document, index, and vector stores. The vector store is set to the OpensearchVectorStore, while the document and index stores are set to their default values.</li></ul><p><strong>e)</strong> <strong>Ingesting Data using VectorStoreIndex</strong></p><ul><li>Utilize VectorStoreIndex, a Llama Index type that uses a vector store for storing and retrieving vector embeddings. VectorStoreIndex requires documents (representing the list of documents to be stored), the storage_context (in which the vector store is defined), and service_context (containing the services used by the index, such as the LLM model, embedding model, and chunk creator) as input parameters.</li></ul><h3>Establishing an OpenSearchVectorClient Instance for the Target Index</h3><p><strong>a.</strong> <strong>Setting Up Variables</strong></p><p>Variables are configured for initializing the OpenSearchVectorClient. These include the endpoint (URL of the OpenSearch server), the index (name of the index in OpenSearch where data is stored), the vector dimension (embedding dimension for each word), the text field (field name in the index containing text data), the embedding field (field name in the index containing vector embeddings), and the username and password for authentication.</p><p><strong>b.</strong> <strong>Initializing the OpenSearchVectorClient</strong></p><p>The OpenSearchVectorClient is initialized with the configured variables, allowing interaction with the OpenSearch server.</p><p><strong>c.</strong> <strong>Initializing the OpenSearchVectorStore</strong></p><p>Initialization of the OpenSearchVectorStore is performed using the OpenSearchVectorClient. This store utilizes OpenSearch as its backend and is used for storing and retrieving vector embeddings.</p><p><strong>d.</strong> <strong>Creating the VectorStoreIndex</strong></p><p>The VectorStoreIndex.from_vector_store method is used to create an index object, taking the vector_store as input, which was initialized with the index name where the data is stored.</p><h3>Creating a Query Engine with Custom Prompt Using Llama Index</h3><ul><li>The process involves a retriever function that inputs a question and retrieves document chunks from the vector store based on the question’s similarity search.</li><li>These retrieved document chunks are then passed to the prompt as context.</li><li>Subsequently, the prompt asks the LLM to generate an answer for the question by passing the modified prompt as input to the LLM.</li></ul><h3>Enhancing LLM Evaluation with Arize Phoenix</h3><p>Phoenix LLM Evaluation refers to assessing the performance and effectiveness of Large Language Models (LLMs) using the Phoenix observability library. This evaluation methodology involves utilizing Phoenix’s features and tools specifically designed for evaluating LLM-based applications. By leveraging Phoenix, users can conduct rigorous evaluations, analyze model behavior, diagnose performance issues, and optimize the development and maintenance of LLM applications. Phoenix LLM Evaluation aims to provide a streamlined and efficient approach to evaluating LLM models, ensuring transparency, reproducibility, and scalability in the evaluation process.</p><p><strong>LLM Evaluation</strong></p><p>Large Language Model (LLM) evaluation involves systematically assessing the performance and capabilities of LLM applications. It measures language generation quality, response relevance, bias detection, interpretability, and adherence to task requirements. Through standardized metrics, benchmark datasets, and interpretability tools, LLM evaluation provides insights into model strengths and weaknesses. It includes human assessments, automated metrics calculation, interpretability analysis, and comparison against benchmarks. By rigorously evaluating LLM models, stakeholders can make informed decisions on model improvements and deployment strategies for real-world applications.</p><p><strong>Components in Phoenix LLM Evaluation</strong></p><p>a) <strong>Pre-tested Eval Templates:</strong> Offers a collection of ready-made evaluation templates for common use cases, such as RAG relevance, toxicity, and code generation.</p><p>b) <strong>Custom Dataset Support:</strong> Supports custom datasets and evaluation creation, allowing for tailored evaluations based on specific use cases.</p><p>c) <strong>Fast On-Batch Processing:</strong> Optimized for high-throughput evaluations, ensuring efficient utilization of API keys and resources.</p><p>d) <strong>Benchmark Scripts and Datasets:</strong> Provides benchmark scripts and datasets for rigorous evaluation of models and prompts.</p><p>e) <strong>LangChain and LlamaIndex Integration:</strong> Enables evaluation directly within LangChain and LlamaIndex callback systems.</p><p>f) <strong>One-Click Explanations:</strong> Facilitates instant explanation of evaluation outcomes without requiring additional configuration.</p><p>g) <strong>Span Level Evaluation:</strong> Conducts evaluations at the granular level of individual spans within the LLM application, providing greater insight into performance issues.</p><p>h) <strong>Data Science Rigor:</strong> Employs data science principles to test model and template combinations, improving evaluation reliability.</p><p>i) <strong>Extensibility:</strong> Encourages modification and creation of custom evaluation templates to meet unique use cases.</p><p>j) <strong>Open Source:</strong> Available as an open-source project, fostering collaboration and innovation among the developer community.</p><h3>How Phoenix LLM Evaluation Works</h3><figure><img alt="How Phoenix LLM Evaluation Works" src="https://cdn-images-1.medium.com/max/975/1*oH49pQt0sG0NtFROos9-3A.png" /></figure><p>a) <strong>Data Preparation:</strong> You would prepare a dataset containing questions, their corresponding references, and the LLM-generated answers. This could involve formatting the data as needed for efficient evaluation.</p><p>b) <strong>Defining Evaluation Prompt Template:</strong> Create a prompt template specific to the task. For example, if you want to measure factual accuracy, you might create a prompt asking whether certain statements made by the LLM are true or false, given the reference material.</p><p>c) <strong>Defining Evaluation LLM Model:</strong> Choose an appropriate LLM model for performing the evaluation. Responses do not have to be the same as the original LLM generating the responses; it can be a separate model specifically trained for evaluation purposes.</p><p>d) <strong>Final Label Generation and Explanation:</strong> After evaluating the LLM-generated answers against the reference materials, assign a final label indicating the quality of the response. This label may reflect various aspects such as factual correctness, relevancy, coherence, etc., depending on the chosen evaluation metric(s). Optionally, provide explanations for the assigned labels. These explanations help understand why the response received a particular score, thus improving transparency and enabling further analysis.</p><h3>Type of LLM Evaluations</h3><p><strong>a)</strong> <strong>Retrieval (RAG) Relevance</strong></p><p>A relevance evaluator plays a critical role in determining whether a retrieved document chunk contains relevant information that answers a given query. This evaluation is essential for assessing the effectiveness of retrieval systems, ensuring that the retrieved information is pertinent and can adequately address the specific question posed by the user.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes two pieces of information:</p><ol><li>Question: The user’s specific inquiry.</li><li>Reference text: retrieved documents containing potentially relevant information.</li></ol><p><strong>Processing</strong>: It analyzes both the question and the reference text, focusing on the specific details of the question.</p><p><strong>Output</strong>: It outputs a single word:</p><ol><li>“relevant”: If the reference text contains information that could potentially answer the question.</li><li>“unrelated”: If the reference text contains no relevant information for the specific question.</li></ol><p><strong>b)</strong> <strong>Hallucination Evaluation</strong></p><p>The Hallucination Evaluation task involves presenting a query, a reference text, and a generated answer. The answer is generated based on the reference text, and the task is to determine if the answer contains false information (hallucination) by comparing it to the reference text. In the context of generating responses or answers, “hallucination” refers to providing information that is not supported by the reference text or assuming details that are not explicitly stated or available in the reference text. It essentially means generating content that goes beyond the scope or context of the original information provided. Hallucinations can lead to inaccurate or misleading responses, as the generated content may not be grounded in the facts or details provided in the reference text.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes this information:</p><ol><li>Question: The user’s specific inquiry.</li><li>Reference text: retrieved documents containing potentially relevant information.</li><li>Answer: LLM generated an answer based on the reference.</li></ol><p><strong>Processing</strong>: It analyzes the question, answer, and reference text, focusing on the specific details of the question and reference text.</p><p><strong>Output</strong>: It outputs a single word:</p><ol><li>“factual”: If the answer to the question is correct relative to the reference text and does not contain made-up information.</li><li>“hallucinated”: If the answer provides factually inaccurate information to the query based on the reference text.</li></ol><p><strong>c)</strong> <strong>QA Correctness</strong></p><p>It refers to evaluating whether a question posed to a system was correctly answered based on the data retrieved by the system. Unlike retrieval evaluations focusing on the chunks of data returned, QA correctness evaluates the system’s overall performance in providing accurate answers to questions. This evaluation is crucial for assessing the effectiveness of a Large Language Model (LLM) in detecting issues in Q&amp;A systems, particularly regarding the accuracy and correctness of the answers provided based on the context data retrieved.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes three pieces of information:</p><ol><li>Question: The user’s specific inquiry.</li><li>Reference text: Retrieved documents containing potentially relevant information.</li><li>Answer: The response provided to the question.</li></ol><p><strong>Processing</strong>: It compares the answer provided with the question and analyzes whether the information in the answer aligns with the details provided in the reference text.</p><p><strong>Output</strong>: It outputs a single word:</p><ol><li>“correct”: If the question is correctly and fully answered by the provided answer based on the reference text.</li><li>“incorrect”: If the question is incorrect or only partially answered by the provided answer compared to the reference text.</li></ol><p><strong>d)</strong> <strong>Toxicity Evaluation</strong></p><p>Toxicity evaluation refers to assessing written or verbal content to determine whether it contains elements considered toxic. In AI and natural language processing, toxicity evaluation typically involves analyzing text data to identify language that may be harmful, offensive, hateful, demeaning, or threatening towards individuals or groups.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes a single piece of information:</p><ol><li>Text: The written content to be examined for toxicity.</li></ol><p><strong>Processing</strong>: It analyzes the text, focusing heavily on the concept of toxicity as defined by making hateful statements, demeaning or disparaging others, using inappropriate language, or threatening physical or emotional violence.</p><p><strong>Output</strong>: It outputs a single word:</p><ol><li>“toxic”: If the text meets the definition of toxicity by containing hateful statements, demeaning language, inappropriate language, or threats of violence.</li><li>“non-toxic”: If the text contains no words, sentiments, or meaning, that could be considered toxic.</li></ol><p><strong>e)</strong> <strong>Code Generation Evaluation</strong></p><p>Code Generation Evaluation involves the assessment of code produced through an automated generation process. It aims to determine the correctness and readability of the generated code. The evaluation examines whether the code is clear, concise, and easy to understand, and identifies whether the code is unnecessarily complex or convoluted for the task it aims to accomplish. The evaluation process typically involves reviewing the generated code against a specific query or task and providing a judgment on its readability, marking it as “readable” or “unreadable” based on established criteria.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes a single piece of information:</p><ol><li>Question: The user’s specific inquiry.</li><li>Code: LLM Generated code</li></ol><p><strong>Processing</strong>: It analyzes the provided code, focusing on readability, simplicity, and clarity.</p><p><strong>Output</strong>: It outputs a single word:</p><ol><li>“readable”: If the code is readable, focusing on simplicity and clarity.</li><li>“unreadable”: If the code is unreadable or needlessly complex for what it’s trying to accomplish.</li></ol><p><strong>f)</strong> <strong>Summarization Evaluation</strong></p><p>Summarization Evaluation involves assessing the quality of a generated summary in comparison to the original document. It aims to determine if the summary effectively captures the key information and is concise, coherent, and independent relative to the content of the original text. The evaluation process typically involves examining the summary and original document and judging whether the summary provides a comprehensive overview of the main points without unnecessary details, maintains logical coherence, and can be understood independently of the original document. The evaluation results help assess the performance of automated summarization systems and ensure that the generated summaries meet the desired standards of quality and usefulness.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes two pieces of information:</p><ol><li>Summary: A summary of the document.</li><li>Original Document: The full text of the document.</li></ol><p><strong>Processing</strong>: It compares the summary text to the original document, focusing on whether the summary is comprehensive, concise, coherent, and independent relative to the original document.</p><p><strong>Output</strong>: It outputs a single string:</p><ol><li>“good”: If the summary is comprehensive, concise, coherent, and independent relative to the original document.</li><li>“bad”: If the summary is not comprehensive, concise, coherent, and independent relative to the original document.</li></ol><p><strong>g)</strong> <strong>Reference (Citation) Link Evaluation</strong></p><p>Reference (Citation) Link Evaluation involves assessing the relevance and accuracy of a provided reference link in response to a query or question. It aims to determine whether the linked content adequately addresses the user’s inquiry and whether the information within the link is trustworthy and aligned with the context of the conversation or question the user poses.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes two pieces of information:</p><ol><li>Question: The user’s specific inquiry.</li><li>Documentation URL Text: Extracted text of reference link.</li></ol><p><strong>Processing</strong>: It analyzes the documentation text to determine if it adequately addresses the questions posed by the customer in the conversation. It evaluates whether the information provided in the documentation aligns with the inquiries raised by the customer.</p><p><strong>Output</strong>: It outputs a single word:</p><ol><li>“correct”: If the documentation text answers the question the customer had in the conversation, even if it’s in a general way or partially.</li><li>“incorrect”: If the text does not answer the question in the conversation or doesn’t contain information that would allow you to answer the specific question.</li></ol><p><strong>h)</strong> <strong>AI vs. Human (Groundtruth) Evaluation</strong></p><p>AI vs. Human (Groundtruth) Evaluation compares answers produced by artificial intelligence models against those provided by humans, typically experts or individuals with domain knowledge. It aims to determine how AI-generated responses align with the substance and completeness of human-ground truth answers.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes four pieces of information:</p><ol><li>Question: The user’s specific inquiry.</li><li>Ground Truth Answer: The definitive and accurate response from an expert or trusted source.</li><li>AI Answer: The response generated by an artificial intelligence model in relation to the user’s specific inquiry.</li></ol><p><strong>Processing</strong>: It compares the AI-generated answer to the human ground truth answer, focusing on whether the AI answer correctly matches, in substance, the human answer. If the AI answer correctly answers the question or contains the main idea of the human answer, it is considered “correct”. If the AI answer diverges from or does not contain the main idea of the human answer, it is considered “incorrect”.</p><p><strong>Output</strong>: It outputs a single word:</p><ol><li>“correct”: If the AI answer correctly matches the human ground truth answer or contains the human answer’s main idea.</li><li>“incorrect”: If the AI answer diverges from or does not contain the main idea of the human ground truth answer.</li></ol><p><strong>i)</strong> <strong>SQL code evaluation</strong></p><p>SQL code evaluation is the process of analyzing and determining the correctness of SQL queries in accordance with given criteria. It involves assessing whether SQL queries adhere to the syntax rules of the SQL language and accurately retrieve or manipulate data based on the provided database schema and the question’s requirements. The evaluation ensures that SQL queries are syntactically and semantically correct, producing accurate results without errors or deviations from the intended functionality.</p><p><strong>Here’s what it does:</strong></p><p><strong>Input</strong>: It takes three pieces of information:</p><ol><li>Question: The user’s specific inquiry.</li><li>Database Schema: The structure and organization of a SQL database, defining tables, fields, and relationships</li><li>SQL query: The query is generated based on the database schema.</li></ol><p><strong>Processing</strong>: It evaluates the SQL query against the database schema, considering syntax and semantic correctness. The goal is to determine whether the SQL query accurately reflects the information in the database schema and effectively addresses the user’s inquiry.</p><p><strong>Output</strong>: It outputs a single word:</p><ol><li>“correct”: If the SQL query is based on the database schema or assumes information available in the question.</li><li>“incorrect”: If the SQL query provides factually inaccurate information or does not align with the database schema relative to the question.</li></ol><h3><strong>Conclusion</strong></h3><p>Integrating Phoenix with the Llama Index provides a complete framework for evaluating LLM models and RAG applications. By leveraging the capabilities of Phoenix, we can conduct rigorous evaluations, diagnose performance issues, and optimize the development and maintenance of LLM applications. This ensures transparency, reproducibility, and scalability in the evaluation process, contributing to the advancement of natural language processing and machine learning technologies.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86639a465311" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>