2023-11-30 07:57:01 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2024-01-05 12:27:57 +08:00
|
|
|
|
2023-11-30 07:57:01 +08:00
|
|
|
from chat.ask_codebase.chains.smart_qa import SmartQA
|
|
|
|
|
2024-05-09 21:40:09 +08:00
|
|
|
from lib.ide_service import IDEService
|
2023-11-30 07:57:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def query(question, lsp_brige_port):
|
|
|
|
root_path = os.getcwd()
|
2023-12-08 18:28:36 +08:00
|
|
|
|
2023-11-30 07:57:01 +08:00
|
|
|
# Create an instance of SmartQA
|
|
|
|
smart_qa = SmartQA(root_path)
|
|
|
|
|
|
|
|
# Use SmartQA to get the answer
|
2023-12-08 18:28:36 +08:00
|
|
|
answer = smart_qa.run(
|
|
|
|
question=question,
|
|
|
|
verbose=False,
|
|
|
|
dfs_depth=3,
|
|
|
|
dfs_max_visit=10,
|
|
|
|
bridge_url=f"http://localhost:{lsp_brige_port}",
|
|
|
|
)
|
2023-11-30 07:57:01 +08:00
|
|
|
|
|
|
|
# Print the answer
|
2023-12-18 09:14:42 +08:00
|
|
|
# key is model name, value is (prompt_token_price, completion_token_price)
|
2023-12-18 09:14:42 +08:00
|
|
|
prices = {
|
|
|
|
"gpt-4": (0.03, 0.06),
|
2023-12-18 09:14:42 +08:00
|
|
|
"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),
|
2023-12-18 09:14:42 +08:00
|
|
|
# if model not in above list, use this price
|
2023-12-18 09:14:42 +08:00
|
|
|
"others": (0.001, 0.002),
|
2023-12-18 09:14:42 +08:00
|
|
|
}
|
2023-11-30 07:57:01 +08:00
|
|
|
print(answer[0])
|
2023-12-18 09:14:42 +08:00
|
|
|
spent_money = 0.0
|
|
|
|
token_usages = answer[2].get("usages", [])
|
2023-12-18 09:14:42 +08:00
|
|
|
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
|
2023-12-27 15:42:51 +08:00
|
|
|
spent_money = spent_money / 0.7
|
2023-12-27 15:26:17 +08:00
|
|
|
print(f"***/ask-code has costed approximately ${spent_money:.4f} USD for this question.***")
|
2023-11-30 07:57:01 +08:00
|
|
|
|
2023-12-18 09:14:42 +08:00
|
|
|
|
2023-11-30 07:57:01 +08:00
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
if len(sys.argv) < 3:
|
|
|
|
print("Usage: python index_and_query.py query [question] [port]")
|
|
|
|
sys.exit(1)
|
2023-12-08 18:28:36 +08:00
|
|
|
|
2024-02-03 23:35:09 +08:00
|
|
|
port = IDEService().get_lsp_brige_port()
|
2023-12-08 18:28:36 +08:00
|
|
|
|
2023-11-30 07:57:01 +08:00
|
|
|
question = sys.argv[2]
|
|
|
|
query(question, port)
|
|
|
|
sys.exit(0)
|
|
|
|
except Exception as e:
|
|
|
|
print("Exception: ", e, file=sys.stderr, flush=True)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-12-08 18:28:36 +08:00
|
|
|
main()
|