자습서: OpenAI 모델을 쿼리하는 외부 모델 엔드포인트 만들기
이 문서에서는 MLflow 배포 SDK를 사용하여 완성, 채팅 및 포함을 위해 OpenAI 모델을 제공하는 외부 모델 엔드포인트를 구성하고 쿼리하기 위한 단계별 지침을 제공합니다. 외부 모델에 대해 자세히 알아봅니다.
서비스 UI를 사용하여 이 작업을 수행하려면 엔드포인트를 제공하는 외부 모델 만들기를 참조하세요.
요구 사항
- Databricks Runtime 13.0 ML 이상.
- MLflow 2.9 이상
- OpenAI API 키.
- Databricks CLI 버전 0.205 이상을 설치합니다.
(선택 사항) 0단계: Databricks 비밀 CLI를 사용하여 OpenAI API 키 저장
3단계에서 또는 Azure Databricks 비밀을 사용하여 API 키를 일반 텍스트 문자열로 제공할 수 있습니다.
OpenAI API 키를 비밀로 저장하려면 Databricks 비밀 CLI(버전 0.205 이상)를 사용할 수 있습니다. 비밀에 REST API를 사용할 수도 있습니다.
다음은 명명된 비밀 범위를 만든 다음 해당 my_openai_secret_scope
범위에 비밀을 openai_api_key
만듭니다.
databricks secrets create-scope my_openai_secret_scope
databricks secrets put-secret my_openai_secret_scope openai_api_key
1단계: 외부 모델 지원을 사용하여 MLflow 설치
다음을 사용하여 외부 모델 지원을 사용하여 MLflow 버전을 설치합니다.
%pip install mlflow[genai]>=2.9.0
2단계: 외부 모델 엔드포인트 만들기 및 관리
Important
이 섹션의 코드 예제에서는 공개 미리 보기 MLflow 배포 CRUD SDK를 사용하는 방법을 보여 줍니다.
LLM(대규모 언어 모델)에 대한 외부 모델 엔드포인트를 만들려면 MLflow 배포 SDK의 메서드를 사용합니다 create_endpoint()
. 서비스 UI에서 외부 모델 엔드포인트를 만들 수도 있습니다.
다음 코드 조각은 구성 섹션에 지정된 대로 OpenAI gpt-3.5-turbo-instruct
에 served_entities
대한 완성 엔드포인트를 만듭니다. 엔드포인트에는 name
과 openai_api_key
을 각각의 필드에 고유한 값으로 꼭 채워야 합니다.
import mlflow.deployments
client = mlflow.deployments.get_deploy_client("databricks")
client.create_endpoint(
name="openai-completions-endpoint",
config={
"served_entities": [{
"name": "openai-completions",
"external_model": {
"name": "gpt-3.5-turbo-instruct",
"provider": "openai",
"task": "llm/v1/completions",
"openai_config": {
"openai_api_key": "{{secrets/my_openai_secret_scope/openai_api_key}}"
}
}
}]
}
)
다음 코드 조각에서는 위와 동일한 완성 엔드포인트를 만드는 다른 방법을 위해 OpenAI API 키를 일반 텍스트 문자열로 제공하는 방법을 보여 줍니다.
import mlflow.deployments
client = mlflow.deployments.get_deploy_client("databricks")
client.create_endpoint(
name="openai-completions-endpoint",
config={
"served_entities": [{
"name": "openai-completions",
"external_model": {
"name": "gpt-3.5-turbo-instruct",
"provider": "openai",
"task": "llm/v1/completions",
"openai_config": {
"openai_api_key_plaintext": "sk-yourApiKey"
}
}
}]
}
)
Azure OpenAI를 사용하는 경우 구성 섹션에서 Azure OpenAI 배포 이름, 엔드포인트 URL 및 API 버전을 openai_config
지정할 수도 있습니다.
client.create_endpoint(
name="openai-completions-endpoint",
config={
"served_entities": [
{
"name": "openai-completions",
"external_model": {
"name": "gpt-3.5-turbo-instruct",
"provider": "openai",
"task": "llm/v1/completions",
"openai_config": {
"openai_api_type": "azure",
"openai_api_key": "{{secrets/my_openai_secret_scope/openai_api_key}}",
"openai_api_base": "https://my-azure-openai-endpoint.openai.azure.com",
"openai_deployment_name": "my-gpt-35-turbo-deployment",
"openai_api_version": "2023-05-15"
},
},
}
],
},
)
엔드포인트를 업데이트하려면 update_endpoint()
사용합니다. 다음 코드 조각은 엔드포인트의 속도 제한을 사용자당 분당 20개의 호출로 업데이트하는 방법을 보여 줍니다.
client.update_endpoint(
endpoint="openai-completions-endpoint",
config={
"rate_limits": [
{
"key": "user",
"renewal_period": "minute",
"calls": 20
}
],
},
)
3단계: 외부 모델 엔드포인트에 요청 보내기
Important
이 섹션의 코드 예제에서는 MLflow Deployments SDK 메서드를 predict()
사용하는 방법을 보여 줍니다.
MLflow Deployments SDK의 predict()
메서드를 사용하여 외부 모델 엔드포인트에 채팅, 완료 및 포함 요청을 보낼 수 있습니다.
다음은 OpenAI에서 호스트하는 gpt-3.5-turbo-instruct
요청을 보냅니다.
completions_response = client.predict(
endpoint="openai-completions-endpoint",
inputs={
"prompt": "What is the capital of France?",
"temperature": 0.1,
"max_tokens": 10,
"n": 2
}
)
completions_response == {
"id": "cmpl-8QW0hdtUesKmhB3a1Vel6X25j2MDJ",
"object": "text_completion",
"created": 1701330267,
"model": "gpt-3.5-turbo-instruct",
"choices": [
{
"text": "The capital of France is Paris.",
"index": 0,
"finish_reason": "stop",
"logprobs": None
},
{
"text": "Paris is the capital of France",
"index": 1,
"finish_reason": "stop",
"logprobs": None
},
],
"usage": {
"prompt_tokens": 7,
"completion_tokens": 16,
"total_tokens": 23
}
}
4단계: 다른 공급자의 모델 비교
모델 제공은 Open AI, Anthropic, Cohere, Amazon Bedrock, Google Cloud Vertex AI 등을 비롯한 많은 외부 모델 공급자를 지원합니다. 공급자 간에 LLM을 비교할 수 있으므로 AI Playground사용하여 애플리케이션의 정확도, 속도 및 비용을 최적화할 수 있습니다.
다음 예제에서는 Anthropic claude-2
에 대한 엔드포인트를 만들고 해당 응답을 OpenAI gpt-3.5-turbo-instruct
를 사용하는 질문과 비교합니다. 두 응답 모두 표준 형식이 같으므로 쉽게 비교할 수 있습니다.
Anthropic claude-2에 대한 엔드포인트 만들기
import mlflow.deployments
client = mlflow.deployments.get_deploy_client("databricks")
client.create_endpoint(
name="anthropic-completions-endpoint",
config={
"served_entities": [
{
"name": "claude-completions",
"external_model": {
"name": "claude-2",
"provider": "anthropic",
"task": "llm/v1/completions",
"anthropic_config": {
"anthropic_api_key": "{{secrets/my_anthropic_secret_scope/anthropic_api_key}}"
},
},
}
],
},
)
각 엔드포인트의 응답 비교
openai_response = client.predict(
endpoint="openai-completions-endpoint",
inputs={
"prompt": "How is Pi calculated? Be very concise."
}
)
anthropic_response = client.predict(
endpoint="anthropic-completions-endpoint",
inputs={
"prompt": "How is Pi calculated? Be very concise."
}
)
openai_response["choices"] == [
{
"text": "Pi is calculated by dividing the circumference of a circle by its diameter."
" This constant ratio of 3.14159... is then used to represent the relationship"
" between a circle's circumference and its diameter, regardless of the size of the"
" circle.",
"index": 0,
"finish_reason": "stop",
"logprobs": None
}
]
anthropic_response["choices"] == [
{
"text": "Pi is calculated by approximating the ratio of a circle's circumference to"
" its diameter. Common approximation methods include infinite series, infinite"
" products, and computing the perimeters of polygons with more and more sides"
" inscribed in or around a circle.",
"index": 0,
"finish_reason": "stop",
"logprobs": None
}
]