File size: 4,259 Bytes
4767080
2a306a5
 
 
 
 
4767080
2a306a5
4767080
 
 
 
 
2a306a5
4767080
 
 
 
b387c8e
2a306a5
 
 
 
 
 
 
 
b387c8e
2a306a5
 
 
 
 
b387c8e
2a306a5
 
 
 
 
 
 
 
 
 
b387c8e
2a306a5
 
 
 
 
 
 
 
 
 
 
b387c8e
2a306a5
 
 
 
 
 
b387c8e
2a306a5
 
 
b387c8e
 
 
 
 
2a306a5
 
 
b387c8e
2a306a5
 
 
b387c8e
2a306a5
 
b387c8e
2a306a5
 
 
 
 
 
b387c8e
2a306a5
 
 
 
 
 
 
 
b387c8e
 
 
 
 
 
 
 
 
 
 
 
2a306a5
 
 
b387c8e
 
2a306a5
 
 
b387c8e
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
114
115
116
117
118
119
120
import requests
import json
from db import get_portfolio, execute_trade

def get_crypto_price(symbol: str):
    """
    Binance.US üzerinden güncel kripto fiyatını çeker.
    """
    symbol = symbol.upper()
    if not symbol.endswith('USDT'):
        symbol += 'USDT'
        
    url = f"https://api.binance.us/api/v3/ticker/price?symbol={symbol}"
    try:
        response = requests.get(url, timeout=5)
        data = response.json()
        if 'price' in data:
            return float(data['price'])
        return f"Hata: {symbol} fiyatı bulunamadı."
    except Exception as e:
        return f"API Hatası: {str(e)}"

TOOLS_SCHEMA = [
    {
        "type": "function",
        "function": {
            "name": "get_crypto_price",
            "description": "Belirtilen kripto paranın anlık güncel fiyatını (USD) getirir.",
            "parameters": {
                "type": "object",
                "properties": {
                    "symbol": {
                        "type": "string",
                        "description": "Kripto para sembolü. Örn: BTC, ETH"
                    }
                },
                "required": ["symbol"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_portfolio",
            "description": "Kullanıcının cüzdanındaki mevcut USDT ve kripto varlıkları getirir.",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "execute_trade",
            "description": "Kripto alım veya satım işlemi gerçekleştirir.",
            "parameters": {
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "enum": ["buy", "sell"],
                        "description": "İşlem türü ('buy' veya 'sell')."
                    },
                    "symbol": {
                        "type": "string",
                        "description": "İşlem yapılacak kripto paranın sembolü (Örn: BTC)."
                    },
                    "usdt_amount": {
                        "type": "number",
                        "description": "İşleme ayrılacak toplam USD tutarı (Sadece USD cinsinden miktar biliniyorsa kullanılır)."
                    },
                    "amount": {
                        "type": "number",
                        "description": "İşlem yapılacak kripto miktarı (Sadece kripto miktarı biliniyorsa kullanılır)."
                    },
                    "price": {
                        "type": "number",
                        "description": "İşlemin gerçekleştirileceği anlık birim fiyat (get_crypto_price'dan alınmalıdır)."
                    }
                },
                "required": ["action", "symbol", "price"]
            }
        }
    }
]

def handle_tool_call(name, arguments):
    """LLM araç çağrılarını yönlendirir ve çalıştırır."""
    if isinstance(arguments, str):
        arguments = json.loads(arguments)
        
    if name == "get_crypto_price":
        return str(get_crypto_price(arguments.get("symbol")))
    elif name == "get_portfolio":
        return json.dumps(get_portfolio())
    elif name == "execute_trade":
        price = float(arguments.get("price"))
        usdt_amount = arguments.get("usdt_amount")
        amount = arguments.get("amount")
        
        # Miktar hesaplama (USDT verilmişse coin miktarını otomatik hesapla)
        if usdt_amount and price > 0:
            calculated_amount = float(usdt_amount) / price
        elif amount:
            calculated_amount = float(amount)
        else:
            return "Hata: usdt_amount veya amount belirtilmelidir."
            
        result = execute_trade(
            action=arguments.get("action"),
            symbol=arguments.get("symbol").upper().replace("USDT", ""),
            amount=calculated_amount,
            price=price
        )
        return json.dumps(result, ensure_ascii=False)
    else:
        return "Hata: Bilinmeyen fonksiyon."