面向开发者的LLM入门教程-问答英文版: 英文版 1.加载向量数据库 from langchain.vectorstores import Chroma from langchain.embeddings.openai import OpenAIEmbeddin……
哈喽!伙伴们,我是小智,你们的AI向导。欢迎来到每日的AI学习时间。今天,我们将一起深入AI的奇妙世界,探索“面向开发者的LLM入门教程-问答英文版”,并学会本篇文章中所讲的全部知识点。还是那句话“不必远征未知,只需唤醒你的潜能!”跟着小智的步伐,我们终将学有所成,学以致用,并发现自身的更多可能性。话不多说,现在就让我们开始这场激发潜能的AI学习之旅吧。
面向开发者的LLM入门教程-问答英文版:
英文版
1.加载向量数据库
from langchain.vectorstores import Chroma
from langchain.embeddings.openai import OpenAIEmbeddings
persist_directory = ‘docs/chroma/cs229_lectures/’
embedding = OpenAIEmbeddings()
vectordb = Chroma(persist_directory=persist_directory,
embedding_function=embedding)print(vectordb._collection.count())
209
向量检索
question = “What are major topics for this class?”
docs = vectordb.similarity_search(question,k=3)
print(len(docs))
3
2.构造检索式问答链
# 使用 ChatGPT3.5,温度设置为0
from langchain.chat_models import ChatOpenAI# 导入检索式问答链
from langchain.chains import RetrievalQAllm = ChatOpenAI(model_name=llm_name, temperature=0)
# 声明一个检索式问答链
qa_chain = RetrievalQA.from_chain_type(
llm,
retriever=vectordb.as_retriever()
)# 可以以该方式进行检索问答
question = “What are major topics for this class?”
result = qa_chain({“query”: question})
print(result[“result”])
The context does not provide a clear answer to this question.
3.基于模板的检索式问答链
from langchain.prompts import PromptTemplate
# Build prompt
template = “””Use the following pieces of context to answer the question at the
end. If you don’t know the answer, just say that you don’t know, don’t try to
make up an answer. Use three sentences maximum. Keep the answer as concise as
possible. Always say “thanks for asking!” at the end of the answer.
{context}
Question: {question}
Helpful Answer:”””
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
# Run chain
qa_chain = RetrievalQA.from_chain_type(
llm,
retriever=vectordb.as_retriever(),
return_source_documents=True,
chain_type_kwargs={“prompt”: QA_CHAIN_PROMPT}
)question = “Is probability a class topic?”
result = qa_chain({“query”: question})
print(“ANswering:”)
print(result[“result”])
print(“Source document: “)
print(result[“source_documents”][0])
ANswering:
Yes, probability is assumed to be a prerequisite for this class. The instructor
assumes familiarity with basic probability and statistics, and will go over some
of the prerequisites in the discussion sections as a refresher course. Thanks for
asking!
Source document:
page_content=”of this class will not be very program ming intensive, although we
will do some nprogramming, mostly in either MATLAB or Octa ve. I’ll say a bit
more about that later. nI also assume familiarity with basic proba bility and
statistics. So most undergraduate nstatistics class, like Stat 116 taught here
at Stanford, will be more than enough. I’m gonna nassume all of you know what ra
ndom variables are, that all of you know what expectation nis, what a variance
or a random variable is. And in case of some of you, it’s been a while nsince
you’ve seen some of this material. At some of the discussion sections, we’ll
actually ngo over some of the prerequisites, sort of as a refresher course
under prerequisite class. nI’ll say a bit more about that later as well.
nLastly, I also assume familiarity with basi c linear algebra. And again, most
undergraduate nlinear algebra courses are more than enough. So if you’ve taken
courses like Math 51, n103, Math 113 or CS205 at Stanford, that would be more
than enough. Basically, I’m ngonna assume that all of you know what matrix es
and vectors are, that you know how to nmultiply matrices and vectors and
multiply matrix and matrices, that you know what a matrix inverse is. If you know
what an eigenvect or of a matrix is, that’d be even better. nBut if you don’t
quite know or if you’re not qu ite sure, that’s fine, too. We’ll go over it in
nthe review sections.” metadata={‘source’: ‘docs/cs229_lectures/MachineLearningLecture01.pdf’, ‘page’: 4}
4.基于MapReduce的检索式问答链
qa_chain_mr = RetrievalQA.from_chain_type(
llm,
retriever=vectordb.as_retriever(),
chain_type=”map_reduce”
)question = “Is probability a class topic?”
result = qa_chain_mr({“query”: question})print(result[“result”])
There is no clear answer to this question based on the given portion of the
document. The document mentions statistics and algebra as topics that may be
covered, but it does not explicitly state whether probability is a class topic.
5.基于Refine的检索式问答链
qa_chain_mr = RetrievalQA.from_chain_type(
llm,
retriever=vectordb.as_retriever(),
chain_type=”refine”
)
question = “Is probability a class topic?”
result = qa_chain_mr({“query”: question})
print(result[“result”])
The topic of probability is assumed to be a prerequisite and not a main topic of
the class. The instructor assumes that students are familiar with basic
probability and statistics, including random variables, expectation, variance,
and basic linear algebra. The class will cover learning algorithms, including a
discussion on overfitting and the probabilistic interpretation of linear
regression. The instructor will use this probabilistic interpretation to derive
the first classification algorithm, which will be discussed in the class. The
classification problem involves predicting a discrete value, such as in medical
diagnosis or housing sales. The class will not be very programming-intensive, but
some programming will be done in MATLAB or Octave. The instructor will provide a
refresher course on the prerequisites in some of the discussion sections.
Additionally, the discussion sections will be used to cover extensions for the
material taught in the main lectures, as machine learning is a vast field with
many topics to explore.
6.实验
qa_chain = RetrievalQA.from_chain_type(
llm,
retriever=vectordb.as_retriever()
)
question = “Is probability a class topic?”
result = qa_chain({“query”: question})
print(“Q: “, question)
print(“A: “, result[“result”])
question = “why are those prerequesites needed?”
result = qa_chain({“query”: question})
print(“Q: “, question)
print(“A: “, result[“result”])
Q: Is probability a class topic?
A: Yes, probability is a topic that will be assumed to be familiar to students
in this class. The instructor assumes that students have a basic understanding of
probability and statistics, and will go over some of the prerequisites as a
refresher course in the discussion sections.
Q: why are those prerequesites needed?
A: The prerequisites are needed because in this class, the instructor assumes
that all students have a basic knowledge of computer science and knowledge of
basic computer skills and principles. This includes knowledge of big-O notation
and linear algebra, which are important concepts in machine learning. Without
this basic knowledge, it may be difficult for students to understand the material
covered in the class.
嘿,伙伴们,今天我们的AI探索之旅已经圆满结束。关于“面向开发者的LLM入门教程-问答英文版”的内容已经分享给大家了。感谢你们的陪伴,希望这次旅程让你对AI能够更了解、更喜欢。谨记,精准提问是解锁AI潜能的钥匙哦!如果有小伙伴想要了解学习更多的AI知识,请关注我们的官网“AI智研社”,保证让你收获满满呦!
还没有评论呢,快来抢沙发~