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 兼容端点
修改 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启动代理
$ litellm --config /path/to/config.yaml
向 LiteLLM 代理服务器发送请求
- OpenAI Python v1.0.0+
- curl
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)curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"model": "my-model",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
}'
嵌入
- SDK
- 代理
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)
- 设置 config.yaml
model_list:
- model_name: my-model
litellm_params:
model: llamafile/sentence-transformers/all-MiniLM-L6-v2 # add llamafile/ prefix to route as OpenAI provider
api_base: http://localhost:8080/v1 # add api base for OpenAI compatible provider
- 启动代理
$ litellm --config /path/to/config.yaml
# RUNNING on http://0.0.0.0:4000
- 测试一下!
curl -L -X POST 'http://0.0.0.0:4000/embeddings' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{"input": ["hello world"], "model": "my-model"}'