-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathresponses.py
More file actions
157 lines (130 loc) · 4.87 KB
/
responses.py
File metadata and controls
157 lines (130 loc) · 4.87 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
148
149
150
151
152
153
154
155
156
157
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
"""End-to-end example for the OpenAI Responses API client.
Run with::
python examples/responses.py
Requires a loaded model and a started web service.
"""
from __future__ import annotations
import json
from foundry_local_sdk import (
Configuration,
FoundryLocalManager,
FunctionToolDefinition,
InputImageContent,
InputTextContent,
MessageItem,
)
MODEL_ALIAS = "phi-4-mini"
def setup():
config = Configuration(app_name="ResponsesExample")
FoundryLocalManager.initialize(config)
mgr = FoundryLocalManager.instance
mgr.download_and_register_eps()
model = mgr.catalog.get_model(MODEL_ALIAS)
if model is None:
raise RuntimeError(f"Model '{MODEL_ALIAS}' not found in catalog")
if not model.is_cached:
print(f"Downloading {MODEL_ALIAS}...")
model.download(progress_callback=lambda p: print(f" {p:.1f}%", end="\r"))
print()
print(f"Loading {model.alias}...", end="")
model.load()
print("loaded!")
mgr.start_web_service()
client = mgr.create_responses_client(model.id)
return mgr, model, client
def basic_create(client):
print("\n=== 1. Basic create ===")
resp = client.create("What is 2 + 2? Answer in one word.")
print(f"status={resp.status} text={resp.output_text!r}")
def streaming(client):
print("\n=== 2. Streaming ===")
print("assistant: ", end="", flush=True)
for event in client.create_streaming("Count from 1 to 5, separated by spaces."):
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
elif event.type == "response.completed":
response = getattr(event, "response", None)
usage = getattr(response, "usage", None) if response is not None else None
total = getattr(usage, "total_tokens", None) if usage is not None else None
print(f"\n(completed{f', {total} tokens' if total is not None else ''})")
def multi_turn(client):
print("\n=== 3. Multi-turn ===")
first = client.create("My favorite color is green. Remember that.", store=True)
print(f"first id={first.id!r}")
second = client.create(
"What is my favorite color?",
previous_response_id=first.id,
)
print(f"second: {second.output_text!r}")
def tool_calling(client):
print("\n=== 4. Tool calling ===")
tools = [
FunctionToolDefinition(
name="multiply_numbers",
description="Multiply two integers together.",
parameters={
"type": "object",
"properties": {
"a": {"type": "integer"},
"b": {"type": "integer"},
},
"required": ["a", "b"],
},
)
]
resp = client.create("What is 7 times 6?", tools=tools)
# Find a function_call item in the output (if the model produced one).
for item in resp.output:
if getattr(item, "type", None) == "function_call":
print(f"call {item.name}({item.arguments})")
args = json.loads(item.arguments)
answer = args["a"] * args["b"]
follow = client.create(
[
MessageItem(role="user", content="What is 7 times 6?"),
item,
# The function_call_output is sent back keyed by call_id
{"type": "function_call_output", "call_id": item.call_id, "output": str(answer)},
],
tools=tools,
)
print(f"final: {follow.output_text!r}")
return
print(f"no tool call — got text: {resp.output_text!r}")
def vision(client):
print("\n=== 5. Vision ===")
# Requires a vision-capable model. Replace with a real PNG to see real output.
tiny_png = bytes.fromhex(
"89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4"
"890000000d49444154789c6300010000000500010d0a2db40000000049454e44"
"ae426082"
)
msg = MessageItem(
role="user",
content=[
InputTextContent(text="Describe this image in one sentence."),
InputImageContent.from_bytes(tiny_png, "image/png"),
],
)
try:
resp = client.create([msg])
print(f"vision response: {resp.output_text!r}")
except Exception as e:
print(f"(skipped — model may not support vision: {e})")
def main():
mgr, model, client = setup()
try:
basic_create(client)
streaming(client)
multi_turn(client)
tool_calling(client)
vision(client)
finally:
mgr.stop_web_service()
model.unload()
if __name__ == "__main__":
main()