跳到内容

LLM

pipeline pipeline

LLM 流水线通过大型语言模型 (LLM) 运行提示词。此流水线会根据模型路径自动检测 LLM 框架。

示例

以下显示了使用此流水线的一个简单示例。

from txtai import LLM

# Create LLM pipeline
llm = LLM()

# Run prompt
llm(
  """
  Answer the following question using the provided context.

  Question:
  What are the applications of txtai?

  Context:
  txtai is an open-source platform for semantic search and
  workflows powered by language models.
  """
)

# Instruction tuned models typically require string prompts to
# follow a specific chat template set by the model
llm(
  """
  <|im_start|>system
  You are a friendly assistant.<|im_end|>
  <|im_start|>user
  Answer the following question...<|im_end|>
  <|im_start|>assistant
  """
)

# Chat messages automatically handle templating
llm([
  {"role": "system", "content": "You are a friendly assistant."},
  {"role": "user", "content": "Answer the following question..."}
])

# Set the default role to user and string inputs are converted to chat messages
llm("Answer the following question...", defaultrole="user")

LLM 流水线会自动检测底层 LLM 框架。这也可以手动设置。

此流水线支持 Hugging Face Transformersllama.cpp通过 LiteLLM 托管的 API 模型

有关 LiteLLM 模型可用选项,请参阅 LiteLLM 文档。llama.cpp 模型支持本地和 HF Hub 上的远程 GGUF 路径。

from txtai import LLM

# Transformers
llm = LLM("meta-llama/Meta-Llama-3.1-8B-Instruct")
llm = LLM("meta-llama/Meta-Llama-3.1-8B-Instruct", method="transformers")

# llama.cpp
llm = LLM("microsoft/Phi-3-mini-4k-instruct-gguf/Phi-3-mini-4k-instruct-q4.gguf")
llm = LLM("microsoft/Phi-3-mini-4k-instruct-gguf/Phi-3-mini-4k-instruct-q4.gguf",
           method="llama.cpp")

# LiteLLM
llm = LLM("ollama/llama3.1")
llm = LLM("ollama/llama3.1", method="litellm")

# Custom Ollama endpoint
llm = LLM("ollama/llama3.1", api_base="http://localhost:11434")

# Custom OpenAI-compatible endpoint
llm = LLM("openai/llama3.1", api_base="http://localhost:4000")

# LLM APIs - must also set API key via environment variable
llm = LLM("gpt-4o")
llm = LLM("claude-3-5-sonnet-20240620")

模型可以外部加载并传递给流水线。这对于 Transformers 尚不支持和/或需要特殊初始化的模型非常有用。

import torch

from transformers import AutoModelForCausalLM, AutoTokenizer
from txtai import LLM

