Python Advanced AI Agent Tutorial - LlamaIndex, Ollama and Multi-LLM!

Tech With Tim
18 Apr 202453:57

Summary

TLDR本视频教程向观众展示了如何构建一个先进的人工智能代理,该代理能够使用多个语言模型(LM)进行操作。视频首先概述了所需的工具和技术,包括Python、Llama Index和Ollama,后者允许在本地计算机上运行开源的LM。接着,视频通过一系列步骤引导观众创建了一个能够读取PDF文档和Python代码文件的代理,并通过这些文件生成代码。此外,还介绍了如何使用Llama Pass来提高复杂文档(如PDF)解析的准确性。最后,视频通过一个交互式循环,允许用户输入提示,代理会利用其工具生成响应,展示了代理的实际应用。整个项目的过程不仅展示了AI开发的潜力,也体现了如何将不同的工具和模型组合起来解决实际问题。

Takeaways

  • 🚀 介绍了如何构建一个使用多个AI模型的高级AI代理,以及如何在本地运行这些代理。
  • 📚 通过一系列步骤,包括连接代理、解析输出等,展示了AI开发的潜力。
  • 🛠️ 使用了名为Oh Lama和Lama Index的工具,这些工具适合初学者和中级程序员学习和使用。
  • 📈 通过VS Code演示了如何利用AI代理读取代码文件并生成基于现有代码的单元测试。
  • 🔄 展示了AI代理如何使用不同的工具来获取正确的信息并生成代码。
  • 📋 介绍了如何使用Lama Index框架来处理和传递数据给不同的AI模型。
  • 🔧 讲解了如何使用Lama工具在本地计算机上运行开源的AI模型,无需依赖外部API。
  • 🔍 通过Lama Pass工具,可以更好地解析和理解复杂文档,如包含表格和图表的PDF文件。
  • 📝 展示了如何将AI生成的代码输出并保存到文件中,包括错误处理和文件保存逻辑。
  • 🔄 通过多步骤流程,包括使用多个AI模型和工具,实现了从代码读取到生成和保存的完整过程。
  • 🌐 强调了使用开源模型和框架的重要性,以及它们在AI开发中的实用性和可访问性。
  • 📌 提供了关于如何构建和使用高级AI代理的完整教程,包括所需的技术和工具。

Q & A

  • 视频中展示的AI代理是如何工作的?

    -视频中的AI代理通过使用多个工具和模型来执行任务。首先,它通过Lama Index和Lama Pass等工具加载和解析数据,如PDF文件。然后,代理会利用这些数据生成代码或回答问题。AI代理可以根据给定的提示选择合适的工具来完成任务,并将结果传递给另一个模型进行进一步处理。

  • 为什么视频中提到使用Lama Index和Lama Pass?

    -Lama Index是一个开源框架,能够处理数据加载、索引、查询和评估,特别适合用于构建大型机器学习模型(LM)的应用程序。Lama Pass提供了生产级别的上下文增强功能,特别是对于包含嵌入式对象(如表格和图形)的复杂文档,如PDF文件,Lama Parse可以提供更好的解析效果。

  • 视频中提到的“RAG”是什么意思?

    -RAG代表检索增强生成(Retrieval-Augmented Generation)。这是一种结合了检索(从大量数据中检索信息)和增强生成(基于检索到的信息生成响应)的技术。在视频中,AI代理使用RAG能力来提高其生成代码和回答问题的准确性。

  • 如何将AI代理生成的代码写入文件?

    -视频中展示了一个多步骤的过程,首先使用一个模型生成代码,然后使用另一个模型来解析输出,并将解析后的数据格式化为一个Python字典。最后,使用Python的文件操作将代码写入到指定的文件中,确保不会覆盖已存在的文件,并在输出文件夹中创建新的文件。

  • 视频中的AI代理在生成代码时使用了哪些技术?

    -视频中的AI代理使用了多个技术来生成代码。首先,它使用Lama Index来加载和索引PDF文件中的数据。然后,使用Lama Pass中的Lama Parse来解析PDF文件。接着,使用一个专门的代码生成模型(如Code Llama)来生成代码。最后,使用一个通用模型来解析和格式化生成的代码,以便写入文件。

  • 为什么视频中的AI代理在生成代码时可能会有错误?

    -视频中的AI代理使用的是基于本地运行的开源模型,这些模型可能没有像ChatGPT那样的大型商业模型那样的计算能力和复杂性。因此,生成的代码可能需要一些调整和修正,比如添加缺失的括号或删除转义字符,才能成为一个完全功能的代码。

  • 如何确保AI代理生成的代码是有效的?

    -视频中提到了使用一个额外的模型来分析生成的代码结果,并确定它是否是有效的代码。此外,还使用了错误处理和重试逻辑来确保代码生成过程的稳定性。如果代码生成过程中出现错误,代理会重试最多三次。如果仍然失败,会提示用户重新输入提示。

  • 视频中提到的“输出管道”是什么?

    -输出管道(output pipeline)是Lama Index中的一个概念,它允许用户将多个查询处理步骤组合在一起,形成一个连贯的处理流程。在视频中,输出管道用于将生成的代码通过一系列的解析和格式化步骤,最终生成一个可以写入文件的格式。

  • 如何使用视频中展示的AI代理来生成单元测试?

    -视频中展示了如何通过给代理一个提示,比如“读取test.py文件的内容,并为我编写一个简单的Python单元测试”,来生成单元测试。代理会使用Code Reader工具读取文件内容,然后使用Code Llama模型生成代码,最后使用另一个模型来解析输出并保存到文件。

  • 视频中的AI代理是如何决定使用哪些工具的?

    -AI代理会根据给定的提示和上下文来决定使用哪些工具。它通过内置的逻辑和对工具描述的理解来选择最合适的工具来完成任务。例如,如果需要读取API文档,代理可能会选择使用API文档工具;如果需要读取代码文件,它可能会选择使用Code Reader工具。

  • 视频中提到的GitHub仓库有什么作用?

    -视频中提到的GitHub仓库用于存放示例项目的数据和代码。它包含了一个数据目录,里面有用于演示的PDF文件和Python文件,以及一个requirements.txt文件,列出了项目所需的所有Python库及其版本。GitHub仓库还提供了安装和设置代理的说明。

Outlines

00:00

😀 AI开发入门:构建使用多个模型的高级AI代理

本段落介绍了如何构建一个使用多个模型的高级AI代理。通过一系列步骤,包括连接代理、解析输出,以及使用本地运行的LMS(Lambda)和Lama Index框架,向观众展示了AI开发的高级应用。特别强调了即使初学者或中级程序员也能跟随视频逐步构建项目。

05:00

🛠️ 安装和设置:为AI代理配置环境和依赖

详细说明了如何安装和设置所需的依赖项,包括创建Python虚拟环境、安装必要的Python库,以及如何通过GitHub获取示例代码和数据文件。此外,还介绍了如何安装和使用Oh Lama工具来运行本地的AI模型。

10:02

📚 利用Lama Pass解析PDF文档

本段落展示了如何使用Lama Pass工具来解析PDF文档,将其转换为更易于AI模型理解的格式。介绍了Lama Pass的注册和使用方法,以及如何通过环境变量在代码中使用API密钥。

15:04

🔍 创建向量索引和查询引擎以查询API文档

描述了如何创建一个向量索引来存储和查询PDF文档中的数据,以及如何利用查询引擎根据上下文回答问题。还介绍了如何将查询引擎封装为工具,供AI代理使用。

20:08

🤖 构建AI代理:集成工具并测试功能

介绍了如何构建一个AI代理,该代理可以利用先前创建的工具来读取和分析代码,生成代码,并回答有关代码的问题。展示了如何将工具和模型传递给代理,并测试代理是否能够正确地利用这些工具。

25:08

📝 输出解析:将AI生成的代码写入文件

本段落讲解了如何处理AI代理生成的代码输出,包括使用Pedantic模型和输出解析器来格式化输出,以及如何将格式化后的代码保存到文件中。还介绍了如何通过查询管道将多个步骤结合起来,以及如何处理可能发生的错误。

30:09

🏁 完成与总结:保存生成的代码并回顾项目

最后,确保了生成的代码能够正确保存到文件中,并且不会覆盖已存在的文件。还对整个项目进行了回顾,强调了使用本地模型的局限性,并鼓励观众尝试不同的提示来测试代理的功能。最后,感谢Lama Index对视频的赞助,并邀请观众如果对此类视频感兴趣就留言、点赞和订阅。

Mindmap

Keywords

💡AI代理

AI代理是指在视频中展示的人工智能程序,它能够执行多种任务,如读取代码、生成代码或回答问题。在视频的主题中,AI代理使用多个工具来完成复杂的编程任务,展示了AI在软件开发领域的应用潜力。

