forked from Arize-ai/openinference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponses_with_structured_outputs.py
More file actions
82 lines (75 loc) · 3.08 KB
/
responses_with_structured_outputs.py
File metadata and controls
82 lines (75 loc) · 3.08 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
import json
import openai
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from openinference.instrumentation.openai import OpenAIInstrumentor
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)
client = openai.OpenAI()
response = client.responses.create(
model="gpt-4o-mini",
input=[
{
"role": "system",
"content": "You are a UI generator AI. Convert the user input into a UI.",
},
{"role": "user", "content": "Make a User Profile Form"},
],
text={
"format": {
"type": "json_schema",
"name": "ui",
"description": "Dynamically generated UI",
"schema": {
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "The type of the UI component",
"enum": ["div", "button", "header", "section", "field", "form"],
},
"label": {
"type": "string",
"description": "The label of the UI component, used for "
"buttons or form fields",
},
"children": {
"type": "array",
"description": "Nested UI components",
"items": {"$ref": "#"},
},
"attributes": {
"type": "array",
"description": "Arbitrary attributes for the UI component, "
"suitable for any element",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the attribute, for "
"example onClick or className",
},
"value": {
"type": "string",
"description": "The value of the attribute",
},
},
"required": ["name", "value"],
"additionalProperties": False,
},
},
},
"required": ["type", "label", "children", "attributes"],
"additionalProperties": False,
},
"strict": True,
},
},
)
ui = json.loads(response.output_text)
print(ui)