uncloseai.com/languages/python/httpx-async/examples.py

108 lines
3.6 KiB
Python

#!/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())