إشعار
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تسجيل الدخول أو تغيير الدلائل.
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تغيير الدلائل.
توفر هذه المقالة إرشادات خطوة بخطوة لتكوين والاستعلام عن نقطة نهاية نموذج خارجي تخدم نماذج OpenAI للإكمال والدردشة والتضمينات باستخدام MLflow Deployments SDK. تعرف على المزيد حول النماذج الخارجية.
إذا كنت تفضل استخدام واجهة مستخدم العرض لإنجاز هذه المهمة، فشاهد إنشاء نقطة نهاية خدمة نموذج خارجي.
المتطلبات
- Databricks Runtime 13.0 ML أو أعلى.
- MLflow 2.9 أو أعلى.
- مفاتيح OpenAI API.
- تثبيت Databricks CLI الإصدار 0.205 أو أعلى.
(اختياري) الخطوة 0: تخزين مفتاح OpenAI API باستخدام Databricks Secrets CLI
يمكنك توفير مفاتيح API الخاصة بك إما كسلاسل نص عادي في الخطوة 3 أو باستخدام أسرار Azure Databricks.
لتخزين مفتاح OpenAI API كبيانات سرية، يمكنك استخدام Databricks Secrets CLI (الإصدار 0.205 وما فوق). يمكنك أيضا استخدام واجهة برمجة تطبيقات REST للبيانات السرية.
ينشئ التالي النطاق السري المسمى ، 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: إنشاء نقطة نهاية نموذج خارجي وإدارتها
هام
توضح أمثلة التعليمات البرمجية في هذا القسم استخدام معاينة عامة لنشر MLflow CRUD SDK.
لإنشاء نقطة نهاية نموذج خارجي لنموذج لغة كبير (LLM)، استخدم create_endpoint() الأسلوب من MLflow Deployments SDK. يمكنك أيضا إنشاء نقاط نهاية نموذج خارجي في واجهة مستخدم الخدمة.
ينشئ مقتطف التعليمات البرمجية التالي نقطة نهاية الإكمال ل 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 لنقطة النهاية وإصدار واجهة برمجة التطبيقات في 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: إرسال الطلبات إلى نقطة نهاية نموذج خارجي
هام
توضح أمثلة التعليمات البرمجية في هذا القسم استخدام أسلوب MLflow Deployments SDK predict() .
يمكنك إرسال طلبات الدردشة والإكمال والتضمين إلى نقطة نهاية نموذج خارجي باستخدام أسلوب MLflow Deployments SDK predict() .
يرسل التالي طلبا إلى gpt-3.5-turbo-instruct مستضاف من قبل OpenAI.
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 الذكاء الاصطناعي و Anthropic و Cohere و Amazon Bedrock و Google Cloud Vertex الذكاء الاصطناعي والمزيد. يمكنك مقارنة LLMs عبر الموفرين، مما يساعدك على تحسين دقة تطبيقاتك وسرعتها وتكلفتها باستخدام الذكاء الاصطناعي Playground.
ينشئ المثال التالي نقطة نهاية ل Anthropic claude-2 ويقارن إجابته بسؤال يستخدم OpenAI gpt-3.5-turbo-instruct. كلتا الاستجابتين لهما نفس التنسيق القياسي، مما يجعل من السهل مقارنتها.
إنشاء نقطة نهاية ل كلود-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
}
]