开源模型破局OpenAI服务限制,15分钟灵活搭建RAG和Agent应用

简介: 今天,我们做了两个实验,目标在15分钟内,完成下载社区的开源模型,部署成API,替换LlamaIndex中RAG和LangChain中OpenAI接口Agent的最佳实践,并取得符合预期的结果。

实验一

实验目标:Qwen2+Ollama+LlamaIndex实现RAG应用

实验时长:15分钟

运行设备:Mac,CPU,GPU均可

环境安装:****

复制代码
pip install llama-index llama_index.llms.ollama llama-index-embeddings-huggingface modelscope

复制模型路径,创建名为"ModelFile"的meta文件,内容如下:

复制代码
FROM /mnt/workspace/qwen2-7b-instruct-q5_k_m.gguf
# set the temperature to 0.7 [higher is more creative, lower is more coherent]
PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER repeat_penalty 1.05
TEMPLATE """{{ if and .First .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}<|im_start|>user
{{ .Prompt }}<|im_end|>
<|im_start|>assistant
{{ .Response }}"""
# set the system message
SYSTEM """
You are a helpful assistant.
"""

使用ollama create命令创建自定义模型并运行

复制代码
ollama create myqwen2 --file ./ModelFile
ollama run myqwen2

然后运行如下RAG代码:

复制代码
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.core.embeddings import resolve_embed_model
from llama_index.llms.ollama import Ollama
import sys
# load doc qwen2 readme
documents = SimpleDirectoryReader("/mnt/workspace/content").load_data()
# bge embedding model
Settings.embed_model = resolve_embed_model("local:/mnt/workspace/bge-base-en-v1.5")
# ollama
Settings.llm = Ollama(model="myqwen2", request_timeout=30.0)
# create index
index = VectorStoreIndex.from_documents(documents)
# Either way we can now query the index
query_engine = index.as_query_engine()
response = query_engine.query("What is the maximum context length supported by Qwen2?")
print(response)

Output

实验二

实验目标:Qwen2+Ollama+Langchain实现Agent应用

实验时长:15分钟

运行设备:Mac,CPU,GPU均可

环境安装:

****

复制代码
pip install langchain_openai langchain langchain_experimental

模型下载:

使用modelscope-cli下载qwen2模型:

复制代码
modelscope download --model=qwen/Qwen2-7B-Instruct-GGUF --local_dir . qwen2-7b-instruct-q5_k_m.gguf

运行Qwen2(本地文件,也可以直接用ollama hub)

复制模型路径,创建名为"ModelFile"的meta文件,内容如下:

复制代码
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
from langchain_openai import ChatOpenAI
import pandas as pd
# 下载csv文件
df = pd.read_csv(
    "https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv"
)
agent = create_pandas_dataframe_agent(
    ChatOpenAI(api_key='ollama', # ollama 不需要使用真实的API key
        base_url = 'http://127.0.0.1:11434/v1',
        model="myqwen2"),
    df,
    verbose=True,
    allow_dangerous_code=True
)
agent.invoke("how many rows are there?")

使用ollama create命令创建自定义模型并运行

复制代码
ollama create myqwen2 --file ./ModelFile
ollama run myqwen2

运行如下function call 代码(代码解释器):

复制代码
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
from langchain_openai import ChatOpenAI
import pandas as pd
# 下载csv文件
df = pd.read_csv(
    "https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv"
)
agent = create_pandas_dataframe_agent(
    ChatOpenAI(api_key='ollama', # ollama 不需要使用真实的API key
        base_url = 'http://127.0.0.1:11434/v1',
        model="myqwen2"),
    df,
    verbose=True,
    allow_dangerous_code=True
)
agent.invoke("how many rows are there?")

Output

相关推荐
潇楠Web3哨兵5 分钟前
《「潇楠WEB3哨兵」Agent 全栈架构:从记忆系统到技能扩展,桌面端 AI 投研助手的完整技术实现》
web3·agent
Baihai_IDP17 分钟前
为什么 AI Agent 重新爱上了文件系统(Filesystems)
人工智能·llm·agent
AI精钢28 分钟前
RAG 的 Chunking 有什么好方案?从原理到实战选型
llm·向量检索·rag·ai工程·chunking
明月(Alioo)40 分钟前
给 AI Agent 装上“大脑“:Java语言中Code Interpreter 的设计与实现
java·ai·agent
AI精钢41 分钟前
如何提高 RAG 的检索质量?这才是真正的瓶颈所在
大模型·llm·向量检索·rag·ai工程
薛定谔的猫3691 小时前
深入浅出:大语言模型 Agent 的工作原理与应用
人工智能·自动化·大模型·llm·ai agent
薛定谔的猫3692 小时前
AI Agent 与 MCP 协议:构建标准化大模型交互的新范式
ai·llm·agent·mcp·software engineering
风雅GW2 小时前
多 Agent 系统设计参考框架(OpenClaw 实现版)
人工智能·ai·agent·openclaw
Vastog2 小时前
skill最佳实践
agent·ai应用开发
庞轩px2 小时前
Embedding与向量语义——大模型是怎样“理解”文字的?
人工智能·自然语言处理·embedding·向量检索·余弦相似度·rag·高维向量空间