<?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 Emanuel Ferreira on Medium]]></title>
        <description><![CDATA[Stories by Emanuel Ferreira on Medium]]></description>
        <link>https://medium.com/@manelferreira_?source=rss-a69e36cc88c1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JgHroaF5hL6kliu-9rfCxg.jpeg</url>
            <title>Stories by Emanuel Ferreira on Medium</title>
            <link>https://medium.com/@manelferreira_?source=rss-a69e36cc88c1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 22:45:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@manelferreira_/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[How to build LLM Agents in TypeScript with LlamaIndex.TS]]></title>
            <link>https://medium.com/llamaindex-blog/how-to-build-llm-agents-in-typescript-with-llamaindex-ts-a88ed364a7aa?source=rss-a69e36cc88c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/a88ed364a7aa</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[retrieval-augmented]]></category>
            <category><![CDATA[agents]]></category>
            <dc:creator><![CDATA[Emanuel Ferreira]]></dc:creator>
            <pubDate>Thu, 08 Feb 2024 20:19:03 GMT</pubDate>
            <atom:updated>2024-02-08T20:19:03.819Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hjkDtn0HuL3yhPjTnritZA.png" /></figure><p>Agents are autonomous systems that can execute end-to-end tasks without much or fewer instructions. These agents are capable of solving tasks related to questions and answering, using tools to achieve a desired behavior, or even planning tasks.</p><p>In this article, we will explore a few capabilities of LlamaIndex.TS’s built-in agents to achieve a set of goals. We have two parts:</p><ol><li><a href="#cb81">Building a simple agent for calculation.</a></li><li><a href="#b49b">Using agents with your personal/private data to answer questions.</a></li></ol><p>All the full code examples will be at the end of the post.</p><h3>Setup</h3><p>To start you need to have llamaindex package installed on your node environment setup and an OpenAI key.</p><p>To install the package:</p><pre>npm install llamaindex </pre><p>To set up the OpenAI key you can set your environment variable:</p><pre>export OPENAI_API_KEY=sk-***************</pre><h3>Agent for calculations</h3><p>The first agent will be responsible for getting a user input of numbers and based on this selecting the right tools to achieve the task</p><h4>Import the classes:</h4><pre>import { FunctionTool, OpenAIAgent } from &quot;llamaindex&quot;;</pre><h4>Function Tool</h4><p>The first step will be creating tools that the agent will have access to, we will be creating an add function and a multiply function.</p><p>OpenAI provides us a function calling API which we can send our function arguments and get back a response</p><p>We start by creating two functions:</p><pre>// Define a function to sum two numbers<br>function sum({ a, b }: { a: number; b: number }): number {<br>  return a + b;<br>}<br><br>// Define a function to multiply two numbers<br>function multiply({ a, b }: { a: number; b: number }): number {<br>  return a * b;<br>}</pre><p>Now we can set up the FunctionTool class which will be given to the agent, this class requires the properties of the function and metadata for the tool, helping the Large Language Models (LLMs) to identify which tool the LLM should use and the parameters.</p><pre>// Sum properties to give to the LLM<br>const sumJSON = {<br>  type: &quot;object&quot;,<br>  properties: {<br>    a: {<br>      type: &quot;number&quot;,<br>      description: &quot;The first number&quot;,<br>    },<br>    b: {<br>      type: &quot;number&quot;,<br>      description: &quot;The second number&quot;,<br>    },<br>  },<br>  required: [&quot;a&quot;, &quot;b&quot;],<br>};<br><br>// Multiply properties to give to the LLM<br>const multiplyJSON = {<br>  type: &quot;object&quot;,<br>  properties: {<br>    a: {<br>      type: &quot;number&quot;,<br>      description: &quot;The number to multiply&quot;,<br>    },<br>    b: {<br>      type: &quot;number&quot;,<br>      description: &quot;The multiplier&quot;,<br>    },<br>  },<br>  required: [&quot;a&quot;, &quot;b&quot;],<br>};<br><br>// Create sum function tool<br>const sumFunctionTool = new FunctionTool(sum, {<br>  name: &quot;sum&quot;,<br>  description: &quot;Use this function to sum two numbers&quot;,<br>  parameters: sumJSON,<br>});<br><br>// Creat multiply function tool<br>const multiplyFunctionTool = new FunctionTool(multiply, {<br>  name: &quot;multiply&quot;,<br>  description: &quot;Use this function to multiply two numbers&quot;,<br>  parameters: multiplyJSON,<br>});</pre><h4>Chat with Agent</h4><p>Now we have the tools to give to the agent, we can set up the agent:</p><pre>// Setup the agent with the respective tools<br>const agent = new OpenAIAgent({<br>  tools: [sumFunctionTool, multiplyFunctionTool],<br>  verbose: true,<br>});</pre><p>And then ask a question:</p><pre>// Chat with LLM<br>const response = await agent.chat({<br>  message: &quot;How much is 5 + 5? then multiply by 2&quot;,<br>});<br><br>// Agent output<br>console.log(String(response));</pre><p>Now the agent will choose the right tools to achieve the desired task using the functions provided by you. Then you should see an output as:</p><pre>=== Calling Function ===<br>Calling function: sum with args: {<br>  &quot;a&quot;: 5,<br>  &quot;b&quot;: 5<br>}<br>Got output 10<br>==========================<br>=== Calling Function ===<br>Calling function: multiply with args: {<br>  &quot;a&quot;: 10,<br>  &quot;b&quot;: 2<br>}<br>Got output 20<br>==========================<br>The result of adding 5 and 5 is 10. When you multiply 10 by 2, the result is 20.</pre><h3>Using Agents with your documents</h3><p>The second agent will be responsible for going through a set of Dan Abramov essays and answering questions based on the available data.</p><p>Firstly, we will import the necessary classes and functions</p><h4>Import the classes and functions</h4><pre>import {<br>  OpenAIAgent,<br>  SimpleDirectoryReader,<br>  VectorStoreIndex,<br>  SummaryIndex,<br>  QueryEngineTool,<br>} from &quot;llamaindex&quot;;</pre><h4>Loading Documents</h4><p>Now we will load the documents and insert them into a local vector store index which will be responsible for storing the documents and allowing the agent to query the most relevant data for the task and a summarize vector index which can better help on tasks summary related.</p><pre>// Load the documents<br>const documents = await new SimpleDirectoryReader().loadData({<br>  directoryPath: &quot;node_modules/llamaindex/examples&quot;,<br>});<br><br>// Create a vector index from the documents<br>const vectorIndex = await VectorStoreIndex.fromDocuments(documents);<br>const summaryIndex = await SummaryIndex.fromDocuments(documents)</pre><h4>Creating the Query Engine Tool</h4><p>Now we will create the tooling that allows the Agents to access the Vector Index:</p><pre>// Create a query engine from the vector index<br>const abramovQueryEngine = vectorIndex.asQueryEngine();<br>const abramovSummaryEngine = summaryIndex.asQueryEngine();<br><br>// Create a QueryEngineTool with the vector engine<br>const vectorEngineTool = new QueryEngineTool({<br>  queryEngine: abramovQueryEngine,<br>  metadata: {<br>    name: &quot;abramov_query_engine&quot;,<br>    description: &quot;Use this engine to answer specific questions about Abramov&quot;,<br>  },<br>});<br><br>// Create a QueryEngineTool with the summary engine<br>const summaryEngineTool = new QueryEngineTool({<br>  queryEngine: abramovSummaryEngine,<br>  metadata: {<br>    name: &quot;abramov_summary_engine&quot;,<br>    description: &quot;Use this engine to generate summaries about Abramov&quot;,<br>  },<br>});</pre><h4>Creating the OpenAI Agent</h4><p>Now we can create and provide the necessary tools for the agent</p><pre>// Setup the agent <br>const agent = new OpenAIAgent({<br>  tools: [vectorEngineTool, summaryEngineTool],<br>  verbose: true,<br>});</pre><h4>Chat with Agent</h4><p>Now you can chat with the agent about Dan Abramov and it will select the right tools to achieve the goal.</p><pre><br>// Chat with Agent<br>const response = await agent.chat({<br>  message: &quot;Where he worked in his 20s?&quot;,<br>});<br><br>// Log output<br>console.log(String(response));</pre><p>The tool output</p><pre>=== Calling Function ===<br>Calling function: abramov_query_engine with args: {<br>  &quot;query&quot;: &quot;work experience in 20s&quot;<br>}<br>Got output The individual had their first job as a software developer in their 20s. They worked for a Russian-American outsourcing company and their salary was $18k/year.<br>==========================<br>In his 20s, Abramov worked as a software developer for a Russian-American outsourcing company. His salary during that time was $18,000 per year.</pre><h3>Conclusion</h3><p>Autonomous agents are powerful when talking about creating workflows and automation that can take your business to the next level or make your life easier.</p><p>There is still a long way to go before they are fully autonomous but the arrival of LLMs is allowing the first steps of these reasoning and decision-making engines</p><p>Do you already use agents in your day-to-day? and wanna discuss agents or help with agents? Reach me on <a href="https://twitter.com/manelferreira_">Twitter</a></p><h3>References</h3><p>Code example: <a href="https://github.com/EmanuelCampos/agents-typescript-example">https://github.com/EmanuelCampos/agents-typescript-example</a></p><p>LLamaIndexTS Documentation: <a href="https://ts.llamaindex.ai/modules/agent/openai">https://ts.llamaindex.ai/modules/agent/openai</a></p><p><a href="https://www.llamaindex.ai/">https://www.llamaindex.ai/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a88ed364a7aa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/llamaindex-blog/how-to-build-llm-agents-in-typescript-with-llamaindex-ts-a88ed364a7aa">How to build LLM Agents in TypeScript with LlamaIndex.TS</a> was originally published in <a href="https://medium.com/llamaindex-blog">LlamaIndex Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improving RAG effectiveness with Retrieval-Augmented Dual Instruction Tuning (RA-DIT)]]></title>
            <link>https://medium.com/llamaindex-blog/improving-rag-effectiveness-with-retrieval-augmented-dual-instruction-tuning-ra-dit-01e73116655d?source=rss-a69e36cc88c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/01e73116655d</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[llamaindex]]></category>
            <category><![CDATA[fine-tuning]]></category>
            <dc:creator><![CDATA[Emanuel Ferreira]]></dc:creator>
            <pubDate>Wed, 18 Oct 2023 22:13:06 GMT</pubDate>
            <atom:updated>2023-10-18T22:17:19.230Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XWkLojc-jVuAIG7tObFoNQ.png" /></figure><h4>Introduction</h4><p>Large Language Models (LLMs) improve performance by accessing external data for background knowledge tasks related. However, existing approaches require costly modifications during LM’s pre-training or integrating the data store after the model has been trained. On the downside, both strategies lead to suboptimal performance.</p><p>To address this problem an <strong>AI Research team at Meta</strong> has proposed a method called <a href="https://arxiv.org/pdf/2310.01352.pdf"><strong>RA-DIT: RETRIEVAL-AUGMENTED DUAL INSTRUCTION TUNING</strong></a> that allows any LLM to be upgraded to include retrieval features.</p><p>In this blog post, we will explore RA-DIT capabilities to have better performance on Retrieval Augmentation Generation (RAG) through building the dataset and fine-tuning the models.</p><p>The RA-DIT approach involves two distinct fine-tuning steps:</p><ol><li>Update a pre-trained LM to better use retrieved information.</li><li>Update the retriever to return more relevant results</li></ol><h4>How it works</h4><p>The RA-DIT approach separately fine-tunes the LLM and the retriever. The LLM is updated to maximize the probability of the correct answer given the retrieval-augmented instructions, while the retriever is updated to minimize how much the document is semantically similar (relevant) to the query.</p><p>Below we are going through each step from generating the fine-tuning dataset, fine-tuning the language model for better predictions, and refining the retrieval search process.</p><h4>Fine-tuning Dataset</h4><p>The fine-tuning dataset is tailored to enhance the language model’s ability to leverage knowledge and boost its contextual awareness during prediction generation. Generating Q/A pairs, summarizing data, and incorporating chain-of-thought reasoning can lead to improved results when integrated with the models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZLxD4NgMUR8jbY6g" /></figure><p>Following our <a href="https://docs.llamaindex.ai/en/stable/examples/finetuning/knowledge/finetune_retrieval_aug.html#fine-tuning-with-retrieval-augmentation">LamaIndex implementation</a>, we retrieve the top_k nodes, generate Q/A pairs from the documents, and then augment the data. We use the Q/A pairs through the QueryResponseDataset module, which returns a (query, response) pair for the fine-tuning dataset. While the retrieval fine-tuning data set is created on Q/A pairs data.</p><h4>Language Model Fine-tuning</h4><p>With our fine-tuning dataset in hand, we can refine our LLM to achieve two main benefits: Adapt the LLM to better utilization of relevant background knowledge and train the LLM to produce accurate predictions even with incorrectly retrieved chunks, empowering the model to rely on its own knowledge.</p><h4>Retriever Fine-tuning</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*S26MPvFfyHSse14N" /></figure><p>The retriever is fine-tuned using the LM-Supervised Retrieval (LSR) method. In this approach, the LLM assesses the information fetched by the retriever. If the LLM finds the information misaligned with the given query, it sends feedback to the retriever. Using this feedback, the retriever refines its search process, ensuring it fetches data that the LLM can effectively use. This collaboration enhances the overall quality of the answers provided.</p><h4>Evaluation</h4><p>To assess the suggested method, the authors employed specific datasets and metrics. Let’s delve into each of these to grasp the experimental results better.</p><h4>Metrics</h4><p>An “exact match” (EM) metric was used to measure how closely the model’s prediction matches the ground truth answer.</p><h4>Dataset</h4><p>The methodology was tested on two distinct tasks:</p><ol><li>Knowledge-intensive tasks.</li><li>Commonsense reasoning.</li></ol><p>Let’s explore the datasets utilized for both of these tasks.</p><h4>Knowledge-intensive dataset</h4><p>For knowledge-intensive tasks the selected datasets predominantly focus on the model’s capacity to access, understand, and relay deep and specific knowledge. They encompass questions rooted in facts, general trivia, and complex domain-specific queries;</p><p>The datasets used are MMLU, Natural Questions (NQ), TriviaQA, and a subset of tasks from the KILT benchmark.</p><h4><strong>Commonsense reasoning dataset</strong></h4><p>Commonsense reasoning datasets challenge the model’s ability to reason and infer based on general knowledge and everyday scenarios. They contain questions and scenarios that typically don’t rely on deep domain knowledge but rather on intuitive and general world understanding.</p><p>The datasets used are BoolQ, PIQA, SIQA, HellaSwag, WinoGrande, ARC-E, ARC-C, OBQA.</p><p>For a better understanding of how these datasets were utilized you can check the <a href="https://arxiv.org/pdf/2310.01352.pdf">paper</a> for better understanding.</p><h4>Results</h4><p>In a comparative analysis of model performance on knowledge-intensive (Table 2 below) and commonsense reasoning tasks (Table 3 below), three models were considered:</p><ul><li>LLAMA 65B</li><li>LLAMA 65B REPLUG (only retrieval augmentation)</li><li>RA-DIT 65B</li></ul><h4>Knowledge Intensive Tasks</h4><p>Evaluations are conducted in 0-shot, 5-shot, and 64-shot fine-tuning settings.</p><p><strong>0-shot Analysis:</strong></p><ul><li>RA-DIT 65B demonstrated superior performance with an average EM score of 50.5 across all tasks.</li><li>It outperformed LLAMA 65B REPlug (43.1 average) and significantly surpassed LLAMA 65B (32.9 average).</li></ul><p><strong>5-shot Analysis:</strong></p><ul><li>RA-DIT 65B maintained its lead with an average EM score of 55.2.</li><li>LLAMA 65B REPlug followed closely with 52.7, while LLAMA 65B achieved an average of 45.0.</li></ul><p>In a separate evaluation for 64-shot fine-tuning, two models were analyzed: ATLAS and RA-DIT 65B.</p><p><strong>64-shot Fine-tuning:</strong></p><ul><li>RA-DIT 65B achieved an average performance of 60.9 across all tasks, slightly surpassing ATLAS, which obtained an average score of 56.8</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*l5nYZmXQaP4kl0bq" /></figure><h4>Commonsense reasoning</h4><p><em>RA-DIT 65B</em> was benchmarked in order to evaluate the impact of retrieval-augmented instruction tuning on the LLMs parametric knowledge and reasoning capabilities.</p><p>In this experiment without retrieval augmentation, <em>RA-DIT</em> showed improvements over base <em>LLAMA</em> 65B models on 7 of 8 evaluation datasets, indicating that the parametric knowledge and reasoning capabilities of the LLM component are in general preserved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*b3uv899KzIwGNYx5" /></figure><p>In summary, RA-DIT 65B consistently delivered great results, surpassing its competitors in multiple scenarios, underscoring its proficiency and aptitude in knowledge-intensive tasks while showing that the parametric knowledge and reasoning capabilities of the LLM are still preserved.</p><h4>Conclusion</h4><p>The RA-DIT approach provides a structured method to enhance how Large Language Models utilize external data. Through the dual fine-tuning of both the model and the retriever, we target better accuracy and context-awareness in responses.</p><p>The incorporation of the LSR technique fosters a more efficient data retrieval process, ensuring that the generated answers are both relevant and informed, the final results show that RA-DIT surpasses un-tuned RALM approaches like REPLUG showing competitive results.</p><p>You can explore more about the LLamaIndex implementation at: <a href="https://docs.llamaindex.ai/en/stable/examples/finetuning/knowledge/finetune_retrieval_aug.html#fine-tuning-with-retrieval-augmentation">https://docs.llamaindex.ai/en/stable/examples/finetuning/knowledge/finetune_retrieval_aug.html#fine-tuning-with-retrieval-augmentation</a></p><h4>References</h4><p>RA-DIT paper: <a href="https://arxiv.org/abs/2310.01352">https://arxiv.org/abs/2310.01352</a></p><p>Connect with me on <a href="https://twitter.com/manelferreira_">Twitter</a> and <a href="https://www.linkedin.com/in/emanuelcferreira/">Linkedin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=01e73116655d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/llamaindex-blog/improving-rag-effectiveness-with-retrieval-augmented-dual-instruction-tuning-ra-dit-01e73116655d">Improving RAG effectiveness with Retrieval-Augmented Dual Instruction Tuning (RA-DIT)</a> was originally published in <a href="https://medium.com/llamaindex-blog">LlamaIndex Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>