The call_api function provides a unified interface to interact with Anthropic, Together, and OpenAI models. It handles model-specific logic, including prompt modification for JSON output and cost calculation.
Supported Model Families:
- Anthropic: Models containing
claude in the name. - Together: Models containing
llama, qwen, or qwq. - OpenAI: Models like
gpt-4o or o1 series.
Key Features:
- JSON Output: If
json_output=True, the function appends specific instructions to the prompt to ensure the model returns a raw JSON dictionary without markdown code blocks (e.g., no ```json). For supported models (like meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo on Together or OpenAI's json_object mode), it sets the appropriate response_format. - Cost Tracking: Returns both the text response and the calculated cost based on token usage.
Note: For o1 models, the function uses max_completion_tokens instead of max_tokens to comply with OpenAI's API requirements for that model family.
# Example usage with a generic client
response, cost = call_api(
client=client,
model="gpt-4o",
prompt_messages=[{"role": "user", "content": "Your prompt here"}],
json_output=True
)