向量嵌入与检索(八)-----一起学LangGraph构建agent,大模型入门到精通,收藏这篇就足够了!
在本教程中,我们将学习如何利用本地ollama的向量模型进行嵌入;以及利用内存数据库进行向量检索。同时附上了如何自定义向量嵌入的教程。
参考文档教程:
https://www.langchain.com.cn/docs/integrations/text_embedding/ollama/
在本教程中,我们将学习如何利用本地ollama的向量模型进行嵌入;以及利用内存数据库进行向量检索。同时附上了如何自定义向量嵌入的教程。
#安装依赖:
安装uv
pip install uv -i https://pypi.tuna.tsinghua.edu.cn/simple/
安装依赖包
uv pip install langgraph “langchain[openai]” langchain-community langchain-ollama -i https://pypi.tuna.tsinghua.edu.cn/simple/
使用 Ollama 进行嵌入
“”" 现在我们有了拆分的文档,我们可以将它们索引到用于语义搜索的向量存储中。 “”"
拉取向量的命令: ollama pull bge-m3:latest
“”"
# 1. 创建向量嵌入模型
from langchain_ollama import OllamaEmbeddings
local_embeddings = OllamaEmbeddings(
model="bge-m3:latest",
base_url="192.168.3.84:11434",
)
print(local_embeddings)
model='bge-m3:latest' base_url='192.168.3.84:11434' client_kwargs={} async_client_kwargs={} sync_client_kwargs={} mirostat=None mirostat_eta=None mirostat_tau=None num_ctx=None num_gpu=None keep_alive=None num_thread=None repeat_last_n=None repeat_penalty=None temperature=None stop=None tfs_z=None top_k=None top_p=None
# 2 单个嵌入示例
text1 = "LangChain is the framework for building context-aware reasoning applications"
single_vector = local_embeddings.embed_query(text1)
print(str(single_vector)[:100]) # Show the first 100 characters of the vector
[-0.01606138, -0.011700507, 0.0042311954, -0.011310199, -0.017729746, -0.051959366, 0.019131344, 0.0
#3 嵌入多个文本
text2 = (
"LangGraph is a library for building stateful, multi-actor applications with LLMs"
)
two_vectors = local_embeddings.embed_documents([text, text2])
for vector in two_vectors:
print(str(vector)[:100]) # Show the first 100 characters of the vector
[-0.01606138, -0.011700507, 0.0042311954, -0.011310199, -0.017729746, -0.051959366, 0.019131344, 0.0
[-0.07276119, 1.7207447e-05, -0.001866579, -0.03082587, -0.018602427, -0.008962815, -0.019794874, -0
# 4 使用内存数据库进行向量检索
# https://python.langchain.com/v0.2/api_reference/core/vectorstores/langchain_core.vectorstores.in_memory.InMemoryVectorStore.html
from langchain_core.vectorstores import InMemoryVectorStore
text1 = "LangChain is the framework for building context-aware reasoning applications"
text2 = "LangGraph is a library for building stateful, multi-actor applications with LLMs"
text3 = "我是AI小新,你的智能助手,很高兴为你服务。"
text4 = "我会根据你的需求,提供各种帮助和服务。"
vectorstore = InMemoryVectorStore.from_texts(
[text1,text2,text3,text4],
embedding=local_embeddings,
)
results = vectorstore.similarity_search_with_score(
query="你是谁", k=2,similarity_threshold=0.6
)
for doc, score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.502678] 我是AI小新,你的智能助手,很高兴为你服务。 [{}]
* [SIM=0.433895] 我会根据你的需求,提供各种帮助和服务。 [{}]
# Use as Retriever:
retriever = vectorstore.as_retriever(
search_type="mmr",
search_kwargs={"k":2, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("你是谁?")
for doc, score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.502678] 我是AI小新,你的智能助手,很高兴为你服务。 [{}]
* [SIM=0.433895] 我会根据你的需求,提供各种帮助和服务。 [{}]
# 如果内置的嵌入方法满足不了你,你可以自定义嵌入的方法;
"""
自定义嵌入
参考文档:
https://python.langchain.ac.cn/docs/how_to/custom_embeddings/
自定义嵌入
LangChain 与许多 第三方嵌入模型 集成。在本指南中,我们将向您展示如何创建自定义 Embedding 类,以防内置的类尚不存在。嵌入在自然语言处理应用中至关重要,因为它们将文本转换为算法可以理解的数字形式,从而实现广泛的应用,例如相似性搜索、文本分类和聚类。
使用标准的 Embeddings 接口实现嵌入,将允许您的嵌入在现有的 LangChain 抽象中使用(例如,作为 VectorStore 的驱动嵌入,或使用 CacheBackedEmbeddings 进行缓存)。
"""
#自定义嵌入 的使用 示例
from typing import List
from langchain_core.embeddings import Embeddings
classParrotLinkEmbeddings(Embeddings):
"""ParrotLink embedding model integration.
# TODO: Populate with relevant params.
Key init args — completion params:
model: str
Name of ParrotLink model to use.
See full list of supported init args and their descriptions in the params section.
# TODO: Replace with relevant init params.
Instantiate:
.. code-block:: python
from langchain_parrot_link import ParrotLinkEmbeddings
embed = ParrotLinkEmbeddings(
model="...",
# api_key="...",
# other params...
)
Embed single text:
.. code-block:: python
input_text = "The meaning of life is 42"
embed.embed_query(input_text)
.. code-block:: python
# TODO: Example output.
# TODO: Delete if token-level streaming isn't supported.
Embed multiple text:
.. code-block:: python
input_texts = ["Document 1...", "Document 2..."]
embed.embed_documents(input_texts)
.. code-block:: python
# TODO: Example output.
# TODO: Delete if native async isn't supported.
Async:
.. code-block:: python
await embed.aembed_query(input_text)
# multiple:
# await embed.aembed_documents(input_texts)
.. code-block:: python
# TODO: Example output.
"""
def__init__(self, model: str):
self.model = model
defembed_documents(self, texts: List[str]) -> List[List[float]]:
"""Embed search docs."""
return [[0.5, 0.6, 0.7] for _ in texts]
defembed_query(self, text: str) -> List[float]:
"""Embed query text."""
return self.embed_documents([text])[0]
# optional: add custom async implementations here
# you can also delete these, and the base class will
# use the default implementation, which calls the sync
# version in an async executor:
# async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
# """Asynchronous Embed search docs."""
# ...
# async def aembed_query(self, text: str) -> List[float]:
# """Asynchronous Embed query text."""
# ...
# 自定义嵌入的示例
custom_embeddings = ParrotLinkEmbeddings("自定义嵌入模型")
# 多个嵌入的示例
print(custom_embeddings.embed_documents(["我是AI小新", "我是自定义嵌入示例"]))
# 单个嵌入的示例
print(custom_embeddings.embed_query("你是谁"))
[[0.5, 0.6, 0.7], [0.5, 0.6, 0.7]]
[0.5, 0.6, 0.7]
下一节,会介绍完整的rag的应用的agent搭建;
完整代码访问github地址:
https://github.com/aixiaoxin123/langgraph_project
大模型算是目前当之无愧最火的一个方向了,算是新时代的风口!有小伙伴觉得,作为新领域、新方向
人才需求必然相当大,与之相应的人才缺乏、人才竞争自然也会更少,那转行去做大模型是不是一个更好的选择呢?是不是更好就业
呢?是不是就暂时能抵抗35岁中年危机呢?
答案当然是这样,大模型必然是新风口!
那如何学习大模型 ?
由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。但是具体到个人,只能说是:
最先掌握AI的人,将会比较晚掌握AI的人有竞争优势。
这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。
但现在很多想入行大模型的人苦于现在网上的大模型老课程老教材
,学也不是不学也不是,基于此我用做产品的心态来打磨这份大模型教程
,深挖痛点并持续修改了近100余次
后,终于把整个AI大模型的学习路线完善出来!
在这个版本当中:
您只需要听我讲,跟着我做即可,为了让学习的道路变得更简单,这份大模型路线+学习教程已经给大家整理并打包分享出来
, 😝有需要的小伙伴,可以 扫描下方二维码领取
🆓↓↓↓
一、大模型经典书籍(免费分享)
AI大模型已经成为了当今科技领域的一大热点,那以下这些大模型书籍就是非常不错的学习资源。
二、640套大模型报告(免费分享)
这套包含640份报告的合集,涵盖了大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。(几乎涵盖所有行业)
三、大模型系列视频教程(免费分享)
四、2025最新大模型学习路线(免费分享)
我们把学习路线分成L1到L4四个阶段,一步步带你从入门到进阶,从理论到实战。
L1阶段:启航篇丨极速破界AI新时代
L1阶段:了解大模型的基础知识,以及大模型在各个行业的应用和分析,学习理解大模型的核心原理、关键技术以及大模型应用场景。
L2阶段:攻坚篇丨RAG开发实战工坊
L2阶段:AI大模型RAG应用开发工程,主要学习RAG检索增强生成:包括Naive RAG、Advanced-RAG以及RAG性能评估,还有GraphRAG在内的多个RAG热门项目的分析。
L3阶段:跃迁篇丨Agent智能体架构设计
L3阶段:大模型Agent应用架构进阶实现,主要学习LangChain、 LIamaIndex框架,也会学习到AutoGPT、 MetaGPT等多Agent系统,打造Agent智能体。
L4阶段:精进篇丨模型微调与私有化部署
L4阶段:大模型的微调和私有化部署,更加深入的探讨Transformer架构,学习大模型的微调技术,利用DeepSpeed、Lamam Factory等工具快速进行模型微调,并通过Ollama、vLLM等推理部署框架,实现模型的快速部署。
L5阶段:专题集丨特训篇 【录播课】
全套的AI大模型学习资源
已经整理打包,有需要的小伙伴可以微信扫描下方二维码
,免费领取
更多推荐
所有评论(0)