为 DiscourseAI 自托管一个开源 LLM

Discourse AI 插件有许多功能需要启用大型语言模型(LLM),例如摘要、AI 助手、AI 搜索、AI 机器人。虽然您可以使用第三方 API,例如配置 OpenAI 的 API 密钥配置 Anthropic 的 API 密钥,但 Discourse AI 自第一天起就被构建为不依赖于这些服务。

使用 HuggingFace TGI 运行

HuggingFace 提供了一个很棒的容器镜像,可以帮助您快速启动和运行。

例如:

mkdir -p /opt/tgi-cache
docker run --rm --gpus all --shm-size 1g -p 8080:80 \
  -v /opt/tgi-cache:/data \
  ghcr.io/huggingface/text-generation-inference:latest \
  --model-id mistralai/Mistral-7B-Instruct-v0.2

应该可以让您在本地主机的 8080 端口上使用 Mistral 7B Instruct 的本地实例启动并运行,然后可以使用以下命令进行测试:

curl http://localhost:8080/ \
    -X POST \
    -H 'Content-Type: application/json' \
    -d '{"inputs":"<s>[INST] What is your favourite condiment? [/INST] Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!</s] [INST] Do you have mayonnaise recipes? [/INST]","parameters":{"max_new_tokens":500, "temperature":0.5,"top_p": 0.9}}'

使用 vLLM 运行

Discourse AI 支持的另一种自托管 LLM 选项是 vLLM,这是一个非常流行的项目,根据 Apache 许可证获得许可。

以下是如何开始使用模型的:

mkdir -p /opt/vllm-cache
docker run --gpus all \
  -v /opt/vllm-cache:/root/.cache/huggingface \
  -e "MODEL=mistralai/Mistral-7B-Instruct-v0.2" \
  -p 8080:8000 --ipc=host vllm/vllm-openai:latest

您可以使用以下命令进行测试:

curl -X POST http://localhost:8080/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "mistralai/Mistral-7B-Instruct-v0.2",
"prompt": "<s> [INST] What was the latest released hero for Dota 2? [/INST] The latest released hero for Dota 2 was", "max_tokens": 200}'

使用 Ollama 运行

Ollama 是在本地运行开源模型的另一个流行选项。它简化了模型管理,并提供了一个与 OpenAI 兼容的 API。

ollama pull mistral
ollama serve

这将在 http://localhost:11434 启动一个本地服务器,Discourse AI 可以使用 Ollama 提供程序连接到该服务器。

使其可用于您的 Discourse 实例

大多数情况下,由于需要 GPU,您将在专用服务器上运行此程序。进行此类操作时,我建议运行反向代理,执行 TLS 终止,并保护端点,使其只能由您的 Discourse 实例连接。

配置 Discourse AI

LLM 连接现在通过管理界面进行配置,而不是通过站点设置。导航到 /admin/plugins/discourse-ai/ai-llms 并添加一个新的 LLM:

  1. 单击 New 以添加模型
  2. 选择一个 Provider — 根据您的推理服务器选择 vLLMHugging FaceOllama
  3. 输入推理端点的 URL(例如 http://your-server:8080
  4. 如果您的端点需要 API key,请输入它
  5. 填写 model nametokenizermax prompt tokens 和其他模型详细信息

添加 LLM 后,通过 ai_default_llm_model 站点设置将其设置为默认值,或通过 /admin/plugins/discourse-ai/ai-features 中其代理程序的配置将其分配给特定功能。

17 个赞