summaryrefslogtreecommitdiff
path: root/ai_api_module.py
blob: f69527eca9dfd6ddeca538dc7c974f6a85ada75f (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python
# COPYRIGHT 2025 Thomas Grothe
import requests
import json
import os
import sys
from util import * 

#this is a python module to provide REST API access to some AI model
# use a CLI tool or custom webtool to use it
# docs: TODO

# each function shall return a tuple containing [success bool, response dict]

### CONFIGURATION HERE (yes you could move this into a config file if desired, but i don't need that currently)
# Base URL and endpoints configuration
api_key_env_key = 'GEMINI_API_KEY'
api_key = None
urlbase = 'https://generativelanguage.googleapis.com/v1beta'

endpoints = {
    'model': {
        'list': '/models',
        'generate': '/models/{model}:generateContent',
        'count_tokens': '/models/{model}:countTokens',
    },
    'file': {
        'list': '/files',
        'get': '/files/{name}',
        'delete': '/files/{name}',
    }
}

# Conversation state management
current_conversation = []
system_instruction = None  # System instruction separate from conversation

def get_api_key():
    global api_key
    """Try to get API key from environment variables"""
    if api_key == None:
        if os.environ.get(api_key_env_key):
            api_key = os.environ.get(api_key_env_key)
            return api_key
    return None

def set_system_instruction(instruction):
    """Set the system instruction for the conversation"""
    global system_instruction
    system_instruction = instruction
    log(f"System instruction set: {instruction}")

def list_models(output_mode=''):
    """List available models from Gemini API"""
    headers = {"Content-Type": "application/json"}
    url = f"{urlbase}{endpoints['model']['list']}?key={api_key}"
    
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        
        data = response.json()
        
        if 'j' in output_mode:
            return [True, data]
        
        models = []
        for model in data.get('models', []):
            # Filter to only generation-capable models
            if 'generateContent' in model.get('supportedGenerationMethods', []):
                models.append({
                    'name': model.get('name', ''),
                    'display_name': model.get('displayName', ''),
                    'description': model.get('description', ''),
                    'supported_methods': model.get('supportedGenerationMethods', []),
                    'input_token_limit': model.get('inputTokenLimit', 0),
                    'output_token_limit': model.get('outputTokenLimit', 0),
                })
        return [True, models]
        
    except requests.exceptions.RequestException as e:
        log(f"Error listing models: {e}")
        return [False, str(e)]

def count_tokens(model_id, text):
    """Count tokens for given text"""
    if not model_id:
        model_id = "gemini-2.0-flash-exp"
    
    headers = {"Content-Type": "application/json"}
    endpoint = endpoints['model']['count_tokens'].format(model=model_id)
    url = f"{urlbase}{endpoint}?key={api_key}"
    
    payload = {
        "contents": [{
            "parts": [{"text": text}]
        }]
    }
    
    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()
        data = response.json()
        return [True, data.get('totalTokens', 0)]
    except requests.exceptions.RequestException as e:
        log(f"Error counting tokens: {e}")
        return [False, str(e)]

def query(model_id, query_text, top_p=0.9, temperature=0.7, stream=False, max_tokens=8192):
    """Send a query to the Gemini model with conversation context"""
    global current_conversation, system_instruction
    
    if not model_id:
        model_id = "gemini-2.0-flash-exp"
    
    # Add user message to conversation
    current_conversation.append({
        "role": "user",
        "parts": [{"text": query_text}]
    })
    
    headers = {"Content-Type": "application/json"}
    endpoint = endpoints['model']['generate'].format(model=model_id)
    url = f"{urlbase}{endpoint}?key={api_key}"
    
    payload = {
        "contents": current_conversation,
        "generationConfig": {
            "temperature": temperature,
            "topP": top_p,
            "maxOutputTokens": max_tokens,
        }
    }
    
    # Add system instruction if set
    if system_instruction:
        payload["systemInstruction"] = {
            "parts": [{"text": system_instruction}]
        }
    
    log(f"Sending query to {model_id}: {query_text}")
    
    try:
        if stream:
            return _stream_response(url, payload, headers)
        else:
            return _non_stream_response(url, payload, headers)
            
    except requests.exceptions.RequestException as e:
        log(f"Error querying model: {e}")
        print(f"Error: {e}")
        return None

def _non_stream_response(url, payload, headers):
    """Handle non-streaming response"""
    global current_conversation
    
    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()
    
    data = response.json()
    
    # Check for prompt feedback (safety issues)
    if 'promptFeedback' in data:
        feedback = data['promptFeedback']
        if 'blockReason' in feedback:
            error_msg = f"Prompt blocked: {feedback['blockReason']}"
            log(error_msg)
            return error_msg
    
    if 'candidates' in data and len(data['candidates']) > 0:
        candidate = data['candidates'][0]
        
        # Check finish reason
        finish_reason = candidate.get('finishReason', '')
        if finish_reason and finish_reason not in ['STOP', '']:
            log(f"Warning: Finish reason: {finish_reason}")
        
        if 'content' in candidate:
            assistant_message = candidate['content']
            current_conversation.append(assistant_message)
            
            # Extract text from parts
            text_parts = []
            for part in assistant_message.get('parts', []):
                if 'text' in part:
                    text_parts.append(part['text'])
            
            response_text = ''.join(text_parts)
            log(f"Response: {response_text}")
            
            # Log token usage if available
            if 'usageMetadata' in data:
                metadata = data['usageMetadata']
                log(f"Token usage - Prompt: {metadata.get('promptTokenCount', 0)}, "
                    f"Response: {metadata.get('candidatesTokenCount', 0)}, "
                    f"Total: {metadata.get('totalTokenCount', 0)}")
            
            return response_text
    
    log(f"Unexpected response structure: {data}")
    return None

def _stream_response(url, payload, headers):
    """Handle streaming response"""
    global current_conversation
    
    # Gemini streaming uses SSE with alt=sse parameter
    stream_url = url + "&alt=sse"
    
    response = requests.post(stream_url, json=payload, headers=headers, stream=True)
    response.raise_for_status()
    
    accumulated_text = []
    
    for line in response.iter_lines():
        if line:
            line = line.decode('utf-8')
            if line.startswith("data: "):
                data_str = line[6:]
                try:
                    data = json.loads(data_str)
                    if 'candidates' in data:
                        for candidate in data['candidates']:
                            if 'content' in candidate:
                                for part in candidate['content'].get('parts', []):
                                    if 'text' in part:
                                        text = part['text']
                                        accumulated_text.append(text)
                                        sys.stdout.write(text)
                                        sys.stdout.flush()
                except json.JSONDecodeError:
                    pass
    
    print()  # New line after streaming
    
    # Add assistant response to conversation
    full_response = ''.join(accumulated_text)
    if full_response:
        current_conversation.append({
            "role": "model",
            "parts": [{"text": full_response}]
        })
        log(f"Streamed response: {full_response}")
    
    return full_response

def save_conversation(filepath=None):
    """Save current conversation to file"""
    global system_instruction
    
    if filepath is None:
        filepath = f"{logdir}/conversation_{tnow()}.json"
    
    # Ensure directory exists
    os.makedirs(os.path.dirname(filepath), exist_ok=True)
    
    conversation_data = {
        "system_instruction": system_instruction,
        "messages": current_conversation,
        "saved_at": tnow()
    }
    
    with open(filepath, 'w') as f:
        json.dump(conversation_data, f, indent=2)
    
    log(f"Conversation saved to {filepath}")
    return filepath

def load_conversation(filepath):
    """Load conversation from file"""
    global current_conversation, system_instruction
    
    try:
        with open(filepath, 'r') as f:
            data = json.load(f)
        
        # Handle both old and new formats
        if isinstance(data, list):
            # Old format: just messages
            current_conversation = data
            system_instruction = None
        else:
            # New format: with metadata
            current_conversation = data.get('messages', [])
            system_instruction = data.get('system_instruction')
        
        log(f"Conversation loaded from {filepath}")
        return True
    except Exception as e:
        log(f"Error loading conversation: {e}")
        return False

def list_saved_conversations(directory=None):
    """List all saved conversation files"""
    if directory is None:
        directory = logdir
    
    try:
        conversations = []
        for filename in os.listdir(directory):
            if filename.startswith('conversation_') and filename.endswith('.json'):
                filepath = os.path.join(directory, filename)
                with open(filepath, 'r') as f:
                    data = json.load(f)
                    
                conversations.append({
                    'filename': filename,
                    'filepath': filepath,
                    'saved_at': data.get('saved_at', 'unknown'),
                    'message_count': len(data.get('messages', data if isinstance(data, list) else [])),
                })
        
        # Sort by saved_at
        conversations.sort(key=lambda x: x['saved_at'], reverse=True)
        return [True, conversations]
    except Exception as e:
        log(f"Error listing conversations: {e}")
        return [False, str(e)]

def clear_conversation():
    """Clear current conversation context"""
    global current_conversation, system_instruction
    current_conversation = []
    system_instruction = None
    log("Conversation cleared")