nursimakgul's picture
Upload 4 files
dd98846 verified
Raw
History Blame Contribute Delete
4.64 kB
"""Tool fonksiyonlari ve JSON semalari. app.py bunlari kullanir."""
import requests
COINGECKO = "https://api.coingecko.com/api/v3"
# CoinGecko'da sik kullanilan coin isimleri -> id eslemesi (kullanici 'btc' yazarsa da anlasin)
COIN_ALIAS = {
"btc": "bitcoin", "bitcoin": "bitcoin",
"eth": "ethereum", "ethereum": "ethereum",
"usdt": "tether", "tether": "tether",
"bnb": "binancecoin", "binancecoin": "binancecoin",
"sol": "solana", "solana": "solana",
"xrp": "ripple", "ripple": "ripple",
"ada": "cardano", "cardano": "cardano",
"doge": "dogecoin", "dogecoin": "dogecoin",
"avax": "avalanche-2", "avalanche": "avalanche-2",
"trx": "tron", "tron": "tron",
}
def _coin_id(name: str) -> str:
return COIN_ALIAS.get(name.strip().lower(), name.strip().lower())
def get_crypto_price(coin: str, currency: str = "usd") -> dict:
"""Bir kripto paranin guncel fiyatini CoinGecko'dan ceker."""
cid = _coin_id(coin)
cur = currency.strip().lower()
r = requests.get(f"{COINGECKO}/simple/price",
params={"ids": cid, "vs_currencies": cur}, timeout=15)
r.raise_for_status()
data = r.json()
if cid not in data or cur not in data[cid]:
return {"error": f"Fiyat bulunamadi: {coin} / {currency}"}
return {"coin": cid, "currency": cur, "price": data[cid][cur]}
def convert_currency(amount: float, from_currency: str, to_currency: str) -> dict:
"""Bir para birimindeki tutari digerine cevirir (kripto ya da fiat).
Kripto->fiat, fiat->kripto ve fiat->fiat destekler; CoinGecko kurlarini kullanir."""
frm = from_currency.strip().lower()
to = to_currency.strip().lower()
amount = float(amount)
# ikisi de kripto degilse (fiat->fiat), bitcoin uzerinden koprule
frm_is_coin = frm in COIN_ALIAS
to_is_coin = to in COIN_ALIAS
if frm_is_coin and not to_is_coin:
# kripto -> fiat
p = get_crypto_price(frm, to)
if "error" in p: return p
return {"amount": amount, "from": frm, "to": to,
"result": round(amount * p["price"], 4)}
if not frm_is_coin and to_is_coin:
# fiat -> kripto
p = get_crypto_price(to, frm)
if "error" in p: return p
return {"amount": amount, "from": frm, "to": to,
"result": round(amount / p["price"], 8)}
if frm_is_coin and to_is_coin:
# kripto -> kripto (ikisini de usd'ye cevirip oranla)
p1 = get_crypto_price(frm, "usd"); p2 = get_crypto_price(to, "usd")
if "error" in p1: return p1
if "error" in p2: return p2
return {"amount": amount, "from": frm, "to": to,
"result": round(amount * p1["price"] / p2["price"], 8)}
# fiat -> fiat: bitcoin fiyatini iki para biriminde alip oranla
p1 = get_crypto_price("bitcoin", frm); p2 = get_crypto_price("bitcoin", to)
if "error" in p1: return p1
if "error" in p2: return p2
return {"amount": amount, "from": frm, "to": to,
"result": round(amount * p2["price"] / p1["price"], 4)}
# --- Model icin JSON Sema (Tool/Function Definition) ---
TOOLS = [
{
"type": "function",
"function": {
"name": "get_crypto_price",
"description": "Bir kripto paranin belirtilen para birimindeki guncel fiyatini getirir.",
"parameters": {
"type": "object",
"properties": {
"coin": {"type": "string", "description": "Kripto para adi ya da sembolu (orn: bitcoin, btc, ethereum, sol)"},
"currency": {"type": "string", "description": "Para birimi (orn: usd, try, eur). Varsayilan usd."},
},
"required": ["coin"],
},
},
},
{
"type": "function",
"function": {
"name": "convert_currency",
"description": "Bir tutari bir para biriminden digerine cevirir. Kripto ve normal para (fiat) destekler.",
"parameters": {
"type": "object",
"properties": {
"amount": {"type": "number", "description": "Cevrilecek miktar"},
"from_currency": {"type": "string", "description": "Kaynak birim (orn: usd, btc, eur)"},
"to_currency": {"type": "string", "description": "Hedef birim (orn: try, eth, usd)"},
},
"required": ["amount", "from_currency", "to_currency"],
},
},
},
]
# isim -> fonksiyon eslemesi (app.py cagirirken kullanir)
TOOL_FUNCS = {
"get_crypto_price": get_crypto_price,
"convert_currency": convert_currency,
}