AI教程 2025年01月17日
0 收藏 0 点赞 390 浏览 7818 个字
摘要 :

面向开发者的LLM入门教程-聊天Chat英文版: 英文版 1.复习 # 加载向量库,其中包含了所有课程材料的 Embedding。 from langchain.vectorstores import Chroma from lang……

哈喽!伙伴们,我是小智,你们的AI向导。欢迎来到每日的AI学习时间。今天,我们将一起深入AI的奇妙世界,探索“面向开发者的LLM入门教程-聊天Chat英文版”,并学会本篇文章中所讲的全部知识点。还是那句话“不必远征未知,只需唤醒你的潜能!”跟着小智的步伐,我们终将学有所成,学以致用,并发现自身的更多可能性。话不多说,现在就让我们开始这场激发潜能的AI学习之旅吧。

面向开发者的LLM入门教程-聊天Chat英文版

面向开发者的LLM入门教程-聊天Chat英文版:

英文版

1.复习

# 加载向量库,其中包含了所有课程材料的 Embedding。
from langchain.vectorstores import Chroma
from langchain.embeddings.openai import OpenAIEmbeddings
import panel as pn # GUI
# pn.extension()

persist_directory = ‘docs/chroma/cs229_lectures’
embedding = OpenAIEmbeddings()
vectordb = Chroma(persist_directory=persist_directory,
embedding_function=embedding)

# 对向量库进行基本的相似度搜索
question = “What are major topics for this class?”
docs = vectordb.similarity_search(question,k=3)
print(len(docs))

3

创建一个LLM

from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(model_name=llm_name, temperature=0)
llm.predict(“Hello world!”)

‘Hello there! How can I assist you today?’

创建一个基于模板的检索链

# 初始化一个 prompt 模板,创建一个检索 QA 链,然后传入一个问题并得到一个结果。
# 构建 prompt
from langchain.prompts import PromptTemplate
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(input_variables=[“context”,
“question”],template=template,)
# 运行 chain
from langchain.chains import RetrievalQA
question = “Is probability a class topic?”
qa_chain = RetrievalQA.from_chain_type(llm,
retriever=vectordb.as_retriever(),
return_source_documents=True,
chain_type_kwargs={“prompt”:
QA_CHAIN_PROMPT})
result = qa_chain({“query”: question})
print(result[“result”])

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!

2.记忆

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
memory_key=”chat_history”, # 与 prompt 的输入变量保持一致。
return_messages=True # 将以消息列表的形式返回聊天记录,而不是单个字符串
)

3.对话检索链

from langchain.chains import ConversationalRetrievalChain
retriever=vectordb.as_retriever()
qa = ConversationalRetrievalChain.from_llm(
llm,
retriever=retriever,
memory=memory
)
question = “Is probability a class topic?”
result = qa({“question”: question})
print(“Q: “, question)
print(“A: “, result[‘answer’])
question = “why are those prerequesites needed?”
result = qa({“question”: question})
print(“Q: “, question)
print(“A: “, result[‘answer’])

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 familiarity with basic
probability and statistics, and that most undergraduate statistics classes will
be more than enough.
Q: why are those prerequesites needed?
A: The reason for requiring familiarity with basic probability and statistics as
prerequisites for this class is that the class assumes that students already know
what random variables are, what expectation is, what a variance or a random
variable is. The class will not spend much time reviewing these concepts, so
students are expected to have a basic understanding of them before taking the
class.

4.定义一个聊天机器人

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter,
RecursiveCharacterTextSplitter
from langchain.vectorstores import DocArrayInMemorySearch
from langchain.document_loaders import TextLoader
from langchain.chains import RetrievalQA, ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from langchain.document_loaders import PyPDFLoader

def load_db(file, chain_type, k):
“””
该函数用于加载 PDF 文件,切分文档,生成文档的嵌入向量,创建向量数据库,定义检索器,并创建聊
天机器人实例。

参数:
file (str): 要加载的 PDF 文件路径。
chain_type (str): 链类型,用于指定聊天机器人的类型。
k (int): 在检索过程中,返回最相似的 k 个结果。

返回:

