mirror of
https://github.com/hanzoai/engine.git
synced 2026-11-20 14:17:52 +00:00
* Implement qwen3 embedding * Attention mask * A bit of cleanup * Clippy * Add docs
49 lines
1.4 KiB
Python
49 lines
1.4 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]}
|
|
# )
|
|
|
|
res = client.embeddings.create(
|
|
model="default",
|
|
input=[
|
|
"task: search result | query: What is graphene?",
|
|
"task: search result | query: What is an apple?",
|
|
],
|
|
)
|
|
|
|
print(res.data[0].embedding[:10])
|
|
print(res.data[1].embedding[:10])
|
|
print(res.usage)
|