RGA DEMO 下部

复制代码
#加载llm模型通过ollama最好别用ollama我是没经济条件
from langchain_community.llms import Ollama
llm = Ollama(model="qwen1_5-4b-chat-q2_k")


#pip install langchain_ollama -i https://pypi.tuna.tsinghua.edu.cn/simple
#OllamaEmbeddings 要写地址本地也要写, Ollama_llm本地的不用写地址
from langchain_ollama import OllamaEmbeddings
embeddings = OllamaEmbeddings(model="lrs33/bce-embedding-base_v1",base_url="http://localhost:11434/")


#pip install -qU langchain-postgres -i https://pypi.tuna.tsinghua.edu.cn/simple
from langchain_postgres import PGVector
from langchain_postgres.vectorstores import PGVector

CONNECTION_STRING = "postgresql+psycopg2://postgres:qaz142434@192.168.159.130:5432/postgres"
# 矢量存储名
COLLECTION_NAME = "yaofang_test"
# 连接数据库创建"客户端"
vectorstore = PGVector(
    collection_name=COLLECTION_NAME,
    connection=CONNECTION_STRING,
    embeddings=embeddings,
)

#设置检索条件
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 6})
# 一个对话模板,内含2个变量context和question
template = """根据以下上下文回答问题:
{context}
回答: {question}
"""
# 基于模板生成提示
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template(template)


# 生成输出解析器
from langchain_core.output_parsers import StrOutputParser
output_parser = StrOutputParser()

# 将检索索引器和输入内容(问题)生成检索
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
setup_and_retrieval = RunnableParallel(
    {"context": retriever, "question": RunnablePassthrough()}
)

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

#流式输出
for chunk in rag_chain.stream("java 共有多少种设计模式"):
    print(chunk, end="", flush=True)

输出结果:

此代码流程是:创建向量数据库客户端,连接对应表,设置检索问题向量距离最近的top回调数据,大语言模型推理答案流式输出。

结合这上下部简单的RGA demo 就完成了,当然距离真正的RGA 差十万八千里后续会基于这个骨架开发进一步功能至少提供个UI界面正在考虑使用javaAPI模式,还是python gradio方式好纠结。

相关推荐
PyHaVolask19 分钟前
Python 爬虫进阶:直接请求 JSON 接口与开发者工具使用
爬虫·python·请求头·反爬·json接口·chrome开发者工具
larance22 分钟前
安装dify的几个问题
python
2301_7735536224 分钟前
CSS如何对用户访问过的链接进行降级颜色处理_使用-visited伪类改变颜色
jvm·数据库·python
2301_8152795232 分钟前
Golang怎么理解Go的sync.Pool底层_Golang如何理解Pool的本地缓存和GC清理机制【详解】
jvm·数据库·python
2301_7641505632 分钟前
MySQL迁移过程如何避免数据不一致_利用强一致性备份方案
jvm·数据库·python
m0_7164300735 分钟前
Redis如何处理预热失效引起的开局雪崩
jvm·数据库·python
m0_3776182338 分钟前
c++文件锁使用方法 c++如何实现多进程文件同步
jvm·数据库·python
gmaajt42 分钟前
mysql多字段搜索如何设计组合索引_mysql索引查询加速
jvm·数据库·python
2301_7775993743 分钟前
MySQL如何快速排查慢查询安全隐患_分析slow_query_log进行优化
jvm·数据库·python
m0_747854521 小时前
如何检测受保护链接(如 Twitter)的可访问性
jvm·数据库·python