parent
181060e3ed
commit
73597f1c35
@ -1,62 +0,0 @@
|
||||
import os
|
||||
import litellm
|
||||
import json
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
# Example dummy function hard coded to return the same weather
|
||||
# In production, this could be your backend API or an external API
|
||||
def get_current_weather(location, unit="fahrenheit"):
|
||||
"""Get the current weather in a given location"""
|
||||
if "tokyo" in location.lower():
|
||||
return json.dumps({"location": "Tokyo", "temperature": "10", "unit": "celsius"})
|
||||
elif "san francisco" in location.lower():
|
||||
return json.dumps({"location": "San Francisco", "temperature": "72", "unit": "fahrenheit"})
|
||||
elif "paris" in location.lower():
|
||||
return json.dumps({"location": "Paris", "temperature": "22", "unit": "celsius"})
|
||||
else:
|
||||
return json.dumps({"location": location, "temperature": "unknown"})
|
||||
|
||||
|
||||
def test_parallel_function_call():
|
||||
try:
|
||||
# Step 1: send the conversation and available functions to the model
|
||||
messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
|
||||
tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_current_weather",
|
||||
"description": "Get the current weather in a given location",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {
|
||||
"type": "string",
|
||||
"description": "The city and state, e.g. San Francisco, CA",
|
||||
},
|
||||
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
|
||||
},
|
||||
"required": ["location"],
|
||||
},
|
||||
},
|
||||
}
|
||||
]
|
||||
response = litellm.completion(
|
||||
model="gpt-4o-mini",
|
||||
messages=messages,
|
||||
tools=tools,
|
||||
tool_choice="auto", # auto is default, but we'll be explicit
|
||||
api_key=os.getenv("OPENAI_API_KEY"),
|
||||
)
|
||||
# print("\nFirst LLM Response:\n", response)
|
||||
# response_message = response.choices[0].message
|
||||
# tool_calls = response_message.tool_calls
|
||||
# print(tool_calls)
|
||||
print(response.model_dump_json(indent=4))
|
||||
except Exception as e:
|
||||
print(f"Error occurred: {e}")
|
||||
|
||||
test_parallel_function_call()
|
@ -1,119 +0,0 @@
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
import requests
|
||||
|
||||
mcp = FastMCP("OKXCryptoPrice")
|
||||
|
||||
mcp.settings.port = 8001
|
||||
|
||||
@mcp.tool(
|
||||
name="get_okx_crypto_price",
|
||||
description="Get the current price and basic information for a given cryptocurrency from OKX exchange.",
|
||||
)
|
||||
def get_okx_crypto_price(symbol: str) -> str:
|
||||
"""
|
||||
Get the current price and basic information for a given cryptocurrency using OKX API.
|
||||
|
||||
Args:
|
||||
symbol (str): The cryptocurrency trading pair (e.g., 'BTC-USDT', 'ETH-USDT')
|
||||
|
||||
Returns:
|
||||
str: A formatted string containing the cryptocurrency information
|
||||
|
||||
Example:
|
||||
>>> get_okx_crypto_price('BTC-USDT')
|
||||
'Current price of BTC/USDT: $45,000'
|
||||
"""
|
||||
try:
|
||||
if not symbol:
|
||||
return "Please provide a valid trading pair (e.g., 'BTC-USDT')"
|
||||
|
||||
# Convert to uppercase and ensure proper format
|
||||
symbol = symbol.upper()
|
||||
if not symbol.endswith("-USDT"):
|
||||
symbol = f"{symbol}-USDT"
|
||||
|
||||
# OKX API endpoint for ticker information
|
||||
url = f"https://www.okx.com/api/v5/market/ticker?instId={symbol}"
|
||||
|
||||
# Make the API request
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
|
||||
if data.get("code") != "0":
|
||||
return f"Error: {data.get('msg', 'Unknown error')}"
|
||||
|
||||
ticker_data = data.get("data", [{}])[0]
|
||||
if not ticker_data:
|
||||
return f"Could not find data for {symbol}. Please check the trading pair."
|
||||
|
||||
price = float(ticker_data.get("last", 0))
|
||||
change_24h = float(ticker_data.get("last24h", 0))
|
||||
change_percent = float(ticker_data.get("change24h", 0))
|
||||
|
||||
base_currency = symbol.split("-")[0]
|
||||
return f"Current price of {base_currency}/USDT: ${price:,.2f}\n24h Change: {change_percent:.2f}%"
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
return f"Error fetching OKX data: {str(e)}"
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="get_okx_crypto_volume",
|
||||
description="Get the 24-hour trading volume for a given cryptocurrency from OKX exchange.",
|
||||
)
|
||||
def get_okx_crypto_volume(symbol: str) -> str:
|
||||
"""
|
||||
Get the 24-hour trading volume for a given cryptocurrency using OKX API.
|
||||
|
||||
Args:
|
||||
symbol (str): The cryptocurrency trading pair (e.g., 'BTC-USDT', 'ETH-USDT')
|
||||
|
||||
Returns:
|
||||
str: A formatted string containing the trading volume information
|
||||
|
||||
Example:
|
||||
>>> get_okx_crypto_volume('BTC-USDT')
|
||||
'24h Trading Volume for BTC/USDT: $1,234,567'
|
||||
"""
|
||||
try:
|
||||
if not symbol:
|
||||
return "Please provide a valid trading pair (e.g., 'BTC-USDT')"
|
||||
|
||||
# Convert to uppercase and ensure proper format
|
||||
symbol = symbol.upper()
|
||||
if not symbol.endswith("-USDT"):
|
||||
symbol = f"{symbol}-USDT"
|
||||
|
||||
# OKX API endpoint for ticker information
|
||||
url = f"https://www.okx.com/api/v5/market/ticker?instId={symbol}"
|
||||
|
||||
# Make the API request
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
|
||||
if data.get("code") != "0":
|
||||
return f"Error: {data.get('msg', 'Unknown error')}"
|
||||
|
||||
ticker_data = data.get("data", [{}])[0]
|
||||
if not ticker_data:
|
||||
return f"Could not find data for {symbol}. Please check the trading pair."
|
||||
|
||||
volume_24h = float(ticker_data.get("vol24h", 0))
|
||||
base_currency = symbol.split("-")[0]
|
||||
return f"24h Trading Volume for {base_currency}/USDT: ${volume_24h:,.2f}"
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
return f"Error fetching OKX data: {str(e)}"
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run the server on port 8000 (you can change this to any available port)
|
||||
mcp.run(transport="sse")
|
Loading…
Reference in new issue