Delen via


Batch LLM-inferentie uitvoeren met behulp van AI Functions

Belangrijk

Deze functie is beschikbaar als openbare preview.

In dit artikel wordt beschreven hoe u batchdeductie uitvoert met behulp van AI Functions.

U kunt batchdeductie uitvoeren met behulp van taakspecifieke AI-functies of de functie voor algemeen gebruik, ai_query. De voorbeelden in dit artikel zijn gericht op de flexibiliteit van ai_query en hoe u deze kunt gebruiken in pijplijnen en werkstromen voor batchdeductie.

Er zijn twee hoofdmanieren om ai_query te gebruiken voor batchinference:

  • Batchinference met behulp van ai_query en door Databricks gehoste basismodellen: Wanneer u deze methode gebruikt, configureert Databricks een model-serving-eindpunt dat automatisch schaalt op basis van de werkbelasting. Bekijk welke vooraf ingerichte LLM's worden ondersteund.
  • Batchdeductie met behulp van ai_query en een model voor eindpunt dat u zelf configureert: deze methode is vereist voor batchdeductiewerkstromen die gebruikmaken van basismodellen die buiten Databricks worden gehost, verfijnde basismodellen of traditionele ML-modellen. Na de implementatie kan het eindpunt rechtstreeks worden gebruikt met ai_query. Zie Batch-inference met behulp van aangepaste modellen of bijgestelde basismodellen.

vereisten voor

  • Een werkruimte in een door Foundation Model-API's ondersteunde regio.
  • Querymachtiging voor de Delta-tabel in Unity Catalog die de gegevens bevat die u wilt gebruiken.
  • Stel de pipelines.channel in de tabeleigenschappen in op 'preview' om ai_query()te kunnen gebruiken. Zie Vereisten voor een voorbeeldquery.

Batch LLM-inferentie met behulp van ai_query en door Databricks gehoste basismodellen

Wanneer u een door Databricks gehost en vooraf ingericht fundamentmodel gebruikt voor batchinferentie, configureert Databricks namens u een ingericht doorvoerpunt dat automatisch wordt geschaald op basis van de werken.

Als u deze methode wilt gebruiken voor batch inferentie, specificeert u het volgende in uw aanvraag.

  • De vooraf geconfigureerde LLM die u wilt gebruiken in ai_query. Selecteer een van de ondersteunde vooraf geconfigureerde LLM's.
  • De Unity Catalog-invoertabel en uitvoertabel.
  • De modelprompt en eventuele modelparameters.
SELECT text, ai_query(
    "databricks-meta-llama-3-1-8b-instruct",
    "Summarize the given text comprehensively, covering key points and main ideas concisely while retaining relevant details and examples. Ensure clarity and accuracy without unnecessary repetition or omissions: " || text
) AS summary
FROM uc_catalog.schema.table;

Implementeer batch-inferentie pijplijnen

In deze sectie wordt beschreven hoe u AI-functies kunt integreren in andere Databricks-gegevens en AI-producten om volledige batchdeductiepijplijnen te bouwen. Deze pijplijnen kunnen end-to-end-werkstromen uitvoeren die opname, voorverwerking, deductie en naverwerking omvatten. Pijplijnen kunnen worden gemaakt in SQL of Python en worden geïmplementeerd als:

  • Geplande werkstromen met Databricks-werkstromen
  • Werkprocessen voor streaming-inferentie met Structured Streaming

Batch-inferentietaken automatiseren met behulp van Databricks werkstromen

Plan batch-inferentietaken en automatiseer AI-pijplijnen.

SQL

