A. 最终效果

在这里插入图片描述

B. 文本分块代码

#%%
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter


pdf_path = '2023-LiuGuokai-Meas.pdf'
pdf_reader = PdfReader(pdf_path)
text = ""
for page in pdf_reader.pages:
    text += page.extract_text()

# split into chunks
text_splitter = CharacterTextSplitter(
    separator="\n",
    chunk_size=1000,
    chunk_overlap=200,
    length_function=len
    )

chunks = text_splitter.split_text(text)
      
# %%

C. 对话问答代码

# [嵌入模型 · Ollama 博客 - Ollama 中文](https://ollama.org.cn/blog/embedding-models)

# 步骤1:生成嵌入

import ollama
import chromadb

documents = [
  "Llamas are members of the camelid family meaning they're pretty closely related to vicuñas and camels",
  "Llamas were first domesticated and used as pack animals 4,000 to 5,000 years ago in the Peruvian highlands",
  "Llamas can grow as much as 6 feet tall though the average llama between 5 feet 6 inches and 5 feet 9 inches tall",
  "Llamas weigh between 280 and 450 pounds and can carry 25 to 30 percent of their body weight",
  "Llamas are vegetarians and have very efficient digestive systems",
  "Llamas live to be about 20 years old, though some only live for 15 years and others live to be 30 years old",
]

client = chromadb.Client()
collection = client.create_collection(name="docs")

# store each document in a vector embedding database
for i, d in enumerate(documents):
  response = ollama.embeddings(model="mxbai-embed-large", prompt=d)
  embedding = response["embedding"]
  collection.add(
    ids=[str(i)],
    embeddings=[embedding],
    documents=[d]
  )


  # 步骤2:检索

  # an example prompt
prompt = "What animals are llamas related to?"

# generate an embedding for the prompt and retrieve the most relevant doc
response = ollama.embeddings(
  prompt=prompt,
  model="mxbai-embed-large"
)
results = collection.query(
  query_embeddings=[response["embedding"]],
  n_results=1
)
data = results['documents'][0][0]

# 步骤3:生成
# generate a response combining the prompt and data we retrieved in step 2
output = ollama.generate(
  model="qwen2:7b",
  prompt=f"Using this data: {data}. Respond to this prompt: {prompt}"
)

print(output['response'])
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