text_completion() / /completions

text_completion() / /completions

LiteLLM의 text_completion() 함수와 프록시 /completions 엔드포인트에 대한 문서예요. OpenAI 텍스트 완성 파라미터를 모든 지원 제공자에서 번역해 사용할 수 있어요.

출처: 문서

본문

개요

기능 지원 비고
비용 추적 모든 지원 모델과 동작
로깅 모든 통합에서 동작
최종 사용자 추적
스트리밍
폴백 지원 모델 간 동작
로드밸런싱 지원 모델 간 동작
가드레일 입력 프롬프트와 출력 텍스트에 적용 (비스트리밍 전용)
지원 제공자 모든 채팅 완성 제공자

사용법

  • LiteLLM Python SDK
  • LiteLLM 프록시 서버
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 프록시 서버 시작:

litellm --config config.yaml
  • OpenAI Python SDK
  • Curl 요청
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 ***" \
    --data '{
        "model": "gpt-3.5-turbo-instruct",
        "prompt": "Say this is a test",
        "max_tokens": 7
    }'

입력 파라미터

LiteLLM은 OpenAI 텍스트 완성 파라미터를 모든 지원 제공자에서 받아들이고 번역해요.

필수 필드

  • model: string - 사용할 모델 ID
  • prompt: string 또는 array - 완성을 생성할 프롬프트

선택 필드

  • best_of: integer - best_of 완성을 서버 측에서 생성하고 "최고"를 반환
  • echo: boolean - 완성과 함께 프롬프트를 에코 백
  • frequency_penalty: number - -2.0과 2.0 사이 숫자. 양수 값은 기존 빈도에 따라 새 토큰을 페널티
  • logit_bias: map - 완성에 지정된 토큰이 나타날 가능성 수정
  • logprobs: integer - 가장 가능성이 높은 logprobs 토큰의 로그 확률 포함. 최대 5
  • max_tokens: integer - 생성할 최대 토큰 수
  • n: integer - 각 프롬프트에 대해 생성할 완성 수
  • presence_penalty: number - -2.0과 2.0 사이 숫자. 양수 값은 지금까지 텍스트에 나타나는지에 따라 새 토큰을 페널티
  • seed: integer - 지정하면 시스템이 결정적 샘플을 시도
  • stop: string 또는 array - API가 토큰 생성을 멈출 시퀀스 최대 4개
  • stream: boolean - 부분 진행을 스트리밍할지 여부. 기본값 false
  • suffix: string - 삽입된 텍스트 완성 뒤에 오는 접미사
  • temperature: number - 사용할 샘플링 온도, 0에서 2 사이
  • top_p: number - 온도 샘플링의 대안인 nucleus sampling
  • user: string - 최종 사용자를 나타내는 고유 식별자

출력 형식

완성 호출에서 기대할 수 있는 정확한 JSON 출력 형식이에요: OpenAI 출력 형식을 따름

  • 비스트리밍 응답
  • 스트리밍 응답
{
  "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 Usage
Azure OpenAI Usage

더 알아보기 (Learn more)