SELECT
   *,
   ai_query('databricks-meta-llama-3-3-70b-instruct', request => concat("You are an opinion mining service. Given a piece of text, output an array of json results that extracts key user opinions, a classification, and a Positive, Negative, Neutral, or Mixed sentiment about that subject.


AVAILABLE CLASSIFICATIONS
Quality, Service, Design, Safety, Efficiency, Usability, Price


Examples below:


DOCUMENT
I got soup. It really did take only 20 minutes to make some pretty good soup. The noises it makes when it's blending are somewhat terrifying, but it gives a little beep to warn you before it does that. It made three or four large servings of soup. It's a single layer of steel, so the outside gets pretty hot. It can be hard to unplug the lid without knocking the blender against the side, which is not a nice sound. The soup was good and the recipes it comes with look delicious, but I'm not sure I'll use it often. 20 minutes of scary noises from the kitchen when I already need comfort food is not ideal for me. But if you aren't sensitive to loud sounds it does exactly what it says it does..


RESULT
[
 {'Classification': 'Efficiency', 'Comment': 'only 20 minutes','Sentiment': 'Positive'},
 {'Classification': 'Quality','Comment': 'pretty good soup','Sentiment': 'Positive'},
 {'Classification': 'Usability', 'Comment': 'noises it makes when it's blending are somewhat terrifying', 'Sentiment': 'Negative'},
 {'Classification': 'Safety','Comment': 'outside gets pretty hot','Sentiment': 'Negative'},
 {'Classification': 'Design','Comment': 'Hard to unplug the lid without knocking the blender against the side, which is not a nice sound', 'Sentiment': 'Negative'}
]


DOCUMENT
", REVIEW_TEXT, '\n\nRESULT\n')) as result
FROM catalog.schema.product_reviews
LIMIT 10

Python


import json
from pyspark.sql.functions import expr

# Define the opinion mining prompt as a multi-line string.
opinion_prompt = """You are an opinion mining service. Given a piece of text, output an array of json results that extracts key user opinions, a classification, and a Positive, Negative, Neutral, or Mixed sentiment about that subject.

AVAILABLE CLASSIFICATIONS
Quality, Service, Design, Safety, Efficiency, Usability, Price

Examples below:

DOCUMENT
I got soup. It really did take only 20 minutes to make some pretty good soup.The noises it makes when it's blending are somewhat terrifying, but it gives a little beep to warn you before it does that.It made three or four large servings of soup.It's a single layer of steel, so the outside gets pretty hot. It can be hard to unplug the lid without knocking the blender against the side, which is not a nice sound.The soup was good and the recipes it comes with look delicious, but I'm not sure I'll use it often. 20 minutes of scary noises from the kitchen when I already need comfort food is not ideal for me. But if you aren't sensitive to loud sounds it does exactly what it says it does.

RESULT
[
 {'Classification': 'Efficiency', 'Comment': 'only 20 minutes','Sentiment': 'Positive'},
 {'Classification': 'Quality','Comment': 'pretty good soup','Sentiment': 'Positive'},
 {'Classification': 'Usability', 'Comment': 'noises it makes when it's blending are somewhat terrifying', 'Sentiment': 'Negative'},
 {'Classification': 'Safety','Comment': 'outside gets pretty hot','Sentiment': 'Negative'},
 {'Classification': 'Design','Comment': 'Hard to unplug the lid without knocking the blender against the side, which is not a nice sound', 'Sentiment': 'Negative'}
]

DOCUMENT
"""

# Escape the prompt so it can be safely embedded in the SQL expression.
escaped_prompt = json.dumps(opinion_prompt)

# Read the source table and limit to 10 rows.
df = spark.table("catalog.schema.product_reviews").limit(10)

# Apply the LLM inference to each row, concatenating the prompt, the review text, and the tail string.
result_df = df.withColumn(
    "result",
    expr(f"ai_query('databricks-meta-llama-3-3-70b-instruct', request => concat({escaped_prompt}, REVIEW_TEXT, '\\n\\nRESULT\\n'))")
)

# Display the result DataFrame.
display(result_df)

AI-functies met Structured Streaming

Pas AI-inferentie toe in near-real-time of micro-batchsituaties met behulp van ai_query en Structured Streaming.

Stap 1. Uw statische Delta-tabel lezen

Uw statische Delta-tabel lezen alsof deze een stroom was


from pyspark.sql import SparkSession
import pyspark.sql.functions as F

spark = SparkSession.builder.getOrCreate()

# Spark processes all existing rows exactly once in the first micro-batch.
df = spark.table("enterprise.docs")  # Replace with your table name containing enterprise documents
df.repartition(50).write.format("delta").mode("overwrite").saveAsTable("enterprise.docs")
df_stream = spark.readStream.format("delta").option("maxBytesPerTrigger", "50K").table("enterprise.docs")

# Define the prompt outside the SQL expression.
prompt = (
    "You are provided with an enterprise document. Summarize the key points in a concise paragraph. "
    "Do not include extra commentary or suggestions. Document: "
)

Stap 2. ai_query toepassen

Spark verwerkt dit slechts één keer voor statische gegevens, tenzij er nieuwe rijen in de tabel binnenkomen.


df_transformed = df_stream.select(
    "document_text",
    F.expr(f"""
      ai_query(
        'llama_3_8b',
        CONCAT('{prompt}', document_text)
      )
    """).alias("summary")
)

Stap 3: De samengevatte uitvoer schrijven

De samengevatte uitvoer naar een andere Delta-tabel schrijven


# Time-based triggers apply, but only the first trigger processes all existing static data.
query = df_transformed.writeStream \
    .format("delta") \
    .option("checkpointLocation", "/tmp/checkpoints/_docs_summary") \
    .outputMode("append") \
    .toTable("enterprise.docs_summary")

query.awaitTermination()

Batch-inferentie met behulp van aangepaste modellen of nauwkeurig afgestemde basis modellen

In de notebookvoorbeelden in deze sectie worden batchinferentiebelastingen gedemonstreerd die gebruikmaken van aangepaste of fijn afgestemde basismodellen om meerdere inputs te verwerken. Voor de voorbeelden is een bestaand model vereist dat een eindpunt biedt dat gebruikmaakt van Foundation Model-API's die zijn ingericht voor doorvoer.

LLM-batch-inferentie met behulp van een aangepast funderingsmodel

In het volgende voorbeeldnotebook wordt een geconfigureerd doorvoers-eindpunt gecreëerd en batch-LLM-inferentie uitgevoerd met behulp van Python en het Meta Llama 3.1 70B-model. Het biedt ook richtlijnen voor het benchmarken van uw workload voor batch-inferentie en het maken van een geprovisioneerd doorvoer model voor een service-eindpunt.

LLM batch-inferentie met een aangepast basismodel en een geconfigureerd throughput-eindpunt notebook

Notitieblok ophalen

LLM-batch-inferentie met behulp van een embeddingmodel

In het volgende voorbeeldnotitieblok wordt een geconfigureerd throughput-eindpunt gemaakt en batch LLM-inferentie uitgevoerd met behulp van Python en uw keuze uit de GTE Large (Engels) of BGE Large (Engels) insluitingsmodellen.

Insluitingen van LLM-batchinferenties met een geconfigureerd doorvoereindpunt in een notitieblok

Notitieboekje ophalen

Batch-inferentie en gestructureerde gegevensextractie

In het volgende voorbeeldnotebook ziet u hoe u eenvoudige gestructureerde gegevensextractie kunt uitvoeren met behulp van ai_query om onbewerkte, ongestructureerde gegevens te transformeren in georganiseerde, bruikbare informatie via geautomatiseerde extractietechnieken. In dit notebook wordt ook getoond hoe u Mosaic AI Agent Evaluation kunt gebruiken om de nauwkeurigheid te evalueren met behulp van gegevens uit de werkelijkheid.

Notebook voor batch-inferentie en gestructureerde gegevensextractie

Notitieblok ophalen

Batch-inferentie met behulp van BERT voor de herkenning van benoemde entiteiten

In het volgende notebook ziet u een voorbeeld van een traditionele ML-model batch-inferentie met behulp van BERT.

Batch-inferentie met behulp van BERT voor herkenning van benoemde entiteiten notebook

Notitieblok ophalen