首页 > 教程攻略 > ai资讯 >掌控心理学:使用 Mistral-7B 和 LangChain 构建专家 RAG

掌控心理学:使用 Mistral-7B 和 LangChain 构建专家 RAG

来源:互联网 时间:2026-08-02 13:10:07

大语言模型(LLM)的时代已经全面到来,它们几乎成了我们数字生活中的标配——从写代码到研究菜谱,无所不包。但实话实说,即便是最顶尖的模型,也有几个绕不开的坎儿。

比如信息访问的边界:大语言模型无法快速触及训练集之外的数据。想象一下,你的人工智能助手给不出实质性答案,却只会滔滔不绝地告诉你“如何去找到答案”——这就有点尴尬了。再比如幻觉错误:模型偶尔会冒出看起来像是凭空杜撰的回应,让人既困惑又沮丧。

掌控心理学:使用 Mistral-7B 和 LangChain 构建专家 RAG

我们都经历过这样的时刻:模型因为训练数据有限而卡壳,就像一位擅长泛泛而谈的朋友,真要帮忙时就露了怯。拿 ChatGPT-3.5 来说,你问它一些关于性格改变的深刻问题,它给的回音常常像从励志小册子里复制粘贴的——套路很熟,但缺了真正的理解。

人工智能世界变化太快,每过一阵子就冒出一个带着“超强扩展训练数据”的新模型,让人眼花缭乱。但如果我们想要一个能像真正的心理学家那样提供建议的助理呢?如果能给大语言模型更多的“情报”,让它精准地定位那些深刻问题的答案,岂不是更好?

微调听起来像是个解决方案,但它并不是万灵丹,反而自带一堆风险和复杂性:模型漂移——用新数据不断微调可能导致原始性能偏离,结果难以预料;成本和复杂性——除了技术门槛,迭代微调需要大量计算资源和专业知识,意味着不菲的投资。

这就是检索增强生成(RAG)登场的原因。在这个系列里,我们会一步步展开探索:

  • 了解 RAG:

    检索增强生成模型到底是什么?它们如何工作?
  • 用 Mistral 7B 实现:

    通过 HuggingFace 和 LangChain 搭建 RAG 的分步指南。
  • 现实应用:

    通过实际例子,看看一个“小型 RAG”如何摇身变成能深入探讨心理问题的专家助理。

在深入技术细节之前,先对齐几个关键概念。

1. 文本嵌入

文本嵌入可以理解为“计算机脑中的文字魔法”——把单词、短语甚至整句话转化为数字,让机器理解它们的含义。好比把文字翻译成只有计算机才能读懂的密码。过去有 Word2Vec、GloVe 这些老方法,现在 BERT 等新方法玩得更溜。

举个例子:“国王” - “男人” + “女人”,结果居然能算出“女王”。这个数学技巧常常准确得惊人,足以说明文本嵌入在理解词语关系上有多出色。

文本嵌入一路进化,从基础方法到基于 Transformer 的模型,后者利用注意力机制来理解上下文,能处理更大的文本块。“国王”、“女王”、“男人”、“女人”之间的语义关系可以用二维图形直观展示。

如果想深入了解,可以看看 OpenAI 和 Cohere 的相关资源。

2. 矢量数据库

处理海量复杂数据有多头疼?传统数据库往往力不从心。矢量数据库就像数据世界的超级英雄,轻松拿下那些高维度的数据点。

想象一下:数据点散布在网格上,每个点由独特的属性表示——这有点像根据口味而不是颜色或形状来给水果分类。区别在于,文本的属性是用文本嵌入(向量)来表征的。

矢量数据库通过余弦相似度等技巧来计算数据点之间的相似度,就像 Google 根据匹配程度给你展示搜索结果一样。它擅长理解混乱的数据,帮你快速找到想要的东西。

(图片来源:KDnuggets)

有兴趣的话,可以查看更多关于矢量数据库的资料。

3. RAG 揭幕

到底什么是 RAG?检索增强生成模型就像一座桥梁,给大语言模型提供外部数据访问权限,让它能生成富含上下文洞察的回答。无论是引用最新新闻、讲座记录,还是心理学文献,RAG 都能让模型带着新深度和相关性来回应。

它怎么工作的?把 RAG 想象成配备了向量搜索机制的大语言模型。过程简化下来就是:

  • 数据库设置:

    用编码文档填充矢量数据库。
  • 查询编码:

    用句子转换器把输入查询变成向量。
  • 相关上下文检索:

    根据查询从数据库里检索相关上下文。
  • 提示大语言模型:

    结合检索到的上下文和原始查询,指导模型生成富含上下文的回答。

高级 RAG 架构图如下:

(图略)

4. 心理专家 RAG

在打开代码编辑器之前,先聊聊为什么 RAG 在心理学领域潜力巨大。想象一个数字伴侣,不仅能理解你的问题,还能从庞大的心理学文献库中提取相关见解。有了 RAG,只需要敲几下键盘,就能获得个性化的心理健康支持和共情回应。下面直接进入代码部分。

4.1 入门:安装和设置库

