53 lines
2.5 KiB
Plaintext
53 lines
2.5 KiB
Plaintext
To implement a Python function using OpenRouter’s API to send content to an AI model, receive results, and track the costs, you can follow this approach:
|
||
|
||
1. **API Request for Chat Completion**:
|
||
- You’ll need to send a `POST` request to `https://openrouter.ai/api/v1/chat/completions`, including the model identifier, your message content, and your API key in the headers.
|
||
- The request body should include the AI model you want to use (e.g., `"openai/gpt-3.5-turbo"`) and the `messages` array.
|
||
|
||
2. **Tracking Costs**:
|
||
- The response will contain `usage` data, which includes `prompt_tokens`, `completion_tokens`, and `total_tokens`. This data allows you to calculate the cost based on token usage directly from the response.
|
||
- Alternatively, to get detailed cost data, OpenRouter provides the `/api/v1/generation` endpoint. You can use the `id` from your initial response to query for specific stats, such as exact token counts and the calculated cost.
|
||
|
||
3. **Checking Account Balance**:
|
||
- Currently, OpenRouter doesn’t appear to provide a direct API endpoint for account balance checks within their documented API endpoints, so this may need to be monitored from their platform dashboard directly.
|
||
|
||
Here’s a basic Python function demonstrating the API call, token tracking, and cost retrieval:
|
||
|
||
```python
|
||
import requests
|
||
|
||
def query_openrouter(content, model="openai/gpt-3.5-turbo"):
|
||
url = "https://openrouter.ai/api/v1/chat/completions"
|
||
headers = {
|
||
"Authorization": "Bearer YOUR_API_KEY",
|
||
"Content-Type": "application/json"
|
||
}
|
||
data = {
|
||
"model": model,
|
||
"messages": [{"role": "user", "content": content}]
|
||
}
|
||
|
||
response = requests.post(url, headers=headers, json=data)
|
||
result = response.json()
|
||
|
||
if "usage" in result:
|
||
usage_data = result["usage"]
|
||
cost_query_url = f"https://openrouter.ai/api/v1/generation?id={result['id']}"
|
||
cost_response = requests.get(cost_query_url, headers=headers)
|
||
cost_data = cost_response.json()
|
||
|
||
return {
|
||
"reply": result["choices"][0]["message"]["content"],
|
||
"prompt_tokens": usage_data["prompt_tokens"],
|
||
"completion_tokens": usage_data["completion_tokens"],
|
||
"total_cost": cost_data["data"]["total_cost"]
|
||
}
|
||
else:
|
||
return {"error": "No usage data available in the response"}
|
||
|
||
# Example usage
|
||
result = query_openrouter("What is the meaning of life?")
|
||
print(result)
|
||
```
|
||
|
||
For further details, OpenRouter’s official documentation outlines more on usage and token tracking options. |