채팅 완성 모델 쿼리하기

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( # Query the Meta Llama 3 AI model
  name="databricks-meta-llama-3-3-70b-instruct",

messages=[ ChatMessage( # Message with "SYSTEM" role to influence how the agent responds to future queries role=ChatMessageRole.SYSTEM, content=f"You are a helpful assistant. " ),
ChatMessage( # Message with the "USER" role to ask the AI model a question role=ChatMessageRole.USER, content="Can you explain what a fibonacci sequence is?" ), ], max_tokens=128, # Limit the number of words that the AI model can use in the response )
# Parse the AI model response and print the content 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 다루기

쿼리 응답 구조

서빙 엔드포인트 쿼리 응답 파싱:

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()
# Query the Meta Llama 3 AI model
response = w.serving_endpoints.query(
  name="databricks-meta-llama-3-3-70b-instruct",

messages=[ ChatMessage( # Ask the AI agent to summarize the text stored in variable some_long_text role=ChatMessageRole.USER, content=f"Summarize the text stored in {some_long_text}" ), ],
max_tokens=100, # Set the maximum number of words in the response to 100 )
# Print the parsed response from the AI model 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...