mirror of
https://github.com/wassname/openrouter-python-sdk-retry-errors.git
synced 2026-07-31 12:30:30 +08:00
first commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
OPENROUTER_API_KEY="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"
|
||||
@@ -0,0 +1 @@
|
||||
OPENROUTER_API_KEY=""
|
||||
@@ -0,0 +1,25 @@
|
||||
# Run SDK examples with uv
|
||||
|
||||
This guide explains how to create and activate a Python virtual environment on macOS, install necessary dependencies, and run the `.py` scripts.
|
||||
|
||||
## Steps to Set Up and Run the Script
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- [Install uv](https://docs.astral.sh/uv/getting-started/installation/#pypi)
|
||||
|
||||
|
||||
### Configure auth
|
||||
|
||||
- Rename .env.template to .env and populate the .env file with your client ID and secret
|
||||
|
||||
```bash
|
||||
OPENROUTER_API_KEY=""
|
||||
```
|
||||
|
||||
### Run the script with the .env values
|
||||
|
||||
```bash
|
||||
cd examples/
|
||||
uv run --env-file=.env script.py
|
||||
```
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
dependencies = [
|
||||
"openrouter",
|
||||
"os",
|
||||
]
|
||||
|
||||
import os
|
||||
from openrouter import OpenRouter
|
||||
|
||||
with OpenRouter(
|
||||
bearer_auth=os.getenv("OPENROUTER_API_KEY"),
|
||||
) as sdk:
|
||||
result = sdk.chat.complete(
|
||||
model="openai/gpt-3.5-turbo",
|
||||
messages=[
|
||||
{"role": "user", "content": "Hello, world!"},
|
||||
],
|
||||
)
|
||||
|
||||
print(result)
|
||||
@@ -0,0 +1,26 @@
|
||||
|
||||
dependencies = [
|
||||
"openrouter",
|
||||
"os",
|
||||
]
|
||||
|
||||
import os
|
||||
from openrouter import OpenRouter, models
|
||||
|
||||
with OpenRouter(
|
||||
bearer_auth=os.getenv("OPENROUTER_API_KEY", ""),
|
||||
) as open_router:
|
||||
|
||||
res = open_router.chat.complete_stream(messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Hello, how are you?",
|
||||
},
|
||||
|
||||
], model="openai/gpt-3.5-turbo", stream=True, temperature=1, top_p=1)
|
||||
|
||||
with res as event_stream:
|
||||
for event in event_stream:
|
||||
if event.data.choices and event.data.choices[0].delta.content:
|
||||
print(event.data.choices[0].delta.content, end='', flush=True)
|
||||
print() # New line at the end
|
||||
Reference in New Issue
Block a user