先下载ollama
在分别执行下面的命令
ollama run mxbai-embed-large
ollama run deepseek-r1
然后执行下面的代码:
点击查看全文
import ollama
from langchain_community.vectorstores import FAISS
import numpy as np
# Step 1: Generate embeddings using ollama
model = 'mxbai-embed-large'
prompt = '自然语言'
# Generate the embedding for the query sentence
query_embedding = ollama.embeddings(model=model, prompt=prompt)
# Ensure the embedding is a numpy array
query_embedding = np.array(query_embedding['embedding']) # Access the 'embedding' key if it's a dictionary
# Step 2: Create a FAISS vector store and add some example embeddings
# Example sentences (you can replace these with your own data)
sentences = [
"自处理人类语言。NLP涵盖了从文本分析、机器翻译到情感分析等多个任务。在文本分析中,NLP通过词汇分析、句法分析和语义理解等技术,将非结构化的自然语言转换为机器可以处理的形式。机器翻译则利用深度学习模型,如Transformer架构,实现不同语言之间的自动转换。情感分析则通过分析文本中的情感词汇和句子结构,判断文本的情感倾向。这些技术不仅提高了人机交互的效率,还在医疗、金融等多个领域发挥了重要作用。深度学习:推动NLP发展的核心技术然语言处理:人工智能的关键领域",
"Rayleigh scattering causes the blue color of the sky.",
"The ocean is blue because it reflects the sky.",
"Rainbows are formed by the refraction of light.",
"Rayleigh scattering is more effective for shorter wavelengths like blue."
]
# Generate embeddings for the example sentences
sentence_embeddings = []
for sentence in sentences:
embedding = ollama.embeddings(model=model, prompt=sentence)
sentence_embeddings.append(np.array(embedding['embedding'])) # Access the 'embedding' key if it's a dictionary
# Create a FAISS index
dimension = len(query_embedding) # Now this will work
faiss_index = FAISS.from_embeddings(
list(zip(sentences, sentence_embeddings)), # Pair sentences with their embeddings
embedding=np.zeros(dimension) # Dummy embedding function (not used directly)
)
# Step 3: Perform an approximate nearest neighbor search
# Query the FAISS index with the query embedding
distances, indices = faiss_index.index.search(np.array([query_embedding]), k=3) # Find top 3 matches
# Display the results
print("Top matching sentences:")
for i, idx in enumerate(indices[0]):
print(f"{i + 1}: {sentences[idx]} (Distance: {distances[0][i]})")最后请求ollma中的deepseek进行汇总回答即可 网友回复
有没有不依赖embedding向量的RAG技术?
有没有支持实时打断语音通话并后台帮你执行任何的ai模型?
开源ai大模型文件格式GGUF、MLX、Safetensors、 ONNX 有什么区别?
出海挣钱支付收款PayPal、Wise 、PingPong、Stripe如何选择?
如何实现类似google的图片隐形水印添加和识别技术?
linux上如何运行任意windows程序?
ai能写出比黑客还厉害的零日漏洞等攻击工具攻击任意软件系统工程?
js如何获取浏览器的音频上下文指纹、Canvas指纹、WebGL渲染特征?
为啥ai开始抛弃markdown文本,重新偏好html文本了?
网站有没有办法鉴别访问请求是由ai操控chrome-devtools-mcp发出的?


