1 line
8.8 KiB
JSON
1 line
8.8 KiB
JSON
{"repo": "cameronjoejones/deepseek-stock-ai-chatbot", "files": {"api.py": {"dde51711d4f53320e07332310515e97245163fd0": {"t": 1785674064.3394675, "n": 3945, "h": "73db0afe50629c9a", "c": "from datetime import datetime, timedelta\nfrom typing import Dict, List\n\nimport pandas as pd\nimport requests\nimport streamlit as st\nfrom openai import OpenAI\n\n# constants\nDEEPSEEK_BASE_URL = \"https://api.deepseek.com\"\nDEEPSEEK_MODEL = \"deepseek-chat\" # can be changed to deepseek-reasoner\nFMP_BASE_URL = \"https://financialmodelingprep.com/api/v3\"\nSYSTEM_PROMPT = \"\"\"You are a stock market analyst. Your role is to use this data \nto answer questions about the stock\"\"\" # deepseek initial prompt context\nstock_api_key = st.secrets[\"stock_api_key\"]\ndeepseek_api_key = st.secrets[\"deepseek_api_key\"]\n\n\ndef get_stock_data(api_key: str, symbol: str) -> pd.DataFrame:\n \"\"\"\n Fetch historical stock data using the Financial Modeling Prep API.\n\n Args:\n api_key (str): FMP API key\n symbol (str): Stock symbol ('AAPL', 'GOOGL', etc)\n\n Returns:\n pd.DataFrame: Historical stock data with columns:\n - Open: Opening price\n - High: Highest price\n - Low: Lowest price\n - Close: Closing price\n - Adjusted Close: Adjusted closing price\n - Volume: Trading volume\n\n Raises:\n ValueError: If API request fails or no data is found\n \"\"\"\n # calculate date range (5 months of data)\n end_date = datetime.now() - timedelta(days=1)\n start_date = end_date - timedelta(days=5 * 30)\n\n try:\n url = f\"{FMP_BASE_URL}/historical-price-full/{symbol}\"\n params = {\n \"from\": start_date.strftime(\"%Y-%m-%d\"),\n \"to\": end_date.strftime(\"%Y-%m-%d\"),\n \"apikey\": api_key\n }\n response = requests.get(url, params=params)\n response.raise_for_status()\n\n data = response.json()\n historical_data = data.get('historical', [])\n if not historical_data:\n raise ValueError(f\"No historical data found for symbol: {symbol}\")\n\n df = pd.DataFrame(historical_data)\n df['date'] = pd.to_datetime(df['date'])\n df.set_index('date', inplace=True)\n\n # column name mapping\n column_mapping = {\n 'open': 'Open',\n 'high': 'High',\n 'low': 'Low',\n 'close': 'Close',\n 'adjClose': 'Adjusted Close',\n 'volume': 'Volume'\n }\n df.rename(columns=column_mapping, inplace=True)\n\n return df\n\n except requests.RequestException as e:\n raise ValueError(f\"Failed to fetch stock data: {str(e)}\")\n except Exception as e:\n raise ValueError(f\"Error processing stock data: {str(e)}\")\n\n\ndef deepseek_chat(api_key: str, messages: List[Dict[str, str]]) -> str:\n \"\"\"\n Generate chat responses using the DeepSeek API.\n\n Args:\n api_key (str): DeepSeek API key\n messages\n\n Returns:\n str: response from the model\n \"\"\"\n try:\n print(f\"starting client with base URL: {DEEPSEEK_BASE_URL}\")\n client = OpenAI(\n api_key=api_key,\n base_url=DEEPSEEK_BASE_URL,\n timeout=30.0\n )\n\n full_messages = [\n {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n *messages\n ]\n\n #API request\n response = client.chat.completions.create(\n model=DEEPSEEK_MODEL,\n messages=full_messages,\n max_tokens=1000, # change to add more content\n stream=False\n )\n\n if not response or not hasattr(response, 'choices') or not response.choices:\n print(\"Empty or invalid response received:\", response)\n raise ValueError(\"Invalid response from API\")\n\n print(\"Successful response received\")\n return response.choices[0].message.content\n\n except Exception as e:\n import traceback\n error_details = traceback.format_exc()\n print(f\"Error details: {error_details}\")\n st.error(f\"API error: {str(e)}\")\n return \"Cannot connect to the API. Please try again later.\"\n"}, "242b76abc98cdc0e4bf44748734a316c6aac8d3c": {"t": 1785750722.0935752, "n": 3945, "h": "73db0afe50629c9a", "c": "from datetime import datetime, timedelta\nfrom typing import Dict, List\n\nimport pandas as pd\nimport requests\nimport streamlit as st\nfrom openai import OpenAI\n\n# constants\nDEEPSEEK_BASE_URL = \"https://api.deepseek.com\"\nDEEPSEEK_MODEL = \"deepseek-chat\" # can be changed to deepseek-reasoner\nFMP_BASE_URL = \"https://financialmodelingprep.com/api/v3\"\nSYSTEM_PROMPT = \"\"\"You are a stock market analyst. Your role is to use this data \nto answer questions about the stock\"\"\" # deepseek initial prompt context\nstock_api_key = st.secrets[\"stock_api_key\"]\ndeepseek_api_key = st.secrets[\"deepseek_api_key\"]\n\n\ndef get_stock_data(api_key: str, symbol: str) -> pd.DataFrame:\n \"\"\"\n Fetch historical stock data using the Financial Modeling Prep API.\n\n Args:\n api_key (str): FMP API key\n symbol (str): Stock symbol ('AAPL', 'GOOGL', etc)\n\n Returns:\n pd.DataFrame: Historical stock data with columns:\n - Open: Opening price\n - High: Highest price\n - Low: Lowest price\n - Close: Closing price\n - Adjusted Close: Adjusted closing price\n - Volume: Trading volume\n\n Raises:\n ValueError: If API request fails or no data is found\n \"\"\"\n # calculate date range (5 months of data)\n end_date = datetime.now() - timedelta(days=1)\n start_date = end_date - timedelta(days=5 * 30)\n\n try:\n url = f\"{FMP_BASE_URL}/historical-price-full/{symbol}\"\n params = {\n \"from\": start_date.strftime(\"%Y-%m-%d\"),\n \"to\": end_date.strftime(\"%Y-%m-%d\"),\n \"apikey\": api_key\n }\n response = requests.get(url, params=params)\n response.raise_for_status()\n\n data = response.json()\n historical_data = data.get('historical', [])\n if not historical_data:\n raise ValueError(f\"No historical data found for symbol: {symbol}\")\n\n df = pd.DataFrame(historical_data)\n df['date'] = pd.to_datetime(df['date'])\n df.set_index('date', inplace=True)\n\n # column name mapping\n column_mapping = {\n 'open': 'Open',\n 'high': 'High',\n 'low': 'Low',\n 'close': 'Close',\n 'adjClose': 'Adjusted Close',\n 'volume': 'Volume'\n }\n df.rename(columns=column_mapping, inplace=True)\n\n return df\n\n except requests.RequestException as e:\n raise ValueError(f\"Failed to fetch stock data: {str(e)}\")\n except Exception as e:\n raise ValueError(f\"Error processing stock data: {str(e)}\")\n\n\ndef deepseek_chat(api_key: str, messages: List[Dict[str, str]]) -> str:\n \"\"\"\n Generate chat responses using the DeepSeek API.\n\n Args:\n api_key (str): DeepSeek API key\n messages\n\n Returns:\n str: response from the model\n \"\"\"\n try:\n print(f\"starting client with base URL: {DEEPSEEK_BASE_URL}\")\n client = OpenAI(\n api_key=api_key,\n base_url=DEEPSEEK_BASE_URL,\n timeout=30.0\n )\n\n full_messages = [\n {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n *messages\n ]\n\n #API request\n response = client.chat.completions.create(\n model=DEEPSEEK_MODEL,\n messages=full_messages,\n max_tokens=1000, # change to add more content\n stream=False\n )\n\n if not response or not hasattr(response, 'choices') or not response.choices:\n print(\"Empty or invalid response received:\", response)\n raise ValueError(\"Invalid response from API\")\n\n print(\"Successful response received\")\n return response.choices[0].message.content\n\n except Exception as e:\n import traceback\n error_details = traceback.format_exc()\n print(f\"Error details: {error_details}\")\n st.error(f\"API error: {str(e)}\")\n return \"Cannot connect to the API. Please try again later.\"\n"}}, ".streamlit/example_secrets.toml": {"dde51711d4f53320e07332310515e97245163fd0": {"t": 1785745361.4467845, "n": 85, "h": "8239510ec19d26dd", "c": "deepseek_api_key = \"<your_deepseek_api_key>\"\n\nstock_api_key = \"<your_stock_api_key>\"\n"}, "242b76abc98cdc0e4bf44748734a316c6aac8d3c": {"t": 1785750723.814347, "n": 85, "h": "8239510ec19d26dd", "c": "deepseek_api_key = \"<your_deepseek_api_key>\"\n\nstock_api_key = \"<your_stock_api_key>\"\n"}}}} |