Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from utils import promptGPT
- import requests
- import os
- import re
- alert_api_key = os.getenv("ALERT_API_KEY")
- from coingecko_api import get_price
- from embeddings import get_similar_embeddings
- # should we break up prompt into 2 gpts? One to decide which alert, another to specify template with more targeted examples -> would the extra step cause unnecessary time/computation?
- system_prompt_template = """As an AI, you have the capability to create crypto alerts. There are three different types of alerts you can create: price alerts, percent price alerts, and periodic price alerts.
- For price alerts, a user will provide you with a coin name, price, and direction. For percent price alerts, a user will provide you with a coin name, percent, direction, and time window.
- For periodic price alerts, a user will provide you with a coin name and time interval. Your job is to decide which of these three alerts the user input corresponds to and fill in the JSON templates provided below.
- Respond with ONLY the filled JSON output.
- PRICE ALERT TEMPLATE:
- {
- "type": "price",
- "currency": "{coin name}",
- "target_currency": "USD",
- "price": "{price}",
- "channel": { "name": "webhook" },
- "exchange": "Coinbase"
- }
- EXAMPLE INPUT: "Notify me when ETH reaches above $2000"
- EXAMPLE OUTPUT:
- {
- "type": "price",
- "currency": "ETH",
- "target_currency": "USD",
- "price": "2000",
- "channel": { "name": "webhook" },
- "exchange": "Coinbase"
- }
- PERCENT PRICE ALERT TEMPLATE:
- {
- "type": "percent_price",
- "currency": "{coin name}",
- "percent": "{percent}",
- "direction": "up OR down",
- "window": "{time window}", SYSTEM NOTE: unit of measurement in minutes
- "channel": { "name": "webhook" },
- "exchange": "Coinbase"
- }
- EXAMPLE INPUT: "Let me know when bitcoin increases by 10% in the last hour"
- EXAMPLE OUTPUT:
- {
- "type": "percent_price",
- "currency": "BTC",
- "percent": "10",
- "direction": "up",
- "window": "60",
- "channel": { "name": "webhook" },
- "exchange": "Coinbase"
- }
- PERIODIC PRICE ALERT TEMPLATE:
- {
- "type": "periodic_price",
- "currency": "{coin name}",
- "target_currency": "USD",
- "window": "{time interval}", SYSTEM NOTE: unit of measurement in minutes
- "channel": { "name": "webhook" },
- "exchange": "Coinbase"
- }
- EXAMPLE INPUT: "Give me the price of chainlink every day"
- EXAMPLE OUTPUT:
- {
- "type": "periodic_price",
- "currency": "LINK",
- "target_currency": "USD",
- "window": "1440",
- "channel": { "name": "webhook" },
- "exchange": "Coinbase"
- }
- Here are more relevant examples:
- {EXAMPLES_STRING}
- """
- # Given the GPT response for the coin name, check if it exists in our coingecko coins list and return the corresponding symbol and id if so
- def get_symbol_and_id(coin_name):
- with open("market-data/coin_ids.json") as json_file:
- coin_ids = json.load(json_file)
- # GPT response has correct coin symbol
- check_symbol = next(filter(lambda d: d.get("symbol").upper() == coin_name.upper(), coin_ids), None)
- if check_symbol != None:
- return check_symbol["symbol"].upper(), check_symbol["id"]
- # GPT response has correct name
- check_name = next(filter(lambda d: d.get("name").upper() == coin_name.upper(), coin_ids), None)
- if check_name != None:
- return check_name["symbol"].upper(), check_name["id"]
- # Does not exist in list
- return "",""
- def get_direction(coin_id, desired_price):
- current_price = get_price(coin_id)
- print(current_price)
- return 'above' if (current_price < desired_price) else 'below'
- def alert_LLM(user_input):
- examples = get_similar_embeddings(user_input, 4, vector_dir='vectordb-alerts')
- print('retreived relevant alert example embeddings. num of examples: ', len(examples))
- EXAMPLES_STRING = ""
- for example in examples:
- EXAMPLES_STRING += example
- system_prompt = system_prompt_template.replace("{EXAMPLES_STRING}", EXAMPLES_STRING)
- llm_result = promptGPT(system_prompt, user_input)
- print('alert llm result: ', llm_result)
- #general top level curly command match
- command_match = re.search(r'{.*}', llm_result, re.DOTALL)
- command_json_str = command_match.group(0)
- json_llm_result = json.loads(command_json_str)
- coin_symbol, coin_id = get_symbol_and_id(json_llm_result["currency"])
- if coin_symbol == "":
- print("This coin is not supported")
- return
- json_llm_result["currency"] = coin_symbol
- return_comment = None
- if json_llm_result["type"] == "price":
- json_llm_result["direction"] = get_direction(coin_id, int(json_llm_result["price"]))
- return_comment = f'alert set for {json_llm_result["currency"]} at {json_llm_result["price"]}'
- elif json_llm_result["type"] == "periodic_price":
- f'alert set for {json_llm_result["currency"]} periodically every {json_llm_result["window"]} minute'
- else:
- return_comment = f'alert set for when {json_llm_result["currency"]} goes {json_llm_result["direction"]} {json_llm_result["percent"]} percent'
- headers = {
- "Content-Type": "application/json",
- }
- response = requests.post("https://api.cryptocurrencyalerting.com/v1/alert-conditions", auth=(alert_api_key, ""),
- headers=headers, json=json_llm_result)
- print('alert status code: ', response.status_code)
- print(response.json())
- if response.status_code == 200:
- print('returning: ', return_comment)
- return_comment
- else:
- if response:
- text_json = response.text
- text = json.loads(text_json)
- return f'unable to set alert at this time {text["message"]}'
- def get_price(coin_id):
- url = f'https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd'
- headers = {'accept': 'application/json'}
- response = requests.get(url, headers=headers)
- return response.json()[coin_id]['usd']
- def get_coin_ids_list():
- url = "https://api.coingecko.com/api/v3/coins/list"
- response = requests.get(url)
- # alert_LLM("Create an alert when eth reaches $1")
- # alert_LLM("let me know when the price of eth hits 1901")
- # alert_LLM("Notify me if DOGE price falls by 10% within 30 minutes")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement