我使用 Snowflake (SiS) 中的 Streamlit 製作了一個令牌計數檢查應用程式

DDD
發布: 2024-09-14 12:15:10
原創
635 人瀏覽過

介紹

您好,我是 Snowflake 的銷售工程師。我想透過各種貼文與大家分享我的一些經驗和實驗。在本文中,我將向您展示如何使用 Snowflake 中的 Streamlit 建立應用程式來檢查令牌計數並估算 Cortex LLM 的成本。

註:本文僅代表個人觀點,不代表Snowflake。

Snowflake (SiS) 中的 Streamlit 是什麼?

Streamlit 是一個 Python 函式庫,可讓您使用簡單的 Python 程式碼建立 Web UI,無需 HTML/CSS/JavaScript。您可以在應用程式庫中查看範例。

Snowflake 中的 Streamlit 可讓您直接在 Snowflake 上開發和執行 Streamlit Web 應用程式。只需一個 Snowflake 帳戶即可輕鬆使用,非常適合將 Snowflake 表資料整合到 Web 應用程式中。

關於 Snowflake 中的 Streamlit(官方 Snowflake 文件)

什麼是雪花皮層?

Snowflake Cortex 是 Snowflake 中的一套生成式 AI 功能。 Cortex LLM 允許您使用 SQL 或 Python 中的簡單函數呼叫在 Snowflake 上執行的大型語言模型。

大型語言模型 (LLM) 函數(Snowflake Cortex)(官方 Snowflake 文件)

功能概述

影像

I made a token count check app using Streamlit in Snowflake (SiS)

註:圖中文字來自芥川龍之介的《蜘蛛絲》。

特徵

  • 使用者可以選擇 Cortex LLM 模型
  • 顯示使用者輸入文字的字元和標記計數
  • 顯示標記與字元的比例
  • 根據 Snowflake 信用定價計算預估成本

注意:Cortex LLM 定價表 (PDF)

先決條件

  • 具有 Cortex LLM 存取權限的雪花帳戶
  • snowflake-ml-python 1.1.2 或更高版本

注意:Cortex LLM 區域可用性(官方 Snowflake 文件)

原始碼

import streamlit as st
from snowflake.snowpark.context import get_active_session
import snowflake.snowpark.functions as F

# Get current session
session = get_active_session()

# Application title
st.title("Cortex AI Token Count Checker")

# AI settings
st.sidebar.title("AI Settings")
lang_model = st.sidebar.radio("Select the language model you want to use",
                              ("snowflake-arctic", "reka-core", "reka-flash", 
                              "mistral-large2", "mistral-large", "mixtral-8x7b", "mistral-7b", 
                              "llama3.1-405b", "llama3.1-70b", "llama3.1-8b", 
                              "llama3-70b", "llama3-8b", "llama2-70b-chat", 
                              "jamba-instruct", "gemma-7b")
)

# Function to count tokens (using Cortex's token counting function)
def count_tokens(model, text):
    result = session.sql(f"SELECT SNOWFLAKE.CORTEX.COUNT_TOKENS('{model}', '{text}') as token_count").collect()
    return result[0]['TOKEN_COUNT']

# Token count check and cost calculation
st.header("Token Count Check and Cost Calculation")

input_text = st.text_area("Select a language model from the left pane and enter the text you want to check for token count:", height=200)

# Let user input the price per credit
credit_price = st.number_input("Enter the price per Snowflake credit (in dollars):", min_value=0.0, value=2.0, step=0.01)

# Credits per 1M tokens for each model (as of 2024/8/30, mistral-large2 is not supported)
model_credits = {
    "snowflake-arctic": 0.84,
    "reka-core": 5.5,
    "reka-flash": 0.45,
    "mistral-large2": 1.95,
    "mistral-large": 5.1,
    "mixtral-8x7b": 0.22,
    "mistral-7b": 0.12,
    "llama3.1-405b": 3,
    "llama3.1-70b": 1.21,
    "llama3.1-8b": 0.19,
    "llama3-70b": 1.21,
    "llama3-8b": 0.19,
    "llama2-70b-chat": 0.45,
    "jamba-instruct": 0.83,
    "gemma-7b": 0.12
}

if st.button("Calculate Token Count"):
    if input_text:
        # Calculate character count
        char_count = len(input_text)
        st.write(f"Character count of input text: {char_count}")

        if lang_model in model_credits:
            # Calculate token count
            token_count = count_tokens(lang_model, input_text)
            st.write(f"Token count of input text: {token_count}")

            # Ratio of tokens to characters
            ratio = token_count / char_count if char_count > 0 else 0
            st.write(f"Token count / Character count ratio: {ratio:.2f}")

            # Cost calculation
            credits_used = (token_count / 1000000) * model_credits[lang_model]
            cost = credits_used * credit_price

            st.write(f"Credits used: {credits_used:.6f}")
            st.write(f"Estimated cost: ${cost:.6f}")
        else:
            st.warning("The selected model is not supported by Snowflake's token counting feature.")
    else:
        st.warning("Please enter some text.")
登入後複製

結論

此應用程式可以更輕鬆地估計 LLM 工作負載的成本,尤其是在處理日語等語言時,字元數和標記數之間經常存在差距。我希望你覺得它有用!

公告

Snowflake 最新動態 X 更新

我正在分享 Snowflake 在 X 上的最新動態。如果您有興趣,請隨時關注!

英文版

雪花新鮮事機器人(英文版)
https://x.com/snow_new_en

日文版

雪花What's New Bot(日文版)
https://x.com/snow_new_jp

變更歷史記錄

(20240914) 初始貼文

日本原創文章

https://zenn.dev/tsubasa_tech/articles/4dd80c91508ec4

以上是我使用 Snowflake (SiS) 中的 Streamlit 製作了一個令牌計數檢查應用程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!