-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
147 lines (108 loc) · 4.56 KB
/
api.py
File metadata and controls
147 lines (108 loc) · 4.56 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
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse
import os
from .core.gimie_methods import extract_gimie
from .core.models import convert_jsonld_to_pydantic, convert_pydantic_to_zod_form_dict
from .core.genai_model import llm_request_repo_infos, llm_request_userorg_infos
from .core.users_parser import parse_github_user
from .core.orgs_parser import parse_github_organization
from .utils.utils import merge_jsonld
from pprint import pprint
app = FastAPI()
@app.get("/")
def index():
return {"title": f"Hello, welcome to the Git Metadata Extractor v0.2.0. Gimie Version 0.7.2. LLM Model {os.environ['MODEL']}"}
@app.get("/v1/extract/json/{full_path:path}")
async def extract(full_path:str):
jsonld_gimie_data = extract_gimie(full_path, format="json-ld")
try:
llm_result = await llm_request_repo_infos(str(full_path), output_format="json-ld", max_tokens=30000)
merged_results = merge_jsonld(jsonld_gimie_data, llm_result)
pydantic_data = convert_jsonld_to_pydantic(merged_results["@graph"])
except Exception as e:
pydantic_data = convert_jsonld_to_pydantic(jsonld_gimie_data["@graph"])
print(f"Warning: LLM service failed, using fallback data: {e}")
zod_data = convert_pydantic_to_zod_form_dict(pydantic_data)
return {"link": full_path,
"output": zod_data}
@app.get("/v1/extract/json-ld/{full_path:path}")
async def extract_jsonld(full_path:str):
jsonld_gimie_data = extract_gimie(full_path, format="json-ld")
try:
llm_result = await llm_request_repo_infos(str(full_path), max_tokens=20000)
except Exception as e:
raise HTTPException(
status_code=424,
detail=f"Error from LLM service: {e}"
)
merged_results = merge_jsonld(jsonld_gimie_data, llm_result)
return {"link": full_path,
"output": merged_results}
@app.get("/v1/org/llm/json/{full_path:path}")
async def get_org_json(full_path: str):
try:
org_metadata = parse_github_organization(full_path.split("/")[-1])
parsed_org_metadata = await llm_request_userorg_infos(org_metadata, item_type="org")
org_metadata_dict = org_metadata.model_dump()
org_metadata_dict.update(parsed_org_metadata)
except Exception as e:
raise HTTPException(
status_code=424,
detail=f"Error from Organization JSON service: {e}"
)
return {"link": full_path,
"output": org_metadata_dict}
@app.get("/v1/user/llm/json/{full_path:path}")
async def get_user_json(full_path: str):
try:
user_metadata = parse_github_user(full_path.split("/")[-1])
parsed_user_metadata = await llm_request_userorg_infos(user_metadata, item_type="user")
user_metadata_dict = user_metadata.model_dump()
user_metadata_dict.update(parsed_user_metadata)
except Exception as e:
raise HTTPException(
status_code=424,
detail=f"Error from Get User service: {e}"
)
return {"link": full_path,
"output": user_metadata_dict}
@app.get("/v1/repository/gimie/json-ld/{full_path:path}")
async def gimie(full_path:str):
try:
gimie_output = extract_gimie(full_path, format="json-ld")
except Exception as e:
raise HTTPException(
status_code=424,
detail=f"Error from Gimie service: {e}"
)
return {"link": full_path,
"output": gimie_output}
@app.get("/v1/repository/llm/json-ld/{full_path:path}")
async def llm_jsonld(full_path:str):
try:
llm_result = await llm_request_repo_infos(str(full_path), max_tokens=20000)
except Exception as e:
raise HTTPException(
status_code=424,
detail=f"Error from LLM service: {e}"
)
return {"link": full_path,
"output": llm_result}
@app.get("/v1/repository/llm/json/{full_path:path}")
async def llm_json(full_path:str):
jsonld_gimie_data = extract_gimie(full_path, format="json-ld")
try:
llm_result = await llm_request_repo_infos(str(full_path), gimie_output=jsonld_gimie_data, output_format="json", max_tokens=20000)
except Exception as e:
raise HTTPException(
status_code=424,
detail=f"Error from LLM service: {e}"
)
return {"link": full_path,
"output": llm_result}
@app.exception_handler(ValueError)
async def value_error_exception_handler(request: Request, exc: ValueError):
return JSONResponse(
status_code=400,
content={"message": str(exc)},
)