feat: Implement AI analysis using Groq, replacing Gemini API key configuration.

This commit is contained in:
2569718930@qq.com
2026-02-26 23:02:41 +08:00
parent 59655b4088
commit 98451ff9e5
4 changed files with 38 additions and 26 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ TELEGRAM_BOT_TOKEN=your_bot_token_here
TELEGRAM_CHAT_ID=your_chat_id_here
# AI
GEMINI_API_KEY=your_gemini_api_key_here
GROQ_API_KEY=your_groq_api_key_here
# Proxy Setting (optional)
HTTPS_PROXY=http://127.0.0.1:7890
+3 -4
View File
@@ -1017,16 +1017,15 @@ def start_bot():
if line.strip():
msg_lines.append(f"- {line.strip()}")
# --- 6. Gemini AI 数据分析 ---
# --- 6. Groq AI 数据分析 ---
try:
from src.analysis.ai_analyzer import get_ai_analysis
# 发送请求时可以稍微提示一下正在请求AI
bot.send_message(message.chat.id, f"🧠 Gemini 正在分析 {city_name} 的趋势数据...")
# Groq 极快,通常不用发送“正在分析”的提示,直接拼接
ai_result = get_ai_analysis(clean_insights, city_name, temp_symbol)
if ai_result:
msg_lines.append(f"\n{ai_result}")
except Exception as e:
logger.error(f"加载/调用 AI 分析失败: {e}")
logger.error(f"调用 Groq AI 分析失败: {e}")
bot.send_message(message.chat.id, "\n".join(msg_lines), parse_mode="HTML")
-1
View File
@@ -5,4 +5,3 @@ python-dotenv
pytz
numpy
web3
google-generativeai
+34 -20
View File
@@ -1,21 +1,24 @@
import os
import google.generativeai as genai
import requests
from loguru import logger
def get_ai_analysis(weather_insights: str, city_name: str, temp_symbol: str) -> str:
"""
调用 Gemini API 对天气态势进行简短的交易分析
通过 Groq API (LLaMA 3.3 70B) 对天气态势进行极速交易分析
"""
api_key = os.getenv("GEMINI_API_KEY")
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
logger.warning("GEMINI_API_KEY 未配置,跳过 AI 分析")
logger.warning("GROQ_API_KEY 未配置,跳过 AI 分析")
return ""
try:
genai.configure(api_key=api_key)
# 使用最新的 Gemini 3 Flash 预览版模型
model = genai.GenerativeModel('gemini-3-flash-preview')
# Groq 完全兼容 OpenAI 的 API 格式,直接用 requests 简单直观
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
prompt = f"""
你是一个专业的天气衍生品(如 Polymarket)交易员。你的任务是根据当前天气数据推测今日最高温度趋势,进行交易决策。
请严格根据以下我提供的【{city_name}】的实时天气数据和规则策略进行分析。
@@ -25,22 +28,33 @@ def get_ai_analysis(weather_insights: str, city_name: str, temp_symbol: str) ->
【输出要求】
1. 语言必须极端简练,直击要害,整体不超过60个字。
2. 必须给出一个明确的操作建议。假设市场针对的是“今天温度是否会涨到预报峰值”结论可以是:下注YES(看涨)、下注NO(看跌)、或 观望。
2. 必须给出一个明确的操作建议(针对“今天温度是否会涨到预报峰值”)。结论可以是:下注YES、下注NO、或 观望。
3. 必须包含 1-10 的信心指数。
4. 严格按照以下HTML格式输出,不要带任何 Markdown 代码块标记(如 ```html:
4. 严格按照以下HTML格式输出:
🤖 <b>Gemini AI 决策</b>
- 💡 逻辑: [一句话说明核心支撑逻辑,例如:最热时段已过且低于预报3度,涨水无望。]
🤖 <b>Groq AI 决策</b>
- 💡 逻辑: [一句话说明核心支撑逻辑]
- 🎯 建议: <b>[下注YES / 下注NO / 观望]</b> (信心: [1-10]/10)
"""
# 强制使用 REST 传输方式,这对代理更友好
response = model.generate_content(prompt, transport='rest')
text = response.text.strip()
payload = {
"model": "llama-3.3-70b-specdec", # 改用高性能版本
"messages": [
{"role": "system", "content": "你是不讲废话、只看数据的专业气象分析师。"},
{"role": "user", "content": prompt}
],
"temperature": 0.5,
"max_tokens": 150
}
# 索非亚直连应该没问题
response = requests.post(url, json=payload, headers=headers, timeout=10)
response.raise_for_status()
# 简单清理可能的 markdown 标记
text = text.replace("```html", "").replace("```markdown", "").replace("```", "").strip()
result = response.json()
content = result['choices'][0]['message']['content'].strip()
return text
return content
except Exception as e:
logger.error(f"Gemini API 调用失败: {e}")
return f"\n⚠️ Gemini 分析暂不可用 ({str(e)[:30]})"
logger.error(f"Groq API 调用失败: {e}")
return f"\n⚠️ Groq 分析暂不可用 ({str(e)[:30]})"