💡Lama Index

Lama Index是一个开源框架,用于构建和管理大型语言模型(LM)的应用程序。视频中提到使用Lama Index来加载数据、创建索引、查询和评估数据,它是实现AI代理功能的关键技术之一。

💡本地模型(Local Model)

本地模型指的是在个人计算机上运行的AI模型,而不是依赖于远程服务器或云服务。视频中提到使用Olama来运行本地模型,这样可以避免使用API密钥或支付给ChatGPT等服务平台的费用。

💡检索增强生成(RAG)

RAG是一种结合检索和生成的AI技术,能够利用额外的信息来增强模型的输出。视频中的AI代理使用RAG技术来读取和生成代码,展示了如何将RAG应用于实际的编程任务。

💡PDF解析

PDF解析是指将PDF文件中的信息提取和转换成可被程序处理的格式。在视频中,使用Lama Parse工具来提高PDF文档的解析质量,这对于从PDF中提取API文档并供AI代理使用至关重要。

💡代码生成

代码生成是指AI代理根据给定的提示或现有代码自动创建新的代码片段。视频中展示了如何利用AI代理和不同的工具来生成用于API的Python单元测试代码。

💡向量存储索引

向量存储索引是一种数据库技术,用于快速检索和查询信息。在视频中,通过创建向量嵌入将文本数据转换为可以在多维空间中查询的格式,从而允许AI代理快速找到所需的信息。

💡查询引擎

查询引擎是Lama Index中的一个工具,允许用户向加载了数据的索引发出查询。在视频中,查询引擎被用来响应关于API文档的问题,展示了如何利用查询引擎来获取和处理信息。

💡环境变量

环境变量是在计算机操作系统中定义的值,用于存储系统配置信息。视频中提到使用环境变量来存储Lama Cloud的API密钥,这是为了在代码中安全地使用API密钥而不直接暴露它。

💡错误处理

错误处理是编程中用于处理程序执行中出现的异常情况的代码。在视频中,为了确保代码生成的可靠性,实施了错误处理机制,如重试逻辑和异常捕获,以提高系统的健壮性。

💡文件输出

文件输出是指将数据或结果写入到文件系统中的文件中。视频中的AI代理生成的代码需要被写入到文件中,这涉及到选择合适的文件名和处理文件写入过程中可能出现的错误。

Highlights

介绍如何构建一个使用多个AI模型的高级AI代理。

通过一系列步骤运行AI代理,包括连接两个代理、解析输出等。

即使对于初学者或中级程序员,也可以跟随视频进行实践。

使用名为Oh Lama和Lama Index的开源框架进行本地开发。

演示如何利用AI代理读取test.py文件并生成Python单元测试代码。

AI代理能够读取数据文件,并基于现有代码生成新的代码。

使用不同的工具来处理不同的任务,例如读取文件、解析PDF等。

介绍如何使用Lama Index框架来加载数据、索引数据、查询数据并评估数据。

使用Olam来在本地计算机上运行开源的Lem模型,无需支付额外费用。

通过Lama Pass工具,可以更好地解析包含嵌入对象的复杂文档,如表格和图表。

创建一个新的Lama Cloud账户并获取API密钥,以便使用Lama Pass工具。

展示如何通过代码创建一个AI代理,并为其提供多种工具。

AI代理可以根据提供的工具和上下文生成代码并回答问题。

通过创建一个输出管道,将一个Lem的结果传递给另一个Lem进行格式化。

使用Pedantic库来定义输出数据的结构,并将其解析为Python字典对象。

通过错误处理和重试逻辑,确保代码生成和文件保存的稳定性。

最终,AI代理能够根据用户的提示生成代码,并将其保存到文件中。

Transcripts

play00:00

If you're interested in AI development, then you're in the right place.

play00:03

Today I'm going to be showing you how to build an advanced

play00:06

AI agent that uses multiple lamps.

play00:09

We're going to run our agent through a series of steps.

play00:12

We're going to connect the two agents together.

play00:14

We're going to parse the output.

play00:15

And you're really going to see how to get a bit more advanced here

play00:18

and how to do some really cool stuff with LMS running on your own computer.

play00:23

We're going to be doing everything locally.

play00:25

We're going to use something known as oh Lama and Lama Index.

play00:28

And you're really going to get a taste of what AI development is capable of

play00:31

in the short video

play00:33

that even beginner or intermediate programmers can follow along with.

play00:36

So with that said, let's get into a quick demo.

play00:38

Then I'll walk you through a step by step tutorial on how to build this project.

play00:42

So I'm here inside of VS code, and I'm going to give you a demo of how

play00:45

this works.

play00:46

And the concept is that we're going to provide some data to the model.

play00:50

This data will be a coding project that we've worked on.

play00:53

We've kept it simple for this video.

play00:55

But if you scaled this up you could provide it a lot more code files

play00:58

and it can handle all of those potentially at the same time.

play01:01

So you can see that we have a Readme dot pdf file in our data directory.

play01:05

And this is a simple kind of documentation for an API that we've written.

play01:09

We then have a test.py file.

play01:11

And this is the implementation of that API in Python.

play01:15

So the idea here is we want our agent to be able to read in this information

play01:19

and then generate some code based on the existing code that we already have.

play01:24

So I've just run the agent here,

play01:25

and I've given this a sample prompt that says read the contents of test.py

play01:29

and write me a simple unit test in Python for the API.

play01:33

Now the idea here

play01:34

is that we've provided this agent some different tools that can utilize,

play01:37

and it will decide when it needs

play01:39

to utilize the tools and use them to get the correct information.

play01:43

So in this case, it will use a tool to read in the test on py file.

play01:46

It will then use a tool

play01:48

to actually parse and get access to the information in the readme dot pdf.

play01:52

It's then going to generate some code for us.

play01:54

And then what we'll do

play01:55

is use another model to parse that output and save it into a file.

play01:59

So you can see that when I ran this, it did exactly that.

play02:02

Now it doesn't always give us the best result

play02:04

because we are running these models locally.

play02:06

And I don't have a supercomputer here, so I can't run the best possible models,

play02:10

but it's showing you what's possible.

play02:12

And obviously all of this is free.

play02:13

You don't need to pay for anything

play02:14

because we're running it locally using all open source models.

play02:17

So what this did here is generate this test API file.

play02:20

You can see there's a few minor mistakes, but if I fix these up by just adding

play02:24

the correct parentheses and removing the escape characters here you see that

play02:28

we actually have a functioning unit test written for our flask API.

play02:33

So this is pretty cool.

play02:34

We might need to change this a little bit

play02:36

to make it work, but it just outputted all of that for us

play02:39

based on the context of the files that we provided to it.

play02:42

Now, if we go here and read through

play02:44

kind of the output we're getting from the model,

play02:46

you can see that it's actually sharing with us its thought process.

play02:49

So it says the current language of the user is English.

play02:51

I need to use this tool to help me answer the question.

play02:53

It's using the Code Reader tool.

play02:55

It passes the file name equal to test.py.

play02:58

It reads in the contents of test.py and then it says okay,

play03:02

I can answer without using any more tools.

play03:03

I'll use the user's language to answer,

play03:06

to write a simple unit test, blah blah blah.

play03:07

You do this and then write all of this code.

play03:10

Now behind the scenes,

play03:11

what actually happens is we pass the output of this using a secondary model

play03:16

and take just the code and then generate it into a file,

play03:20

and that file name will be generated by a different LM.

play03:23

To make sure it's appropriate for the type of code that we have.

play03:26

So it is a multi-step process here.

play03:28

It worked quite well.

play03:30

And you can see it also gave us a description of what the finished code was.

play03:33

So that's what I'm going to be showing you how to build.

play03:35

I know that it might seem simple, but it's actually fairly complex,

play03:38

and it uses a lot of different tools which are really interesting to learn about.

play03:42

With that said, let's get into the full tutorial here

play03:45

and I'll explain to you how we can build this out completely from scratch.

play03:49

So let's get started by understanding the tools

play03:51

and technologies that we need to use to actually build out this project.

play03:54

Now, what we need to do for this project is we need to load some data.

play03:58

We need to pass that to our LM.

play04:00

We then need to take the result of one L11, pass it to another LM,

play04:04

and then we need to actually use a tool and save this to a file.

play04:08

There's a few different steps here.

play04:09

And if we wanted to build this out

play04:10

completely from scratch that would take us a very long time.

play04:14

So instead we're going to use a framework.

play04:16

Now of course we're using Python, but we're also going to use Lama index.

play04:19

Now we've teamed up with them for this video.

play04:21

But don't worry, they are completely free and they provide an open source framework

play04:25

