Merge pull request #4092 from noitcudni/main

This commit is contained in:
paul-gauthier 2025-05-26 10:43:53 -07:00 committed by GitHub
commit ebaad9d865
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,6 +8,7 @@ import platform
import sys
import time
from dataclasses import dataclass, fields
from datetime import datetime
from pathlib import Path
from typing import Optional, Union
@ -874,6 +875,24 @@ class Model(ModelSettings):
def is_ollama(self):
return self.name.startswith("ollama/") or self.name.startswith("ollama_chat/")
def github_copilot_token_to_open_ai_key(self):
# check to see if there's an openai api key
# If so, check to see if it's expire
openai_api_key = 'OPENAI_API_KEY'
if openai_api_key not in os.environ or (
int(dict(x.split("=") for x in os.environ[openai_api_key].split(";"))['exp']) < int(datetime.now().timestamp())
):
import requests
headers = {
'Authorization': f"Bearer {os.environ['GITHUB_COPILOT_TOKEN']}",
'Editor-Version': self.extra_params['extra_headers']['Editor-Version'],
'Copilot-Integration-Id': self.extra_params['extra_headers']['Copilot-Integration-Id'],
'Content-Type': 'application/json',
}
res = requests.get("https://api.github.com/copilot_internal/v2/token", headers=headers)
os.environ[openai_api_key] = res.json()['token']
def send_completion(self, messages, functions, stream, temperature=None):
if os.environ.get("AIDER_SANITY_CHECK_TURNS"):
sanity_check_messages(messages)
@ -915,6 +934,10 @@ class Model(ModelSettings):
dump(kwargs)
kwargs["messages"] = messages
# Are we using github copilot?
if 'GITHUB_COPILOT_TOKEN' in os.environ:
self.github_copilot_token_to_open_ai_key()
res = litellm.completion(**kwargs)
return hash_object, res