Integrate Morpheus API Gateway with OpenAI Python SDK
Learn how to integrate the Morpheus API Gateway with OpenAI’s official Python SDK to build AI-powered applications with free, decentralized AI inference. This guide covers basic chat completions, streaming responses, tool calling, and async operations.
The Morpheus API Gateway provides free AI inference through a decentralized compute marketplace. Since it’s fully OpenAI-compatible, you can use the official OpenAI Python SDK by simply pointing it to the Morpheus base URL.
The Morpheus API Gateway is currently in Open Beta, providing free access to AI inference without requiring wallet connections or staking MOR tokens.
Query the available models using the Morpheus API:
list_models.py
Copy
from openai import OpenAIimport osclient = OpenAI( api_key=os.getenv("MORPHEUS_API_KEY"), base_url="https://api.mor.org/api/v1")# List all available modelsmodels = client.models.list()for model in models.data: print(f"Model: {model.id}")
Common Morpheus Models
Popular models available through Morpheus:
llama-3.3-70b:web - Meta’s Llama 3.3 with web search capabilities
llama-3.3-70b - Meta’s Llama 3.3 base model
qwen3-235b:web - Qwen 3 with web search capabilities
qwen3-235b - Qwen 3 base model
Model availability may vary based on provider availability in the Morpheus marketplace. The API automatically routes to the highest-rated provider for your selected model. The :web suffix indicates models optimized for web browsing tasks.
For real-time output, enable streaming to receive tokens as they’re generated:
streaming_chat.py
Copy
from openai import OpenAIimport osclient = OpenAI( api_key=os.getenv("MORPHEUS_API_KEY"), base_url="https://api.mor.org/api/v1")stream = client.chat.completions.create( model="llama-3.3-70b:web", messages=[ {"role": "user", "content": "Write a short story about artificial intelligence."} ], stream=True, temperature=0.8)for chunk in stream: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="", flush=True)print() # New line after streaming completes
Streaming provides a better user experience by showing output immediately rather than waiting for the entire response.
Ensure the API key is properly loaded from environment variables
Check that the key hasn’t been deleted from your Morpheus account
Copy
import os# Debug API key loadingapi_key = os.getenv("MORPHEUS_API_KEY")print(f"API key loaded: {api_key is not None}")print(f"API key length: {len(api_key) if api_key else 0}")if not api_key: raise ValueError("MORPHEUS_API_KEY environment variable not set")
Tool calls fail or return unexpected results
Cause: Incorrect tool schema, missing function implementations, or model limitations.Solution:
Verify tool schemas match the JSON Schema specification
Ensure all required parameters are marked correctly
Provide detailed descriptions for tools and parameters
Test with different models (llama-3.3-70b often performs better)
Copy
# Good tool definition{ "type": "function", "function": { "name": "get_weather", "description": "Get current weather for a specific location. Use this when the user asks about weather conditions.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "City name, e.g., 'San Francisco' or 'Tokyo'" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit to use" } }, "required": ["location"] } }}
Streaming stops prematurely
Cause: Network interruption, timeout, or model completion.Solution:
Check the finish_reason in the response
Implement error handling for streams
Use appropriate timeout values
Copy
try: stream = client.chat.completions.create( model="llama-3.3-70b", messages=[{"role": "user", "content": "Long task"}], stream=True, timeout=120.0 # Longer timeout for streaming ) for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True) # Check finish reason if chunk.choices[0].finish_reason: print(f"\nFinish reason: {chunk.choices[0].finish_reason}")except Exception as e: print(f"Stream error: {str(e)}")
Model not found errors
Cause: Requested model is not available or misspelled.Solution:
List available models first
Use exact model names including suffixes (:web)
Check model availability in the marketplace
Copy
# List available modelsmodels = client.models.list()available_models = [model.id for model in models.data]print("Available models:", available_models)# Verify model exists before usingdesired_model = "llama-3.3-70b:web"if desired_model in available_models: response = client.chat.completions.create( model=desired_model, messages=[{"role": "user", "content": "Hello"}] )else: print(f"Model {desired_model} not available. Using default.")
Async operations not working
Cause: Incorrect async/await usage or event loop issues.Solution:
You’ve successfully integrated the Morpheus API Gateway with OpenAI’s Python SDK! Key takeaways:
OpenAI Compatibility: Morpheus works seamlessly with the official OpenAI Python SDK by using a custom base_url
Flexible Deployment: Use synchronous or asynchronous clients based on your application needs
Streaming Support: Real-time streaming responses work identically to OpenAI’s API
Tool Calling: Define and execute custom functions with JSON schema-based tool definitions
Free Inference: Build AI applications with free, decentralized inference during the Open Beta
The combination of Morpheus’s free, decentralized AI inference and the OpenAI Python SDK’s robust features enables you to build powerful AI applications without infrastructure costs or vendor lock-in.