that can handle a lot of this heavy lifting and specifically is really good

play04:29

at loading in data and passing it to our different labs.

play04:34

I'm on their website right now just because it explains it nicely, but

play04:37

you can see Lamb Index is the leading data framework for building LM applications.

play04:42

It allows us to load in the data which you'll see right here,

play04:45

index the data, query it and then evaluate it.

play04:48

And also gives us

play04:49

a bunch of tools to connect different loops together to parse output.

play04:53

You're going to see a bunch of advanced features in this video.

play04:56

Now, as well as using Lama index, we're going to use something called a Lama.

play05:00

Now Olam allows us to run open source.

play05:02

Lem's locally on our computer.

play05:05

That means we don't need to pay for ChatGPT.

play05:07

We don't have to have an open AI API key.

play05:09

We can do all of this locally.

play05:12

So the summary here is that we're using Python lama index o lama.

play05:16

We're also going to throw in another tool which is new from Lama index called

play05:19

Lama Pass. Don't worry it's free as well.

play05:21

And all of that is going to allow us to build out this advanced AI agent

play05:25

that has Rag capabilities Rag meaning retrieval, augmented generation.

play05:29

Whenever we're taking this extra information

play05:31

and passing it into the model that's really known as right.

play05:34

I have an entire video that discusses how Rag works.

play05:37

You can check that out here.

play05:39

But for now, let's get into the tutorial and let's see exactly how we can

play05:42

build this application.

play05:44

So I'm back on my computer and we're going to start by installing

play05:46

all the different dependencies that we need.

play05:48

We're then going to set up a Lama.

play05:50

And then we'll start writing all of this code again.

play05:53

Oh Lamas. How we run the models locally.

play05:54

We need to install that first before we can start utilizing it.

play05:58

Now there are some prerequisites here.

play05:59

So what we're going to do

play06:00

is I have a GitHub repository that I'll link in the description.

play06:04

And in that GitHub repository you'll find a data directory

play06:08

that contains a Readme file and a test.py file.

play06:11

Now you don't need to use the specific piece of code, but

play06:14

what you should do is create a data directory in a directory in VS code.

play06:18

So I've just opened up a new one here in VS code, made a new folder called data,

play06:22

and then put these two files inside of here.

play06:24

Specifically we want some kind of PDF file in some kind of Python file.

play06:28

We can have multiple of them if you want,

play06:30

but the concept is this is the data that we're going to use

play06:33

for writing the retrieval augmented generation.

play06:35

So you need something inside of here. So

play06:37

either take it from the GitHub repository or populate it with your own data.

play06:41

Now from the GitHub repository as well there is a requirements.txt file.

play06:45

Please copy the contents of that file and paste it into a requirements.txt file

play06:50

in your directory, or simply download that file.

play06:54

This is just going to save you a lot of headache,

play06:56

because it has all of the specific versions of Python libraries

play06:59

that we need in order for this project to function properly.

play07:03

So really again, we want to get this data

play07:04

directory populated with some kind of PDF and some kind of Python file.

play07:08

We then want this requirements.txt.

play07:10

TXT file again.

play07:12

You can find it from the link in the description.

play07:14

I'll put a direct link to the requirements.txt

play07:17

so you can just copy the contents of the file.

play07:19

Or you can download the file directory directly.

play07:22

Sorry. And put it inside of your VS code folder.

play07:25

Now that we have that,

play07:26

what we're going to do is make a new Python virtual environment.

play07:29

That's where we're going to install

play07:30

all these different dependencies that we have this isolated on our system.

play07:34

So to do that we're going to type the command python 3-MV env

play07:39

and then I'm going to go with the name of AI.

play07:41

You can name this anything that you want.

play07:43

If you're on Mac or Linux this is the correct command.

play07:46

If you're on windows you can change this to simply be Python.

play07:49

And this should

play07:50

make a new virtual environment for you in the current directory.

play07:53

This is where we'll install all of the different Python dependencies.

play07:57

Now once we've done that, we need to activate the virtual environment.

play07:59

The command will be different depending on your operating system.

play08:02

If you are on windows or sorry if you're on Mac or Linux, so Mac or Linux.

play08:06

Here you can type source

play08:08

the name of your virtual environment, which in this case is I Symbian

play08:12

and then slash activate and you'll know this is activated.

play08:15

If you see the I prefix you can ignore this base prefix.

play08:18

For me it's just because I have kind of a separate installation in Python.

play08:22

But you should see this prefix in your terminal indicating

play08:24

that this is activated.

play08:26

Now if you're on windows what you're going to do is open up PowerShell

play08:29

and you should be able to type slash I slash

play08:32

and then this I believe is scripts and then slash activate.

play08:37

And that should activate the virtual environment for you.

play08:39

Otherwise you can just look up how to activate a virtual environment on windows.

play08:43

And again make sure you have this prefix once the virtual environment is activated.

play08:47

If we want to deactivate it we can type deactivate.

play08:50

We're not going to do that though, and we can install the different packages

play08:54

that we need.

play08:55

So what we can do is type pip3, install dash r and then requirements.txt.

play09:01

Notice this is in the current directory where we are.

play09:04

When I do that, it's going to read through all of the different requirements

play09:07

and then install them in this Python installation

play09:10

or in this virtual environment.

play09:12

So we'll do that.

play09:13

It's going to read through requirements.txt

play09:15

and install everything for you. This is going to take a second.

play09:17

For me it's already cached so it's going pretty quickly.

play09:20

And that should be it.

play09:22

So once this is finished I'll be right back

play09:23

and then we can move on to the next steps.

play09:25

All right. So all of that has been installed.

play09:27

And the next thing we need to do is install a lama alarm.

play09:31

Again. Let's just run all of this locally.

play09:33

So in order to install Lama I'm just going to clear in my terminal here.

play09:37

And I'm going to go to a new browser window and paste in this URL here.

play09:41

I'm going to leave it in the description.

play09:42

Now this is the GitHub page for Lama.

play09:45

And it shows you the installation setup steps here.

play09:47

So again this will be linked in the description.

play09:49

So if you're on Mac or Windows you can see the download buttons right here.

play09:53

Linux.

play09:54

This will be the command I'm on Mac.

play09:56

So I'll just click on download.

play09:58

When I do that it's going to download the Lama installation for me.

play10:01

Once that's done I'm going to unzip this.

play10:03

And then I'm going to run the installer.

play10:05

Now I've already installed it, but I will still run you through the steps.

play10:08

And then on windows same thing.

play10:09

You're going to download this and then run through the installer.

play10:12

And what this will do is download a kind of terminal tool for you

play10:16

that you'll be able to utilize to interact with Allama.

play10:19

So you can see it says Lama Run.

play10:20

And that's something like Lama two.

play10:22

And this will actually download the Lama to model for you.

play10:25

And then allow you to utilize it and interact with it.

play10:28

In our case, we're actually going to use the Mistral model.

play10:30

But there's a bunch of different models here that you could install,

play10:33

and there's a bunch of other ones as well.

play10:35

These are just some examples of ones that you can use.

play10:37

Okay.

play10:38

So what we'll do here is unzip this folder.

play10:41

And once it's unzipped we're going to run the installer.

play10:43

So you can see here that I can click on oh Lama two.

play10:47

That's going to load the installation tool for me.

play10:49

So I'm going to go ahead and click on open.

play10:51

We're going to move it into applications.

play10:54

And then we should be good to go with a lama.

play10:56

So we'll go next.

play10:58

And then it says install the command line okay.

play11:00

So we're going to go ahead and install it again I already have this installed.

play11:03

So I'm not going to run through this tool.

play11:04

But once you do that you should be able to run

play11:06

the O Lama command, which we'll do in just one second.

play11:09

All right.

play11:10

So once you've gone

play11:10

through that installer, what you can do is simply open up a terminal,

play11:14

which we're going to do here.

play11:16

And we can type.

play11:17

Let me just zoom in here so we can read this

play11:19

O Lama and just make sure that that command works.

play11:21

So if we get some kind of output here we're good.

play11:24

And then we can type O Lama run.

play11:26

And then we're going to run the Mistral model.

play11:29

Okay.

play11:29

So you can see here it shows you all the different models you can potentially run.

play11:32

In this case this one is seven billion parameters.

play11:35

It's 4.1GB.

play11:37

There's a lot larger models here which obviously would perform better.

play11:40

But they need some more intense hardware to actually run properly.

play11:44

So we're going to install Mistral

play11:45

which is only four gigabytes by doing a lama run Mistral.

play11:49

It's going to then download that model for you and then we can utilize it.

play11:53

Now in my case it's already downloaded. So what I can do

play11:55

is start interacting with it by typing something like Hello World.

play11:58

And then it's going to give me some kind of output.