qa (ConversationalRetrievalChain): 创建的聊天机器人实例。
“””
# 载入文档
loader = PyPDFLoader(file)
documents = loader.load()
# 切分文档
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000,
chunk_overlap=150)
docs = text_splitter.split_documents(documents)
# 定义 Embeddings
embeddings = OpenAIEmbeddings()
# 根据数据创建向量数据库
db = DocArrayInMemorySearch.from_documents(docs, embeddings)
# 定义检索器
retriever = db.as_retriever(search_type=”similarity”, search_kwargs={“k”: k})
# 创建 chatbot 链,Memory 由外部管理
qa = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(model_name=llm_name, temperature=0),
chain_type=chain_type,
retriever=retriever,
return_source_documents=True,
return_generated_question=True,
)
return qa

import panel as pn
import param

# 用于存储聊天记录、回答、数据库查询和回复
class cbfs(param.Parameterized):
chat_history = param.List([])
answer = param.String(“”)
db_query = param.String(“”)
db_response = param.List([])

def __init__(self, **params):
super(cbfs, self).__init__( **params)
self.panels = []
self.loaded_file = “docs/cs229_lectures/MachineLearning-Lecture01.pdf”
self.qa = load_db(self.loaded_file,”stuff”, 4)

# 将文档加载到聊天机器人中
def call_load_db(self, count):
“””
count: 数量
“””
if count == 0 or file_input.value is None: # 初始化或未指定文件 :
return pn.pane.Markdown(f”Loaded File: {self.loaded_file}”)
else:
file_input.save(“temp.pdf”) # 本地副本
self.loaded_file = file_input.filename
button_load.button_style=”outline”
self.qa = load_db(“temp.pdf”, “stuff”, 4)
button_load.button_style=”solid”
self.clr_history()
return pn.pane.Markdown(f”Loaded File: {self.loaded_file}”)
# 处理对话链
def convchain(self, query):
“””
query: 用户的查询
“””
if not query:
return pn.WidgetBox(pn.Row(‘User:’, pn.pane.Markdown(“”, width=600)),
scroll=True)
result = self.qa({“question”: query, “chat_history”: self.chat_history})
self.chat_history.extend([(query, result[“answer”])])
self.db_query = result[“generated_question”]
self.db_response = result[“source_documents”]
self.answer = result[‘answer’]
self.panels.extend([
pn.Row(‘User:’, pn.pane.Markdown(query, width=600)),
pn.Row(‘ChatBot:’, pn.pane.Markdown(self.answer, width=600, style=
{‘background-color’: ‘#F6F6F6’}))
])
inp.value = ” # 清除时清除装载指示器
return pn.WidgetBox(*self.panels,scroll=True)
# 获取最后发送到数据库的问题
@param.depends(‘db_query ‘, )
def get_lquest(self):
if not self.db_query :
return pn.Column(
pn.Row(pn.pane.Markdown(f”Last question to DB:”, styles=
{‘background-color’: ‘#F6F6F6’})),
pn.Row(pn.pane.Str(“no DB accesses so far”))
)
return pn.Column(
pn.Row(pn.pane.Markdown(f”DB query:”, styles={‘background-color’:
‘#F6F6F6’})),
pn.pane.Str(self.db_query )
)

# 获取数据库返回的源文件
@param.depends(‘db_response’, )
def get_sources(self):
if not self.db_response:
return
rlist=[pn.Row(pn.pane.Markdown(f”Result of DB lookup:”, styles=
{‘background-color’: ‘#F6F6F6’}))]
for doc in self.db_response:
rlist.append(pn.Row(pn.pane.Str(doc)))
return pn.WidgetBox(*rlist, width=600, scroll=True)

# 获取当前聊天记录
@param.depends(‘convchain’, ‘clr_history’)
def get_chats(self):
if not self.chat_history:
return pn.WidgetBox(pn.Row(pn.pane.Str(“No History Yet”)), width=600,
scroll=True)
rlist=[pn.Row(pn.pane.Markdown(f”Current Chat History variable”, styles=
{‘background-color’: ‘#F6F6F6’}))]
for exchange in self.chat_history:
rlist.append(pn.Row(pn.pane.Str(exchange)))
return pn.WidgetBox(*rlist, width=600, scroll=True)
# 清除聊天记录
def clr_history(self,count=0):
self.chat_history = []
return

5.创建聊天机器人

# 初始化聊天机器人
cb = cbfs()
# 定义界面的小部件
file_input = pn.widgets.FileInput(accept=’.pdf’) # PDF 文件的文件输入小部件
button_load = pn.widgets.Button(name=”Load DB”, button_type=’primary’) # 加载数据库
的按钮
button_clearhistory = pn.widgets.Button(name=”Clear History”,
button_type=’warning’) # 清除聊天记录的按钮
button_clearhistory.on_click(cb.clr_history) # 将清除历史记录功能绑定到按钮上
inp = pn.widgets.TextInput( placeholder=’Enter text here…’) # 用于用户查询的文本输入
小部件
# 将加载数据库和对话的函数绑定到相应的部件上
bound_button_load = pn.bind(cb.call_load_db, button_load.param.clicks)
conversation = pn.bind(cb.convchain, inp)
jpg_pane = pn.pane.Image( ‘./img/convchain.jpg’)
# 使用 Panel 定义界面布局
tab1 = pn.Column(
pn.Row(inp),
pn.layout.Divider(),
pn.panel(conversation, loading_indicator=True, height=300),
pn.layout.Divider(),
)
tab2= pn.Column(
pn.panel(cb.get_lquest),
pn.layout.Divider(),
pn.panel(cb.get_sources ),
)
tab3= pn.Column(
pn.panel(cb.get_chats),
pn.layout.Divider(),
)
tab4=pn.Column(
pn.Row( file_input, button_load, bound_button_load),
pn.Row( button_clearhistory, pn.pane.Markdown(“Clears chat history. Can use
to start a new topic” )),
pn.layout.Divider(),
pn.Row(jpg_pane.clone(width=400))
)
# 将所有选项卡合并为一个仪表盘
dashboard = pn.Column(
pn.Row(pn.pane.Markdown(‘# ChatWithYourData_Bot’)),
pn.Tabs((‘Conversation’, tab1), (‘Database’, tab2), (‘Chat History’, tab3),
(‘Configure’, tab4))
)
dashboard

嘿,伙伴们,今天我们的AI探索之旅已经圆满结束。关于“面向开发者的LLM入门教程-聊天Chat英文版”的内容已经分享给大家了。感谢你们的陪伴,希望这次旅程让你对AI能够更了解、更喜欢。谨记,精准提问是解锁AI潜能的钥匙哦!如果有小伙伴想要了解学习更多的AI知识,请关注我们的官网“AI智研社”,保证让你收获满满呦!

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.ai-blog.cn/2862.html

相关推荐
01-27

Kimi神级写作指令-充当正则表达式生成器的提示词: 正则表达式是不是让你又爱又恨?想匹配特定文本…

427
01-27

Kimi神级写作指令-充当数学家的提示词: 数学计算是不是让你头大?复杂的表达式、繁琐的步骤,是不…

390
01-27

Kimi神级写作指令-充当全栈软件开发人员的提示词: 想开发一个Web应用程序,却不知道从何下手?或…

390
01-27

Kimi神级写作指令-充当对弈棋手的提示词: 喜欢下棋但找不到对手?或者想提升棋艺却苦于没有合适的…

390
01-27

Kimi神级写作指令-作为专业DBA的提示词: 数据库查询是不是让你头大?写SQL语句时总是担心性能不够…

390
01-27

Kimi神级写作指令-作为项目经理的提示词: 项目管理是不是让你头大?进度拖延、任务混乱、团队沟通…

390
01-27

Kimi神级写作指令-作为 IT 专家的提示词: 电脑蓝屏、软件崩溃、网络连接失败……这些技术问题是不是…

390
01-27

Kimi神级写作指令-担任 SVG 设计师的提示词: 你是不是经常需要一些简单的图像素材,但又不想打开…

390
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力原创内容

快速提升站内名气成为大牛

扫描二维码

手机访问本站