-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathespn_openai.py
More file actions
52 lines (40 loc) · 1.21 KB
/
espn_openai.py
File metadata and controls
52 lines (40 loc) · 1.21 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
"""
Extract commentaries from the given url.
"""
import pandas as pd
from pydantic import BaseModel
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv
from markdownify import markdownify as md
import httpx
class Commentary(BaseModel):
time: str
text: str
class Match(BaseModel):
commentaries: list[Commentary]
def main():
"""
Extract commentaries from the given passage.
"""
url = "https://espn.com/soccer/commentary/_/gameId/704489"
resp = httpx.get(url, follow_redirects=True)
resp.raise_for_status()
content = md(resp.text, strip=["a", "img"])
load_dotenv(find_dotenv())
client = OpenAI()
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{
"role": "system",
"content": "Extract commentaries from the given passage. Do not fabricate any information.",
},
{"role": "user", "content": f"Passage:{content}"},
],
response_format=Match,
)
event = completion.choices[0].message.parsed
commentaries = pd.DataFrame(event.dict()["commentaries"])
print(commentaries)
if __name__ == "__main__":
main()