上一篇Llama-index的学习,我们通过查询数据,没有得到比较理想的结果,这篇学习,我们做一些改进,让Llama-index生成的结果更精准一些。
代码如下:
#导入库
from llama_index.readers.database import DatabaseReader
from sqlalchemy import create_engine
from llama_index.llms.ollama import Ollama
from llama_index.core.settings import Settings
from llama_index.embeddings.ollama import OllamaEmbedding
#设置模型
Settings.llm=Ollama(model="llama3")
emb=OllamaEmbedding(model_name="nomic-embed-text")
Settings.embed_model=emb
#从MySQL读数据
eng=create_engine("mysql+pymysql://root:12345678@localhost:3306/school")
reader=DatabaseReader(engine=eng)
docs=reader.load_data(query="SELECT * FROM school.students order by address desc")
#建index
from llama_index.core import VectorStoreIndex
index=VectorStoreIndex.from_documents(docs)
#下面我们使用自己定义的查询策略
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.core.postprocessor import SimilarityPostprocessor
#设置top_k
retriever=VectorIndexRetriever(index=index,similarity_top_k=10)
#创建查询引擎,similarity_cutoff为相似度阈值
q_eng=RetrieverQueryEngine(
retriever=retriever,
node_postprocessors=[SimilarityPostprocessor(similarity_cutoff=0.0)]
)
#查询
res=q_eng.query("列出家庭地址是柳州的学生信息。")
print(res)
结果如下:
According to the provided student information, the students with their family addresses in 柳州 are:
* id: 7, name: 伍军, age: 17, address: 柳州, sex: 女
* id: 9, name: 李春阳, age: 19, address: 柳州, sex: 男
* id: 1, name: 李和平, age: 19, address: 柳州, sex: 男
* id: 6, name: 王大海, age: 19, address: 柳州, sex: 男
* id: 4, name: 李同基, age: 19, address: 柳州, sex: 女
可以查到所有记录了。