-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.py
More file actions
891 lines (770 loc) · 36 KB
/
module.py
File metadata and controls
891 lines (770 loc) · 36 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
import requests
import json
import os
import time
import pickle
import pathlib
import cachetools
import sys
import logging
from plugin_manager import PluginManager
import traceback
from typing import Dict, Generator, List, Tuple, Optional, Any, Union
import utils
utils.import_or_install("tabulate")
utils.import_or_install("rich")
from tabulate import tabulate
from rich.console import Console
from rich.logging import RichHandler
console = Console()
# Setup logging
logger = logging.getLogger(__name__)
def configure_logging(level=logging.INFO, _console=None):
"""Configure logging with the specified level"""
global console
if _console:
console = _console
logging.basicConfig(
level=level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
force=True, # Override any existing configuration
handlers=[RichHandler(console=console)],
)
logger.setLevel(level)
# Log the new verbosity level based on the level
if level <= logging.DEBUG:
logger.debug("Logging level set to DEBUG")
elif level <= logging.INFO:
logger.info("Logging level updated to INFO")
elif level <= logging.WARNING:
logger.warning("Logging level updated to WARNING")
# Initialize with default logging level
configure_logging(logging.ERROR)
# Setup cache
file_path = pathlib.Path(__file__).parent / "data/models_cache.pickle"
try:
with open(file_path, "rb") as f:
cache = pickle.load(f)
logger.debug(f"Loaded models cache from {file_path}")
except FileNotFoundError:
logger.info("Cache file not found, creating a new one.")
# Set cache to expire after 1 day (86400 seconds) instead of 1 hour
cache = cachetools.TTLCache(maxsize=100, ttl=86400)
# Model aliases for easier reference
MODELS = {
"claude": "claude-3.5-sonnet",
"claude7": "claude-3.7-sonnet",
"gpt4": "gpt-4o",
"gpt3": "gpt-3.5-turbo",
"o3": "o3-mini"
}
def save_cache() -> None:
"""Save the models cache to disk"""
try:
with open(file_path, "wb") as f:
pickle.dump(cache, f)
logger.debug(f"Saved models cache to {file_path}")
except Exception as e:
logger.error(f"Failed to save cache: {e}")
class Copilot:
def __init__(self, model: Optional[str] = None) -> None:
self.access_token = os.environ.get("TOKEN")
self.request_timeout = 60 # Default timeout in seconds
self.max_retries = 2
self.retry_delay = 5 # seconds between retries
self.token: Optional[str] = None
self.plugin_manager: Optional[PluginManager] = None
self.tools: Dict[str, Dict[str, Any]] = {} # Dictionary to store registered tools
self.set_model(model)
logger.debug(f"Initializing Copilot with model: {self.model}")
if not self.access_token:
self.access_token = self.GetAccessToken()
def set_model(self, model_name:str):
model_name = model_name or os.environ.get("MODEL") or "gpt-4o-2024-08-06"
self.model = MODELS.get(model_name, model_name)
logger.debug(f"Changing Copilot model to: {self.model}")
def register_tool_manager(self, manager: PluginManager) -> None:
"""
Register a plugin manager to handle tool capabilities
Args:
manager: PluginManager instance containing loaded plugins
"""
self.plugin_manager = manager
logger.info(f"Plugin manager registered with {len(manager.plugins)} plugins")
logger.debug(f"Available plugins: {list(manager.plugins.keys())}")
def GetAccessToken(self) -> str:
"""Authenticate with GitHub and get an access token"""
logger.info("Getting GitHub access token...")
console.print("[yellow]Authentication needed[/yellow]")
Loginheaders = {
"accept": "application/json",
"editor-version": "Neovim/0.6.1",
"editor-plugin-version": "copilot.vim/1.16.0",
"content-type": "application/json",
"user-agent": "GithubCopilot/1.155.0",
}
try:
logger.debug(f"Sending auth request with headers: {Loginheaders}")
login = requests.post(
"https://github.com/login/device/code",
headers=Loginheaders,
data='{"client_id":"Iv1.b507a08c87ecfe98","scope":"read:user"}',
timeout=self.request_timeout,
).json()
if "error" in login:
error_msg = login.get("error_description", login.get("error"))
logger.error(f"Authentication error: {error_msg}")
sys.exit(1)
console.print(
f'Visit [cyan]{login["verification_uri"]}[/cyan] and enter code [bold green]{login["user_code"]}[/bold green] to authenticate.'
)
logger.info(f"User code: {login['user_code']}")
logger.info(f"Verification URI: {login['verification_uri']}")
logger.debug(
f"Device code obtained: {login['device_code'][:5]}... (truncated)"
)
logger.debug(f"Expires in: {login.get('expires_in', 'N/A')} seconds")
while True:
time.sleep(5)
device_code = login["device_code"]
try:
logger.debug(
f"Polling for token with device code {device_code[:5]}..."
)
oauth = requests.post(
"https://github.com/login/oauth/access_token",
headers=Loginheaders,
data=f'{{"client_id":"Iv1.b507a08c87ecfe98","device_code":"{device_code}","grant_type":"urn:ietf:params:oauth:grant-type:device_code"}}',
timeout=self.request_timeout,
).json()
if "error" in oauth:
if oauth.get("error") == "authorization_pending":
logger.info("Authorization pending, waiting...")
console.print(
"[yellow]Waiting for authorization...[/yellow]"
)
continue
logger.error(f"OAuth error: {oauth}")
if oauth.get("access_token"):
console.print("[green]Authentication successful![/green]")
logger.info("Token acquired successfully")
logger.debug(
f"Token: {oauth['access_token'][:5]}... (truncated)"
)
break
except requests.exceptions.RequestException as e:
logger.error(f"Error during authentication: {e}")
logger.debug(f"Full error: {str(e)}")
time.sleep(2)
os.environ["TOKEN"] = oauth["access_token"]
logger.info("New token acquired and stored in environment")
return oauth["access_token"]
except requests.exceptions.RequestException as e:
logger.error(f"Failed to authenticate: {e}")
logger.debug(f"Authentication request exception details: {str(e)}")
sys.exit(1)
def headers(self) -> Dict[str, str]:
"""Generate headers for API requests"""
if not self.token:
logger.debug("No token found, fetching internal token")
self.token = self.internal_token()
headers = {
"authorization": f"Bearer {self.token}",
"editor-plugin-version": "copilot-chat/0.17.1",
"Editor-Version": "vscode/1.91.1",
"copilot-vision-request": "true",
}
logger.debug(
f"Generated API headers with token: {self.token[:5]}... (truncated)"
)
return headers
def internal_token(self) -> str:
"""Get internal GitHub Copilot token"""
logger.info("Getting internal Copilot token")
if not self.access_token:
raise ValueError("No access token found")
for attempt in range(self.max_retries):
try:
headers = {
"authorization": f"token {self.access_token}",
"editor-version": "Neovim/0.6.1",
"editor-plugin-version": "copilot.vim/1.16.0",
"user-agent": "GithubCopilot/1.155.0",
}
logger.debug(
f"Internal token request attempt {attempt+1}/{self.max_retries}"
)
logger.debug(
f"Using access token: {self.access_token[:5]}... (truncated)"
)
resp = requests.get(
"https://api.github.com/copilot_internal/v2/token",
headers=headers,
timeout=self.request_timeout,
)
resp.raise_for_status()
token = resp.json().get("token")
if not token:
logger.error("No token in response")
logger.debug(f"Response content: {resp.text}")
if attempt < self.max_retries - 1:
time.sleep(self.retry_delay)
continue
raise ValueError("No token in response")
logger.info("Internal token acquired successfully")
logger.debug(f"Token: {token[:5]}... (truncated)")
logger.debug(f"Token expiry: {resp.json().get('expires_at', 'N/A')}")
return token
except requests.exceptions.RequestException as e:
if attempt < self.max_retries - 1:
logger.warning(
f"Error getting token (attempt {attempt+1}/{self.max_retries}): {e}"
)
logger.debug(f"Full error: {str(e)}")
time.sleep(self.retry_delay)
else:
logger.error(
f"Failed to get token after {self.max_retries} attempts: {e}"
)
raise e
raise Exception("Failed to get token")
def req(self, **kwargs) -> requests.Response:
"""Make a request to the Copilot API with retries and error handling"""
logger.debug(f"Making API request using model: {self.model}")
json_data = kwargs.pop("json") if "json" in kwargs else {}
json_data["model"] = self.model
logger.info(f"Making request to Copilot API with model: {self.model}")
if "stream" in kwargs and kwargs["stream"]:
logger.info("Streaming mode enabled")
# Log message content length but not the actual content
if "messages" in json_data:
logger.debug(f"Request contains {len(json_data['messages'])} messages")
for i, msg in enumerate(json_data["messages"]):
role = msg.get("role", "unknown")
name = msg.get("name", "N/A")
if isinstance(msg.get("content"), list):
content_types = [
item.get("type", "unknown") for item in msg.get("content", [])
]
logger.debug(
f"Message {i}: role={role}, name={name}, content_types={content_types}"
)
else:
content_len = len(str(msg.get("content", "")))
logger.debug(
f"Message {i}: role={role}, name={name}, content_length={content_len}"
)
logger.debug(f"complete json: {json.dumps(json_data,indent=2)}")
response = None
for attempt in range(self.max_retries):
try:
start_time = time.time()
logger.debug(f"API request attempt {attempt+1}/{self.max_retries}")
response = requests.post(
"https://api.githubcopilot.com/chat/completions",
headers=self.headers(),
json=json_data,
timeout=self.request_timeout,
**kwargs,
)
response.raise_for_status()
elapsed_time = time.time() - start_time
logger.info(f"API request successful in {elapsed_time:.2f}s")
return response
except requests.exceptions.RequestException as e:
if response is not None and response.status_code == 401:
logger.warning("Token expired, refreshing...")
self.token = self.internal_token()
if response is not None and response.status_code == 429:
# get the time to wait from the response
wait_time = response.headers.get("Retry-After", 30)
logger.error(f"headers: {response.headers}")
logger.error(f"Rate limited, waiting {wait_time} seconds")
time.sleep(int(wait_time))
if response is not None:
logger.error(f"text: {response.text}")
logger.error(f"status: {response.status_code}")
logger.error(f"json_data: {json.dumps(json_data)}")
else:
logger.error(f"no response {response}")
if attempt < self.max_retries - 1:
logger.warning(
f"Request failed (attempt {attempt+1}/{self.max_retries}): {e}"
)
if response:
logger.debug(f"Response status: {response.status_code}")
logger.debug(f"Response headers: {response.headers}")
try:
logger.debug(
f"Response content: {response.text[:200]}... (truncated)"
)
except:
pass
time.sleep(self.retry_delay)
else:
logger.error(
f"Request failed after {self.max_retries} attempts: {e}"
)
raise e
raise Exception("Request failed")
@staticmethod
def parse_iter_json(r: requests.Response) -> Generator[Union[str,dict], None, None]:
"""Parse streaming JSON responses from the API"""
for raw in r.iter_lines():
if not raw:
continue
if raw.startswith(b"data: "):
raw = raw.replace(b"data: ", b"",1)
# logger.debug(f"Received content chunk of length ({len(raw)}) [{raw}]")
if raw == b"[DONE]":
logger.debug("Stream complete - received [DONE] marker")
break
if raw.startswith(b'{"choices":'):
try:
json_rep = json.loads(raw)
choices = json_rep.get("choices", [])
if choices:
first_choice = choices[0]
# Handle streaming delta format for tool calls
if "delta" in first_choice:
delta = first_choice["delta"]
# Handle content in delta
if delta.get("content"):
yield delta["content"]
# Handle tool calls in delta
if delta.get("tool_calls"):
tool_call_chunks = delta["tool_calls"]
logger.debug(f"Received tool call chunks: {tool_call_chunks}")
yield tool_call_chunks
# Handle standard message format
elif "message" in first_choice:
message = first_choice["message"]
if message.get("content"):
yield message["content"]
if message.get("tool_calls"):
yield message["tool_calls"]
if "error" in json_rep:
error_msg = json_rep["error"].get("message", str(json_rep["error"]))
logger.error(f"API error: {error_msg}")
raise Exception(error_msg)
except Exception as e:
error_trace = traceback.format_exc()
logger.error(f"Error parsing JSON response: {e}")
logger.error(f"Full error details: {error_trace}")
logger.debug(
f"Raw data that caused error: {raw[:2000]}... (truncated)"
)
continue
# try:
# raw = raw.decode("utf-8").replace("data: ", "")
# data = json.loads(raw)
# except Exception as e:
# logger.error(f"Error decoding response: {e}, {raw}")
# continue
# if b"error" in data:
# error_msg = data["error"].get("message", str(data["error"]))
# logger.error(f"API error: {error_msg}")
# raise Exception(error_msg)
# choices = data.get("choices")
# if choices:
# if "finish_reason" in choices[0]:
# logger.debug(
# f"Stream finished with reason: {choices[0]['finish_reason']}"
# )
# break
# if "delta" in choices[0] and "content" in choices[0]["delta"]:
# content = choices[0]["delta"]["content"]
# yield content
def question_iter(
self, history: List[Dict[str, Any]], temperature: float = 0.4
) -> str:
"""Stream responses from the API and yield content incrementally"""
start_time = time.time()
logger.info(f"Starting streaming request with temperature: {temperature}")
json_data = {
"messages": history,
"temperature": temperature,
"top_p": 1,
"n": 1,
"max_tokens": 4096,
"stream": True,
"intent": True,
}
logger.debug(f"manager: {self.plugin_manager}")
# Add tools if available and model supports them
if self.plugin_manager and self.model_supports_tool_calls():
self.plugin_manager.load_plugins()
tools = self.plugin_manager.get_tool_configs()
if tools:
json_data["tools"] = tools
logger.info(f"Added {len(tools)} tools to the streaming request")
try:
r = self.req(json=json_data, stream=True)
resultat = ""
chunk_count = 0
# Track if we need to check for tool calls
tools_call_detected = False
tools_call_chunks:list[dict] = [] # Store chunks as a list instead
for content in self.parse_iter_json(r):
# Check if this might be a tool call (will be a list or dict)
if isinstance(content, (list, dict)):
# logger.debug(f"Received tool chunk [{content}]")
tools_call_detected = True
# Append to our list of tool call chunks
if isinstance(content, list):
tools_call_chunks.extend(content)
else:
tools_call_chunks.append(content)
# logger.debug(f"Accumulated tool call chunk: {content}")
# Don't print tool call JSON to console
continue
elif isinstance(content, str):
console.print(content, end="")
resultat += content
chunk_count += 1
elapsed_time = time.time() - start_time
logger.info(f"Streaming request completed in {elapsed_time:.2f}s")
logger.info(f"Received {chunk_count} content chunks")
# If we detected tool call chunks, process them
if tools_call_detected and self.plugin_manager and tools_call_chunks:
logger.info(f"Processing {len(tools_call_chunks)} tool call chunks")
tool_calls_by_index = {}
for chunk in tools_call_chunks:
index = chunk.get("index", 0)
func_details = chunk.get("function", {})
if index not in tool_calls_by_index:
tool_calls_by_index[index] = {
"name": func_details.get("name"),
"id": chunk.get("id"),
"arguments": func_details.get("arguments", "")
}
else:
if func_details.get("name"):
tool_calls_by_index[index]["name"] = func_details.get("name")
if func_details.get("arguments"):
tool_calls_by_index[index]["arguments"] += func_details.get("arguments", "")
tool_calls = []
for call_data in tool_calls_by_index.values():
tool_calls.append({
"id": call_data["id"],
"type": "function",
"function": {
"name": call_data["name"],
"arguments": call_data["arguments"]
}
})
history.append({
"role": "assistant",
"content": "",
"tool_calls": tool_calls
})
for index, call_data in tool_calls_by_index.items():
function_name = call_data["name"]
try:
logger.info(f"Executing plugin: {function_name}")
args = json.loads(call_data["arguments"])
result = self.plugin_manager.execute_plugin(function_name, **args)
history.append({
"role": "tool",
"name": function_name,
"tool_call_id": call_data["id"],
"content": json.dumps(result) if not isinstance(result, str) else result
})
console.print(f"\n[Tool: {function_name}({call_data['arguments']}) executed]")
console.print(f"\n==Results:\n{json.dumps(result) if not isinstance(result, str) else result}]")
except Exception as e:
error_trace = traceback.format_exc()
logger.error(f"Failed to process plugin {function_name}: {e}")
logger.error(f"Full error details: {error_trace}")
history.append({
"role": "tool",
"name": function_name,
"tool_call_id": call_data["id"],
"content": "error, tool not found"
})
history.append({
"role": "user",
"content": "Please answer/continue based on the tool results."
})
return self.question_iter(history, temperature)
return resultat
except Exception as e:
error_trace = traceback.format_exc()
logger.error(f"Error during API call: {e}")
logger.debug(f"Full error details: {error_trace}")
return f"An error occurred: {e}\n\nFull traceback:\n{error_trace}"
def question(
self, history: List[Dict[str, str]], temperature: float = 0.4, **args
) -> str:
"""Send a question to the API and get a complete response"""
start_time = time.time()
logger.info(f"Starting non-streaming request with temperature: {temperature}")
json_data = {
"messages": history,
"temperature": temperature,
"role": "system",
"n": 1,
"top_p": 1,
}
# Add tools if available and model supports them
if self.tools and self.model_supports_tool_calls():
tools = []
for name, tool_info in self.tools.items():
tools.append({
"type": "function",
"function": {
"name": name,
"description": tool_info["description"],
"parameters": tool_info["parameters"]
}
})
if tools:
json_data["tools"] = tools
logger.info(f"Added {len(tools)} tools to the request")
if args:
json_data.update(args)
logger.debug(f"Added additional arguments: {', '.join(args.keys())}")
try:
r = self.req(json=json_data)
json_rep = r.json()
elapsed_time = time.time() - start_time
logger.info(f"Non-streaming request completed in {elapsed_time:.2f}s")
if "choices" in json_rep and len(json_rep["choices"]) > 0:
message = json_rep["choices"][0]["message"]
if "content" in message and message["content"]:
content = message["content"]
logger.info(f"Received text response of length {len(content)}")
return content
elif "tool_calls" in message and message["tool_calls"]:
tool_calls = message["tool_calls"]
logger.info(f"Received {len(tool_calls)} tool calls")
# Execute tool calls if appropriate
results = False
for call in tool_calls:
function_name = call["function"]["name"]
if function_name in self.tools:
try:
args = json.loads(call["function"]["arguments"])
logger.info(f"Executing tool: {function_name}")
logger.debug(f"Tool arguments: {args}")
result = self.tools[function_name]["function"](args)
# Add result to history
# history.append({
# "role": "assistant",
# "tool_calls": [call]
# })
history.append({
"type": "function_call",
"id": call["id"],
"name": call["name"],
"arguments": call["arguments"]
})
history.append({
"role": "tool",
"tool_call_id": call["id"],
"name": function_name,
"content": json.dumps(result) if not isinstance(result, str) else result
})
# Collect results
results = True
logger.info(f"Tool execution complete: {function_name}")
except Exception as e:
error_msg = f"Error executing {function_name}: {str(e)}"
logger.error(error_msg)
results = True
# Add error to history
history.append({
"role": "tool",
"tool_call_id": call["id"],
"name": function_name,
"content": f"Error: {str(e)}"
})
# Call the model again with the tool results
if results:
logger.info("Making follow-up request with tool results")
# Add a user message to ensure the history ends with a user message
history.append({
"role": "user",
"content": "Please continue based on the tool results."
})
return self.question(history, temperature)
else:
return "Tool calls received but no tools were executed."
else:
logger.error(f"Unexpected message format: {message}")
return "Error: Unexpected API response format"
else:
logger.error(f"Unexpected API response: {json_rep}")
return "Error: Unexpected API response format"
except Exception as e:
logger.error(f"Error during API call: {e}")
logger.debug(f"Full error details: {str(e)}")
return f"An error occurred: {e}"
def model_supports_tool_calls(self) -> bool:
"""Check if the current model supports tool calls"""
# Query the models list to check if current model supports tool_calls
try:
for model_data in cache.get("models_data", []):
if model_data.get("id") == self.model:
return model_data.get("capabilities", {}).get("supports", {}).get("tool_calls", False)
# If we don't have the model in cache, assume it supports tools if it's one of these
return False
except Exception as e:
logger.warning(f"Error checking tool call support: {e}")
# Default to assuming support for certain model families
return False
def get_models(self, _print: bool = True) -> Tuple[List, List]:
"""Get available models from the API with caching"""
cache_key = "models_list" # Create a fixed cache key for models
# Check if we have a cached result
if cache_key in cache:
logger.info("Using cached models list")
TABLE, headers = cache[cache_key]
if _print:
console.print(tabulate(TABLE, headers=headers))
return TABLE, headers
# If not in cache, fetch from API
start_time = time.time()
logger.info("Retrieving available models from API")
try:
response = requests.get(
"https://api.githubcopilot.com/models",
headers=self.headers(),
timeout=self.request_timeout,
)
response.raise_for_status()
data = response.json()
logger.info(f"Retrieved {len(data.get('data', []))} models")
except requests.exceptions.RequestException as e:
logger.error(f"Failed to get models: {e}")
return [], []
MODELS_INFO: dict = {}
for model in data.get("data", []):
if model["object"] != "model":
continue
model_limits = model.get("capabilities", {}).get("limits", {})
model_limits_inputs = list(
filter(
lambda a: a > 0,
[
model_limits.get("max_context_window_tokens", -1),
model_limits.get("max_prompt_tokens", -1),
],
)
)
if not model["version"] in MODELS_INFO:
MODELS_INFO[model["version"]] = {}
MODEL = MODELS_INFO[model["version"]]
MODEL["ids"] = []
MODEL["name"] = model["name"]
MODEL["max_prompt"] = (
min(model_limits_inputs) if len(model_limits_inputs) > 0 else -1
)
MODEL["allow_calls"] = (
model.get("capabilities", {})
.get("supports", {})
.get("tool_calls", False)
)
MODEL["allow_stream"] = (
model.get("capabilities", {})
.get("supports", {})
.get("streaming", False)
)
MODEL["type"] = model.get("capabilities", {}).get("type", "")
MODEL["vision"] = "|".join(
model_limits.get("vision", {}).get("supported_media_types", [])
).replace("image/", "")
MODELS_INFO[model["version"]]["ids"].append(model["id"])
# sort by max_prompt
TABLE = []
for version, model in sorted(
MODELS_INFO.items(), key=lambda x: x[1]["max_prompt"]
):
if model["type"] == "embeddings":
continue
# id print the smallest id in length
TABLE.append(
[
min(model["ids"], key=len),
model["name"],
# version,
"|".join(model["ids"]),
model["max_prompt"],
model["allow_calls"],
model["allow_stream"],
model["vision"],
model["type"],
]
)
headers = [
"id",
"name",
"version",
"max prompt tokens",
"allow calls",
"streaming",
"vision",
"type",
]
if _print:
console.print(tabulate(TABLE, headers=headers))
elapsed_time = time.time() - start_time
logger.info(f"Models processing completed in {elapsed_time:.2f}s")
# Store result in cache
cache[cache_key] = (TABLE, headers)
save_cache()
return TABLE, headers
if __name__ == "__main__":
import requests
import base64
import argparse
parser = argparse.ArgumentParser(
description="Test GitHub Copilot API with an image"
)
parser.add_argument("--model", type=str, default="gpt-4o", help="Model to use")
parser.add_argument("--image", type=str, help="Image URL or file path")
parser.add_argument(
"--verbose", "-v", action="count", default=0, help="Increase verbosity"
)
args = parser.parse_args()
# Configure logging level based on verbosity
if args.verbose == 0:
configure_logging(logging.WARNING)
elif args.verbose == 1:
configure_logging(logging.INFO)
else:
configure_logging(logging.DEBUG)
copilot = Copilot(args.model)
if args.image:
image_path = args.image
# Check if it's a URL or a file path
if image_path.startswith(("http://", "https://")):
image_url = image_path
image_b64 = base64.b64encode(requests.get(image_url).content).decode(
"utf-8"
)
else:
# It's a file path
with open(image_path, "rb") as f:
image_b64 = base64.b64encode(f.read()).decode("utf-8")
console.print(
copilot.question_iter(
[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_b64}"
},
},
],
}
]
)
)
else:
# Just list models if no image is provided
copilot.get_models()