设计多步 AI 工作流

Snowflake 生成式 AI 入门

James Cha-Earley

Senior Developer Advocate, Snowflake

Cortex 评论工作流

目标:搭建国际化住客反馈自动化系统

$$

  • 将每条评论翻译为英文
  • 提炼要点摘要
  • 分类并路由到对应团队
  • 生成回复
  • 翻回原语言

一个流程:收到评论后,翻译、摘要与分类,并发送回复

Snowflake 生成式 AI 入门

提取评论

-- SQL cell
SELECT DESCRIPTION
FROM HOTELS.REVIEWS
WHERE LANGUAGE = 'es'
LIMIT 1;
# Python cell
df = cell1.to_pandas()
review_text = df["DECRIPTION"].iloc[0]
Snowflake 生成式 AI 入门

西班牙语评论

print(review_text)
Buen hotel y bien situado pero desafortunadamente no me toco buena suerte con 
el servicio; el aire no servía; pedí la cama extra tres veces; la cafetera estaba
dañada; y me sacaron las maletas fuera porque argumentaron que no hice Check out;
siendo que era un día despues; pero su sistema lo marco antes; una total
descortesía hecharme del cuarto y cancelar mis llaves; regresar de caminar y darse
cuenta que han tomado tus cosas fuera es increíble y mas aun sin ninguna disculpa;
yo no volvería ahí aunque la vrd el hotel es bueno y su jubilación el trato
me decepciono
Snowflake 生成式 AI 入门

翻译

translated = translate(
    text=review_text,
    from_language="es",
    to_language="en"
)

print(translated)
Good hotel and well located, but unfortunately, I didn't have good luck with the service; the air
conditioning didn't work; I asked for the extra bed three times; the coffee maker was broken; 
and they took my bags out because they argued that I hadn't checked out; even though it was a day
later; but their system marked it as checked out; a total discourtesy to throw me out of the room
and cancel my keys; returning from a walk and realizing they've taken your things out is
incredible and even more so without any apology; I wouldn't go back there even though the hotel
itself is good and their retirement the treatment disappoints me.
Snowflake 生成式 AI 入门

摘要

summary = summarize(text=translated)

print(summary)
酒店位置不错,但服务令人失望。空调不工作,多次请求也未提供加床,咖啡机损坏。酒店工作人员在仍未到退房时间时拿走行李并注销房卡,做法失礼,令投诉者感到失望。
Snowflake 生成式 AI 入门

分类

topic = classify_text(
    text=summary,
    labels=["staff", "cleanliness", "pricing", "room", "food"]
)

print(topic)
{
  "label": "staff"
}
Snowflake 生成式 AI 入门

文本生成

response = complete(
        prompt=f"Write a brief and professional response to this review: {summary}",
        model='llama3.1-8b',
        options={'temperature':0.3, 'max_tokens':120})

print(response)
感谢您的反馈。很高兴您认可我们的地理位置,但对您遇到的服务问题深感抱歉。我们理解这给您带来的不便与挫败。您的意见已转达给相关团队,我们将改进,避免类似情况再次发生。
Snowflake 生成式 AI 入门

回复翻译

translated_response = translate(
    text=response,
    from_language="en",
    to_language="es"
)

print(translated_response)
Gracias por compartir sus comentarios. Si bien nos alegra saber que encontró conveniente
la ubicación, lamentamos sinceramente los inconvenientes que experimentó con el servicio.
Entendemos lo frustrante que debió haber sido enfrentar múltiples inconvenientes durante
su estadía. Sus comentarios han sido compartidos con el equipo para asegurarnos de que
estas situaciones se aborden y no vuelvan a ocurrir.
Snowflake 生成式 AI 入门

Cortex 成本模型

一个 Cortex 工作流,显示输入 tokens、计算和输出 tokens 都有相应成本

1 图像由 ChatGPT-4o 生成
Snowflake 生成式 AI 入门

控制成本

$$

  • 精简输入,仅处理相关文本

  • 限制输出长度

  • 降低 temperature

# 限制 complete 的成本
complete(prompt=prompt, 
         model='llama3.1-8b', 
         options={
            'max_tokens':120,

'temperature':0})
Snowflake 生成式 AI 入门

Cortex 最佳实践

  • 高效串联模型
# 若后续有多步调用,先做摘要
summarize()
text_classify()
complete()
translate()
  • 日志
  • 缓存
  • 批处理流水线
Snowflake 生成式 AI 入门

让我们来练习!

Snowflake 生成式 AI 入门

Preparing Video For Download...