跳过内容

快速入门

安装Outlines之后,快速熟悉这个库的最快方法是了解它的几个核心元素。我们建议您快速浏览一下此页面,以查看Outlines所提供的所有内容,然后再深入文档

核心元素

模型

使用Outlines编写程序的第一步是初始化模型。在此步骤中,权重将被加载到设备上:

import outlines

model = outlines.models.transformers(
    "microsoft/Phi-3-mini-4k-instruct",
    device="cuda"  # optional device argument, default is cpu
)

Outlines 支持各种推理引擎和模型权重类型。有关不同模型的更多详细信息,请参阅 Outlines Models 文档页面。

生成

一旦模型被初始化,您可以构建一个 outlines.generate 生成器。此生成器可以直接使用提示进行调用。

(Outlines 结构化生成完整文档)

generator = outlines.generate.text(model)

result = generator("Question: What's 2+2? Answer:", max_tokens=100)
print(result)
# The answer is 4

# Outlines also supports streaming output
stream = generator.stream("What's 2+2?", max_tokens=4)
for i in range(5):
    token = next(stream)
    print(repr(token))
# '2'
# '+'
# '2'
# ' equals'
# '4'

除了通过 outlines.generate.text 的典型语言模型生成行为外,Outlines 还支持结构化生成,这保证了模型生成的标记将遵循预定义的结构。结构可以通过正则表达式模式、JSON 模式、python 对象类型或定义可解析语言的 Lark 语法(如 SQL 或 Python)来定义。

示例:使用 pydantic 来强制执行 JSON 模式

from enum import Enum
from pydantic import BaseModel, constr, conint

class Character(BaseModel):
    name: constr(max_length=10)
    age: conint(gt=18, lt=99)
    armor: (Enum('Armor', {'leather': 'leather', 'chainmail': 'chainmail', 'plate': 'plate'}))
    strength: conint(gt=1, lt=100)

generator = outlines.generate.json(model, Character)

character = generator(
    "Generate a new character for my awesome game: "
    + "name, age (between 1 and 99), armor and strength. "
    )
print(character)
# Character(name='Zara', age=25, armor=<Armor.leather: 'leather'>, strength=85)

使用 vLLM 和 FastAPI 部署

Outlines可以作为LLM服务部署,使用vLLMFastAPI。服务器支持异步处理传入的请求,并利用vLLM的性能。

首先启动服务器:

python -m outlines.serve.serve --model="microsoft/Phi-3-mini-4k-instruct"

或者您可以使用Outlines的官方Docker映像启动服务器:

docker run -p 8000:8000 outlinesdev/outlines --model="microsoft/Phi-3-mini-4k-instruct"

默认情况下,这将在 http://127.0.0.1:8000 启动一个服务器(不过请查看控制台的输出)。如果未设置 --model 参数,将使用 OPT-125M 模型。

然后,您可以在 shell 中通过传递提示和JSON Schema规范来查询模型,以获取输出的结构:

curl http://127.0.0.1:8000/generate \
    -d '{
        "prompt": "Question: What is a language model? Answer:",
        "schema": {"type": "string"}
        }'

或者使用来自另一个python程序的 requests 库。你可以阅读 vLLM 文档 获取更多细节。

实用工具

提示模板

提示可能导致代码杂乱。Outlines的提示函数是包含提示模板的python函数,在它们的文档字符串中。我们使用一种强大的模板语言,使您能够直接从提示中循环遍历列表、字典,添加条件等。当调用时,提示函数返回渲染的模板:

import outlines

@outlines.prompt
def few_shots(instructions, examples, question):
    """{{ instructions }}

    Examples
    --------

    {% for example in examples %}
    Q: {{ example.question }}
    A: {{ example.answer }}

    {% endfor %}
    Question
    --------

    Q: {{ question }}
    A:
    """

instructions = "Please answer the following question following the examples"
examples = [
    {"question": "2+2=?", "answer":4},
    {"question": "3+3=?", "answer":6}
]
question = "4+4 = ?"

prompt = few_shots(instructions, examples, question)
print(prompt)
# Please answer the following question following the examples

# Examples
# --------

# Q: 2+2=?
# A: 4

# Q: 3+3=?
# A: 6

# Question
# --------

# Q: 4+4 = ?
# A:

Outlines 函数

一旦您完成了对提示和输出结构的实验,将所有这些封装在一个可以从程序的其他部分调用的单一函数中是很有用的。这就是outlines.Function允许您做到的:

from pydantic import BaseModel

import outlines


@outlines.prompt
def tell_a_joke(topic):
    """Tell me a joke about {{ topic }}."""

class Joke(BaseModel):
    setup: str
    punchline: str

generate_joke = outlines.Function(
    tell_a_joke,
    Joke,
    "microsoft/Phi-3-mini-4k-instruct"
)
from .function import generate_joke

response = generate_joke("baseball")

# haha
# Joke(setup='Why was the baseball in a bad mood?', punchline='Because it got hit around a lot.')

您可以直接从Outlines加载存储在GitHub上的功能。例如,Someone将一个功能存储在joke.py文件中,该文件位于TheirRepo仓库的根目录下:

import outlines

joke = outlines.Function.from_github("Someone/TheirRepo/joke")
response = joke("baseball")
It make it easier for the community to collaborate on the infinite number of use cases enabled by these models!

进一步探索

如果您需要更多灵感,可以查看食谱或观看Remi Louf在Outlines上的AI工程师世界博览会演讲。如果您有任何问题或文档请求,请通过GitHubTwitterDiscord与我们联系。