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}")
ChatMessage() क्लास के पैरामीटर:
ChatMessage:
content: str
role: ChatMessageRole
उदाहरण चैट मैसेज:
ChatMessage(
role=ChatMessageRole.USER,
content="Can you summarize what happened in world war 1?"
)
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?"
)
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}")
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
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}")
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