我們一直在致力於一門新的AI 數據課程,向您展示如何透過將電子商務數據從Stripe 移動到在Supabase 上運行的PGVector 來構建AI 聊天機器人,透過Airbyte PGVector 連接器創建OpenAI 嵌入,使用OpenAI 用戶端程式庫將自然語言支援新增至應用程式。這是我們的許多客戶正在實施的一種非常常見的「智慧資料堆疊」應用程式模式。來源和目的地可能會發生變化,但模式(資料來源 > 行動資料並建立嵌入 > 支援向量的資料儲存 > 使用 OpenAI 的 Web 應用程式)保持不變。
由於我們正在開發旨在讓人們上手的課程,因此我們希望讓設定盡可能簡單。其中很大一部分是在 Stripe 中創建足夠的測試數據,這樣就有一個合理的數據集供聊天機器人與之互動。如果您以前使用過 Stripe,您就會知道他們有一個很棒的沙盒,您可以在其中進行試驗。唯一的問題是它沒有預先載入範例資料。
您可以透過 CLI 裝置指令載入一些範例資料集。但是,對於我們的使用來說,這些都不符合需求。我們想要一個更大的數據集,並且由於這些材料將在線上和研討會中使用,因此要求學習者在他們的本地電腦上安裝一些東西,例如CLI,會讓你面臨一大堆複雜的處理。您永遠不知道用戶正在運行什麼作業系統版本,他們是否擁有正確的安裝權限等等。我已經被燒傷太多次了,無法走這條路。
值得慶幸的是,Stripe 還擁有出色的 API 和出色的 Python 客戶端,這意味著我們可以快速創建協作筆記本,供學習者運行和插入我們想要的資料。
透過 !pip install stripe 安裝 stripe 庫並使用 Google Collab 金鑰傳遞測試金鑰後,我們必須為客戶和產品設定一些隨機名稱。目標是插入隨機集合的客戶、不同價格的產品和購買情況。這樣,當我們向聊天機器人詢問諸如“誰購買的商品最便宜?他們支付了多少錢?他們買了什麼?”之類的問題時,就會這樣。有足夠的數據。
import stripe import random from google.colab import userdata stripe.api_key = userdata.get('STRIPE_TEST_KEY') # Sample data for generating random names first_names = ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace", "Hank", "Ivy", "Jack", "Quinton", "Akriti", "Justin", "Marcos"] last_names = ["Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor", "Wall", "Chau", "Keswani", "Marx"] # Sample clothing product names clothing_names = [ "T-Shirt", "Jeans", "Jacket", "Sweater", "Hoodie", "Shorts", "Dress", "Blouse", "Skirt", "Pants", "Shoes", "Sandals", "Sneakers", "Socks", "Hat", "Scarf", "Gloves", "Coat", "Belt", "Tie", "Tank Top", "Cardigan", "Overalls", "Tracksuit", "Polo Shirt", "Cargo Pants", "Capris", "Dungarees", "Boots", "Cufflinks", "Raincoat", "Peacoat", "Blazer", "Slippers", "Underwear", "Leggings", "Windbreaker", "Tracksuit Bottoms", "Beanie", "Bikini" ] # List of random colors colors = [ "Red", "Blue", "Green", "Yellow", "Black", "White", "Gray", "Pink", "Purple", "Orange", "Brown", "Teal", "Navy", "Maroon", "Gold", "Silver", "Beige", "Lavender", "Turquoise", "Coral" ]
接下來,是時候為我們需要的 Stripe 中的每種資料類型添加函數了。
# Function to create sample customers with random names def create_customers(count=5): customers = [] for _ in range(count): first_name = random.choice(first_names) last_name = random.choice(last_names) name = f"{first_name} {last_name}" email = f"{first_name.lower()}.{last_name.lower()}@example.com" customer = stripe.Customer.create( name=name, email=email, description="Sample customer for testing" ) customers.append(customer) print(f"Created Customer: {customer['name']} (ID: {customer['id']})") return customers # Function to create sample products with random clothing names and colors def create_products(count=3): products = [] for _ in range(count): color = random.choice(colors) product_name = random.choice(clothing_names) full_name = f"{color} {product_name}" product = stripe.Product.create( name=full_name, description=f"This is a {color.lower()} {product_name.lower()}" ) products.append(product) print(f"Created Product: {product['name']} (ID: {product['id']})") return products # Function to create prices for the products with random unit_amount def create_prices(products, min_price=500, max_price=5000): prices = [] for product in products: unit_amount = random.randint(min_price, max_price) # Random amount in cents price = stripe.Price.create( unit_amount=unit_amount, currency="usd", product=product['id'] ) prices.append(price) print(f"Created Price: ${unit_amount / 100:.2f} for Product {product['name']} (ID: {price['id']})") return prices # Function to create random purchases for each customer def create_purchases(customers, prices, max_purchases_per_customer=5): purchases = [] for customer in customers: num_purchases = random.randint(1, max_purchases_per_customer) # Random number of purchases per customer for _ in range(num_purchases): price = random.choice(prices) # Randomly select a product's price purchase = stripe.PaymentIntent.create( amount=price['unit_amount'], # Amount in cents currency=price['currency'], customer=customer['id'], payment_method_types=["card"], # Simulate card payment description=f"Purchase of {price['product']} by {customer['name']}" ) purchases.append(purchase) print(f"Created Purchase for Customer {customer['name']} (Amount: ${price['unit_amount'] / 100:.2f})") return purchases
剩下的就是執行腳本並指定我們需要多少資料。
# Main function to create sample data def main(): print("Creating sample customers with random names...") customers = create_customers(count=20) print("\nCreating sample products with random clothing names and colors...") products = create_products(count=30) print("\nCreating prices for products with random amounts...") prices = create_prices(products, min_price=500, max_price=5000) print("\nCreating random purchases for each customer...") purchases = create_purchases(customers, prices, max_purchases_per_customer=10) print("\nSample data creation complete!") print(f"Created {len(customers)} customers, {len(products)} products, and {len(purchases)} purchases.") if __name__ == "__main__": main()
將資料載入到我們的 Stripe Sandbox 後,透過使用 Connector Builder 將 API 端點對應到每種資料類型的串流並設定同步作業,將其連接到 Airbyte 只需幾分鐘。
問題解決了!我們的 Collab Python 腳本對於學習者來說非常容易將測試資料插入 Stripe 中。希望對其他進行類似測試的人有所幫助。
以上是在 Python 中建立 Stripe 測試數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!