[旧代理 👉 新代理在此] 本地LiteLLM代理服务器
一个快速且轻量级的OpenAI兼容服务器,用于调用100多个LLM API。
文档已过时。新文档在此 👉 这里
使用方法
pip install 'litellm[proxy]'
$ litellm --model ollama/codellama
#INFO: Ollama运行在 http://0.0.0.0:8000
测试
在新终端中运行:
$ litellm --test
替换OpenAI基础
import openai
openai.api_base = "http://0.0.0.0:8000"
print(openai.ChatCompletion.create(model="test", messages=[{"role":"user", "content":"Hey!"}]))
其他支持的模型:
- VLLM
- OpenAI兼容服务器
- Huggingface
- Anthropic
- TogetherAI
- Replicate
- Petals
- Palm
- Azure OpenAI
- AI21
- Cohere
$ litellm --model vllm/facebook/opt-125m
$ litellm --model openai/<model_name> --api_base <your-api-base>
$ export HUGGINGFACE_API_KEY=my-api-key #[可选]
$ litellm --model claude-instant-1
$ export ANTHROPIC_API_KEY=my-api-key
$ litellm --model claude-instant-1
$ export TOGETHERAI_API_KEY=my-api-key
$ litellm --model together_ai/lmsys/vicuna-13b-v1.5-16k
$ export REPLICATE_API_KEY=my-api-key
$ litellm \
--model replicate/meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3
$ litellm --model petals/meta-llama/Llama-2-70b-chat-hf
$ export PALM_API_KEY=my-palm-key
$ litellm --model palm/chat-bison
$ export AZURE_API_KEY=my-api-key
$ export AZURE_API_BASE=my-api-base
$ litellm --model azure/my-deployment-name
$ export AI21_API_KEY=my-api-key
$ litellm --model j2-light
$ export COHERE_API_KEY=my-api-key
$ litellm --model command-nightly
教程:与多个LLM + LibreChat/Chatbot-UI/Auto-Gen/ChatDev/Langroid等一起使用
- 多个LLM
- LibreChat
- SmartChatbotUI
- AutoGen
- AutoGen多LLM
- ChatDev
- Langroid
替换OpenAI基础:
import openai
openai.api_key = "any-string-here"
openai.api_base = "http://0.0.0.0:8080" # 你的代理URL
# 调用OpenAI
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hey"}])
print(response)
# 调用Cohere
response = openai.ChatCompletion.create(model="command-nightly", messages=[{"role": "user", "content": "Hey"}])
print(response)
1. 克隆仓库
git clone https://github.com/danny-avila/LibreChat.git
2. 修改docker-compose.yml
OPENAI_REVERSE_PROXY=http://host.docker.internal:8000/v1/chat/completions
3. 在.env中保存假的OpenAI密钥
OPENAI_API_KEY=sk-1234
4. 运行LibreChat:
docker compose up
1. 克隆仓库
git clone https://github.com/dotneet/smart-chatbot-ui.git
2. 安装依赖
npm i
3. 创建你的环境
cp .env.local.example .env.local
4. 设置API密钥和基础
OPENAI_API_KEY="my-fake-key"
OPENAI_API_HOST="http://0.0.0.0:8000
5. 使用docker compose运行
docker compose up -d
pip install pyautogen
from autogen import AssistantAgent, UserProxyAgent, oai
config_list=[
{
"model": "my-fake-model",
"api_base": "http://0.0.0.0:8000", #litellm兼容端点
"api_type": "open_ai",
"api_key": "NULL", # 只是一个占位符
}
]
response = oai.Completion.create(config_list=config_list, prompt="Hi")
print(response) # 运行正常
llm_config={
"config_list": config_list,
}
assistant = AssistantAgent("assistant", llm_config=llm_config)
user_proxy = UserProxyAgent("user_proxy")
user_proxy.initiate_chat(assistant, message="Plot a chart of META and TESLA stock price change YTD.", config_list=config_list)
感谢@victordibia提供此教程。
from autogen import AssistantAgent, GroupChatManager, UserProxyAgent
from autogen.agentchat import GroupChat
config_list = [
{
"model": "ollama/mistralorca",
"api_base": "http://0.0.0.0:8000", # litellm兼容端点
"api_type": "open_ai",
"api_key": "NULL", # 仅作为占位符
}
]
llm_config = {"config_list": config_list, "seed": 42}
code_config_list = [
{
"model": "ollama/phind-code",
"api_base": "http://0.0.0.0:8000", # litellm兼容端点
"api_type": "open_ai",
"api_key": "NULL", # 仅作为占位符
}
]
code_config = {"config_list": code_config_list, "seed": 42}
admin = UserProxyAgent(
name="Admin",
system_message="一位人类管理员。与计划者互动讨论计划。计划执行需要得到这位管理员的批准。",
llm_config=llm_config,
code_execution_config=False,
)
engineer = AssistantAgent(
name="Engineer",
llm_config=code_config,
system_message="""工程师。你遵循批准的计划。你编写python/shell代码来解决任务。将代码包裹在一个指定脚本类型的代码块中。用户无法修改你的代码。因此,不要建议需要他人修改的不完整代码。如果不打算让执行者执行代码,请不要使用代码块。
不要在一个响应中包含多个代码块。不要要求他人复制并粘贴结果。检查执行者返回的执行结果。
如果结果表明存在错误,修复错误并再次输出代码。建议完整的代码而不是部分代码或代码更改。如果错误无法修复或即使代码成功执行后任务仍未解决,分析问题,重新审视你的假设,收集你需要的额外信息,并考虑尝试不同的方法。
""",
)
planner = AssistantAgent(
name="Planner",
system_message="""计划者。提出一个计划。根据管理员和批评者的反馈修订计划,直到管理员批准。
计划可能涉及一个可以编写代码的工程师和一个不编写代码的科学家。
首先解释计划。明确哪些步骤由工程师执行,哪些步骤由科学家执行。
""",
llm_config=llm_config,
)
executor = UserProxyAgent(
name="Executor",
system_message="执行者。执行工程师编写的代码并报告结果。",
human_input_mode="NEVER",
llm_config=llm_config,
code_execution_config={"last_n_messages": 3, "work_dir": "paper"},
)
critic = AssistantAgent(
name="Critic",
system_message="批评者。仔细检查其他代理的计划、主张、代码并提供反馈。检查计划是否包含可验证的信息,如来源URL。",
llm_config=llm_config,
)
groupchat = GroupChat(
agents=[admin, engineer, planner, executor, critic],
messages=[],
max_round=50,
)
manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)
admin.initiate_chat(
manager,
message="""
""",
)
Credits @Nathan for this tutorial.
设置 ChatDev (文档)
git clone https://github.com/OpenBMB/ChatDev.git
cd ChatDev
conda create -n ChatDev_conda_env python=3.9 -y
conda activate ChatDev_conda_env
pip install -r requirements.txt
使用代理运行 ChatDev
export OPENAI_API_KEY="sk-1234"
export OPENAI_API_BASE="http://0.0.0.0:8000"
python3 run.py --task "a script that says hello world" --name "hello world"
pip install langroid
from langroid.language_models.openai_gpt import OpenAIGPTConfig, OpenAIGPT
# 配置 LLM
my_llm_config = OpenAIGPTConfig(
# 代理服务器监听地址
api_base="http://0.0.0.0:8000",
)
# 创建 LLM,一次性交互
llm = OpenAIGPT(my_llm_config)
response = mdl.chat("What is the capital of China?", max_tokens=50)
# 使用此 LLM 创建一个 Agent,将其包装在任务中,并
# 将其作为交互式聊天应用运行:
from langroid.agent.base import ChatAgent, ChatAgentConfig
from langroid.agent.task import Task
agent_config = ChatAgentConfig(llm=my_llm_config, name="my-llm-agent")
agent = ChatAgent(agent_config)
task = Task(agent, name="my-llm-task")
task.run()
Credits @pchalasani 和 Langroid for this tutorial.
本地代理
以下是如何使用本地代理测试不同 GitHub 仓库中的 codellama/mistral 等模型
pip install litellm
$ ollama pull codellama # 我们的本地 CodeLlama
$ litellm --model ollama/codellama --temperature 0.3 --max_tokens 2048
教程:与多个 LLM 一起使用 + Aider/AutoGen/Langroid 等
- 多个 LLM
- ContinueDev
- Aider
- AutoGen
- AutoGen Multi-LLM
- ChatDev
- Langroid
- GPT-Pilot
- guidance
$ litellm
#INFO: litellm 代理运行在 http://0.0.0.0:8000
向您的代理发送请求
import openai
openai.api_key = "any-string-here"
openai.api_base = "http://0.0.0.0:8080" # 您的代理 URL
# 调用 gpt-3.5-turbo
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hey"}])
print(response)
# 调用 ollama/llama2
response = openai.ChatCompletion.create(model="ollama/llama2", messages=[{"role": "user", "content": "Hey"}])
print(response)
Continue-Dev 将 ChatGPT 引入 VSCode。查看如何在此处安装。
在 config.py 中将其设置为默认模型。
default=OpenAI(
api_key="IGNORED",
model="fake-model-name",
context_length=2048, # 根据需要自定义您的模型
api_base="http://localhost:8000" # 您的代理服务器 URL
),
Credits @vividfog for this tutorial.
$ pip install aider
$ aider --openai-api-base http://0.0.0.0:8000 --openai-api-key fake-key
pip install pyautogen
from autogen import AssistantAgent, UserProxyAgent, oai
config_list=[
{
"model": "my-fake-model",
"api_base": "http://localhost:8000", #litellm 兼容端点
"api_type": "open_ai",
"api_key": "NULL", # 只是一个占位符
}
]
response = oai.Completion.create(config_list=config_list, prompt="Hi")
print(response) # 运行良好
llm_config={
"config_list": config_list,
}
assistant = AssistantAgent("assistant", llm_config=llm_config)
user_proxy = UserProxyAgent("user_proxy")
user_proxy.initiate_chat(assistant, message="Plot a chart of META and TESLA stock price change YTD.", config_list=config_list)
Credits @victordibia for this tutorial.
from autogen import AssistantAgent, GroupChatManager, UserProxyAgent
from autogen.agentchat import GroupChat
config_list = [
{
"model": "ollama/mistralorca",
"api_base": "http://localhost:8000", # litellm兼容的端点
"api_type": "open_ai",
"api_key": "NULL", # 只是一个占位符
}
]
llm_config = {"config_list": config_list, "seed": 42}
code_config_list = [
{
"model": "ollama/phind-code",
"api_base": "http://localhost:8000", # litellm兼容的端点
"api_type": "open_ai",
"api_key": "NULL", # 只是一个占位符
}
]
code_config = {"config_list": code_config_list, "seed": 42}
admin = UserProxyAgent(
name="管理员",
system_message="一个人类管理员。与规划者互动以讨论计划。计划执行需要得到该管理员的批准。",
llm_config=llm_config,
code_execution_config=False,
)
engineer = AssistantAgent(
name="工程师",
llm_config=code_config,
system_message="""工程师。你遵循一个批准的计划。你编写用于解决任务的python/shell代码。将代码包裹在指定脚本类型的代码块中。用户无法修改你的代码。因此,不要建议需要其他人修改的不完整代码。如果不打算由执行者执行,请不要使用代码块。
不要在一个响应中包含多个代码块。不要要求其他人复制和粘贴结果。检查执行者返回的执行结果。
如果结果表明有错误,修复错误并再次输出代码。建议完整的代码而不是部分代码或代码更改。如果错误无法修复或即使代码成功执行任务仍未解决,分析问题,重新审视你的假设,收集你需要的额外信息,并考虑尝试不同的方法。
""",
)
planner = AssistantAgent(
name="规划者",
system_message="""规划者。提出一个计划。根据管理员和批评者的反馈修订计划,直到得到管理员的批准。
该计划可能涉及一个可以编写代码的工程师和一个不编写代码的科学家。
首先解释计划。明确指出哪一步由工程师执行,哪一步由科学家执行。
""",
llm_config=llm_config,
)
executor = UserProxyAgent(
name="执行者",
system_message="执行者。执行工程师编写的代码并报告结果。",
human_input_mode="NEVER",
llm_config=llm_config,
code_execution_config={"last_n_messages": 3, "work_dir": "paper"},
)
critic = AssistantAgent(
name="批评者",
system_message="批评者。仔细检查其他代理的计划、主张、代码并提供反馈。检查计划是否包括添加可验证的信息,如来源URL。",
llm_config=llm_config,
)
groupchat = GroupChat(
agents=[admin, engineer, planner, executor, critic],
messages=[],
max_round=50,
)
manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)
admin.initiate_chat(
manager,
message="""
""",
)
感谢@Nathan为本教程提供的帮助。
设置ChatDev(文档)
git clone https://github.com/OpenBMB/ChatDev.git
cd ChatDev
conda create -n ChatDev_conda_env python=3.9 -y
conda activate ChatDev_conda_env
pip install -r requirements.txt
使用代理运行ChatDev
export OPENAI_API_KEY="sk-1234"
export OPENAI_API_BASE="http://0.0.0.0:8000"
python3 run.py --task "a script that says hello world" --name "hello world"
pip install langroid
from langroid.language_models.openai_gpt import OpenAIGPTConfig, OpenAIGPT
# 配置LLM
my_llm_config = OpenAIGPTConfig(
#格式: "local/[LiteLLM代理监听的URL]"
chat_model="local/localhost:8000",
chat_context_length=2048, # 根据模型调整
)
# 创建llm,一次性交互
llm = OpenAIGPT(my_llm_config)
response = mdl.chat("What is the capital of China?", max_tokens=50)
# 使用此LLM创建一个代理,将其包装在一个任务中,并
# 将其作为交互式聊天应用运行:
from langroid.agent.base import ChatAgent, ChatAgentConfig
from langroid.agent.task import Task
agent_config = ChatAgentConfig(llm=my_llm_config, name="my-llm-agent")
agent = ChatAgent(agent_config)
task = Task(agent, name="my-llm-task")
task.run()
感谢@pchalasani和Langroid为本教程提供的帮助。
在你的.env文件中设置openai端点到你的本地服务器。
OPENAI_ENDPOINT=http://0.0.0.0:8000
OPENAI_API_KEY=my-fake-key
注意: Guidance会发送额外的参数,如stop_sequences,如果某些模型不支持这些参数,可能会导致失败。
修复:使用--drop_params标志启动你的代理
litellm --model ollama/codellama --temperature 0.3 --max_tokens 2048 --drop_params
import guidance
# 设置api_base到你的代理
# 设置api_key为任何值
gpt4 = guidance.llms.OpenAI("gpt-4", api_base="http://0.0.0.0:8000", api_key="anything")
experts = guidance('''
{{#system~}}
你是一个有帮助且简洁的助手。
{{~/system}}
{{#user~}}
我想要一个对以下问题的回答:
{{query}}
请列出3位世界级的专家(过去或现在),他们非常擅长回答这个问题?
暂时不要回答问题。
{{~/user}}
{{#assistant~}}
{{gen 'expert_names' temperature=0 max_tokens=300}}
{{~/assistant}}
''', llm=gpt4)
result = experts(query='如何提高生产力?')
print(result)
贡献 使用此服务器与项目?在此处贡献你的教程here!
高级
日志
$ litellm --logs
这将返回最近的日志(发送到LLM API的调用及接收到的响应)。
所有日志都保存到当前目录下的api_logs.json文件中。
配置代理
如果需要:
- 保存API密钥
- 设置litellm参数(例如,丢弃未映射的参数,设置备用模型等)
- 设置特定模型的参数(最大token数、温度、api基础、提示模板)
你可以通过cli仅为此会话设置这些参数,或通过配置文件跨重启持久化这些设置。
保存API密钥
$ litellm --api_key OPENAI_API_KEY=sk-...
LiteLLM将这些密钥保存到本地存储的配置文件中,并跨会话持久化。
LiteLLM Proxy支持所有litellm支持的api密钥。要为特定提供者添加密钥,请检查此列表:
- Huggingface
- Anthropic
- PerplexityAI
- TogetherAI
- Replicate
- Bedrock
- Palm
- Azure OpenAI
- AI21
- Cohere
$ litellm --add_key HUGGINGFACE_API_KEY=my-api-key #[可选]
$ litellm --add_key ANTHROPIC_API_KEY=my-api-key
$ litellm --add_key PERPLEXITYAI_API_KEY=my-api-key
$ litellm --add_key TOGETHERAI_API_KEY=my-api-key
$ litellm --add_key REPLICATE_API_KEY=my-api-key
$ litellm --add_key AWS_ACCESS_KEY_ID=my-key-id
$ litellm --add_key AWS_SECRET_ACCESS_KEY=my-secret-access-key
$ litellm --add_key PALM_API_KEY=my-palm-key
$ litellm --add_key AZURE_API_KEY=my-api-key
$ litellm --add_key AZURE_API_BASE=my-api-base
$ litellm --add_key AI21_API_KEY=my-api-key
$ litellm --add_key COHERE_API_KEY=my-api-key
例如:设置API基础、最大令牌数和温度。
对于该会话:
litellm --model ollama/llama2 \
--api_base http://localhost:11434 \
--max_tokens 250 \
--temperature 0.5
# 在 http://0.0.0.0:8000 上运行的兼容OpenAI的服务器
性能
我们使用wrk对FastAPI服务器进行了500,000次HTTP连接的负载测试,持续1分钟。
以下是我们的结果:
Thread Stats Avg Stdev Max +/- Stdev
Latency 156.38ms 25.52ms 361.91ms 84.73%
Req/Sec 13.61 5.13 40.00 57.50%
383625 requests in 1.00m, 391.10MB read
Socket errors: connect 0, read 1632, write 1, timeout 0
支持/与创始人交流
- 预约演示 👋
- 社区Discord 💭
- 我们的电话号码 📞 +1 (770) 8783-106 / +1 (412) 618-6238
- 我们的电子邮件 ✉️ ishaan@berri.ai / krrish@berri.ai