-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeepseek_ai.py
More file actions
264 lines (219 loc) · 9.59 KB
/
deepseek_ai.py
File metadata and controls
264 lines (219 loc) · 9.59 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""
DeepSeek AI Integration for AlleyBot
Provides intelligent comment generation and content analysis
"""
import os
import requests
import json
from typing import Optional, Dict, Any
class DeepSeekAI:
"""DeepSeek AI client for intelligent content generation"""
def __init__(self):
self.api_key = os.getenv('DEEPSEEK_API_KEY')
self.base_url = "https://api.deepseek.com/v1"
self.model = "deepseek-chat"
if not self.api_key:
print("⚠️ DEEPSEEK_API_KEY not found in environment")
self.enabled = False
else:
self.enabled = True
print("✅ DeepSeek AI initialized")
def chat(self, prompt: str, system_prompt: str = None, max_tokens: int = 500) -> Optional[str]:
"""General-purpose chat method for simple prompts"""
if not self.enabled:
return None
try:
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
messages.append({"role": "user", "content": prompt})
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": self.model,
"messages": messages,
"max_tokens": max_tokens,
"temperature": 0.7
}
response = requests.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=data,
timeout=30
)
if response.status_code == 200:
result = response.json()
return result['choices'][0]['message']['content'].strip()
else:
print(f"❌ DeepSeek chat error: {response.status_code}")
return None
except Exception as e:
print(f"❌ DeepSeek chat failed: {e}")
return None
def generate_comment(self, post_content: str, agent_name: str = None, context: str = None) -> Optional[str]:
"""Generate an intelligent, context-aware comment for a post"""
if not self.enabled:
return None
try:
# Create a sophisticated prompt for comment generation
system_prompt = """You are AlleyBot, an intelligent AI agent that engages with other agents on social platforms.
Your task is to generate a thoughtful, context-aware comment in response to a post. Follow these guidelines:
1. BE CONTEXTUAL - Reference specific points from the post content
2. BE AUTHENTICIC - Sound like a real AI agent, not a generic bot
3. BE VALUABLE - Add insight, ask thoughtful questions, or provide relevant perspective
4. BE CONCISE - Keep comments under 200 characters when possible
5. USE EMOJIS - Include relevant emojis to express emotion
6. BE POSITIVE - Maintain an encouraging, constructive tone
7. AVOID GENERIC RESPONSES - Never use "Great post!" or "Nice!" type comments
Context: You are an AI agent interacting in a crypto/AI/agent ecosystem. You're knowledgeable about:
- AI agent development and autonomy
- DeFi, crypto tokens, and blockchain technology
- Building, shipping, and development culture
- Community building and network effects
- Learning systems and continuous improvement
Generate a comment that shows you actually read and understood the post."""
user_prompt = f"""Generate a thoughtful comment for this post:
Agent: @{agent_name if agent_name else 'Unknown'}
Post: "{post_content}"
{f'Additional Context: {context}' if context else ''}
Requirements:
- Reference specific details from the post
- Add value to the conversation
- Use AlleyBot's personality (intelligent, helpful, slightly technical)
- Include relevant emojis
- Keep it concise and engaging
- Sound authentic and human-like"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": self.model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
"max_tokens": 500,
"temperature": 0.7,
"top_p": 0.9
}
response = requests.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=data,
timeout=30
)
if response.status_code == 200:
result = response.json()
comment = result['choices'][0]['message']['content'].strip()
# Clean up the comment
comment = comment.replace('"', '').replace("'", "")
# Ensure it ends with appropriate punctuation
if not comment.endswith(('.', '!', '?')):
comment += '!'
# Add AlleyBot signature if not too long
if len(comment) < 180 and '🦞' not in comment:
comment += ' 🦞'
return comment
else:
print(f"❌ DeepSeek API error: {response.status_code} - {response.text}")
return None
except Exception as e:
print(f"❌ DeepSeek comment generation failed: {e}")
return None
def analyze_post_sentiment(self, post_content: str) -> Dict[str, Any]:
"""Analyze the sentiment and topics of a post"""
if not self.enabled:
return {"sentiment": "neutral", "topics": [], "confidence": 0.0}
try:
user_prompt = f"""Analyze this post for sentiment and key topics:
Post: "{post_content}"
Return a JSON object with:
- sentiment: (positive, negative, neutral)
- topics: [list of main topics discussed]
- confidence: (0.0-1.0 confidence level)
- engagement_type: (question, statement, announcement, discussion)"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": self.model,
"messages": [
{"role": "system", "content": "You are a content analysis expert. Always respond with valid JSON."},
{"role": "user", "content": user_prompt}
],
"max_tokens": 300,
"temperature": 0.1
}
response = requests.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=data,
timeout=30
)
if response.status_code == 200:
result = response.json()
analysis_text = result['choices'][0]['message']['content'].strip()
try:
return json.loads(analysis_text)
except json.JSONDecodeError:
# Fallback if JSON parsing fails
return {"sentiment": "neutral", "topics": [], "confidence": 0.5}
else:
return {"sentiment": "neutral", "topics": [], "confidence": 0.0}
except Exception as e:
print(f"❌ DeepSeek analysis failed: {e}")
return {"sentiment": "neutral", "topics": [], "confidence": 0.0}
def generate_reply_to_comment(self, original_comment: str, commenter_name: str, post_context: str = None) -> Optional[str]:
"""Generate an intelligent reply to a specific comment"""
if not self.enabled:
return None
try:
user_prompt = f"""Generate a thoughtful reply to this comment:
Original Post Context: {post_context or 'AI/Agent ecosystem discussion'}
Commenter: @{commenter_name}
Their Comment: "{original_comment}"
Requirements:
- Be conversational and friendly
- Reference their specific points
- Add value to the discussion
- Keep it concise (under 150 characters)
- Include relevant emojis
- Sound like AlleyBot (intelligent, helpful AI agent)"""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": self.model,
"messages": [
{"role": "system", "content": "You are AlleyBot, an intelligent AI agent engaging in meaningful conversations."},
{"role": "user", "content": user_prompt}
],
"max_tokens": 500,
"temperature": 0.8
}
response = requests.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=data,
timeout=30
)
if response.status_code == 200:
result = response.json()
reply = result['choices'][0]['message']['content'].strip()
# Clean up and format
reply = reply.replace('"', '').replace("'", "")
if not reply.endswith(('.', '!', '?')):
reply += '!'
return reply
else:
return None
except Exception as e:
print(f"❌ DeepSeek reply generation failed: {e}")
return None
# Global DeepSeek instance
deepseek_ai = DeepSeekAI()