play12:01

Perfect.

play12:01

So what I'm going to do now is I'm going to quit this.

play12:03

So I think I can just type quit or something.

play12:06

or Ctrl c, Ctrl d okay, let's get out of that control D

play12:11

I'm going to close this terminal and I'm going to show you

play12:14

now how we can run this from code.

play12:16

So let it go through, let it install.

play12:18

It is going to take a second because that's the download all of this stuff.

play12:20

And then we'll go back to VS code and see how we interact with Allama

play12:24

from our code.

play12:25

All right. So I'm back inside of VS code here.

play12:27

And I'm going to continue here by creating a file.

play12:29

Now this file will be main.py.

play12:31

And the idea here

play12:32

is just to initially test out Lama and make sure that it's working as an alala.

play12:37

So I'm going to say from and this is going to be lama

play12:40

underscore index dot LMS.

play12:43

If we type this correctly.au

play12:47

lama like that, we're going to import Allama.

play12:51

And then we're going to say hello Lam is equal to oh Lama.

play12:55

And inside of here we're going to say the model is equal to Mistral

play12:58

because this is the one that we want to use.

play13:01

And we can provide a request

play13:02

timeout equal to something like 30s just so that it doesn't take too long.

play13:08

Now that we have the lamp, we should be able to do Lambda run,

play13:13

and then we can say something like hello world.

play13:15

If we say the result is equal to this,

play13:18

we should be able to print out the result.

play13:21

So let's see if that's going to work.

play13:23

I can type Python three and then main.py.

play13:27

We'll give this a second

play13:29

and we'll see

play13:29

if that was a valid command or not, or if we need to use, a different one.

play13:33

So actually my bad here guys,

play13:34

rather than Lambda run, this is going to be Lemke complete.

play13:38

And then we can type in something like hello world.

play13:41

And if we run this, we should see that we get some kind of output.

play13:44

Give this a second. It says, hello.

play13:45

Here's a simple hello, World program.

play13:47

Gives us the output and there we go.

play13:49

So this is just a simple test to make sure that Alama

play13:51

was running locally on our computer.

play13:53

We also can run different types of local models.

play13:55

For example, in a second we're going to run a code generation one.

play13:59

but now you can see that this is indeed working.

play14:01

And we didn't need to have any API key use ChatGPT, etc..

play14:05

This is a local model running on our own computer.

play14:08

So now what we want to do is set up a little bit

play14:11

of the Rag application so we can load in some files,

play14:15

pass that to the LM and see how it can query based on that.

play14:18

All right.

play14:19

So let's go to the top of our program here.

play14:21

And we're going to import a few things that we need in order

play14:23

to load our Python file as well as to load our documentation.

play14:27

We're going to start by looking at how we load in our PDF, which is

play14:30

unstructured or semi-structured data, depending on the way that it's set up.

play14:35

And we're going to use something

play14:35

known as Lama Pass, which can give us a much better parsing of this file.

play14:40

So what I'm going to do is say from Lama

play14:43

underscore pass, we are going to import

play14:46

and then with capital Lama pass like that we'll talk about this in one second.

play14:51

Don't worry I'm going to say it from lama underscore index dot core.

play14:56

And we're going to import the vectors store index

play14:59

and the simple directory reader as well as the prompt template.

play15:04

While we are here we're then going to say

play15:06

from lama underscore index dot core dot.

play15:11

And this is going to be embeddings.

play15:13

And we are going to import the resolve embed model.

play15:17

And I believe for now that is actually all that we need.

play15:20

So let me break down what we're about to do here.

play15:23

We need to load in our data.

play15:25

Now in this case we're loading in PDF documents.

play15:27

But with Lama index we can load in really any type of data we want.

play15:30

And in the previous video I showed you how to load in, for example, CSV data.

play15:34

But in this case we have a PDF.

play15:36

Now what we need to do is we need to parse the PDF into logical portions in chunks.

play15:41

For example, if the PDF had something like a chart, we'd want to extract that

play15:44

because that's some structured data that we could be able to look at.

play15:48

Then once we have that, we need to create a vector store index.

play15:52

Now a vector store index is like a database

play15:55

that allows us to really quickly find the information that we're looking

play15:58

for, rather than having to load the entire PDF at once.

play16:02

What's going to happen is our LM is going to utilize this database and extract

play16:07

just the information that it needs to answer a specific query or a prompt.

play16:12

Now, the way that we'll build

play16:13

this vector store index is by creating something known as vector embeddings.

play16:17

Vector embeddings take our textual data or whatever type of data it is.

play16:21

And they embedded into multidimensional space, which allows us to query for it

play16:26

based on all different types of factors, based on the context,

play16:29

based on the sentiment, we don't really know exactly how it works.

play16:32

It's handled by LMS and some machine learning models in the background, and I'm

play16:36

not quite qualified to talk about it in this short section of the video.

play16:40

But the point is that rather than loading all of the data at once,

play16:44

we're going to query this vector store index,

play16:46

which is like a really, really fast database.

play16:48

It's going to give us the information we need injected into the LM.

play16:52

And then the LM will use that information to answer the prompt.

play16:55

So really all that means for us is we've got to create this index.

play16:59

And I'm going to show you how to do that. All right.

play17:01

So to do this we're going to delete these two lines

play17:03

because these were really just for testing.

play17:05

But we will leave this LM.

play17:06

And what we're going to do is start by setting up a parser.

play17:09

Now the parser is going to be a llama parse.

play17:11

And then we can specify what we want.

play17:13

The result type to be, which in this case is markdown.

play17:16

Now Llama parse is a relatively new product that's provided by Llama Index.

play17:20

What this will do is actually take our documents

play17:23

and push them out to the cloud.

play17:24

They'll then be passed and then that parsing will be returned to us.

play17:28

Now, the reason we use something like this

play17:30

is because it gives us significantly better results when we are trying to query

play17:34

pieces of data from something like a PDF, which is typically unstructured.

play17:38

I'll talk more about it in a second, because we do need to make an account

play17:41

with llama parse. But again, it's totally free.

play17:44

You don't need to pay for it. So we're going to make this parser.

play17:46

And then what we're going to do is we're going to create a file extractor.

play17:51

Now the extractor is going to be a dictionary.

play17:53

And we're going to specify a file extension, which in this case is dot PDF.

play17:57

And we're going to say whenever we find a PDF

play18:00

we want to use this parser which is a llama parse to parse

play18:03

through the PDF, and then give us back some results that we can then load.

play18:08

Next we're going to say documents is equal to.

play18:11

And this is going to be the simple directory reader.

play18:14

And inside of here we're going to specify the directory

play18:16

that we want to read from which is the data directory.

play18:19

And then we're going to specify our file extractor here.

play18:22

So file extractor is equal to the file extractor that we specified.

play18:27

And then we're going to say dot load data okay.

play18:31

So let's write that

play18:32

now if we hover over this you can see that what this is doing is loading

play18:34

data from the input directory.

play18:36

So we're using llama index.

play18:38

We have something called a simple directory reader.

play18:40

Well what this will do is go look in this directory.

play18:43

Grab all of the files that we need

play18:45

and then load them in and use the appropriate file extractor.

play18:49

Now that we've done that, what we can do is

play18:51

we can pass these different documents which have been loaded to the vector

play18:55

store index and create some vector embeddings for them.

play18:58

So we're going to say the embed underscore model is equal

play19:03

to the resolve embed model.

play19:05

And then this is going to look a little funky.

play19:07

But we're going to type local colon.

play19:09

And then bam I/BGE-M3.

play19:15

Now this is a local model that we can use because by default

play19:19

when we create a vector store

play19:20

index it's going to use the OpenAI model, like something like ChatGPT.

play19:24

We don't want to do that.

play19:25

We want to do this locally instead.

play19:27

So what we're doing is we're getting access to a local model.

play19:30

And this model will be able to create the different vector embeddings for us

play19:33

before we inject this data into the vector store index.

play19:37

So I know it seems a bit weird, but we're just grabbing that model.

play19:40

This is the name of it here and we're specifying.

play19:41

We want it locally,

play19:42

which means the first time

play19:43

we run this, it's going to download that model for us and then use it

play19:47

okay.

play19:48

We're then going to say the vector index is equal to the vector store

play19:52

index and then dot from documents.

play19:55

And then we're going to pass the documents

play19:57

that we've loaded here with the simple directory reader.

play19:59

And we're going to specify manually the embed model is equal

play20:03

to the embed model that we got above which is our local embedding model.

play20:07

Now that we've done that we're going to wrap this in

play20:09

something known as a query engine.

play20:11

So we can actually utilize it to get some results.

play20:13

So we're going to say query engine is equal to the vector indexed as

play20:17

