實戰-透過語言模型寫腳本

實戰-透過語言模型寫腳本

實戰-透過語言模型寫腳本

契機是看到591上的房貸計算器,又看到高處不勝寒的房市,於是想做個房價相關的計算器。直接寫程式有點無趣,如果完全透過語言模型來寫程式,應該會很有趣。

1.初次嘗試:直接用VS Code的GPT-4o,一次性把所有要求全部輸出,得到一大包塞在一起的function。初步驗算後,發現有一些位數上的小問題,只要稍微修改一下就可以使用。

def calculate_investment_return(total_price, down_payment_ratio, agent_fee_ratio=0.02, total_regulation_fee=0, renovation_fee=500000, management_fee_per_ping=70, total_pings=0, expected_price_increase=0.5, holding_years=5, land_value_increment_tax_rate=0.2):    # Calculate down payment    ...    # Calculate agent fee    ...    # Calculate total initial cash required    ...    # Calculate monthly mortgage payment    ...    # Calculate monthly management fee    ...    # Calculate total investment over holding period    ...    # Calculate expected selling price and net profit after tax    ...    # Calculate total return and annualized return    ...    return initial_cash_required, monthly_payment, monthly_management_fee, total_investment, total_return, annualized_return

2.功能拆分:要求拆成特定的小功能function,一個計算自備款,一個計算持有成本。這樣快速達成要求,並生成樣本可以看到輸出,效果很不錯。

def calculate_investment_return(house_price, buyer_preparation_ratio, agent_fee_ratio=0.02, total_regulation_fee=0, renovation_fee=500000):    ...    return initial_cash_requireddef calculate_holding_costs(total_price, buyer_preparation_ratio, management_fee_per_ping=70, total_pings=0, holding_years=5):    ...    return monthly_payment, monthly_management_fee, total_investmentdef calculate_profit(total_price, expected_price_increase=0.5, land_value_increment_tax_rate=0.2):    ...    return net_profitdef calculate_total_return(initial_cash_required, total_investment, net_profit, holding_years=5):    ...    return total_return, annualized_return# Calculate initial cash requiredinitial_cash_required = calculate_investment_return(total_price, down_payment_ratio, total_regulation_fee=total_regulation_fee, management_fee_per_ping=management_fee_per_ping, total_pings=total_pings, expected_price_increase=expected_price_increase, holding_years=holding_years, land_value_increment_tax_rate=land_value_increment_tax_rate)# Calculate holding costsmonthly_payment, monthly_management_fee, total_investment = calculate_holding_costs(total_price, down_payment_ratio, management_fee_per_ping=management_fee_per_ping, total_pings=total_pings, holding_years=holding_years)# Calculate profitnet_profit = calculate_profit(total_price, expected_price_increase=expected_price_increase, land_value_increment_tax_rate=land_value_increment_tax_rate)# Calculate total return and annualized returntotal_return, annualized_return = calculate_total_return(initial_cash_required, total_investment, net_profit, holding_years=holding_years)print(f"Initial cash required: {initial_cash_required} TWD")print(f"Monthly mortgage payment: {monthly_payment:.2f} TWD")print(f"Monthly management fee: {monthly_management_fee} TWD")print(f"Total investment over {holding_years} years: {total_investment} TWD")print(f"Total return: {total_return} TWD")print(f"Annualized return: {annualized_return}%")

3.增加難度並優化方法:一次要求寫一個功能特定的小程式,完成後再合成一個大的。變數的長度無限制增長,但優點是程式的命名甚至比人類還要優化,因此即使中間換過模型,也能夠簡單知道該程式要達成的目標。

# 無限增長的主程式...total_net_profit, annualized_return, annual_income_details = calculate_combined_profit(    total_price, buyer_preparation_ratio, management_fee_per_ping, total_pings, holding_years, grace_period_years, rental_period_months, occupancy_rate, monthly_rent, expected_price_increase, land_value_increment_tax_rate)

4.Clean Code優化:最後讓AI針對Clean Code進行一次優化,程式就完工了。結果非常精艷,一個程式如果完全手寫加上debug,可能要花上兩三個晚上的時間,但有AI輔助,幾個小時就可以完成,命名也可自我解讀,令人佩服。

心得: 讓AI去做它擅長的事情,就像有了電燈人們就減少燒煤,有了e-mail就減少實體信件,一切都在向更高效的方向前進。

學會與AI溝通和使用比以往更重要,要能把想法細化,解構問題成AI可以執行的、好理解的步驟,一步一步上階梯。

判斷力或debug能力也更加重要,不僅僅是寫test case來測試的debug,而是了解運作流程,能判斷輸出合理性,找到邏輯漏洞。

與人溝通也是如此,只有了解對方的需求,程序開發者才能知道程式的邊界,哪些可以做,哪些不能做,或是替代方案。

Comments

Loading comments…

Leave a Comment