mirror of
https://github.com/wassname/openrouter-python-sdk-retry-errors.git
synced 2026-08-01 12:40:23 +08:00
This commit is contained in:
@@ -1,23 +1,25 @@
|
||||
<!-- Start SDK Example Usage [usage] -->
|
||||
```python
|
||||
# Synchronous Example
|
||||
from openrouter import OpenRouter, models
|
||||
from openrouter import OpenRouter
|
||||
import os
|
||||
|
||||
|
||||
with OpenRouter(
|
||||
bearer_auth=os.getenv("OPENROUTER_BEARER_AUTH", ""),
|
||||
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
||||
) as open_router:
|
||||
|
||||
res = open_router.chat.complete(messages=[
|
||||
{
|
||||
"role": models.ChatCompletionUserMessageParamRole.USER,
|
||||
"role": "user",
|
||||
"content": "Hello, how are you?",
|
||||
},
|
||||
], stream=False, temperature=1, top_p=1)
|
||||
|
||||
# Handle response
|
||||
print(res)
|
||||
with res as event_stream:
|
||||
for event in event_stream:
|
||||
# handle event
|
||||
print(event, flush=True)
|
||||
```
|
||||
|
||||
</br>
|
||||
@@ -26,24 +28,26 @@ The same SDK client can also be used to make asynchronous requests by importing
|
||||
```python
|
||||
# Asynchronous Example
|
||||
import asyncio
|
||||
from openrouter import OpenRouter, models
|
||||
from openrouter import OpenRouter
|
||||
import os
|
||||
|
||||
async def main():
|
||||
|
||||
async with OpenRouter(
|
||||
bearer_auth=os.getenv("OPENROUTER_BEARER_AUTH", ""),
|
||||
api_key=os.getenv("OPENROUTER_API_KEY", ""),
|
||||
) as open_router:
|
||||
|
||||
res = await open_router.chat.complete_async(messages=[
|
||||
{
|
||||
"role": models.ChatCompletionUserMessageParamRole.USER,
|
||||
"role": "user",
|
||||
"content": "Hello, how are you?",
|
||||
},
|
||||
], stream=False, temperature=1, top_p=1)
|
||||
|
||||
# Handle response
|
||||
print(res)
|
||||
async with res as event_stream:
|
||||
async for event in event_stream:
|
||||
# handle event
|
||||
print(event, flush=True)
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user