概述
开源的本地大型语言模型(LLMs)非常适合那些需要数据隐私的应用场景。SQL是一个很好的例子。本指南展示了如何使用各种本地版本的 LLaMA2 进行文本到SQL的转换。
包安装
- Python
- 安装命令:
! pip install langchain replicate
大型语言模型(LLM)访问方式
有几种方式可以访问 LLaMA2。
本地运行
使用 Ollama.ai 进行本地运行。
外部 API
使用 Replicate 进行外部 API 访问,但这不是私有的。
# 本地运行设置
from langchain_community.chat_models import ChatOllama
llama2_chat = ChatOllama(model="llama2:13b-chat")
llama2_code = ChatOllama(model="codellama:7b-instruct")
# 外部API设置
from langchain_community.llms import Replicate
REPLICATE_API_TOKEN = getpass() # 安全获取API令牌
os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
replicate_id = "meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d"
llama2_chat_replicate = Replicate(
model=replicate_id, input={
"temperature": 0.01, "max_length": 500, "top_p": 1}
)
llm = llama2_chat
数据库连接
连接到 SQLite 数据库。
- 创建特定数据库的代码和步骤:查看这里
from langchain_community.utilities import SQLDatabase
# 创建并连接到 SQLite 数据库
db = SQLDatabase.from_uri("sqlite:///nba_roster.db", sample_rows_in_table_info=0)