You can use the standard OpenAI Python client by simply changing openai.api_base and using our API key header:
Copy
import osimport openairequesty_api_key = "YOUR_REQUESTY_API_KEY" # Safely load your API keytry: # Initialize OpenAI client client = openai.OpenAI( api_key=requesty_api_key, base_url="https://router.requesty.ai/v1", default_headers: { "HTTP-Referer": "<YOUR_SITE_URL>", # Optional "X-Title": "<YOUR_SITE_NAME>", # Optional }, ) # Example request response = client.chat.completions.create( model="openai/gpt-4o", messages=[{"role": "user", "content": "Hello, who are you?"}] ) # Check if the response is successful if not response.choices: raise Exception("No response choices found.") # Print the result print(response.choices[0].message.content)except openai.OpenAIError as e: print(f"OpenAI API error: {e}")except Exception as e: print(f"An unexpected error occurred: {e}")
This makes a request to the local router which proxies it to the appropriate model provider, returning a completion response in OpenAI format. Additionally you can add additional information to the headers you can specify (like HTTP-Referer and X-Title) which can help with analytics and app discoverability.