T
traeai
登录
返回首页
Machine Learning Mastery

使用 Scikit‑LLM 与开源 LLM

8.5Score
使用 Scikit‑LLM 与开源 LLM

TL;DR · AI 摘要

本文展示如何利用 Ollama 本地部署的开源 LLM(如 Llama 3、Mistral、Gemma)与 Scikit‑LLM 结合,完成零样本文本分类,且完全免费。

核心要点

  • 通过 `ollama run <model>` 可在本地拉取并运行 Llama 3、Mistral 或 Gemma,端口默认 11434。
  • Scikit‑LLM 通过 `SKLLMConfig.set_gpt_url("http://localhost:11434/v1")` 将请求路由到本地 Ol
  • 使用 `ZeroShotGPTClassifier` 可在 scikit‑learn 风格下完成零样本分类,示例代码演示了数据拆分、模型初始化与预测流程。

结构提纲

按章节快速跳转。

  1. 阐述使用 Ollama 本地{LLM}与 Scikit‑LLM 进行零样本文本分类的目标与优势。

  2. 说明如何在本地安装 Ollama 并拉取 Llama 3MistralGemma 模型。

  3. 展示如何通过 SKLLMConfig 将 Scikit‑LLM 的请求指向本地 Ollama 端点并设置占位 API key。

  4. 演示创建小型文本分类数据集并使用 train_test_split 进行拆分。

  5. 介绍 ZeroShotGPTClassifier 的初始化、训练与预测步骤。

思维导图

用一张图看清主题之间的关系。

查看大纲文本(无障碍 / 无 JS 友好)
  • Scikit‑LLM + Ollama
    • Ollama
      • Llama 3
      • Mistral
      • Gemma
    • Scikit‑LLM
      • SKLLMConfig
      • ZeroShotGPTClassifier
    • Workflow
      • Install Ollama
      • Configure URL & Key
      • Prepare Data
      • Train / Predict

金句 / Highlights

值得收藏与分享的关键句。

  • SKLLMConfig.set_gpt_url("http://localhost:11434/v1") 将 Scikit‑LLM 的请求路由到本地 Ollama 端点。

    第 1 段

    ⬇︎ 下载 PNG𝕏 分享到 X
  • ZeroShotGPTClassifier 让你在 scikit‑learn 风格下使用本地 LLM 进行零样本文本分类。

    第 1 段

    ⬇︎ 下载 PNG𝕏 分享到 X
  • 通过 `ollama run llama3`、`ollama run mistral` 或 `ollama run gemma` 可在本地快速部署 LLM,完全免费。

    第 1 段

    ⬇︎ 下载 PNG𝕏 分享到 X
#Scikit‑LLM#Ollama#LLM#Zero‑shot#Python
打开原文

使用 Scikit-LLM 与开源 LLM

介绍

本文将教你如何通过 Ollama(一个免费的本地 LLM 仓库)以及 Scikit-LLM Python 库,使用本地托管的大型语言模型(如 Mistral、Gemma 和 Llama 3)来完成文本分类等语言任务,全部免费。

前置条件:安装 Ollama

建议使用 IDE 运行本教程,因为我们需要在 IDE 中与本地安装的 Ollama 交互。对 Ollama 还不熟悉?先阅读这篇文章。下面是安装完 Ollama 后,在本地命令行终端下载本地 LLM 的简要步骤。

bash
# 拉取 Ollama 最受欢迎的可下载模型之一 Llama 3
ollama run llama3

# 或者尝试拉取 Mistral
ollama run mistral

# 或者,如果你今天想挑剔一点,直接拉取 Google 的 Gemma
ollama run gemma

当你在终端看到模型交互窗口后,可以输入 “/bye” 让它在后台保持运行,等待 API 调用。与此同时,在你新建的 Python 项目中,需要安装以下库:

bash
pip install scikit-learn pandas scikit-llm

如果在执行 Python 代码时遇到 “Module not found” 错误,尝试逐个安装上述依赖。

编写 Python 代码

1. 导入所需模块

python
import pandas as pd
from sklearn.model_selection import train_test_split
from skllm.config import SKLLMConfig
from skllm.models.gpt.classification.zero_shot import ZeroShotGPTClassifier

2. 配置 Scikit-LLM 与 Ollama 的通信

python
# 告诉 Scikit-LLM 将云请求路由到默认本地 Ollama 端口
SKLLMConfig.set_gpt_url("http://localhost:11434/v1")

# Scikit-LLM 默认需要一个 key 进行内部校验
# 但因为 Ollama 是本地且免费的,这个字符串在实践中会被忽略
SKLLMConfig.set_openai_key("local-ollama-is-free")

3. 创建并准备数据集

python
data = {
    "review": [
        "The new macOS update is fantastic and runs smoothly.",
        "My battery is draining incredibly fast after the patch.",
        "I need help resetting my account password.",
        "The display on this monitor is breathtakingly crisp.",
        "Customer support hung up on me, very disappointing."
    ],
    "category": [
        "Positive Feedback",
        "Technical Issue",
        "Support Request",
        "Positive Feedback",
        "Negative Feedback"
    ]
}

df = pd.DataFrame(data)
X = df["review"]
y = df["category"]

# 划分训练/测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)

4. 初始化并训练 ZeroShotGPTClassifier

python
print("Initializing ZeroShotGPTClassifier with local Llama 3...")

# 使用 'custom_url::' 前缀告诉系统使用你在 set_gpt_url 中设置的端点
clf = ZeroShotGPTClassifier(model="custom_url::llama3")

# 训练模型
clf.fit(X_train, y_train)

print("Sending data to Ollama for local inference...\n")
predictions = clf.predict(X_test)

5. 输出预测结果

python
for review, prediction in zip(X_test, predictions):
    print(f"Review Text:'{review}'")
    print(f"Predicted Tag: {prediction}")
    print("-" * 50)

结果可能会因测试样本不同而略有差异。


Image 1: Using Scikit-LLM with Open-Source LLMs

100%|███████████████████████████████████████████████████████████|2/2[00:12<00:00,6.36s/it]

Review Text:'My battery is draining incredibly fast after the patch.'

Predicted Tag:Support Request


Review Text:'Customer support hung up on me, very disappointing.'

Predicted Tag:Support Request


Alternatively, you could run your Python script from your terminal. For example, if you named it local_classification.py, execute this command:

1 python local_classification.py

Either way, if you followed all the steps, you should have it working. Well done!

Wrapping Up

This article illustrated how to swap in free, locally run models served through Ollama, such as Llama, Mistral, or Gemma — all for free, and in a few easy steps — thanks to Python’s Scikit-LLM library, which enables the use of cutting-edge LLMs within a familiar classical machine learning workflow.

##### No comments yet.

AI 可能会生成不准确的信息,请核实重要内容

使用 Scikit‑LLM 与开源 LLM | Machine Learning Mastery | traeai