# Load Phi 3.5-mini
path = "microsoft/Phi-3.5-mini-instruct"
model = AutoModelForCausalLM.from_pretrained(
  path,
  torch_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(path)

llm = LLM((model, tokenizer))

更多详细示例请参见以下链接。

笔记本 描述
使用 LLM 进行提示词驱动的搜索 使用大型语言模型 (LLM) 进行嵌入引导和提示词驱动的搜索 Open In Colab
提示词模板和任务链 构建模型提示词并通过工作流将任务连接在一起 Open In Colab
使用 txtai 构建 RAG 流水线 检索增强生成指南,包括如何创建引用 Open In Colab
集成 LLM 框架 集成 llama.cpp、LiteLLM 和自定义生成框架 Open In Colab
使用语义图和 RAG 生成知识 使用语义图和 RAG 进行知识探索和发现 Open In Colab
使用 LLM 构建知识图谱 使用 LLM 驱动的实体提取构建知识图谱 Open In Colab
使用图路径遍历的高级 RAG 图路径遍历以收集复杂数据集用于高级 RAG Open In Colab
使用引导生成的高级 RAG 检索增强和引导生成 Open In Colab
使用 llama.cpp 和外部 API 服务进行 RAG 使用额外的向量和 LLM 框架进行 RAG Open In Colab
txtai 中的 RAG 工作原理 创建 RAG 进程、API 服务和 Docker 实例 Open In Colab
语音到语音 RAG ▶️ 使用 RAG 的完整语音到语音工作流 Open In Colab
生成式音频 使用生成式音频工作流进行故事叙述 Open In Colab
使用图和智能体分析 Hugging Face 帖子 使用图分析和智能体探索丰富的数据集 Open In Colab
赋予智能体自主权 智能体根据自身判断迭代解决问题 Open In Colab
LLM API 入门 使用 OpenAI、Claude、Gemini、Bedrock 等生成嵌入和运行 LLM Open In Colab
使用图和智能体分析领英公司帖子 探索如何通过 AI 提高社交媒体互动 Open In Colab
使用 txtai 解析星辰 探索包含已知恒星、行星、星系的知识图谱 Open In Colab
为 RAG 分块数据 提取、分块和索引内容以实现有效检索 Open In Colab

配置驱动的示例

流水线可以通过 Python 或配置运行。流水线可以在 配置 中使用流水线的小写名称进行实例化。配置驱动的流水线通过 工作流API 运行。

config.yml

# Create pipeline using lower case class name
llm:

# Run pipeline with workflow
workflow:
  llm:
    tasks:
      - action: llm

与上面的 Python 示例类似,底层 Hugging Face 流水线参数模型参数 可以在流水线配置中设置。

llm:
  path: microsoft/Phi-3.5-mini-instruct
  torch_dtype: torch.bfloat16

使用工作流运行

from txtai import Application

# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("llm", [
  """
  Answer the following question using the provided context.

  Question:
  What are the applications of txtai? 

  Context:
  txtai is an open-source platform for semantic search and
  workflows powered by language models.
  """
]))

使用 API 运行

CONFIG=config.yml uvicorn "txtai.api:app" &

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"llm", "elements": ["Answer the following question..."]}'

方法

流水线的 Python 文档。

__init__(path=None, method=None, **kwargs)

创建一个新的 LLM。

参数

名称 类型 描述 默认值
path

模型路径

None
method

llm 模型框架,如果未提供则从 path 推断

None
kwargs

模型关键字参数

{}
源代码位于 txtai/pipeline/llm/llm.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def __init__(self, path=None, method=None, **kwargs):
    """
    Creates a new LLM.

    Args:
        path: model path
        method: llm model framework, infers from path if not provided
        kwargs: model keyword arguments
    """

    # Default LLM if not provided
    path = path if path else "google/flan-t5-base"

    # Generation instance
    self.generator = GenerationFactory.create(path, method, **kwargs)

__call__(text, maxlength=512, stream=False, stop=None, defaultrole='prompt', **kwargs)

生成文本。支持以下输入格式

  • 字符串或字符串列表(指令微调模型必须遵循聊天模板)
  • 包含 rolecontent 键值对的字典列表或列表的列表

参数

名称 类型 描述 默认值
text

text|list

必需
maxlength

最大序列长度

512
stream

如果为 True,则流式传输响应,默认为 False

False
stop

停止字符串列表,默认为 None

None
defaultrole

应用于文本输入的默认角色(用于原始提示词的 prompt(默认)或用于用户聊天消息的 user)

'prompt'
kwargs

其他生成关键字参数

{}

返回值

类型 描述

生成的文本

源代码位于 txtai/pipeline/llm/llm.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def __call__(self, text, maxlength=512, stream=False, stop=None, defaultrole="prompt", **kwargs):
    """
    Generates text. Supports the following input formats:

      - String or list of strings (instruction-tuned models must follow chat templates)
      - List of dictionaries with `role` and `content` key-values or lists of lists

    Args:
        text: text|list
        maxlength: maximum sequence length
        stream: stream response if True, defaults to False
        stop: list of stop strings, defaults to None
        defaultrole: default role to apply to text inputs (prompt for raw prompts (default) or user for user chat messages)
        kwargs: additional generation keyword arguments

    Returns:
        generated text
    """

    # Debug logging
    logger.debug(text)

    # Run LLM generation
    return self.generator(text, maxlength, stream, stop, defaultrole, **kwargs)