Build an AI code generator w/ RAG to write working LangChain
Summary
TLDR在这段视频中,演讲者分享了他们如何利用OpenAI和Lang Chain技术来提高编写代码样板的效率。他们首先尝试了GitHub Co-pilot,但发现其使用的API版本过时,无法满足需求。随后,演讲者通过检索增强生成(Retrieval Augmented Generation)和少量学习(Few Shot Learning)的方法,结合完整的工作代码示例和详细的描述,成功地训练了一个模型,使其能够生成所需的代码。他们还创建了一个向量数据库,用于存储和检索相关示例,并通过Pantic Schema生成了专家的描述。最终,他们展示了一个工作流程,该流程能够根据用户的请求生成准确的代码样板,大大简化了开发过程。演讲者还讨论了如何改进这个系统,使其更加用户友好,并减少手动干预。视频最后,演讲者提供了GitHub链接,供观众查看和使用他们的代码。
Takeaways
- 📝 作者花费了大量时间编写Lang chain的样板代码,用于部署和测试不同的原型和功能。
- 🔄 Lang chain的表达式语言在2022年8月才引入,因此许多模型的知识截止日期远早于此,缺少对Lang chain工作方式的了解。
- 💡 解决这个问题的常见方法是通过检索增强生成(retrieval augmented generation)和少量学习(few shot learning)结合的方法。
- 🚀 通过提供相关的工作代码示例和请求,可以引导模型输出对用户有用的代码。
- ❌ 直接使用GitHub Co-pilot生成Lang chain表达式语言的代码,结果并不理想,使用的是过时的API,没有使用Lang chain。
- 🤖 使用最新的模型进行实验,通过提供明确的指令和Lang chain表达式语言,可以生成更接近预期的代码。
- 🔍 作者采用了检索增强生成的方法,通过向量数据库检索最相关的数据,并将它们添加到上下文中。
- 📚 为了提供足够的上下文,作者生成了多个不同的示例,并为每个示例提供了详细的描述。
- 🔑 作者创建了一个简短的描述,用于驱动相似性搜索,以便在向量数据库中检索最相关的示例。
- 📈 通过设置一个模式,将代码、描述和标签存入we8数据库,作者能够构建一个用于检索的数据库。
- 🔗 最终,作者构建了一个工作流程,通过检索正确的上下文、将其附加到请求中,并通过模型生成代码,成功地解决了问题。
- 🚨 作者意识到需要持续更新示例,以跟上Lang chain的变化,以确保生成的代码保持最新。
Q & A
什么是Lang chain,它在脚本中提到的作用是什么?
-Lang chain是一种编程语言或框架,它在脚本中被用来生成代码原型和功能。脚本中提到,Lang chain不断变化,对于AI模型来说,理解和生成与之相关的工作代码是一个挑战。
脚本中提到的“boilerplate”指的是什么?
-在脚本中,'boilerplate'指的是编写代码时那些重复且标准化的代码段,它们通常在不同的项目中保持不变,但需要为每个项目重复编写。
脚本中提到的“retri retrieval augmented generation”和“few shot learning”是什么?
-这是两种AI技术。'retri retrieval augmented generation'是一种结合了检索和生成的方法,用于提高AI生成内容的相关性和准确性。'few shot learning'是一种机器学习方法,通过提供少量的示例来训练模型执行特定任务。
脚本中提到了使用Pantic来创建一个专家的schema,这是什么意思?
-在脚本中,使用Pantic创建一个专家的schema指的是定义一个专家的框架或模板,包括专家的名称、领域和描述。这个schema用于生成专家的简介,并可以被AI理解和使用。
脚本中提到了GitHub Co-pilot,它在这个过程中的角色是什么?
-GitHub Co-pilot是一个AI编程助手,它可以根据给定的上下文自动生成代码。在脚本中,尝试使用Co-pilot来生成Lang chain的表达式语言,但遇到了问题,因为它使用的是过时的API,并没有正确地使用Lang chain。
脚本中提到的“playground”是指什么?
-在脚本中,'playground'可能指的是一个实验性的环境或平台,用户可以在这个环境中测试和实验不同的AI模型和代码,以查看它们如何响应特定的输入或指令。
脚本中提到的“vector database”是用来做什么的?
-在脚本中,'vector database'被用来存储和检索数据。它通过将数据编码为向量的形式,来优化搜索和检索的效率,特别是在处理大量数据和需要快速检索相似内容时。
脚本中提到的“we8”是什么,它在构建系统时起到了什么作用?
-脚本中提到的'we8'可能是指一个工具或框架,用于帮助构建和管理工作流程。在脚本中,它被用来设置数据库的schema,存储代码示例、标签和描述,以便后续可以检索和使用。
脚本中提到的“prompt”在AI生成过程中扮演了什么角色?
-在AI生成过程中,'prompt'是给AI模型的输入或指令,它告诉模型需要生成什么样的输出。在脚本中,prompt结合了系统消息和用户请求,形成了一个完整的请求,AI模型根据这个请求生成代码或响应。
脚本中提到的“system message”和“human message”有什么不同?
-在脚本中,'system message'指的是由系统生成的消息,它包含了所有必要的上下文信息,如代码示例和描述。而'human message'则是用户的请求,它更直接地表达了用户想要AI做什么。两者结合形成了完整的prompt,供AI模型处理。
脚本中提到的“chain”具体指的是什么?
-在脚本中,'chain'指的是一系列自动化的步骤或过程,它们连接在一起以完成一个复杂的任务。在这种情况下,chain涉及到从数据库检索信息、创建prompt、查询AI模型以及解析输出等步骤。
脚本的作者提到了他们将分享GitHub上的代码,这有什么意义?
-这意味着脚本的作者愿意开放他们的工作,允许其他人查看、使用和提供反馈。这有助于社区学习和改进技术,同时也促进了透明度和协作。
Outlines
🤖 探索使用AI生成Lang Chain代码的挑战
第一段主要讲述了作者在编写Lang Chain代码时遇到的挑战,包括Lang Chain语言的快速变化和AI模型缺乏最新信息的问题。作者提出了使用检索增强生成和少量学习的方法,通过提供相关示例和请求来提高AI生成代码的准确性。同时,作者尝试了GitHub Copilot,但发现它使用的是过时的API,并没有使用Lang Chain。
🔍 利用检索增强生成和向量数据库优化AI代码生成
第二段详细描述了作者如何通过检索增强生成的方法,将大量数据输入向量数据库,找到最相关的数据并添加到上下文中,以提高AI生成代码的相关性。作者生成了多个不同的例子,并为每个例子提供了详细的描述,以帮助AI更好地理解和生成代码。此外,作者还介绍了如何使用we8设置向量数据库的架构,并填充了代码、描述和标签等信息。
📈 实现Lang Chain代码生成的自动化流程
第三段讨论了作者如何通过自动化流程生成Lang Chain代码。作者创建了一个链,该链从查询集合中检索正确的上下文,并将其与请求结合,创建一个包含所有代码示例和上下文的提示。然后,这个提示被用来查询模型并解析输出。作者展示了如何通过这个链生成格式化的专家信息,并且虽然有一点误解,但最终能够成功生成所需的代码。作者强调了持续更新示例以跟上Lang Chain变化的重要性,并提出了未来可能探索的改进方向,包括自动生成描述和优化请求过程。
Mindmap
Keywords
💡Lang chain
💡Boilerplate
💡Prototypes
💡Retrieval Augmented Generation
💡Few Shot Learning
💡Pantic
💡Vector Database
💡Schema
💡GitHub Co-pilot
💡API
💡Shorthand Description
Highlights
作者花费大量时间编写Lang chain的样板代码,用于部署和测试不同的原型和特性。
Lang chain的表达式语言在2022年8月才被引入,许多模型的知识截止日期远早于此,因此它们缺乏对Lang chain工作方式的内置知识。
通过结合检索增强生成和少量学习,可以通过上下文来解决模型知识更新的问题。
使用Pantic创建专家模式,并将其传递给Open AI解析,能够生成专家的名称和描述。
GitHub Co-pilot尝试使用过时的API来生成代码,但并未使用Lang chain,导致生成的代码无法工作。
通过在Playground中使用最新模型,可以生成工作代码,但需要提供足够的上下文。
作者通过生成多个不同的工作示例,并提供详细的描述,来增强检索增强生成的方法。
通过向量数据库检索最相关的示例,并将其添加到上下文中,可以更有效地控制示例的选择。
作者创建了一个简化的描述,用于驱动相似性搜索,并在向量数据库中设置了一个模式来存储示例。
通过查询数据库并审查距离,可以确认检索增强生成的方法是否有效。
作者展示了如何通过设置和检索来创建一个链,该链可以基于请求生成正确的上下文编码示例。
通过提供工作代码示例和上下文,可以生成准确的专家名称和描述,即使Open AI对Lang chain的了解有限。
作者提出了一个解决方案,通过少量学习和检索增强,可以使模型生成有用的代码,即使模型的知识库不包含Lang chain。
为了保持模型的更新,作者需要不断添加新的示例,以适应Lang chain的变化。
作者正在探索如何改进描述的自动生成,以及如何将长形式请求简化为模型能够理解的最佳结果。
作者考虑了如何使整个体验更加用户友好,例如通过自定义UI来生成系统提示,以提高控制和定制性。
作者成功地克服了Open AI缺乏对Lang chain足够了解的问题,并通过GitHub分享了代码,供他人使用和反馈。
Transcripts
hey so I spent a lot of time writing
boilerplate for Lang chain I'm kind of
deploying and testing a bunch of
different prototypes and features um as
a lot of the same setup for all of them
so my question that I posed was is there
a way to have open AI or any of the new
llms accomplish this for me in a way
that's much faster and easier the
problem is that Lang chain basically
keeps changing Lang chain expression
language was only introduced in in
August uh the knowledge cut off for a
lot of these best models is much earlier
than that so there's no knowledge of how
these things work actually just
basically built into their knowledge
base um so the common way of solving
that problem is through basically adding
it in context so using an approach of
retri retrieval augmented generation and
few shot learning if I combine both of
those and basically pass in relevant
examples of how it works a along with my
request can I get it to a point where
this code it
outputs actually works for
me so let's
see if I request please take a
string format it to send it to open AI
use pantic to create a schema an expert
with a name and a description pass it
open AI pars the
response here you can see I've given it
that
description it's able to spit out an
expert after it takes in a
field where this person is expert in
Quantum algorithms and Hardware
design um so that's perfect that's the
end goal here I'll walk through why it
doesn't really work the you know method
without having any of this augmentation
um and then why this one works and how I
got it to actually kind of be more of a
product so that I don't have to just
generate and pass in my own examples
because that wouldn't save me any time
at all
so really quickly if I were go to GitHub
co-pilot and ask it to do this exact
same task give me line chain expression
language used GPT 4106 preview generate
a bio for an expert in a given field
output a name and description 10 words
or less that is possible via pantic
write this for me in
Python so this is what it gave
me I don't think we want to spend too
much time on this but if I you know do
swap in my own key
here we can see you I kind of set up an
expert bio here using
pantic um it uses what looks to be an
outdated API
here it does some really bad
parsing it's got some validation built
in it doesn't use l chain at all uh but
it does kind of attempt the
process does not work so yeah
right from the get-go I can see it's
it's not only you know not using Lang
chain it is using open AI API directly
but it looks like an outdated
version so this approach is not going to
work um let's take approach number two
if I just go into the playground and
kind of you know experiment with this
myself and use the latest model you know
I don't necessarily have any control of
what GitHub is doing or what my prompt
looks like um but here let's just assume
I'm giv it now the instructions use Lang
chain specifically Lang chain expression
language just give me the working code
and then here I give it the exact same
instructions here's kind of a detailed
breakdown except a string format it to
send it to open AI here are the output
instructions using pantic schema pass it
to open AI parse the
response um if I do that now what it
gives me and I think I actually didn't
give it a long enough maximum length
here so got cut off but that's not going
to be our biggest concern
anyway so let's look here it kind of set
up a little bit of an issue here it
doesn't really describe what it actually
wants from from pantic uh it's creating
a prompt and just some wholly new
way it's parsing the raw
response and just creating some sort of
chain concept um none of this actually
uses line chain I don't think any of
these they probably even re I don't
think these are
real um this this one might be uh so it
just gave me gibberish this is not going
to be helpful it's not going to speed it
up at all so I showed you the final
solution I had which
is let's pass in some working code
examples give it that
context run this and it will actually
work so now the question is how do I
actually you know select and generate
good context for it uh so the method I
took is you know taking kind of standard
approach to retrieval augmented
generation where I'm feeding it a bunch
of data into a vector database finding
the most relevant ones adding them to
the context um but specifically here I'm
interested because I don't want to just
pass it kind of you know some window
around something that's interesting I
really want to pass through these entire
examples so what I did is I set out and
generated a bunch of different examples
this one is calling a function this one
is chaining together a bunch of
different chains here's an example of
yeah parsing a response
using pantic schema here's an example of
using retrievable augmented
Generation Um so for each of those I
give it a full working example I
describe it in some detail here about
what the process is actually doing so
that I can kind of you know interpret
and actually understand what's actually
going on under the hood here figure that
might be a little bit extra helpful um
but I don't actually think that at this
point yet it's still going to do a very
good job of giving me relevant results
uh the problem is if I you know talking
about a joke or something it might think
that the most relevant one is the setup
for the joke but you know now it's going
to be calling a function to do that
maybe I was just saying hey I'm going to
pass in something and you generate a and
return a joke um so what's interesting
here is actually not the content of what
I'm requesting or the content of what
it's accomplishing it's really just a
couple of like really basic elements so
I created this little kind of shorthand
up here that I used description of what
is each of these actually
doing creating multiple chains that work
together that's the valuable information
it's pulling out of this it's parsing
results as a string takes an object and
so each of these is actually kind of the
very shorthand description and what I'm
using to power the similarity search um
so once I've created a bunch of these
examples provided my little metal
description up here now all I need to do
is populate the vector database with
that information so I set up a schema
here using we8 which is very easy to
work with um where I basically you know
save the file name the tags I've added
the code that I've added and then the
description which is the meta
description I provided and this is
actually what I'm embedding everything
else is just here to be retrieved from
the database the description is actually
the thing that I'm looking at the
similarity so here is just basically how
do I populate the database I'm going
through and adding a bunch of this
information I need to kind of you know
extract the information from each file
so I can pass through just the code and
separately pass through the description
and the
tags and now you know run through
everything that I have in my example
directory and populate the
database and so I wanted to confirm that
it works I can just kind of quickly
query it here return all of those big
chunks of information where I'm just
kind of you know getting a similarity
like I said on the request that I'm
making and I can even review the
distance just to make sure that it's
working well uh so this is basically how
the querying Works um but when I
actually demonstrate
here how my chain
Works um what I'm trying to really
generate at the end is exactly this I've
got a system message that has the exact
right context coding examples I have the
user message of what they want to
produce and
ultimately give me some code at the end
so if I look at the chain that I created
that's exactly what it
is I do the setup and retrieval so this
F etches from my query collection based
on
my request that I'm going to pass
through and it passes through the
request so that now I can create a
prompt The Prompt is a system message
which is this system template I just
showed with a little bit of context
provided of all those code examples and
then it combines the human message the
human message is much easier it's just
the request I'm trying to make it now
queries the model that I'm interested in
and parses the output so that's really
all the chain is up to so now when I
pass through my request it'll stream
back something
here and if we look at the
output um it looks like it is going to
it may have slightly misinterpreted this
so it did a name of the expert as
opposed to the field that the expert's
in but let's just assume that's correct
and so it looks like it creates a
successful chain here where it takes the
expert name passes it through to a
prompt so the prompt now takes this
prompt template passes in the expert
name and format instructions that it
used from the parser that was correctly
set using pantic so up here it's got the
name which is the name of the expert and
the 10w or less description of the
expert and so now if I copy all of
this and run
it once again we should now have
formatted results um so this
is all it actually ends up being is this
you know simple chain that retrieves the
right context appends it to my message
and you know passes through the request
so now instead of writing all the boiler
plate I just need to think through what
is my request actually trying to
accomplish you know what am I trying to
structure here and it'll you know it
could output all of this for me you know
I can then go in and just kind of modify
and update the templates or something
but the boiler plate is done and so that
was really my end goal of get past the
problem where uh open AI doesn't
necessarily or you know whatever model
I'm using doesn't have the knowledge of
lank chain Can I with few shot learning
and the retrieval kind of Coes it into
having enough knowledge that it creates
working code and the answer so far seems
to be yes it's just going to be up to me
to
provide enough examples that it actually
can generate things uh so to keep this
up to date I'll have to keep adding more
examples as I said Lang chain keeps
changing so the examples may even become
out of date I'll need to update those
but this seems to work better because or
better than just kind of feeding in all
of the documentation and having it try
to understand those bits because you
know unless you have control of what
that context window looks like of what
it retrieves from each one it's either
going to pick pick out kind of like too
little too much um it might kind of get
similarity based on the wrong things and
so by structuring it this way by giving
and kind of controlling the balance of
examples that I'm providing it um it
does a much better job of doing kind of
my tailored use
case uh so this works today you know
things that I'm probably will be
exploring soon you know at what point
does it break down the number of
examples that I provide what's the cost
look like it's pretty small just to
provide a little bit more context um but
not negligable
um and then I think you know the the
part that kind of requires the manual
translation today is giving it a good
description long form and then
condensing that into this kind of short
form description so I think it'll make
sense here once I have at least enough
examples you know see if I can kind of
fine-tune something that would take this
automatically generate these
descriptions for me um to embed and then
at the same time how do I
then pass in the long form request to
what I actually want have it kind of
condense pull out only the you know
really required information make the
request and return the best results but
still kind of maintain this long form
request for what I actually want it to
generate um so I think that would be you
know a way to improve it obviously I
could make this entire experience way
more userfriendly or whatnot or maybe as
opposed to outputting code here I could
just output the messages and you know
just use my own sort of UI or even this
UI to kind of make the request and
customize things here um so maybe it
would just be a nice shorthand to
basically generate the system prompt on
my own and that would allow me to kind
of you know cut out examples I don't
want and you know have some extra
control over it but for today this seems
to work really well I can now generate
uh really quick
prototypes and finally overcome the
problem of open AI not having enough
knowledge to help at working Lang chain
um if you're interested I'll post a link
to the GitHub and you can go check out
the code you know use it for yourself
share some opinions but thanks for
thanks for watching hope you find that
helpful
Browse More Related Video
![](https://i.ytimg.com/vi/iJjSjmZnNlI/hq720.jpg)
The RIGHT WAY To Build AI Agents with CrewAI (BONUS: 100% Local)
![](https://i.ytimg.com/vi/JLmI0GJuGlY/hq720.jpg?sqp=-oaymwEmCIAKENAF8quKqQMa8AEB-AH-CYAC0AWKAgwIABABGGUgUihPMA8=&rs=AOn4CLDMU6k8dBTOIKV-VhORCAgtRf9cZA)
Python Advanced AI Agent Tutorial - LlamaIndex, Ollama and Multi-LLM!
![](https://i.ytimg.com/vi/3G8hsV-V-gQ/hq720.jpg)
ExcaliAI Enhanced: More Visual Thinking Power
![](https://i.ytimg.com/vi/X05ORyy7ZW4/hq720.jpg)
GPT-4o AI Agents: Easily Create Medical Research Agents (Praison AI)
![](https://i.ytimg.com/vi/ofuKRZLtOdY/hq720.jpg?v=66225339)
Nuxt Instance Unavailable - Async Code in Vue and Nuxt with the Composition API (+ raffle 🎁)
![](https://i.ytimg.com/vi/RO-jiWj0VK8/hq720.jpg)
Robocorp Beginners Tutorial - Getting Started with Robocorp RPA
5.0 / 5 (0 votes)