Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/aryan-cs_grok-trader.json
T

1 line
2.7 KiB
JSON

{"repo": "aryan-cs/grok-trader", "files": {"grok_chat.py": {"848f21641f23e78629f1d2077f2da10e2a49eec8": {"t": 1785674550.4195623, "n": 2468, "h": "d15e0de74ec768f8", "c": "import os\nfrom openai import AsyncOpenAI\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\nMODEL_NAME = \"grok-4-1-fast-non-reasoning\"\n\nXAI_API_KEY = os.getenv(\"XAI_API_KEY\")\n\n# Initialize xAI client (uses OpenAI SDK with custom base URL)\nclient = AsyncOpenAI(\n api_key=XAI_API_KEY,\n base_url=\"https://api.x.ai/v1\",\n)\n\n\nasync def stream_chat_response(messages: list[dict], websocket, market_slug: str | None = None):\n \"\"\"\n Stream chat responses from xAI API to the client via WebSocket.\n\n Args:\n messages: List of chat messages with 'role' and 'content'\n websocket: FastAPI WebSocket connection to stream responses to\n market_slug: Optional market slug to provide context to the chat\n \"\"\"\n if not XAI_API_KEY:\n await websocket.send_json(\n {\"message_type\": \"chat\", \"type\": \"error\", \"error\": \"XAI_API_KEY not configured\"}\n )\n return\n\n try:\n # Add system message with market context if market_slug is provided\n messages_with_context = messages.copy()\n if market_slug:\n system_message = {\n \"role\": \"system\",\n \"content\": f\"You are a helpful assistant answering questions about the Polymarket prediction market: '{market_slug}'. Provide relevant information and analysis about this specific market when asked.\"\n }\n # Insert system message at the beginning\n messages_with_context.insert(0, system_message)\n\n # Create streaming chat completion\n stream = await client.chat.completions.create(\n model=MODEL_NAME,\n messages=messages_with_context,\n stream=True,\n temperature=0.7,\n )\n\n # Stream the response\n async for chunk in stream:\n if chunk.choices and len(chunk.choices) > 0:\n delta = chunk.choices[0].delta\n content = delta.content\n\n if content:\n # Send text delta to client\n await websocket.send_json(\n {\"message_type\": \"chat\", \"type\": \"delta\", \"content\": content}\n )\n\n # Send completion signal\n await websocket.send_json({\"message_type\": \"chat\", \"type\": \"complete\"})\n\n except Exception as e:\n error_msg = str(e)\n print(f\"❌ Chat error: {error_msg}\")\n await websocket.send_json({\"message_type\": \"chat\", \"type\": \"error\", \"error\": f\"Error: {error_msg}\"})\n"}}}}