/completions
總覽
| 功能 | 支援 | 備註 |
|---|---|---|
| 成本追蹤 | ✅ | 適用於所有支援的模型 |
| 記錄 | ✅ | 可跨所有整合使用 |
| 終端使用者追蹤 | ✅ | |
| 串流 | ✅ | |
| 備援 | ✅ | 適用於支援的模型之間 |
| 負載平衡 | ✅ | 適用於支援的模型之間 |
| 防護欄 | ✅ | 套用於輸入提示詞與輸出文字(僅限非串流) |
| 支援的提供者 | 所有 Chat Completion 提供者 |
使用方式
- LiteLLM Python SDK
- LiteLLM Proxy Server
from litellm import text_completion
response = text_completion(
model="gpt-3.5-turbo-instruct",
prompt="Say this is a test",
max_tokens=7
)
- 在 config.yaml 中定義模型
model_list:
- model_name: gpt-3.5-turbo-instruct
litellm_params:
model: text-completion-openai/gpt-3.5-turbo-instruct # The `text-completion-openai/` prefix will call openai.completions.create
api_key: os.environ/OPENAI_API_KEY
- model_name: text-davinci-003
litellm_params:
model: text-completion-openai/text-davinci-003
api_key: os.environ/OPENAI_API_KEY
- 啟動 litellm proxy server
litellm --config config.yaml
- OpenAI Python SDK
- Curl Request
from openai import OpenAI
# set base_url to your proxy server
# set api_key to send to proxy server
client = OpenAI(api_key="<proxy-api-key>", base_url="http://0.0.0.0:4000")
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="Say this is a test",
max_tokens=7
)
print(response)
curl --location 'http://0.0.0.0:4000/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \
--data '{
"model": "gpt-3.5-turbo-instruct",
"prompt": "Say this is a test",
"max_tokens": 7
}'
輸入參數
LiteLLM 接受並轉換跨所有支援提供者的 OpenAI Text Completion 參數。
必填欄位
model: string - 要使用的模型 IDprompt: string or array - 要為其產生 completions 的 prompt
選用欄位
best_of: integer - 在伺服器端產生 best_of completions,並回傳「最佳」的一個echo: boolean - 除了 completion 之外,也將 prompt 回顯。frequency_penalty: number - 介於 -2.0 與 2.0 之間的數值。正值會根據新 tokens 的既有出現頻率加以懲罰。logit_bias: map - 修改指定 tokens 出現在 completion 中的可能性logprobs: integer - 在 logprobs 中包含最可能 tokens 的對數機率。最大值為 5max_tokens: integer - 要產生的最大 tokens 數量。n: integer - 每個 prompt 要產生多少個 completions。presence_penalty: number - 介於 -2.0 與 2.0 之間的數值。正值會根據 tokens 是否已出現在目前文字中加以懲罰。seed: integer - 若指定,系統將嘗試產生具決定性的樣本stop: string or array - 最多 4 個序列,API 將在這些序列處停止產生 tokensstream: boolean - 是否回傳部分進度串流。預設為 falsesuffix: string - 插入文字完成後接續的後綴temperature: number - 要使用的取樣溫度,介於 0 與 2 之間。top_p: number - 取樣溫度的替代方案,稱為 nucleus sampling。user: string - 代表您的終端使用者的唯一識別碼
輸出格式
以下是您可從 completion 呼叫預期得到的確切 JSON 輸出格式:
- 非串流回應
- 串流回應
{
"id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
"object": "text_completion",
"created": 1589478378,
"model": "gpt-3.5-turbo-instruct",
"system_fingerprint": "fp_44709d6fcb",
"choices": [
{
"text": "\n\nThis is indeed a test",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 7,
"total_tokens": 12
}
}
{
"id": "cmpl-7iA7iJjj8V2zOkCGvWF2hAkDWBQZe",
"object": "text_completion",
"created": 1690759702,
"choices": [
{
"text": "This",
"index": 0,
"logprobs": null,
"finish_reason": null
}
],
"model": "gpt-3.5-turbo-instruct"
"system_fingerprint": "fp_44709d6fcb",
}
支援的提供者
| 提供者 | 使用方式連結 |
|---|---|
| OpenAI | 使用方式 |
| Azure OpenAI | 使用方式 |