跳至主要內容

Meta Llama

屬性詳細資訊
說明Meta 的 Llama API 提供對 Meta 大型語言模型系列的存取。
LiteLLM 上的提供者路由meta_llama/
支援的端點/chat/completions, /completions, /responses
API 參考Llama API 參考 ↗

必要變數

Environment Variables
os.environ["LLAMA_API_KEY"] = ""  # your Meta Llama API key

支援的模型

資訊

此處列出的所有模型 https://llama.developer.meta.com/docs/models/ 都受支援。我們積極維護模型清單、token 視窗等資訊。在這裡

模型 ID輸入上下文長度輸出上下文長度輸入模態輸出模態
Llama-4-Scout-17B-16E-Instruct-FP8128k4028文字、圖片文字
Llama-4-Maverick-17B-128E-Instruct-FP8128k4028文字、圖片文字
Llama-3.3-70B-Instruct128k4028文字文字
Llama-3.3-8B-Instruct128k4028文字文字

使用方式 - LiteLLM Python SDK

非串流

Meta Llama Non-streaming Completion
import os
import litellm
from litellm import completion

os.environ["LLAMA_API_KEY"] = "" # your Meta Llama API key

messages = [{"content": "Hello, how are you?", "role": "user"}]

# Meta Llama call
response = completion(model="meta_llama/Llama-4-Maverick-17B-128E-Instruct-FP8", messages=messages)

串流

Meta Llama Streaming Completion
import os
import litellm
from litellm import completion

os.environ["LLAMA_API_KEY"] = "" # your Meta Llama API key

messages = [{"content": "Hello, how are you?", "role": "user"}]

# Meta Llama call with streaming
response = completion(
model="meta_llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
messages=messages,
stream=True
)

for chunk in response:
print(chunk)

函式呼叫

Meta Llama Function Calling
import os
import litellm
from litellm import completion

os.environ["LLAMA_API_KEY"] = "" # your Meta Llama API key

messages = [{"content": "What's the weather like in San Francisco?", "role": "user"}]

# Define the function
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
]

# Meta Llama call with function calling
response = completion(
model="meta_llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
messages=messages,
tools=tools,
tool_choice="auto"
)

print(response.choices[0].message.tool_calls)

工具使用

Meta Llama Tool Use
import os
import litellm
from litellm import completion

os.environ["LLAMA_API_KEY"] = "" # your Meta Llama API key

messages = [{"content": "Create a chart showing the population growth of New York City from 2010 to 2020", "role": "user"}]

# Define the tools
tools = [
{
"type": "function",
"function": {
"name": "create_chart",
"description": "Create a chart with the provided data",
"parameters": {
"type": "object",
"properties": {
"chart_type": {
"type": "string",
"enum": ["bar", "line", "pie", "scatter"],
"description": "The type of chart to create"
},
"title": {
"type": "string",
"description": "The title of the chart"
},
"data": {
"type": "object",
"description": "The data to plot in the chart"
}
},
"required": ["chart_type", "title", "data"]
}
}
}
]

# Meta Llama call with tool use
response = completion(
model="meta_llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
messages=messages,
tools=tools,
tool_choice="auto"
)

print(response.choices[0].message.content)

使用方式 - LiteLLM Proxy

請將下列內容新增至您的 LiteLLM Proxy 設定檔:

config.yaml
model_list:
- model_name: meta_llama/Llama-3.3-70B-Instruct
litellm_params:
model: meta_llama/Llama-3.3-70B-Instruct
api_key: os.environ/LLAMA_API_KEY

- model_name: meta_llama/Llama-3.3-8B-Instruct
litellm_params:
model: meta_llama/Llama-3.3-8B-Instruct
api_key: os.environ/LLAMA_API_KEY

啟動您的 LiteLLM Proxy 伺服器:

Start LiteLLM Proxy
litellm --config config.yaml

# RUNNING on http://0.0.0.0:4000
Meta Llama via Proxy - Non-streaming
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="http://localhost:4000", # Your proxy URL
api_key="your-proxy-api-key" # Your proxy API key
)

# Non-streaming response
response = client.chat.completions.create(
model="meta_llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
messages=[{"role": "user", "content": "Write a short poem about AI."}]
)

print(response.choices[0].message.content)
Meta Llama via Proxy - Streaming
from openai import OpenAI

# Initialize client with your proxy URL
client = OpenAI(
base_url="http://localhost:4000", # Your proxy URL
api_key="your-proxy-api-key" # Your proxy API key
)

# Streaming response
response = client.chat.completions.create(
model="meta_llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
messages=[{"role": "user", "content": "Write a short poem about AI."}],
stream=True
)

for chunk in response:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")

如需關於使用 LiteLLM Proxy 的更詳細資訊,請參閱 LiteLLM Proxy 文件

🚅
LiteLLM Enterprise
為正式環境打造的 SSO/SAML、稽核記錄、支出追蹤、多團隊管理與防護欄。
深入瞭解 →