跳到主要内容
MLflow AI Gateway for LLMs (MLflow 大型语言模型人工智能网关) 是一个强大的工具,旨在简化组织内各种大型语言模型 (LLM) 提供商(如 OpenAI 和 Anthropic)的使用和管理。它提供了一个高级接口,通过提供统一的端点来处理特定的 LLM 相关请求,从而简化了与这些服务的交互。

安装和设置

安装带有 MLflow GenAI 依赖项的 mlflow
pip install 'mlflow[genai]'
将 OpenAI API 密钥设置为环境变量
export OPENAI_API_KEY=...
创建配置文件
endpoints:
  - name: completions
    endpoint_type: llm/v1/completions
    model:
      provider: openai
      name: text-davinci-003
      config:
        openai_api_key: $OPENAI_API_KEY

  - name: embeddings
    endpoint_type: llm/v1/embeddings
    model:
      provider: openai
      name: text-embedding-ada-002
      config:
        openai_api_key: $OPENAI_API_KEY
启动网关服务器
mlflow gateway start --config-path /path/to/config.yaml

MLflow 提供的示例

mlflow.langchain 模块提供了用于记录和加载 LangChain 模型的 API。此模块以 langchain 风格导出多元 LangChain 模型,并以 pyfunc 风格导出单变量 LangChain 模型。
有关更多信息,请参阅API 文档和示例

补全示例

import mlflow
from langchain.chains import LLMChain, PromptTemplate
from langchain_community.llms import Mlflow

llm = Mlflow(
    target_uri="http://127.0.0.1:5000",
    endpoint="completions",
)

llm_chain = LLMChain(
    llm=Mlflow,
    prompt=PromptTemplate(
        input_variables=["adjective"],
        template="Tell me a {adjective} joke",
    ),
)
result = llm_chain.run(adjective="funny")
print(result)

with mlflow.start_run():
    model_info = mlflow.langchain.log_model(chain, "model")

model = mlflow.pyfunc.load_model(model_info.model_uri)
print(model.predict([{"adjective": "funny"}]))

嵌入示例

from langchain_community.embeddings import MlflowEmbeddings

embeddings = MlflowEmbeddings(
    target_uri="http://127.0.0.1:5000",
    endpoint="embeddings",
)

print(embeddings.embed_query("hello"))
print(embeddings.embed_documents(["hello"]))

聊天示例

from langchain_community.chat_models import ChatMlflow
from langchain.messages import HumanMessage, SystemMessage

chat = ChatMlflow(
    target_uri="http://127.0.0.1:5000",
    endpoint="chat",
)

messages = [
    SystemMessage(
        content="You are a helpful assistant that translates English to French."
    ),
    HumanMessage(
        content="Translate this sentence from English to French: I love programming."
    ),
]
print(chat(messages))

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.