先把依赖搞定。我们需要一堆库来启动 RAG,直接用 pip install 一条命令搞定:

!pip install -q torch datasets
!pip install -q accelerate==0.21.0 \
                peft==0.4.0 \
                bitsandbytes==0.40.2 \
                trl==0.4.7
!pip install -q langchain \
                sentence_transformers \
                faiss-cpu \
                pypdf
!pip install -U transformers

4.2 导入行业工具

库到位了,引入重型武器:PyTorch、Transformers 和一点 LangChain 魔法。

import os
import torch
from transformers import (
  AutoTokenizer,
  AutoModelForCausalLM,
  BitsAndBytesConfig,
  pipeline
)

from transformers import BitsAndBytesConfig

from langchain.text_splitter import CharacterTextSplitter

from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS

from langchain.prompts import PromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain.llms import HuggingFacePipeline
from langchain.chains import LLMChain
import transformers
import nest_asyncio

nest_asyncio.apply()
from tqdm.notebook import tqdm
from langchain_community.document_loaders import PyPDFLoader

4.3 加载模型和分词器

RAG 应用的灵魂——Mistral-7B。现在加载模型和分词器,高效地塞进单个 GPU。所有操作都是为了优化性能。

# Load Tokenizer
model_name='mistralai/Mistral-7B-Instruct-v0.2'

tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
print("Tokenizer loaded !!")
## bitsandbytes parameters
# Activate 4-bit precision base model loading
use_4bit = True

# Compute dtype for 4-bit base models
bnb_4bit_compute_dtype = "float16"

# Quantization type (fp4 or nf4)
bnb_4bit_quant_type = "nf4"

# Activate nested quantization for 4-bit base models (double quantization)
use_nested_quant = False

compute_dtype = getattr(torch, bnb_4bit_compute_dtype)

bnb_config = BitsAndBytesConfig(
    load_in_4bit=use_4bit,
    bnb_4bit_quant_type=bnb_4bit_quant_type,
    bnb_4bit_compute_dtype=compute_dtype,
    bnb_4bit_use_double_quant=use_nested_quant,
)

# Check GPU compatibility with bfloat16
if compute_dtype == torch.float16 and use_4bit:
    major, _ = torch.cuda.get_device_capability()
    if major >= 8:
        print("=" * 80)
        print("Your GPU supports bfloat16: accelerate training with bf16=True")
        print("=" * 80)
# Load Model
model = AutoModelForCausalLM.from_pretrained(
        model_name,
            quantization_config=bnb_config,
        )

先用一个基础问题测试普通模型:

def get_vanilla_response(question, tokenizer, model):
  inputs = tokenizer.encode_plus(f"[INST] {question} [/INST]", return_tensors="pt")['input_ids'].to('cuda')

  generated_ids = model.generate(inputs,
                                max_new_tokens=1000,
                                do_sample=True)
  output = tokenizer.batch_decode(generated_ids)
  return output[0]

question = "What changes do I need in my personality to make friends easily?"
print(get_vanilla_response(question = question, tokenizer = tokenizer, model = model))

以下是得到的回应(每次运行可能略有不同):

Question:

What changes do I need in my personality to make friends easily?

Response:

Making friends can be a challenge for some people, and it often requires some adjustments to our personality traits and social beha viors. Here are some suggestions for changes that may help you make friends more easily:

  1. Be open and approachable: People are more likely to be drawn to those who are friendly and welcoming. Smile often, make eye contact, and show genuine interest in others.

  2. Be a good listener: Most people love to talk about themselves and share their stories. Be an attentive listener and show empathy when others speak. This will make them feel valued and appreciated.

  3. Be patient and persistent: Making friends takes time and effort. Don’t get discouraged if it takes a while to build a strong connection. Keep putting yourself out there and be persistent.

  4. Be optimistic: Ha ving a positive attitude and a good sense of humor can help attract people to you. People are more likely to want to be around those who make them feel good.

  5. Be reliable and consistent: People value those who are dependable. Keep your commitments and be consistent in your actions and beha viors.

  6. Be authentic to yourself: Don’t try to be someone you’re not just to fit in. People are drawn to authenticity and genuine personality. Be yourself and let your unique qualities shine through.

  7. Be willing to try new things: Being open to new experiences and trying new things can help broaden your horizons and expand your social circle. This can lead to meeting new people and making new friends. keep in mind that making friends is a two-way street, so be open to forming genuine connections and being a good friend in return.

这个答案不错,但依然偏笼统,缺少人情味。接下来用 RAG 来改进。

4.4 打造 RAG 管道

重头戏来了——把所有部件拼在一起,搭出 RAG 管道。用 LangChain 来无缝协调检索和生成过程。

5. 创建文本生成管道

# Create a text generation pipeline
text_generation_pipeline = pipeline(
    model=model,
    tokenizer=tokenizer,
    task="text-generation",
    temperature=0.2,
    repetition_penalty=1.1,
    return_full_text=True,
    max_new_tokens=10000,
)

mistral_llm = HuggingFacePipeline(pipeline=text_generation_pipeline)

