add Python httpx-async example with httpx==0.28.1 and python:3.13-alpine

This commit is contained in:
Russell Ballestrini 2025-10-12 15:21:35 -04:00
parent 4941cce333
commit 19ff284403
3 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,13 @@
# Pin to specific Python version (checked 2025-10-12: python:3.13-alpine is latest stable)
FROM python:3.13-alpine
WORKDIR /app
COPY requirements.txt /app/requirements.txt
COPY examples.py /app/examples.py
RUN chmod +x /app/examples.py
RUN pip3 install --no-cache-dir -r /app/requirements.txt
CMD ["python3", "/app/examples.py"]

View file

@ -0,0 +1,108 @@
#!/usr/bin/env python3
"""
uncloseai.com API Examples in Python using httpx (async)
Demonstrates async HTTP calls to Hermes AI, Qwen Coder, and TTS endpoints
"""
import asyncio
import httpx
async def main():
print("=== uncloseai.com Python (httpx async) Examples ===")
print()
async with httpx.AsyncClient() as client:
# Example 1: Hermes AI Chat
print("1. Hermes AI - General Purpose Chat (async httpx)")
print(" Asking: 'Give a Python Fizzbuzz solution in one line of code?'")
print()
hermes_response = await client.post(
"https://hermes.ai.unturf.com/v1/chat/completions",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer dummy-key"
},
json={
"model": "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic",
"messages": [{"role": "user", "content": "Give a Python Fizzbuzz solution in one line of code?"}],
"temperature": 0.5,
"max_tokens": 150
},
timeout=30.0
)
if hermes_response.status_code == 200:
print("Response:")
print(hermes_response.json()["choices"][0]["message"]["content"])
else:
print(f"Error: {hermes_response.status_code} - {hermes_response.text}")
print()
print("---")
print()
# Example 2: Qwen 3 Coder
print("2. Qwen 3 Coder - Specialized for Code (async httpx)")
print(" Asking: 'Write an async Python function to fetch multiple URLs'")
print()
qwen_response = await client.post(
"https://qwen.ai.unturf.com/v1/chat/completions",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer dummy-key"
},
json={
"model": "hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M",
"messages": [{"role": "user", "content": "Write an async Python function to fetch multiple URLs"}],
"temperature": 0.5,
"max_tokens": 200
},
timeout=30.0
)
if qwen_response.status_code == 200:
print("Response:")
print(qwen_response.json()["choices"][0]["message"]["content"])
else:
print(f"Error: {qwen_response.status_code} - {qwen_response.text}")
print()
print("---")
print()
# Example 3: Text-to-Speech
print("3. Text-to-Speech Generation (async httpx)")
print(" Converting text to speech and saving to speech.mp3")
print()
tts_response = await client.post(
"https://speech.ai.unturf.com/v1/audio/speech",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer YOLO"
},
json={
"model": "tts-1",
"voice": "alloy",
"input": "Hello from async Python with httpx! Today is a wonderful day to build something people love!"
},
timeout=30.0
)
if tts_response.status_code == 200:
with open("speech.mp3", "wb") as f:
f.write(tts_response.content)
import os
file_size = os.path.getsize("speech.mp3")
print(f"✓ Speech file created: speech.mp3 ({file_size} bytes)")
else:
print(f"✗ Failed to create speech file: {tts_response.status_code}")
print()
print("=== Examples Complete ===")
if __name__ == "__main__":
asyncio.run(main())

View file

@ -0,0 +1,2 @@
# Async HTTP library (checked 2025-10-12)
httpx==0.28.1