跳到主要内容

Llamafile

LiteLLM 支持 Llamafile 上的所有模型。

属性详情
描述llamafile 允许您使用单个文件分发和运行 LLM。文档
LiteLLM 上的提供商路由llamafile/ (用于 OpenAI 兼容服务器)
提供商文档llamafile ↗
支持的端点/chat/completions, /embeddings, /completions

快速开始

用法 - litellm.completion (调用 OpenAI 兼容端点)

llamafile 提供了一个用于聊天补全的 OpenAI 兼容端点 - 以下是如何使用 LiteLLM 调用它

要使用 litellm 调用 llamafile,请在您的补全调用中添加以下内容

  • model="llamafile/<您的 llamafile 模型名称>"
  • api_base = "您的托管 llamafile"
import litellm 

response = litellm.completion(
model="llamafile/mistralai/mistral-7b-instruct-v0.2", # pass the llamafile model name for completeness
messages=messages,
api_base="http://localhost:8080/v1",
temperature=0.2,
max_tokens=80)

print(response)

用法 - LiteLLM 代理服务器 (调用 OpenAI 兼容端点)

以下是如何使用 LiteLLM 代理服务器调用 OpenAI 兼容端点

  1. 修改 config.yaml

    model_list:
    - model_name: my-model
    litellm_params:
    model: llamafile/mistralai/mistral-7b-instruct-v0.2 # add llamafile/ prefix to route as OpenAI provider
    api_base: http://localhost:8080/v1 # add api base for OpenAI compatible provider
  2. 启动代理

    $ litellm --config /path/to/config.yaml
  3. 向 LiteLLM 代理服务器发送请求

    import openai
    client = openai.OpenAI(
    api_key="sk-1234", # pass litellm proxy key, if you're using virtual keys
    base_url="http://0.0.0.0:4000" # litellm-proxy-base url
    )

    response = client.chat.completions.create(
    model="my-model",
    messages = [
    {
    "role": "user",
    "content": "what llm are you"
    }
    ],
    )

    print(response)

嵌入

from litellm import embedding   
import os

os.environ["LLAMAFILE_API_BASE"] = "http://localhost:8080/v1"


embedding = embedding(model="llamafile/sentence-transformers/all-MiniLM-L6-v2", input=["Hello world"])

print(embedding)