接下来看看我们用来打造心理助手的数据——几本心理学经典书籍:

  • 丹尼尔·卡尼曼《思考,快与慢》——两种思维系统、认知偏见与决策过程。
  • 罗伯特·B·西奥迪尼《影响力》——说服心理学,影响行为的触发因素。
  • 丹·艾瑞利《可预测的非理性》——非理性行为与决策。
  • 丹尼尔·戈尔曼《情商》——情商在理解自己与他人中的重要性。
  • 艾略特·阿伦森《社会动物》——社会心理学广泛主题。
  • 理查德·H·塞勒 & 卡斯·R·桑斯坦《助推》——微妙的助推如何影响决策。
# add Book paths from Google Drive
pdf_paths = ['/content/drive/MyDrive/Blogs/psychology-gpt/Dan Ariely - Predictably Irrational_ The Hidden Forces That Shape Our Decisions-HarperCollins (2008).pdf',
             '/content/drive/MyDrive/Blogs/psychology-gpt/Daniel Goleman - Emotional Intelligence_ Why it Can Matter More Than IQ-Bloomsbury (2009).pdf',
             '/content/drive/MyDrive/Blogs/psychology-gpt/Daniel Kahneman - Thinking, Fast and Slow  .pdf',
             '/content/drive/MyDrive/Blogs/psychology-gpt/Elliot Aronson - The Social Animal. Tenth Edition-Worth Publishers (2007).pdf',
             '/content/drive/MyDrive/Blogs/psychology-gpt/Richard H. Thaler, Prof. Cass R. Sunstein - Nudge_ Improving Decisions About Health, Wealth, and Happiness-Yale University Press (2008).pdf',
             '/content/drive/MyDrive/Blogs/psychology-gpt/Robert B. Cialdini - Influence_ The Psychology of Persuasion (Collins Business Essentials) (2007).pdf']
docs = []
for pdf_path in tqdm(pdf_paths):
  loader = PyPDFLoader(pdf_path)
  pages = loader.load_and_split()
  docs = docs+pages[8:-10]

# Load chunked documents into the FAISS index
db = FAISS.from_documents(docs,HuggingFaceEmbeddings(model_name='BAAI/bge-base-en-v1.5'))
retriever = db.as_retriever()
# Create prompt template
prompt_template = """
### [INST] Instruction: Answer the question based on your human psychology knowledge, you can also use this auxillary knowledge to help:

{context}

### QUESTION:
{question} [/INST]
 """

# Create prompt from prompt template
prompt = PromptTemplate(
    input_variables=["context", "question"],
    template=prompt_template,
)

# Create llm chain
llm_chain = LLMChain(llm=mistral_llm, prompt=prompt)

rag_chain = (
 {"context": retriever, "question": RunnablePassthrough()}
    | llm_chain
)

5.2 测试我们的 RAG

设置完毕,开始测试。提几个发人深省的问题,看看效果。

# testing on the same question for a comparison
question = "What changes do I need in my personality to make friends easily?"
op = rag_chain.invoke(question)
print(op['text'])

Question: What changes do I need in my personality to make friends easily?

Response:

Based on the information provided in the documents, here are some suggestions for developing social skills to make friends more easily:

  1. Be attuned to others’ needs and feelings: Strive for a balance between being true to yourself and being sensitive to others’ needs. This will help you a void becoming a social chameleon, who may win popularity but sacrifices true satisfaction.

  2. Honesty and authenticity: Be genuine in your interactions with others. People appreciate authenticity and are more likely to form lasting relationships with those who are true to themselves.

  3. Active listening: Pay attention to what others are saying and respond appropriately. This shows respect and interest in their thoughts and experiences.

  4. Empathy: Try to understand others’ perspectives and emotions. This will help you connect with them on a deeper level and build stronger relationships.

  5. Adaptability: Be flexible in your interactions with others. This means being able to adjust your beha vior to fit different social situations and adapt to the needs and preferences of the people around you.

  6. Positive body language: Use open and positive body language to convey warmth and approachability. This includes maintaining eye contact, smiling, and using appropriate facial expressions.

  7. Approachability: Make yourself a vailable and approachable to others. This means being open to new experiences and meeting new people, and creating opportunities for social interaction.

  8. Practice active engagement: Engage fully in conversations and activities with others. This means being present in the moment and actively participating in the interaction, rather than just going through the motions.

By focusing on these areas, you can develop the social skills necessary to make friends more easily and build strong, meaningful relationships.

6. 比较与好处

对比两个答案,能看出它们在如何提升交友社交技能的建议上既有相似之处,也有明显差异。

基于 RAG 的答案像是一位朋友为你提供了一份完整的“拆解指南”——细致、具体,每项技能都配有例子。全是实用技巧和策略,给你清晰的步骤去提升社交水平。相比之下,普通答案更宽泛,偏哲学化。

本质上,RAG 通过利用外部数据源增强了基础语言模型的能力,提升了回应的质量和深度。对于需要特定领域专业知识的任务——比如提供社交技能和心理学建议——RAG 显得特别有价值。

想继续探索的读者,可以拿到入门笔记本自行实验。