-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_output.py
More file actions
40 lines (32 loc) · 1.07 KB
/
json_output.py
File metadata and controls
40 lines (32 loc) · 1.07 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
from openai import OpenAI
from dotenv import load_dotenv
import os
import json
from pydantic import BaseModel
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key = api_key)
class Company(BaseModel):
name: str
class Person(BaseModel):
name: str
designation: str
company: list[Company]
interests: list[str]
# response = client.responses.create(
# model = "gpt-4.1",
# input = [
# {"role" : "user", "content" : "Extract the following pieces of info from the given paragraph/text: {name : str, designation: str, company:str}. Vikas is a data scientist working at kotak."}
# ]
# )
# res = json.loads(response.output_text)
# print(res["name"])
response = client.responses.parse(
model = "gpt-4.1",
input = [
{"role" : "user", "content" : "Vikas is a data scientist working at kotak. He previously worked at nagarro. He likes reading and spending time in nature. "}
],
text_format = Person
)
print(response.output_parsed)
print(response.output_parsed.model_dump())