-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextract_github_repo.py
99 lines (84 loc) · 3.06 KB
/
extract_github_repo.py
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
import base64
import logging
import os
from enum import Enum
import azure.identity
import openai
import requests
import rich
from dotenv import load_dotenv
from pydantic import BaseModel, Field
logging.basicConfig(level=logging.WARNING)
load_dotenv()
if os.getenv("OPENAI_HOST", "azure") == "azure":
if not os.getenv("AZURE_OPENAI_SERVICE") or not os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"):
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
exit(1)
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
token_provider = azure.identity.get_bearer_token_provider(
credential, "https://cognitiveservices.azure.com/.default"
)
client = openai.AzureOpenAI(
api_version="2024-08-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
azure_ad_token_provider=token_provider,
)
model_name = os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT")
else:
if not os.getenv("GITHUB_TOKEN"):
logging.warning("GITHUB_TOKEN env variable is empty. See README.")
exit(1)
client = openai.OpenAI(
base_url="https://models.inference.ai.azure.com",
api_key=os.environ["GITHUB_TOKEN"],
# Specify the API version to use the Structured Outputs feature
default_query={"api-version": "2024-08-01-preview"},
)
model_name = "gpt-4o"
# Define models for Structured Outputs
class Language(str, Enum):
JAVASCRIPT = "JavaScript"
PYTHON = "Python"
DOTNET = ".NET"
class AzureService(str, Enum):
AISTUDIO = "AI Studio"
AISEARCH = "AI Search"
POSTGRESQL = "PostgreSQL"
COSMOSDB = "CosmosDB"
AZURESQL = "Azure SQL"
class Framework(str, Enum):
LANGCHAIN = "Langchain"
SEMANTICKERNEL = "Semantic Kernel"
LLAMAINDEX = "Llamaindex"
AUTOGEN = "Autogen"
SPRINGBOOT = "Spring Boot"
PROMPTY = "Prompty"
class RepoOverview(BaseModel):
name: str
description: str = Field(..., description="A 1-2 sentence description of the project")
languages: list[Language]
azure_services: list[AzureService]
frameworks: list[Framework]
# Fetch a README from a public GitHub repository
url = "https://api.github.com/repos/shank250/CareerCanvas-msft-raghack/contents/README.md"
response = requests.get(url)
if response.status_code != 200:
logging.error(f"Failed to fetch issue: {response.status_code}")
exit(1)
content = response.json()
readme_content = base64.b64decode(content["content"]).decode("utf-8")
# Send request to GPT model to extract using Structured Outputs
completion = client.beta.chat.completions.parse(
model=model_name,
messages=[
{
"role": "system",
"content": "Extract the information from the GitHub issue markdown about this hack submission.",
},
{"role": "user", "content": readme_content},
],
response_format=RepoOverview,
)
output = completion.choices[0].message.parsed
repo_overview = RepoOverview.model_validate(output)
rich.print(repo_overview)