query engine.

play20:18

And the MLM that we're going to use is going to be the Alama l11.

play20:24

Now what this means is that I can now utilize this

play20:27

vector index, as kind of like a question and answer bot.

play20:31

So what I can do is I can ask it a question like,

play20:34

what are the different routes that exist in the API?

play20:37

And it will then go utilize the documents that we've loaded in which

play20:40

in this case are the PDF documents in this readme pdf file,

play20:44

and it will give me results back based on that context.

play20:47

Now, in order to test that we can say query engine Dot,

play20:51

and then we can actually send this a query and we can say

play20:55

what are some of the routes in the API question mark?

play21:00

And then it should give us back some kind of reasonable response.

play21:03

Now we do need to print that. So we'll say result

play21:06

is equal to this.

play21:08

And we will be able to run this code in one second

play21:11

once we get access to the API key for Lambda Pass.

play21:14

So let me show you how we do that.

play21:16

So I am going to show you how to use Lama Pass here.

play21:18

But I quickly want to break down what it actually is.

play21:21

So on February 20th, 2024, Lama Index released Lama Cloud and Lama Pass.

play21:27

Now this brings production

play21:28

grade context augmentation to your LM and Wragg applications.

play21:32

Now Lama Parse specifically is a propriety pre parsing for complex documents

play21:37

that contain embedded objects such as tables and figures.

play21:41

In the past, when you were to do some kind of querying over this data,

play21:44

you get really, really bad results when you have those embedded objects.

play21:48

So Lama Parse is kind of the solution to that,

play21:51

where it will do some parsing and actually break out these embedded objects

play21:55

into something that can be easily ingested and understood by your model.

play21:59

This means you'll be able to answer

play22:00

complex questions that simply weren't possible previously.

play22:04

As you can see, Rag is only as good as your data if the data is not good.

play22:07

If the vector index isn't good, then we're not going to get good results.

play22:10

So the first step here is that we parse out our documents into something

play22:13

that's more effective to pass into the vector index.

play22:17

So when we eventually start using it,

play22:19

we get better results which drastically affect the accuracy.

play22:22

in a good way.

play22:23

So you can kind of read through here and you can see exactly what it does.

play22:26

I'll leave this link in the description.

play22:27

But just understand that what this does is give us much better results

play22:31

when we are parsing more complex documents, specifically things like PDFs.

play22:35

So what we're going to do here is create a new llama cloud account.

play22:38

You can do that just by signing in with your GitHub.

play22:40

I'll leave the link below.

play22:42

And this will give us access to a free API key so we can use the llama parts tool.

play22:46

All right.

play22:47

So once you've created that account or signed in

play22:49

you can simply just click on Use Llama Pass.

play22:51

It's pretty straightforward here.

play22:53

And then what you can do is you can use this with a normal API.

play22:56

Or you can use it

play22:57

directly with llama index, which is exactly what we're doing here.

play23:00

So what we want to do is just get access to an API key here.

play23:03

So we can click on API key and we can generate a new key.

play23:07

I'm just going to call this tutorial.

play23:10

I'm going to press on Create new key.

play23:11

Can read through the docs if you want.

play23:13

But I'm just going to copy this key.

play23:15

And we're going to go back into our Python file.

play23:18

And I'm going to make a new dot env file here.

play23:21

And then I'm going to create an environment

play23:23

variable that stores this key that we'll have access to in our code.

play23:27

So we're going to say that this is llama

play23:28

underscore cloud underscore API underscore key.

play23:32

This is going to be equal to.

play23:33

And then I'm going to paste in that API key.

play23:35

Obviously make sure you don't leak this I'm just showing it to you for this video.

play23:38

And I'll delete it afterwards.

play23:40

Then we're going to go into Main.py and we're just going to load

play23:44

in that environment variable.

play23:45

And it will automatically be detected by our parser.

play23:48

So I know I went through that quickly, but the basic idea is

play23:50

we're going to make a new account here on Llama Cloud.

play23:52

We're then just going to use the free parser.

play23:54

So we're going to go and generate an API key.

play23:57

Once we generate the API key we're going to paste that inside

play24:00

of an environment variable file with the variable Llama Cloud API key.

play24:05

We're going to close this.

play24:06

Then we're going to go to our Python file.

play24:08

And we're going to write the following code

play24:10

which will allow us to automatically load in this environment variable.

play24:14

So we're going to say in lowercase is from dot env

play24:19

imports load underscore dot envy.

play24:23

We need to spell these correctly though.

play24:26

And then we're going to call this function.

play24:28

And what the location v function will do is look for the presence

play24:31

of a dot env file, and then simply load in all of those variables

play24:35

which will give this line right here the parser access to that variable.

play24:40

So we can use llama pass. Great.

play24:43

So now that we've written this code we can test this out.

play24:45

So what I'm going to do is type Python three

play24:48

and then main.py.

play24:50

We're going to wait a second for it to install everything that we need.

play24:53

And then we're going to see if we get some kind of output.

play24:56

All right. So this is finished running.

play24:57

And you'll see then

play24:58

what happened here is it started parsing this file using llama parse.

play25:02

It actually pushed that out to the cloud.

play25:04

Which means if we had hundreds of files, thousands of files etc.,

play25:07

we could actually handle all of those,

play25:09

push them out to one person and get the results back.

play25:11

Then what it did is gave us the result here after querying that PDF.

play25:15

So it says the API supports several routes for performing various operations.

play25:18

These include items, items, items, ID, items, ID, etc.

play25:23

so it used that context to actually answer the question for us.

play25:27

So now that we've created this, this is going to be

play25:30

one of the tools that we provide to our AI agent.

play25:34

The idea here is that we're going to have this,

play25:35

and we're going to have a few other tools.

play25:37

We're going to give it to the agent, and the agent can use this vector index

play25:41

and this query engine to get information

play25:44

about our PDF or about our, API documentation.

play25:48

And then using that information,

play25:49

it can generate a new response and answer questions for us.

play25:53

So the agent is going out there utilizing multiple different tools.

play25:56

Maybe it combines them together, maybe it uses one, maybe it uses two three, etc.

play26:00

and then it aggregates all of the results there and gives us some kind of output.

play26:05

So now let's look at how we start building out the agent.

play26:07

And we'll build out another tool that allows us to read in the Python file.

play26:10

Because right now we're just loading in the PDF.

play26:13

All right.

play26:13

So we're going to go back to the top of our program here.

play26:16

And we're going to say from Lama underscore index dot core

play26:23

dot tools.

play26:24

And we are going to import the query engine tool.

play26:28

And then the tool metadata.

play26:31

Then what we're going to do is we're going to take this query engine.

play26:34

So I'm going to delete this right here.

play26:35

And we're going to wrap it in a tool that we can provide to an AI agent.

play26:39

So I'm going to say tools are equal to.

play26:42

And then this is going to be query engine tool for the query engine.

play26:46

This is going to be the query engine for our PDF or for API documentation.

play26:51

We're then going to say

play26:52

metadata is equal to and then this is going to be the tool metadata.

play26:57

And here we're going to give this a name and a description.

play26:59

Now the name in the description will tell our agent when to use this tool.

play27:04

So we want to be specific.

play27:05

So I'm going to call this API documentation.

play27:09

And then we want to give this a description.

play27:11

So we're going to say the description is equal to.

play27:13

And I'm just going to paste in the description

play27:15

to save us a little bit of typing here okay.

play27:17

So let's paste it in.

play27:18

And this says this gives documentation about code for an API.

play27:21

Use this for reading docs for the API

play27:26

okay.

play27:27

So we're just giving some information about the query engine tool.

play27:30

Now we are going to write another tool in here in a second.

play27:33

But for now I want to make the agent and then show you how we utilize the agent.

play27:37

So we need to import another thing in order to use the agent here.

play27:41

So we're going to go up to the top and we're going to say from

play27:45

Lama index dot.

play27:48

And this is going to be core dot agent.

play27:51

And we're going to import the react agent okay.

play27:55

We're now going to make an agent.

play27:57

So we're going to say agent is equal to react

play27:59

agent dot from underscore tools.

play28:03

And we're going to give it a list of tools that it can use okay.

play28:06

So we're going to say tools.

play28:08

And then we need to give it an LM which we're going to define in one second.

play28:12

We're going to say verbose equals true.

play28:14

If you do this it will give us all of the output

play28:16

and kind of show us the thoughts of the agent.

play28:18

If you don't want to see that, you can make this false.

play28:21

And then we're going to provide some context to this,

play28:24

which for now will be empty, but we'll fill in in one second.

play28:27

Okay. So this is great.

play28:28

But what I want to do now is I want to make another LM

play28:32

that we can use for this agent, because we want this to generate

play28:35

