Interroger un modèle de complétion de clavardage

Databricks avec le SDK Python

Avi Steinberg

Senior Software Engineer

Interroger un point de terminaison de service

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
Databricks avec le SDK Python

ChatMessage

Paramètres de la classe ChatMessage() :

ChatMessage:
  content: str
  role: ChatMessageRole

Exemple de message de clavardage :

ChatMessage(
  role=ChatMessageRole.USER, 
  content="Can you summarize what happened in world war 1?"
)
Databricks avec le SDK Python

ChatMessageRole

Envoyer un ChatMessage avec le rôle SYSTEM :

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

Envoyer un ChatMessage avec le rôle USER :

ChatMessage(
  role=ChatMessageRole.USER, 
  content="How do you use for loops in Python?"
)
Databricks avec le SDK Python

Interroger un modèle LLM de complétion de clavardage

w = WorkspaceClient()
response = w.serving_endpoints.query( # Interroger le modèle IA Meta Llama 3
  name="databricks-meta-llama-3-3-70b-instruct",

messages=[ ChatMessage( # Message avec le rôle « SYSTEM » pour influencer les réponses futures de l'agent role=ChatMessageRole.SYSTEM, content=f"You are a helpful assistant. " ),
ChatMessage( # Message avec le rôle « USER » pour poser une question au modèle IA role=ChatMessageRole.USER, content="Can you explain what a fibonacci sequence is?" ), ], max_tokens=128, # Limiter le nombre de mots que le modèle IA peut utiliser dans la réponse )
# Analyser la réponse du modèle IA et afficher le contenu print(f"RESPONSE:\n{response.choices[0].message.content}")
1 https://docs.databricks.com/aws/en/machine-learning/model-serving/score-foundation-models
Databricks avec le SDK Python

Structure de la réponse à la requête

Analyser la réponse d'un point de terminaison de service :

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
Databricks avec le SDK Python

Exemple : résumer un texte

w = WorkspaceClient()
# Interroger le modèle IA Meta Llama 3
response = w.serving_endpoints.query(
  name="databricks-meta-llama-3-3-70b-instruct",

messages=[ ChatMessage( # Demander à l'agent IA de résumer le texte stocké dans la variable some_long_text role=ChatMessageRole.USER, content=f"Summarize the text stored in {some_long_text}" ), ],
max_tokens=100, # Limiter la réponse à un maximum de 100 mots )
# Afficher la réponse analysée du modèle IA print(f"RESPONSE:\n{response.choices[0].message.content}")
Databricks avec le SDK Python

Exemple : générer du contenu

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"
Databricks avec le SDK Python

Passons à la pratique !

Databricks avec le SDK Python

Preparing Video For Download...