Repository navigation
Expand file tree
/
Copy pathgithub_stats.py
More file actions
94 lines (75 loc) · 2.57 KB
/
Copy pathgithub_stats.py
File metadata and controls
94 lines (75 loc) · 2.57 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
"""Fetch GitHub repo stats using Codex inside a sandbox.
Demonstrates the return_type feature: Codex searches the web for repo
stats and returns structured JSON that is automatically parsed into a
Pydantic model.
Prerequisites:
- OPENAI_API_KEY set in your environment
Usage:
OPENAI_API_KEY=sk-... python examples/github_stats.py
OPENAI_API_KEY=sk-... python examples/github_stats.py --repo pytorch/pytorch
"""
import argparse
import os
import sys
from pydantic import BaseModel
from bespokelabs.sandbox import Sandbox, SandboxExecutionError, json_schema
WORKDIR = os.path.join(os.path.dirname(__file__), ".sandbox_workdir")
OUTPUT_FILE = "/tmp/codex_output.txt"
class RepoStats(BaseModel):
stars: int = 0
forks: int = 0
open_issues: int = 0
language: str = ""
description: str = ""
def main() -> None:
parser = argparse.ArgumentParser(
description="Fetch GitHub repo stats via Codex in a sandbox"
)
parser.add_argument(
"--repo",
default="bespokelabs/curator",
help="GitHub owner/repo (default: bespokelabs/curator)",
)
args = parser.parse_args()
api_key = os.environ.get("OPENAI_API_KEY", "")
if not api_key:
sys.exit("Set OPENAI_API_KEY before running this example.")
prompt = (
f"Look up the GitHub repository {args.repo}. {json_schema(RepoStats)}"
)
print(f"Fetching stats for {args.repo} using Codex...\n")
with Sandbox(
"local",
preset="codex",
env_vars={"OPENAI_API_KEY": api_key},
workdir=WORKDIR,
) as sb:
try:
result = sb.execute_command(
"codex",
args=[
"exec",
"--full-auto",
"--skip-git-repo-check",
"--search",
"-o",
OUTPUT_FILE,
prompt,
],
)
if result.exit_code != 0:
sys.exit(
f"Codex failed (exit {result.exit_code}):\n{result.stderr[:500]}"
)
raw = sb.read_file(OUTPUT_FILE).decode()
stats = Sandbox.parse_as(raw, RepoStats)
except SandboxExecutionError as e:
sys.exit(f"Failed: {e}")
print(f" Repository: {args.repo}")
print(f" Description: {stats.description}")
print(f" Language: {stats.language}")
print(f" Stars: {stats.stars:,}")
print(f" Forks: {stats.forks:,}")
print(f" Open issues: {stats.open_issues:,}")
if __name__ == "__main__":
main()