some code for us rather than just be a general kind of question answer.

play28:39

But so what I'm going to do here is I'm going to say my code.

play28:42

Hello, LM

play28:43

is equal to oh llama.

play28:45

And we're going to use a different LM.

play28:48

And this is going to be model equal to code llama.

play28:51

Now code Llama is something that does code generation specifically.

play28:55

So rather than using the normal Mistral model which we had here,

play28:59

we're just going to use the code L because we want to do code generation.

play29:02

So now I'm going to pass code LM here.

play29:04

And you can see how easy it is to utilize multiple

play29:07

LMS locally on your own computer.

play29:09

Again all of these are open source and when you do this

play29:11

it should automatically download it for you.

play29:14

Okay.

play29:14

Last thing we want to do is provide a bit of context to this model.

play29:18

So just to clean up our code a bit we're going to make a new file.

play29:20

We'll call this prompts.py.

play29:23

And inside of prompts

play29:25

I'm just going to paste in a prompt for the context for this model okay.

play29:29

You can find this from the link in the description from the GitHub.

play29:32

But it says purpose.

play29:33

The primary role of this agent is to assist users by analyzing code.

play29:36

It should be able to generate code and answer questions about code provided.

play29:40

Now you can change that if you want, but that's really what it's doing, right?

play29:43

It's going to read code, analyze it, and then generate some code for us.

play29:47

So that's what we're doing.

play29:48

So now what I want to do is import that context.

play29:50

So I'm going to go to the top and I'm going to say from prompts

play29:55

import and then context.

play29:57

And then down here I'm going to pass that context variable

play30:01

okay. So now we have made an agent.

play30:04

And we can actually test out the agent and see if it utilizes these tools.

play30:09

So let's do a simple while loop here.

play30:12

And let me just make this a bit bigger so we can see it.

play30:14

We're going to say well prompt colon equals to input.

play30:20

And we're going to say enter a prompt.

play30:22

And we're going to say Q to quit.

play30:25

So if you type in Q then we're going to quit.

play30:27

And we're going to say well all of that does not equal Q

play30:31

okay, so let's type this correctly.

play30:33

Then we are going to do the following, which is result equal to agent dot query.

play30:40

And then we're just going to pass in the prompt and then print

play30:44

the result okay.

play30:46

So you might be wondering what we just did.

play30:47

Well we simply wrote an inline

play30:49

variable here using something known as the walrus operator in Python.

play30:53

This just means it's only defined in the while loop,

play30:55

and it will get redefined each time the while loop runs.

play30:58

Just make things a little bit cleaner and we say, okay, let's get some prompt.

play31:02

When it's not equal to Q,

play31:04

then we'll simply take the prompt, pass it to our agent.

play31:07

The agent will then utilize any tools it needs to,

play31:09

and then it will print out the result.

play31:12

So let's test this out and see if it's working.

play31:14

So let's clear and let's run.

play31:17

And this time we're not just going to be using the API documentation vector index.

play31:22

We'll use an agent and it will decide when to utilize that tool.

play31:26

I know it seems a bit weird because we only have one tool right

play31:29

now, but imagine we had 20 tools, 30 tools, 100 tools, and the agent would pick

play31:33

between all of them and have the ability to do some really complex stuff.

play31:37

All right, so this is running. Now I'm going to give it a prompt.

play31:39

I'm going to say something like send a Post request to make

play31:45

a new

play31:46

item using the API in Python.

play31:51

Okay.

play31:51

Let's see what this is going to give us here.

play31:54

And if this is going to work or not okay. Sweet.

play31:56

So it looks like that works.

play31:57

If we go here we can see that we get

play32:00

I need to use a tool to help me answer this question API documentation.

play32:03

It's looking for post items okay.

play32:05

The API documentation provides information on how to create an item

play32:08

using the post method.

play32:09

I can answer the question to create a new item, blah blah blah.

play32:12

And then it generates the response and then it gives it to us here, right?

play32:15

To create a new item we do this import requests

play32:18

URL payload response okay that actually looks good.

play32:22

And then come down here and says this will create a new item.

play32:25

And then we can ask it another question or hit Q to quit.

play32:29

Perfect. So that is working.

play32:30

However I want to add another tool to this agent

play32:34

that allows it to load in our Python files.

play32:37

So llama parse itself can't handle Python files.

play32:40

It's actually not what it's designed for.

play32:42

But what we'll do

play32:43

is we'll write a different tool that can just read in the contents

play32:46

of any code file that we want, and then give that into the LM.

play32:50

So this way can have access to the API documentation.

play32:53

And it can also have access to the code itself.

play32:56

So it can read both of them.

play32:58

So let's start doing that.

play32:59

And the way we'll do that is by writing a new file here called Code reader.py.

play33:05

So let's go inside of Code Reader.

play33:07

And we're going to make a new tool that will then pass to our Lola.

play33:11

So we're going to say from llama underscore index

play33:14

score dot tools

play33:17

imports the function tool.

play33:20

Now this is really cool because what we can do is wrap any Python

play33:24

function as a tool that we can pass to the LLN.

play33:27

So any Python code that you'd want the model to be able to execute

play33:31

it could do that.

play33:32

You just have to give it a description of the tool,

play33:34

and it can actually call that Python function with the correct parameters.

play33:38

This to me is super cool and it really has a lot of potential and possibilities.

play33:42

Now I'm also going to import OS, and then I'm going to define a function

play33:45

which will act as my tool.

play33:46

So I'm going to say the code reader function.

play33:49

And we're going to take in a file name okay.

play33:53

Now what we're going to do is say path is equal to OS dot path dot join.

play33:58

And we're going to join the data directory and the file name

play34:01

because we want to look just inside of data here.

play34:04

Perfect. And then we're going to try to open this.

play34:06

So we're going to say try.

play34:07

We're going to say with open

play34:10

okay.

play34:10

And this is going to be the path.

play34:12

And then we're going to try to open this in read mode as F.

play34:17

Then we're going to say the content equals f dot read.

play34:22

And then we can simply return

play34:24

the file underscore content.

play34:28

And this will be the content okay.

play34:32

Then we're going to have our accept exception

play34:35

as E we got to spell accept correctly.

play34:39

And then what we're going to do here instead is we're going to return

play34:42

some kind of error.

play34:43

And that error is going to be the string of E.

play34:47

And that's it.

play34:48

That's actually all that we need for this function.

play34:50

Now this is something that we can wrap in a tool and we can provide to the agent.

play34:53

It can then call this function

play34:55

and get back either the file content or the error that occurred okay.

play34:59

So we're going to say the code underscore reader is equal to the function tool.

play35:04

And this is going to be dot from underscore defaults.

play35:08

And then we're going to say fn standing for function is equal to the code

play35:11

reader func.

play35:12

And then what we need to do similar to before is we need to give this a name.

play35:16

And we need to give this a description so the agent knows what to use

play35:20

or when to use this.

play35:21

So we're going to call this the code reader.

play35:23

And for the description I'm just going to paste in a description like I had before.

play35:28

Let me see

play35:29

if I can move this on to separate lines okay.

play35:33

Let's just do it like this so that you guys can read it.

play35:36

Okay.

play35:37

So it says this tool can read the contents of code files and return the results.

play35:41

Use this when you need to read the contents of a file.

play35:44

Perfect. That looks good to me.

play35:45

Hopefully that's going to work for us.

play35:47

And now we can go to Main.py and we can import the code reader tool.

play35:52

So we're going to say from code Reader, Import

play35:55

Code Reader which is our tool, our function tool.

play35:59

And now we can just simply pass that in our list of tools.

play36:02

So imagine right you can write any Python function you want.

play36:06

Just wrap it like I said or I did here with the function tool

play36:09

and then just pass it in this list.

play36:11

And now all of a sudden your agent has access to this

play36:15

and it can start manipulating things on your computer, interacting

play36:18

with Python functions.

play36:19

This really makes the possibilities of agents quite unlimited.

play36:22

And that's what I really like about this.

play36:24

Okay, so we have the code reader tool now.

play36:26

And we also have the API documentation.

play36:28

So now our agent should work exactly as before.

play36:31

And we can simply read the contents of that file.

play36:34

So let's try running this and see what result we get

play36:39

when we give it a prompt that asks it to say read that file.

play36:42

Okay.

play36:42

So start parsing the file

play36:45

and then we'll write a prompt and we'll say something like read

play36:47

the contents of test.py and generate some code okay.

play36:51

So read

play36:53

the contents of test up hi

play36:55

and give me the exact same code back.

play36:59

Now remember we're running some local models

play37:01

that don't have a ton of parameters and aren't the best one,

play37:03

so we're not always going to get the best result.

play37:06

But I hope this is going to work,

