mirror of
https://github.com/hanzoai/engine.git
synced 2026-11-20 14:17:52 +00:00
* Add openresponses api support * Add better support * Pass reasoning effor properly * Support XML think tags * Format * Add docs * Add more fields * Bugfixes * Fix qwen3 vl template action * Fix qwen3 vl template action * Think mode for qwen3 model fix * Clippy
69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
from openai import OpenAI
|
||
import httpx
|
||
import textwrap
|
||
import json
|
||
|
||
|
||
def log_response(response: httpx.Response):
|
||
request = response.request
|
||
print(f"Request: {request.method} {request.url}")
|
||
print(" Headers:")
|
||
for key, value in request.headers.items():
|
||
if key.lower() == "authorization":
|
||
value = "[...]"
|
||
if key.lower() == "cookie":
|
||
value = value.split("=")[0] + "=..."
|
||
print(f" {key}: {value}")
|
||
print(" Body:")
|
||
try:
|
||
request_body = json.loads(request.content)
|
||
print(textwrap.indent(json.dumps(request_body, indent=2), " "))
|
||
except json.JSONDecodeError:
|
||
print(textwrap.indent(request.content.decode(), " "))
|
||
print(f"Response: status_code={response.status_code}")
|
||
print(" Headers:")
|
||
for key, value in response.headers.items():
|
||
if key.lower() == "set-cookie":
|
||
value = value.split("=")[0] + "=..."
|
||
print(f" {key}: {value}")
|
||
|
||
|
||
client = OpenAI(api_key="foobar", base_url="http://localhost:1234/v1/")
|
||
|
||
# # Enable this to log requests and responses
|
||
# client._client = httpx.Client(
|
||
# event_hooks={"request": [print], "response": [log_response]}
|
||
# )
|
||
|
||
|
||
# first turn
|
||
resp1 = client.responses.create(
|
||
model="default",
|
||
input=[
|
||
{
|
||
"role": "user",
|
||
"content": [
|
||
{
|
||
"type": "image_url",
|
||
"image_url": {"url": "Pink_flower.jpg"},
|
||
},
|
||
{
|
||
"type": "text",
|
||
"text": "What is this?",
|
||
},
|
||
],
|
||
}
|
||
],
|
||
)
|
||
|
||
print(resp1.output_text)
|
||
print(resp1.reasoning)
|
||
|
||
# follow‑up: no need to resend the first question
|
||
resp2 = client.responses.create(
|
||
model="default", previous_response_id=resp1.id, input="Can you eat it?"
|
||
)
|
||
|
||
print(resp2.output_text)
|
||
print(resp2.reasoning)
|