Elasticsearch:使用在本地计算机上运行的 LLM 以及 Ollama 和 Langchain 构建 RAG 应用程序




from langchain.chat_models import ChatOpenAI
from langchain.llms import LLM
from langchain.prompts import StaticPromptTemplate
from langchain.vectorstores import VectorStore
from langchain.chains import ConversationChain
from langchain.chains.llm_chains import ConversationalLLM
 
# 初始化一个向OpenAI的GPT-3模型查询问题的ChatOpenAI实例
chat_model = ChatOpenAI(temperature=0)
 
# 创建一个向Ollama查询向量的LLM实例
ollama_llm = LLM(ollama_url="http://localhost:7500")
 
# 创建一个向Langchain的RAG查询向量的LLM实例
langchain_rag_llm = LLM(langchain_rag_url="http://localhost:7501")
 
# 创建一个向量库实例,用于存储向量
vectorstore = VectorStore.from_files("./data/vectors.pkl")
 
# 创建一个静态提示模板,用于构建对话提示
prompt_template = StaticPromptTemplate("You are a helpful AI assistant. You have a RAG file with information about a threat, vulnerability, or mitigation. You can answer factual questions about the content, classify items as either a threat, vulnerability, or mitigation, or list items of a specific type.")
 
# 创建一个基于LLM的对话链,使用向量库和提示模板
conversation_chain = ConversationalLLM(
    llms=[ollama_llm, langchain_rag_llm],
    vectorstore=vectorstore,
    prompt_template=prompt_template,
    chain_type="llm_only",
)
 
# 使用提供的ChatOpenAI实例和对话链实例
conversation_agent = ConversationChain(chat_model, conversation_chain)
 
# 示例对话
print(conversation_agent.converse("What is a vulnerability?"))

这个代码示例展示了如何初始化一个简单的对话代理,它使用了三种不同的LLM:一个是直接与OpenAI GPT-3模型交互的ChatOpenAI实例,另一个是通过Ollama服务查询向量的LLM实例,以及一个通过Langchain的RAG查询向量的LLM实例。同时,它使用了一个向量库来存储向量,并定义了一个静态提示模板来构建对话提示。最后,它创建了一个对话链实例,该实例可以接受用户输入并生成相应的回复。

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日