play37:07

or it should give us at least some kind of result.

play37:10

So you can see it says, okay, I need to use a tool.

play37:12

So it says we're going to use the tool code reader file name test.py.

play37:16

And then it gets the contents of the file.

play37:19

And then what it says here is you provide a Python script

play37:20

that contains an in-memory database for simplicity,

play37:23

which implements a list called items.

play37:24

The script defines four endpoints one for creating new items,

play37:27

blah blah, blah and then gives us this whole result.

play37:29

So it didn't give us the code that we wanted,

play37:31

but it did actually give us a description

play37:33

of what was inside of that file, which to me says this is indeed working.

play37:37

And notice it only used this tool.

play37:39

It didn't use the other tool because it didn't need that for that specific prompt.

play37:44

Okay. So I think that's good.

play37:45

That means that it's working and we're able to utilize both the tools.

play37:48

Now the next thing that we need to do is we need to take any code

play37:52

that this model is generating for us, and we need to actually write that

play37:56

into a file.

play37:57

Now this is where we're going to use another lamp.

play37:59

So what we want to do is we want to get the result that we just saw there.

play38:03

We want to determine if it's a valid code,

play38:06

and then we want to take that code and write it into a file.

play38:10

Now in order to do that, we need an Lem to analyze the result of this output.

play38:16

So what we're really going to do. Right. So we're gonna take this result.

play38:18

We're going to pass it to a different Lem in that Lem is going to have

play38:21

the responsibility of taking that result and formatting it

play38:25

into something that we can use to write the code into a file.

play38:29

Now, I'm not sure if I'll be able to show this here,

play38:31

because I think I cleared the console.

play38:32

Yeah, I did, but you would have seen before that it gives us some code output,

play38:36

but the code is mixed with like descriptions

play38:39

and other information that we don't want to write into the file.

play38:42

So the other LMS job is going to be to parse that output

play38:46

into a format where we can take it and we can write it into the file.

play38:50

So let's start writing that.

play38:51

This is where it gets a little bit more complicated,

play38:53

but I also think it's where it gets quite cool.

play38:56

So we're going to go to the top of our program here.

play38:58

And we're going to start

play38:59

importing some things that can do some output parsing for us.

play39:02

So we're going to say from pedantic imports, the base model,

play39:08

we're then going to say from

play39:09

Lama index dot core dot output.

play39:14

And I believe this is underscore parsers.

play39:16

We are going to import the pedantic output parser.

play39:20

We're then going to say from Lama index dot core

play39:25

query pipeline import the query pipeline,

play39:29

which allows us to kind of combine multiple steps in one.

play39:32

So now we're going to scroll all the way down.

play39:34

And after we create our agent

play39:35

and our code alarm, we're going to start handling the output parsing.

play39:39

So we're going to make a class here.

play39:41

And we're going to say class code output.

play39:44

And this is going to be a base model from pedantic.

play39:48

Then what we're going to do is define

play39:50

the type of information that we want our output to be parsed into.

play39:54

Now this is super cool because we can use Lama index and these output parsers

play39:59

to actually convert a result from an LM into a pedantic object.

play40:04

So we can specify the type that we want in the pedantic object.

play40:07

And then lama index.

play40:08

And another lm can actually format the result to match this pedantic object.

play40:13

Super cool.

play40:14

So I'm going to say code and this is going to be type string.

play40:17

I'm going to say description.

play40:19

And I want this to be a string as well.

play40:21

But we could make it other types.

play40:22

But in this case we're just going to need strings.

play40:24

And then I'm going to say file name is a string okay.

play40:27

So I've just made a pedantic object.

play40:29

This is just a class that we're going to use to do our formatting.

play40:32

And then we're going to write some things for our query.

play40:35

So we're going to say parser is equal to the pi Dan tic output parser.

play40:41

If I can write this here and we're going to pass the code output

play40:45

which is specifying, we want to use this pedantic output parser

play40:49

and get our result and pass it into this code output object.

play40:53

We're then going to have a Json prompt underscore string.

play40:57

And this is going to be equal to a parser dot format.

play41:01

And we're going to format the code parser template

play41:05

which is a variable that I'm going to write in one second.

play41:08

So now what we're going to do is go to prompts.

play41:11

And I'm going to write in a prompt here I'll explain how this works.

play41:13

We just got a bear with me, because there is a bit of code that we need to write.

play41:16

Okay. So let me copy this in again.

play41:18

You can find this from the GitHub.

play41:19

Or you can just write it out yourself.

play41:21

And what this says is parse the response from a previous Lem into a description

play41:26

and a string of valid code, and also come up with a valid file name.

play41:29

This can be saved.

play41:31

I also come with a valid file name.

play41:32

This could be saved as that doesn't contain special characters.

play41:35

Here is the response.

play41:36

And then this is the response from the previous all of them.

play41:38

You should parse this into the following Json format.

play41:42

Okay, so this seems weird, but this is what I'm providing

play41:45

to my output parser to tell it how to take the result

play41:49

from this Lem and parse it into the format that I want.

play41:53

So let's import that and then we'll look at how this works.

play41:55

So from here we're going to do the code parser template.

play42:00

And then I'm going to pass the code parser template here.

play42:03

Now with the parser dot format will do is it will take this string.

play42:07

And it will then inject at the end of that string.

play42:10

The format from this pedantic model.

play42:13

So I've written my pedantic output parser.

play42:16

Pass the code output.

play42:17

It's saying hey, this is the format we want.

play42:19

Code string description, string file name string.

play42:22

What the output parser will do when I do parser dot format is

play42:26

it will take this format, find the Json representation of it,

play42:30

and then pass it or inject it into the code parser template.

play42:33

So then when I start using this template on the next step,

play42:37

it knows the type of format we want the output to be in.

play42:40

So now I say my Json underscore

play42:44

prompt underscore template is equal to.

play42:48

And this is a prompt template.

play42:50

And I simply pass my Json prompt string.

play42:53

Now at this stage what we do is we write kind of wrapper on the prompt template.

play42:57

So we can actually inject inside of here.

play43:00

The response.

play43:00

So the response if we look here is this okay.

play43:03

Just bear with me. This will make sense as we get there.

play43:06

And then lastly we're going to make an output

play43:09

pipeline.

play43:10

And the pipeline is going to look like this.

play43:12

It's a query pipeline.

play43:14

And the query pipeline is going to have a chain.

play43:16

And the chain is going to go that we first need to get the Json prompt template.

play43:20

We're then going to get whatever we need in the template.

play43:23

And then we're going to pass that to our lamp.

play43:26

And notice this time I'm using my normal Mistral LM which is this one right here.

play43:30

I'm not using the code LM because I want a different L, for this

play43:34

task, a more general purpose one, not one specifically for code.

play43:38

Okay. So now we have our output pipeline.

play43:40

So the idea here is that what we want to do

play43:42

is we want to take the output pipeline and we want to pass this result to it.

play43:46

And then we're going to get the result back,

play43:48

which is going to be that formatted object that we want to look at.

play43:51

So I know this is a bit complicated, but that was kind of

play43:53

the point of this video was to make it a bit more advanced.

play43:56

And you're going to see now how we do this.

play43:57

So we have the result from Agent Duck Query prompt.

play44:00

Now let's take that result and pass it to our next agent.

play44:05

So we're going to say next result is equal to the output pipeline dot run.

play44:10

And we're going to pass the response equal to the result.

play44:15

So whatever the result was from the first agent that's now what we're passing.

play44:19

Is this variable right here in this code parser template prompt.

play44:23

Then we can print out the next result.

play44:27

And we can see what we get.

play44:28

Now keep in mind this doesn't always work.

play44:31

There is sometimes some errors based on how the parsing occurs.

play44:34

But overall it's pretty good.

play44:36

So let's go up here and let's run our agent again.

play44:40

And let's get it to do a similar thing that it did before where it calls

play44:43

like a post endpoint or something.

play44:45

Okay.

play44:45

So a similar prompt as before, read the contents of test.py

play44:49

and write a Python script that calls the post endpoint to make a new item.

play44:52

Let's type enter in here and let's see what we get.

play44:56

All right.

play44:56

So we can see that we get the kind of thought process here of the first line,

play45:00

which is that it needs to use the code reader tool,

play45:02

which it does, and then it generates this code.

play45:05

And then what happens is

play45:06

we actually get output here from our second line that says assistant.

play45:10

And then it gives us this Python object or this Json object really where

play45:14

we have the code which is all of the code that it generated, which was this.

play45:18

Right.

play45:19

And then it has the what else description.

play45:22

Use the request library in Python to do this.

play45:24

And then it has a file name which is this.

play45:28

So now that we have this kind of output what we want to do

play45:31

