File size: 4,644 Bytes
dd98846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""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,
}