From c4c9cb9855836b62480006b50a9b14583a3c1dfa Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 18 Dec 2023 09:14:42 +0800 Subject: [PATCH 1/4] Add cost calculation for ask-code queries --- ask-code/ask-code.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/ask-code/ask-code.py b/ask-code/ask-code.py index 4c8c69d..5f1c982 100644 --- a/ask-code/ask-code.py +++ b/ask-code/ask-code.py @@ -1,6 +1,7 @@ import os import sys from chat.ask_codebase.chains.smart_qa import SmartQA +from chat.util.openai_util import ChatCompletionUsage sys.path.append(os.path.join(os.path.dirname(__file__), "..", "libs")) sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "libs")) @@ -24,10 +25,28 @@ def query(question, lsp_brige_port): ) # Print the answer + prices = { + "gpt-4": (0.03, 0.06), + "gpt-4-32k": (0.06, 0.12), + "gpt-3.5-turbo": (0.0015, 0.002), + "gpt-3.5-turbo-16k": (0.003, 0.004), + "claude-2": (0.01102, 0.03268), + "starchat-alpha": (0.0004, 0.0004), + "CodeLlama-34b-Instruct": (0.0008, 0.0008), + "llama-2-70b-chat": (0.001, 0.001), + "gpt-3.5-turbo-1106": (0.001, 0.002), + "gpt-4-1106-preview": (0.01, 0.03), + "gpt-4-1106-vision-preview": (0.01, 0.03), + 'others': (0.001, 0.002) + } print(answer[0]) - cost = int(float(answer[2].get("token_usage", {}).get("total_cost", 0)) / 0.7 * 10000) / 10000 - print(f"***/ask-code has costed approximately ${cost} USD for this question.***") - + spent_money = 0.0 + token_usages = answer[2].get("usages", []) + if len(token_usages) > 0: + for token_usage in token_usages: + price = prices.get(token_usage.model, prices['others']) + spent_money += (price[0] * token_usage.prompt_tokens)/1000 + (price[1] * token_usage.completion_tokens)/1000 + print(f"***/ask-code has costed approximately ${spent_money/0.7} USD for this question.***") def main(): try: From 34bda0398b81a747b6fb0c97230e497a2ed2eec0 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 18 Dec 2023 09:14:42 +0800 Subject: [PATCH 2/4] reformat --- ask-code/ask-code.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/ask-code/ask-code.py b/ask-code/ask-code.py index 5f1c982..7a6087c 100644 --- a/ask-code/ask-code.py +++ b/ask-code/ask-code.py @@ -27,27 +27,30 @@ def query(question, lsp_brige_port): # Print the answer prices = { "gpt-4": (0.03, 0.06), - "gpt-4-32k": (0.06, 0.12), - "gpt-3.5-turbo": (0.0015, 0.002), - "gpt-3.5-turbo-16k": (0.003, 0.004), - "claude-2": (0.01102, 0.03268), - "starchat-alpha": (0.0004, 0.0004), - "CodeLlama-34b-Instruct": (0.0008, 0.0008), - "llama-2-70b-chat": (0.001, 0.001), - "gpt-3.5-turbo-1106": (0.001, 0.002), - "gpt-4-1106-preview": (0.01, 0.03), - "gpt-4-1106-vision-preview": (0.01, 0.03), - 'others': (0.001, 0.002) + "gpt-4-32k": (0.06, 0.12), + "gpt-3.5-turbo": (0.0015, 0.002), + "gpt-3.5-turbo-16k": (0.003, 0.004), + "claude-2": (0.01102, 0.03268), + "starchat-alpha": (0.0004, 0.0004), + "CodeLlama-34b-Instruct": (0.0008, 0.0008), + "llama-2-70b-chat": (0.001, 0.001), + "gpt-3.5-turbo-1106": (0.001, 0.002), + "gpt-4-1106-preview": (0.01, 0.03), + "gpt-4-1106-vision-preview": (0.01, 0.03), + "others": (0.001, 0.002), } print(answer[0]) spent_money = 0.0 token_usages = answer[2].get("usages", []) if len(token_usages) > 0: for token_usage in token_usages: - price = prices.get(token_usage.model, prices['others']) - spent_money += (price[0] * token_usage.prompt_tokens)/1000 + (price[1] * token_usage.completion_tokens)/1000 + price = prices.get(token_usage.model, prices["others"]) + spent_money += (price[0] * token_usage.prompt_tokens) / 1000 + ( + price[1] * token_usage.completion_tokens + ) / 1000 print(f"***/ask-code has costed approximately ${spent_money/0.7} USD for this question.***") + def main(): try: if len(sys.argv) < 3: From 73c69afffac381f821b8bd223fb082b7bcf20c06 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 18 Dec 2023 09:14:42 +0800 Subject: [PATCH 3/4] reformat --- ask-code/ask-code.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ask-code/ask-code.py b/ask-code/ask-code.py index 7a6087c..0b08fde 100644 --- a/ask-code/ask-code.py +++ b/ask-code/ask-code.py @@ -1,7 +1,6 @@ import os import sys from chat.ask_codebase.chains.smart_qa import SmartQA -from chat.util.openai_util import ChatCompletionUsage sys.path.append(os.path.join(os.path.dirname(__file__), "..", "libs")) sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "libs")) From d37fbaa18bfbbc613d05e6bba2cbdbd677cb3935 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 18 Dec 2023 09:14:42 +0800 Subject: [PATCH 4/4] Update query function to calculate and print the cost of the question --- ask-code/ask-code.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ask-code/ask-code.py b/ask-code/ask-code.py index 0b08fde..75b579c 100644 --- a/ask-code/ask-code.py +++ b/ask-code/ask-code.py @@ -24,6 +24,7 @@ def query(question, lsp_brige_port): ) # Print the answer + # key is model name, value is (prompt_token_price, completion_token_price) prices = { "gpt-4": (0.03, 0.06), "gpt-4-32k": (0.06, 0.12), @@ -36,17 +37,17 @@ def query(question, lsp_brige_port): "gpt-3.5-turbo-1106": (0.001, 0.002), "gpt-4-1106-preview": (0.01, 0.03), "gpt-4-1106-vision-preview": (0.01, 0.03), + # if model not in above list, use this price "others": (0.001, 0.002), } print(answer[0]) spent_money = 0.0 token_usages = answer[2].get("usages", []) - if len(token_usages) > 0: - for token_usage in token_usages: - price = prices.get(token_usage.model, prices["others"]) - spent_money += (price[0] * token_usage.prompt_tokens) / 1000 + ( - price[1] * token_usage.completion_tokens - ) / 1000 + for token_usage in token_usages: + price = prices.get(token_usage.model, prices["others"]) + spent_money += (price[0] * token_usage.prompt_tokens) / 1000 + ( + price[1] * token_usage.completion_tokens + ) / 1000 print(f"***/ask-code has costed approximately ${spent_money/0.7} USD for this question.***")