is take this output and load it as kind of valid Json.

play45:34

what do you call it? data in Python?

play45:37

I'm going to show you a fancy way to do that.

play45:40

Once we have that, we can

play45:41

then access all the different fields like code description and file name.

play45:45

And we can utilize those to save a new file on our computer.

play45:49

So again we've gone through we've generated the code.

play45:52

We've used our different tools from the Rag pipeline.

play45:55

And then we've now parsed our output into this format where we're able

play45:59

to utilize these different fields.

play46:00

We just now need to load this in,

play46:02

so that it's kind of valid for us to be able to view.

play46:05

Okay.

play46:06

So now that we have that, what we're going to do is the following.

play46:09

We're going to say our cleaned Json

play46:11

is equal to and we're going to go up to the top of our program.

play46:15

And we're going to import s t.

play46:17

Now AST is something that going to allow us to actually load in Python code.

play46:22

So what we'll do is we'll take the output from here

play46:25

and we'll load it in as a Python dictionary.

play46:27

So we're going to say AST dot literal evaluation.

play46:31

And then we're going to convert the next result into a string

play46:35

because it's actually a response object.

play46:38

And we're going to replace the assistant which was kind of

play46:41

what was leading here with an empty string.

play46:45

So all we're doing

play46:46

is removing that assistant that came before that valid Python dictionary,

play46:50

and then we're loading in the rest of this as a Python dictionary object.

play46:55

So now this is actually going to give us a Python dictionary.

play46:57

And what I can do is I can print code

play47:01

generated and then I can print what it is.

play47:04

So I can say my cleaned Json and then access the code.

play47:08

And then I can print my description.

play47:10

So I'm going to go here and go backslash n backslash n

play47:14

description.

play47:16

And this needs to be a backslash not a forward slash okay.

play47:20

And then the description will be

play47:23

the cleaned Json of the description.

play47:28

I can then say my file name is equal to the cleaned Json.

play47:32

And this will be my file name okay.

play47:35

So let's run this now and see if we get the correct output.

play47:38

And then we're just going to add some error handling here.

play47:40

And we're actually going to save the file

play47:42

because it is possible that some errors could occur.

play47:44

So let's save and let's bring this up and let's quit.

play47:48

And let's copy this prompt because this one worked.

play47:51

And we will paste it again.

play47:53

And we'll see if we're able to actually get all of those different fields.

play47:56

And if we load in the Python object properly.

play47:58

All right.

play47:58

So you can see here that we're getting

play47:59

our result code generated which is this create item.

play48:02

And then it has some lambda function here.

play48:04

And then we have the description.

play48:06

And then we didn't print out the file name.

play48:08

But we would have had the file name as well.

play48:10

So it gave us a different result than we had last time.

play48:12

but you can see this is indeed working.

play48:14

And now we can quit and we can move to the next step,

play48:17

which is a little bit of error handling and then actually saving the file.

play48:20

So what we want to do now is just make sure that this works before

play48:24

we move forward.

play48:25

Because it's possible that we could get an error.

play48:27

So what we're going to do is retry this prompt or this sequence of steps

play48:31

a few times to just make sure it's hand or it's working properly.

play48:34

Sorry. Before we move on to the next step.

play48:37

So we're going to say retries are equal to zero.

play48:39

And we're going to say while retry are less than three.

play48:44

So we'll just retry this three times.

play48:46

Then inside of here we're going to do this.

play48:49

And we're going to say try.

play48:51

And we're going to try the following.

play48:53

We're then going to say accept.

play48:55

And this is going to be exception.

play48:57

As if we're going to say retries plus equals one here.

play49:03

And then we're going to break if this happened successfully.

play49:06

So what's going to happen now is we're going to retry this up to three times.

play49:10

So every time we fail something happens here that's wrong.

play49:13

We simply retry it and it will go and do this again

play49:16

with the same prompt that we typed.

play49:18

Now we can also do an error message here.

play49:20

We could say print error occurred retry number.

play49:26

And then we can make this an F string.

play49:27

And we can put in the number of retries.

play49:30

And then we can print out what the error.

play49:32

Actually once okay.

play49:34

So that should be our retry block.

play49:36

I'm now going to come down here and I'm going to say okay

play49:39

if retries is greater

play49:41

than or equal to three which means this block actually failed.

play49:45

We never successfully generated this cleaned Json.

play49:48

Then I'm simply going to say continue, which means we're going to go back up here

play49:52

and ask for another prompt.

play49:54

And I'm going to say print.

play49:56

Unable to process

play50:00

the request, try again.

play50:03

Okay.

play50:03

Now you've probably seen this sequence

play50:04

before, but pretty much we'll try to do this.

play50:06

If it doesn't work, we'll just say, hey,

play50:07

you know, that prompt didn't work for some reason.

play50:09

Okay, give us another one because it's possible

play50:11

they ask us to do something we're not able to do or the outputs not possible.

play50:14

there's all kinds of errors that could occur here.

play50:16

So we're just kind of handling that and cleaning it up a bit with this logic.

play50:20

Now, if we do get down to this point here,

play50:22

that means that we were able to actually generate this code.

play50:25

So what we can do is we can save it to a file.

play50:28

So we can write a little try here and we can say try with open

play50:32

and we can say open the file name which we have here.

play50:37

And then we can say that we want to open this in

play50:40

W as f and we can say f dot.

play50:43

Right.

play50:44

And we can write the clean Json code into that file okay.

play50:50

And then we can say print

play50:52

saved file.

play50:54

And we can just print out the file name.

play50:56

And then we can have an accept here.

play50:58

And we can say print

play51:01

error saving file.

play51:04

Okay.

play51:05

Now just to make sure that we're not going to overwrite a file name

play51:08

that we already have, what we can do is we can do an Aussie path

play51:13

dot join and we can make an output, folder here.

play51:17

So we can say output and then file name.

play51:21

Now we need to import OS.

play51:22

So we'll go to the top of our program and import OS.

play51:26

Sorry I know I'm jumping around all over the place here.

play51:29

Then we can go here and we can make a new folder called output.

play51:33

So now all of the output we'll just go inside of this folder.

play51:35

So we don't accidentally override any files that we already have.

play51:39

Okay. So kind of final run here.

play51:42

Let's give this a shot and see if this works.

play51:45

So let's bring up the code.

play51:48

Let's clear and let's run.

play51:51

And then we'll

play51:52

enter our prompt and we'll see if it gives us that generated code okay.

play51:55

So we're going to use the same prompt as before.

play51:58

And now what we're looking for is that we actually get a generated file

play52:01

inside of this output directory.

play52:03

Okay. So it seemed this did actually work.

play52:06

You can see it has the code generated here.

play52:08

And then if we go into our output we have this create item file.

play52:12

Now we do need to remove this because there was a few characters

play52:16

that I guess it left in here.

play52:17

But what it's doing is looking for an access token.

play52:19

Open up test.py okay f dot read response

play52:23

request post access token response dot status.

play52:26

So it's not perfect.

play52:27

There's a few things that it probably shouldn't be doing here.

play52:31

but overall it gave us kind of some good starting code,

play52:35

or at least kind of prove the point that, hey, we can generate some code.

play52:38

It is attempting to call the API, it is calling it in the correct way.

play52:41

So yeah, I mean, I would call that a success.

play52:43

Obviously we can mess around with a bunch of different prompts.

play52:45

We can see what ones it works for. Once it doesn't work for.

play52:48

Remember we're using these local models which are quite small,

play52:51

which don't have the same capabilities of something like ChatGPT.

play52:54

If we did this at an enterprise level with the best hardware,

play52:57

with the best models, obviously we'd get some better results.

play53:01

But for now, I'm

play53:01

going to quit out of that model and I am going to wrap up the video here.

play53:05

All of this code will be available to download from the link in the description.

play53:09

A massive thank you to Lama index for sponsoring this video.

play53:12

I love working with them.

play53:13

Their framework is incredible and it really just opens my imagination

play53:18

and eyes to what's possible with these labs.

play53:20

I mean, look what we were able to create in about 30 or 45 minutes.

play53:24

Obviously I was walking you through it step by step.

play53:26

I was going slower than I would normally code this out.

play53:28

And we have an advanced AI agent that can do some code outputting and parsing.

play53:33

We're using multiple different agents locally,

play53:35

and we can continue to train these and do some really cool things.

play53:39

Anyways, if you guys want to see more videos

play53:41

like this, definitely leave a comment down below.

play53:43

Like the video?

play53:44

Subscribe to the channel and I will see you in the next one.

Rate This

5.0 / 5 (0 votes)

Related Tags
人工智能代码生成本地模型LMS应用Python编程VS Code教程数据解析RAG能力开源框架云端处理
Do you need a summary in English?