चैट कम्प्लीशन मॉडल को क्वेरी करें

Python SDK के साथ Databricks

Avi Steinberg

Senior Software Engineer

सर्विंग एंडपॉइंट को क्वेरी करें

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import ChatMessage, ChatMessageRole
w = WorkspaceClient()
response = w.serving_endpoints.query(

name="databricks-meta-llama-3-3-70b-instruct",
messages=[ ChatMessage( role=ChatMessageRole.SYSTEM, content="You are a helpful assistant." ), ChatMessage(role=ChatMessageRole.USER, content="<your-question>"), ],
max_tokens=128) print(f"RESPONSE:\n{response.choices[0].message.content}")
1 https://databricks-sdk-py.readthedocs.io/en/stable/workspace/serving/serving_endpoints.html
Python SDK के साथ Databricks

ChatMessage

ChatMessage() क्लास के पैरामीटर:

ChatMessage:
  content: str
  role: ChatMessageRole

उदाहरण चैट मैसेज:

ChatMessage(
  role=ChatMessageRole.USER, 
  content="Can you summarize what happened in world war 1?"
)
Python SDK के साथ Databricks

ChatMessageRole

SYSTEM रोल के साथ ChatMessage भेजें:

ChatMessage(
  role=ChatMessageRole.SYSTEM,
  content="You are a helpful assistant."
)

USER रोल के साथ ChatMessage भेजें:

ChatMessage(
  role=ChatMessageRole.USER, 
  content="How do you use for loops in Python?"
)
Python SDK के साथ Databricks

चैट कम्प्लीशन LLM को क्वेरी करें

w = WorkspaceClient()
response = w.serving_endpoints.query( # Meta Llama 3 AI मॉडल को क्वेरी करें
  name="databricks-meta-llama-3-3-70b-instruct",

messages=[ ChatMessage( # भविष्य की क्वेरियों के जवाबों को प्रभावित करने के लिए "SYSTEM" रोल वाला मैसेज role=ChatMessageRole.SYSTEM, content=f"You are a helpful assistant. " ),
ChatMessage( # AI मॉडल से प्रश्न पूछने के लिए "USER" रोल वाला मैसेज role=ChatMessageRole.USER, content="Can you explain what a fibonacci sequence is?" ), ], max_tokens=128, # प्रतिक्रिया में मॉडल कितने शब्द इस्तेमाल कर सकता है, इसे सीमित करें )
# AI मॉडल की प्रतिक्रिया पार्स करें और सामग्री प्रिंट करें print(f"RESPONSE:\n{response.choices[0].message.content}")
1 https://docs.databricks.com/aws/en/machine-learning/model-serving/score-foundation-models
Python SDK के साथ Databricks

क्वेरी प्रतिक्रिया संरचना

Serving Endpoint क्वेरी प्रतिक्रिया पार्स करें:

from databricks.sdk import WorkspaceClient
response = w.serving_endpoints.query(
    name="model-name", 
    messages=[...])

print(
f"RESPONSE:\n{response.choices[0].message.content}"
)
RESPONSE:
The Fibonacci sequence is a series of 
numbers in which each number is the sum 
of the two preceding numbers, starting 
from 0 and 1. The sequence begins like 
this:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 
55, 89, 144...
To generate the next number in the 
sequence, you simply add the previous 
two numbers. For example:
* 0 + 1 = 1
* 1 + 1 = 2
* 1 + 2 = 3
Python SDK के साथ Databricks

उदाहरण: टेक्स्ट का सार लिखें

w = WorkspaceClient()
# Meta Llama 3 AI मॉडल को क्वेरी करें
response = w.serving_endpoints.query(
  name="databricks-meta-llama-3-3-70b-instruct",

messages=[ ChatMessage( # वैरिएबल some_long_text में सेव टेक्स्ट का सार देने के लिए एजेंट से कहें role=ChatMessageRole.USER, content=f"Summarize the text stored in {some_long_text}" ), ],
max_tokens=100, # प्रतिक्रिया में अधिकतम 100 शब्द सेट करें )
# AI मॉडल से पार्स की गई प्रतिक्रिया प्रिंट करें print(f"RESPONSE:\n{response.choices[0].message.content}")
Python SDK के साथ Databricks

उदाहरण: कंटेंट जनरेट करें

w = WorkspaceClient()
response = w.serving_endpoints.query(
  name="databricks-meta-llama-3-3-70b-instruct",

messages=[ ChatMessage(role=ChatMessageRole.SYSTEM, content=f"You are a ghost writer for famous country singers." ),
ChatMessage(role=ChatMessageRole.USER, content="Write the lyrics to a country song that takes place in Mississippi")], max_tokens=100) print( f"RESPONSE:\n{response.choices[0].message.content}" )
RESPONSE:
"Underneath the magnolia sky, where the
Mississippi River rolls by
I'm sittin' here, thinkin' 'bout you, with the
Delta blues in my soul tonight
The cypress trees are swayin' slow,
and the crickets are singin' our song
But without you, baby, this ol' 
Mississippi night just don't feel like home"
Python SDK के साथ Databricks

अभ्यास करते हैं!

Python SDK के साथ Databricks

Preparing Video For Download...